Tortoise based database models
This commit is contained in:
1 parent
2c7b3d61d1
commit
cccdb7d231
12 files changed
+469
-382
No files matched your search
+58
-46
@@ -67,57 +67,69 @@ class ChatView:
|
||||
self.current_thread_id = None
|
||||
|
||||
self._earliest_message_id = None
|
||||
|
||||
self._load_basic_data()
|
||||
self._load_next_messages(MESSAGE_PAGE_SIZE)
|
||||
|
||||
def _load_basic_data(self):
|
||||
with self.app.sql_session() as session:
|
||||
record = session.execute(
|
||||
select(db.Chat).where(db.Chat.id == self.chat_id)
|
||||
).scalars().first()
|
||||
async def init_load(self):
|
||||
await self._load_basic_data()
|
||||
await self._load_next_messages(MESSAGE_PAGE_SIZE)
|
||||
|
||||
if not record:
|
||||
raise Exception(f"Chat not found with id {self.chat_id}")
|
||||
async def _load_basic_data(self):
|
||||
# Using get_or_none for cleaner existence checks
|
||||
record = await db.Chat.get_or_none(id=self.chat_id)
|
||||
|
||||
if not record:
|
||||
raise Exception(f"Chat not found with id {self.chat_id}")
|
||||
|
||||
self.name = record.name
|
||||
self.description = record.description
|
||||
|
||||
async def _load_next_messages(self, load_count):
|
||||
# Start building the filter
|
||||
filters = {"chat_id": self.chat_id}
|
||||
|
||||
# Add the 'earliest message' condition if it exists
|
||||
if self._earliest_message_id is not None:
|
||||
filters["id__lte"] = self._earliest_message_id
|
||||
|
||||
# Tortoise query: filter, order, and limit
|
||||
records_rev = await db.Message.filter(**filters).order_by("-time").limit(load_count)
|
||||
|
||||
# Update the earliest id for the next load operation
|
||||
if records_rev:
|
||||
# Note: Since we ordered by -time (descending), index 0 is the newest in this batch
|
||||
self._earliest_message_id = records_rev[-1].id # The "earliest" is the one with the smallest ID/Time in this set
|
||||
|
||||
self.name = record.name
|
||||
self.description = record.description
|
||||
if not self.threads:
|
||||
# current thread is the latest known from the newest record in batch
|
||||
self.current_thread_id = records_rev[0].thread_id
|
||||
|
||||
def _load_next_messages(self, load_count):
|
||||
with self.app.sql_session() as session:
|
||||
records_rev = session.execute(
|
||||
select(db.Message).where(
|
||||
db.Message.chat_id == self.chat_id,
|
||||
self._earliest_message_id == None or db.Message.id <= self._earliest_message_id
|
||||
).order_by(db.Message.time.desc())
|
||||
).scalars().fetchmany(load_count)
|
||||
# Go over messages from newest to oldest
|
||||
for record in records_rev:
|
||||
if record.thread_id is None:
|
||||
thread = self.default_thread
|
||||
else:
|
||||
thread = self.thread_map.get(record.thread_id)
|
||||
if not thread:
|
||||
# Assuming Thread is a local class, not the DB model
|
||||
thread = Thread(record.thread_id, self.chat_id)
|
||||
self.thread_map[thread.id] = thread
|
||||
self.threads.insert(0, thread)
|
||||
|
||||
# Create local Message object
|
||||
message = Message(
|
||||
record.id,
|
||||
record.content,
|
||||
record.sender,
|
||||
record.time,
|
||||
record.kind,
|
||||
record.subkind
|
||||
)
|
||||
|
||||
# Update the earliest id for the next load operation
|
||||
if records_rev:
|
||||
self._earliest_message_id = records_rev[0].id
|
||||
if not self.threads:
|
||||
# current thread is the latest known
|
||||
self.current_thread_id = records_rev[-1].thread_id
|
||||
|
||||
# Go over messages from newest to oldest
|
||||
for record in records_rev:
|
||||
if record.thread_id == None:
|
||||
thread = self.default_thread
|
||||
else:
|
||||
thread = self.thread_map.get(record.thread_id)
|
||||
if not thread:
|
||||
thread = Thread(record.thread_id, self.chat_id)
|
||||
self.thread_map[thread.id] = thread
|
||||
self.threads.insert(0, thread)
|
||||
# This is the oldest message so far, add it to the start of the chat
|
||||
message = Message(record.id, record.content, record.sender, record.time, record.kind, record.subkind)
|
||||
|
||||
if message.kind == 'note':
|
||||
thread.notes.insert(0, message)
|
||||
elif message.kind == 'memory':
|
||||
thread.memories.insert(0, message)
|
||||
else:
|
||||
thread.messages.insert(0, message)
|
||||
if message.kind == 'note':
|
||||
thread.notes.insert(0, message)
|
||||
elif message.kind == 'memory':
|
||||
thread.memories.insert(0, message)
|
||||
else:
|
||||
thread.messages.insert(0, message)
|
||||
|
||||
def get_thread(self, thread_id):
|
||||
if thread_id == None:
|
||||
|
||||
Reference in new issue
Block a user