logs-redis-typings-fix
This commit is contained in:
@@ -21,7 +21,7 @@ async def get_unread_counter(chat_id: str, member_id: int) -> int:
|
||||
# NOTE: not an API handler
|
||||
async def load_messages(
|
||||
chat_id: str, limit: int = 5, offset: int = 0, ids: Optional[List[int]] = None
|
||||
) -> List[Message | None]:
|
||||
):
|
||||
"""load :limit messages for :chat_id with :offset"""
|
||||
messages = []
|
||||
try:
|
||||
@@ -32,18 +32,21 @@ async def load_messages(
|
||||
message_ids.extend(mids)
|
||||
if message_ids:
|
||||
message_keys = [f"chats/{chat_id}/messages/{mid}" for mid in message_ids]
|
||||
messages = (await redis.mget(*message_keys)) or []
|
||||
messages = [json.loads(m) if isinstance(m, str) else m for m in messages]
|
||||
replies = []
|
||||
for m in messages:
|
||||
if m:
|
||||
reply_to = m.get("reply_to")
|
||||
if reply_to:
|
||||
reply_to = int(reply_to)
|
||||
if reply_to not in message_ids:
|
||||
replies.append(reply_to)
|
||||
if replies:
|
||||
messages += await load_messages(chat_id, offset, limit, replies)
|
||||
messages = await redis.execute("MGET", *message_keys)
|
||||
if isinstance(messages, list):
|
||||
messages = [json.loads(m) if isinstance(m, str) else m for m in messages]
|
||||
replies = []
|
||||
for m in messages:
|
||||
if m:
|
||||
reply_to = m.get("reply_to")
|
||||
if reply_to:
|
||||
reply_to = int(reply_to)
|
||||
if reply_to not in message_ids:
|
||||
replies.append(reply_to)
|
||||
if replies:
|
||||
more_messages = await load_messages(chat_id, offset, limit, replies)
|
||||
if isinstance(more_messages, list):
|
||||
messages.extend(more_messages)
|
||||
except Exception:
|
||||
import traceback
|
||||
|
||||
@@ -59,9 +62,10 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni
|
||||
chats = []
|
||||
if author_id:
|
||||
cids = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
|
||||
if isinstance(cids, list):
|
||||
if isinstance(cids, set):
|
||||
members_online = (await redis.execute("SMEMBERS", "authors-online")) or []
|
||||
cids = cids[offset : (offset + limit)]
|
||||
# TODO: add sort by chat.created_at with in-memory caching chats service
|
||||
cids = list(cids)[offset : (offset + limit)]
|
||||
lock = asyncio.Lock()
|
||||
if len(cids) == 0:
|
||||
print(f"[resolvers.load] no chats for user with id={author_id}")
|
||||
@@ -93,9 +97,9 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni
|
||||
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"]
|
||||
author_chats = (await redis.execute("SMEMBERS", "chats_by_author/" + str(author_id)))
|
||||
if isinstance(author_chats, list):
|
||||
author_chats = [c for c in author_chats]
|
||||
author_chats = await redis.execute("SMEMBERS", "chats_by_author/" + str(author_id))
|
||||
if isinstance(author_chats, set):
|
||||
author_chats = list(author_chats)
|
||||
messages = []
|
||||
by_chat = by.get("chat")
|
||||
if by_chat in author_chats:
|
||||
@@ -104,12 +108,13 @@ async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
|
||||
return {"messages": [], "error": "chat not exist"}
|
||||
# everyone's messages in filtered chat
|
||||
messages = await load_messages(by_chat, limit, offset)
|
||||
return {
|
||||
"messages": sorted(
|
||||
[m for m in messages if m and m.get("created_at")],
|
||||
key=lambda m: m.get("created_at"),
|
||||
),
|
||||
"error": None,
|
||||
}
|
||||
else:
|
||||
return {"error": "Cannot access messages of this chat"}
|
||||
if isinstance(messages, list):
|
||||
sorted_messages = [m for m in messages if m and m.get("created_at")]
|
||||
return {
|
||||
"messages": sorted(
|
||||
sorted_messages,
|
||||
key=lambda m: m.get("created_at"),
|
||||
),
|
||||
"error": None,
|
||||
}
|
||||
return {"error": "Cannot get messages of this chat"}
|
||||
|
Reference in New Issue
Block a user