streamed messages
This commit is contained in:
1 parent
f2a6fc0cd5
commit
7114d91403
7 files changed
+107
-34
No files matched your search
@@ -1,8 +1,9 @@
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
# Assuming db is your database.py containing the Tortoise models from the previous step
|
||||
|
||||
import database as db
|
||||
from chat import ChatView, Message, MESSAGE_PAGE_SIZE
|
||||
from stream import Stream
|
||||
|
||||
class ChatMixin:
|
||||
async def create_chat(self, user_id, name, description, picture, members=[]):
|
||||
@@ -42,8 +43,9 @@ class ChatMixin:
|
||||
if earliest_index is not None:
|
||||
query = query.filter(id__gt=earliest_index)
|
||||
|
||||
# Order by time descending, then reverse in Python to get chronological order
|
||||
messages = await query.order_by("-time").all()
|
||||
# Order quey by time descending (id orders better)
|
||||
# then reverse in Python to get chronological order
|
||||
messages = await query.order_by("-id").all()
|
||||
messages.reverse()
|
||||
|
||||
return [{
|
||||
@@ -72,6 +74,14 @@ class ChatMixin:
|
||||
|
||||
async def send_chat_message(self, chat_id, content, sender='user', thread_id=None, kind=None, subkind=None, format=None):
|
||||
now = int(datetime.now().timestamp())
|
||||
|
||||
if isinstance(content, Stream):
|
||||
initial_content = "..."
|
||||
else:
|
||||
initial_content = content
|
||||
|
||||
# Set to true for streams
|
||||
updating = False
|
||||
message = await db.Message.create(
|
||||
chat_id=chat_id,
|
||||
time=now,
|
||||
@@ -80,22 +90,48 @@ class ChatMixin:
|
||||
kind=kind or 'message',
|
||||
subkind=subkind,
|
||||
format=format,
|
||||
content=content,
|
||||
content=initial_content,
|
||||
thread_id=thread_id
|
||||
)
|
||||
|
||||
# Fetch chat owner using values_list for efficiency
|
||||
chat = await db.Chat.get(id=chat_id).prefetch_related("user")
|
||||
chat_owner_id = chat.user_id
|
||||
|
||||
# Notification order depends on whether the content is a stream
|
||||
|
||||
async def notify_agents():
|
||||
# Fetch participant agents
|
||||
agent_ids = await db.Membership.filter(chat_id=chat_id).values_list("agent_id", flat=True)
|
||||
for agent_id in agent_ids:
|
||||
if str(agent_id) != str(sender):
|
||||
await self.notify_message_to_agent(agent_id, message)
|
||||
|
||||
self.notify_message_to_user(chat_owner_id, message)
|
||||
def notify_users():
|
||||
nonlocal updating
|
||||
self.notify_message_to_user(chat_owner_id, message, updating)
|
||||
updating = True
|
||||
|
||||
# Fetch participant agents
|
||||
agent_ids = await db.Membership.filter(chat_id=chat_id).values_list("agent_id", flat=True)
|
||||
for agent_id in agent_ids:
|
||||
if str(agent_id) != str(sender):
|
||||
await self.notify_message_to_agent(agent_id, message)
|
||||
if isinstance(content, Stream):
|
||||
notify_users()
|
||||
|
||||
def chunk_fn (chunk):
|
||||
message.content = content.content + "..."
|
||||
notify_users()
|
||||
async def end_fn ():
|
||||
message.content = content.content
|
||||
await message.save()
|
||||
await notify_agents()
|
||||
notify_users()
|
||||
|
||||
content.listen(chunk_fn=chunk_fn, end_fn=end_fn)
|
||||
else:
|
||||
notify_users()
|
||||
await notify_agents()
|
||||
|
||||
# TODO: Chat preview based on the message
|
||||
|
||||
# TODO: return for who lmao
|
||||
return {
|
||||
'id': message.id,
|
||||
'chatId': message.chat_id,
|
||||
@@ -133,7 +169,7 @@ class ChatMixin:
|
||||
await chat_view.init_load()
|
||||
return chat_view
|
||||
|
||||
def notify_message_to_user(self, user_id, message):
|
||||
def notify_message_to_user(self, user_id, message, update=False):
|
||||
if user_id in self.user_connections:
|
||||
user_message = {
|
||||
'action': 'chat-message',
|
||||
@@ -144,6 +180,7 @@ class ChatMixin:
|
||||
'kind': message.kind,
|
||||
'content': message.content,
|
||||
'threadId': message.thread_id,
|
||||
'update': update,
|
||||
}
|
||||
for channel in self.user_connections[user_id]:
|
||||
asyncio.create_task(channel.put(user_message))
|
||||
|
||||
Reference in new issue
Block a user