chat: proper fork behavior in backend, wip frontend

This commit is contained in:
Shivering Ocelot committed 2026-09-25 18:41:16 -05:00
1 parent 7bf1080d19
commit c588a12160
5 files changed
+108 -48

No files matched your search

+25
View File
@@ -17,6 +17,25 @@ def message_json(msg):
"threadId": msg.thread_id,
}
async def aggregate_thread(thread_id):
print(f"aggregate {thread_id}")
query = db.Message.filter(thread_id=thread_id)
# Could do all these in one call?
message_count = await query.count()
print(f" count {message_count}")
if message_count == 0: return
first_message = await query.first()
last_message = await query.last()
print(f" bounds {first_message} {last_message}")
return await db.Thread.get(id=thread_id).update(
first_message_id=first_message.id,
last_message_id=last_message.id,
message_count=message_count
)
class ChatMixin:
async def create_chat(self, user_id, name, description, picture, members=[], temporary=False):
if temporary:
@@ -135,6 +154,12 @@ class ChatMixin:
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:
# Ensure all forks have the correct information
forks = await db.Thread.filter(parent_id=thread_id).all()
for fork in forks:
await aggregate_thread(fork.id)
await db.Thread.filter(parent_id=thread_id, first_message_id__gte=message_id).update(parent_id=new_id)
await db.Message.filter(thread_id=thread_id, id__gte=message_id).update(thread_id=new_id)
# Notify users. what a heavy operation!