This commit is contained in:
2023-10-13 19:45:30 +03:00
parent 1c46fc1d48
commit 84e6841331
8 changed files with 84 additions and 73 deletions

View File

@@ -1,15 +1,16 @@
import json
from typing import Any, Dict, List, Optional, Union
from services.core import get_author, get_network
from services.redis import redis
from services.auth import login_required
from services.schema import query
from validators.chat import Message
from .chats import create_chat
from .unread import get_unread_counter
import asyncio
# NOTE: not an API handler
async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=None):
async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids: Optional[List[str]] = None) -> List[Message]:
"""load :limit messages for :chat_id with :offset"""
if ids is None:
ids = []
@@ -17,22 +18,22 @@ async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=None)
try:
message_ids = [] + ids
if limit:
mids = await redis.lrange(
mids = (await redis.lrange(
f"chats/{chat_id}/message_ids", offset, offset + limit
)
)) or []
mids = [mid for mid in mids]
message_ids += mids
if message_ids:
message_keys = [f"chats/{chat_id}/messages/{mid}" for mid in message_ids]
messages = await redis.mget(*message_keys)
messages = [json.loads(msg.decode("utf-8")) for msg in messages]
messages = (await redis.mget(*message_keys)) or []
replies = []
for m in messages:
rt = m.get("replyTo")
if rt:
rt = int(rt)
if rt not in message_ids:
replies.append(rt)
if m:
rt = json.loads(m).get("replyTo")
if rt:
rt = int(rt)
if rt not in message_ids:
replies.append(rt)
if replies:
messages += await load_messages(chat_id, offset, limit, replies)
except Exception as e:
@@ -41,7 +42,7 @@ async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=None)
@query.field("loadChats")
@login_required
async def load_chats(_, info, limit: int = 50, offset: int = 0):
async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Union[List[Dict[str, Any]], None]]:
"""load :limit chats of current user with :offset"""
author_id = info.context["author_id"]
cids = (await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")) or []
@@ -79,8 +80,8 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0):
async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
"""load :limit messages of :chat_id with :offset"""
author_id = info.context["author_id"]
user_chats = await redis.execute("SMEMBERS", "chats_by_author/" + str(author_id))
user_chats = [c for c in user_chats]
user_chats = (await redis.execute("SMEMBERS", "chats_by_author/" + str(author_id))) or []
user_chats = [c.decode() for c in user_chats]
if user_chats:
messages = []
by_chat = by.get("chat")
@@ -91,7 +92,7 @@ async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
# everyone's messages in filtered chat
messages = await load_messages(by_chat, limit, offset)
return {
"messages": sorted(list(messages), key=lambda m: m["createdAt"]),
"messages": sorted([m for m in messages if m.get("createdAt")], key=lambda m: m.get("createdAt")),
"error": None,
}
else: