wip: thread revamp

This commit is contained in:
Shivering Ocelot committed 2026-09-23 13:44:22 -05:00
1 parent 5b3f8b0f09
commit 9277b5a770
11 files changed
+401 -205

No files matched your search

+43 -16
View File
@@ -3,9 +3,20 @@ from datetime import datetime
import database as db
import temporary as tmp
from chat import ChatView, Message, MESSAGE_PAGE_SIZE
from chat import Chat
from stream import Stream
def message_json(msg):
return {
"id": msg.id,
"time": msg.time,
"sender": msg.sender,
"kind": msg.kind,
"subkind": msg.subkind,
"content": msg.content,
"threadId": msg.thread_id,
}
class ChatMixin:
async def create_chat(self, user_id, name, description, picture, members=[], temporary=False):
if temporary:
@@ -30,6 +41,12 @@ class ChatMixin:
return chat.id
# Gets a chat view from a given chat id
async def get_chat(self, chat_id):
chat = Chat(self, chat_id)
await chat.load()
return chat
async def get_chats(self, user_id):
# Direct filtering on the model class
chats = await db.Chat.filter(user_id=user_id).all()
@@ -74,27 +91,42 @@ class ChatMixin:
# TODO: Stop repetition.
# There's db.Message and chat.Message, also should be combined somehow, or wrap db always.
return [{
"id": message.id,
"time": message.time,
"sender": message.sender,
"kind": message.kind,
"subkind": message.subkind,
"content": message.content,
"threadId": message.thread_id,
} for message in messages]
return [message_json(msg) for msg in messages]
async def get_chat_thread_heads(self, chat_id, limit_message_id=None):
chat = await self.get_chat(chat_id)
messages = await chat.load_thread_heads(limit_message_id)
return [message_json(msg) for msg in messages]
async def get_thread_messages(self, chat_id, thread_id, limit_message_id=None):
chat = await self.get_chat(chat_id)
thread = chat.get_thread(thread_id)
messages = await thread.load_page(limit_message_id)
return [message_json(msg) for msg in messages]
async def create_chat_thread(self, chat_id, name=None):
async def create_chat_thread(self, chat_id, parent_id=None, name=None):
if tmp.is_tmp_id(chat_id):
thread = tmp.Thread.create(self, chat_id, name)
else:
thread = await db.Thread.create(
chat_id=chat_id,
parent_id=parent_id,
name=name
)
print(f"Created thread #{thread.id}: {thread.name}")
return thread.id
async def fork_chat_thread(self, thread_id, message_id=None, name=None):
"""Move all messages from message_id onwards into a new child thread.
If none is given, this is basically create_chat_thread with an implicit chat id."""
thread = await db.Thread.get(id=thread_id)
new_id = await self.create_chat_thread(thread.chat_id, parent_id=thread_id, name=name or thread.name)
if message_id is not None:
await db.Message.filter(thread_id=thread_id, id__gte=message_id).update(thread_id=new_id)
return new_id
async def add_agent_to_chat(self, chat_id, agent_id):
if tmp.is_tmp_id(chat_id):
chat = self.tmp.chats[chat_id]
@@ -232,11 +264,6 @@ class ChatMixin:
await message.save() # Saves the specific instance
print(f"Updated message #{message.id} fields: {summary}")
async def get_chat_view(self, chat_id):
chat_view = ChatView(self, chat_id)
await chat_view.init_load()
return chat_view
def notify_message_to_user(self, user_id, message, update=False):
if user_id in self.user_connections:
# TODO: Server's responsibility, and Message.to_json responsibility as well