inspected
This commit is contained in:
@@ -4,16 +4,21 @@ from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
from services.auth import login_required
|
||||
from services.core import get_author, get_network
|
||||
from services.redis import redis
|
||||
from services.rediscache import redis
|
||||
from services.schema import query
|
||||
from validators.inbox import Message, Chat, ChatMember
|
||||
from validators.chat import Message, ChatPayload
|
||||
from validators.member import ChatMember
|
||||
from .chats import create_chat
|
||||
from .unread import get_unread_counter
|
||||
|
||||
|
||||
async def get_unread_counter(chat_id: str, author_id: int) -> int:
|
||||
unread = await redis.execute("LLEN", f"chats/{chat_id}/unread/{author_id}")
|
||||
return unread or 0
|
||||
|
||||
|
||||
# NOTE: not an API handler
|
||||
async def load_messages(
|
||||
chat_id: str, limit: int = 5, offset: int = 0, ids: Optional[List[str]] = None
|
||||
chat_id: str, limit: int = 5, offset: int = 0, ids: Optional[List[int]] = None
|
||||
) -> List[Message]:
|
||||
"""load :limit messages for :chat_id with :offset"""
|
||||
if ids is None:
|
||||
@@ -22,11 +27,7 @@ async def load_messages(
|
||||
try:
|
||||
message_ids = [] + ids
|
||||
if limit:
|
||||
mids = (
|
||||
await redis.lrange(
|
||||
f"chats/{chat_id}/message_ids", offset, offset + limit
|
||||
)
|
||||
) or []
|
||||
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:
|
||||
@@ -49,14 +50,12 @@ async def load_messages(
|
||||
|
||||
@query.field("loadChats")
|
||||
@login_required
|
||||
async def load_chats(
|
||||
_, info, limit: int = 50, offset: int = 0
|
||||
) -> Dict[str, Union[List[Dict[str, Any]], None]]:
|
||||
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 []
|
||||
members_online = (await redis.execute("SMEMBERS", "authors-online")) or []
|
||||
cids = list(cids)[offset: (offset + limit)]
|
||||
cids = list(cids)[offset : (offset + limit)]
|
||||
chats = []
|
||||
lock = asyncio.Lock()
|
||||
if len(cids) == 0:
|
||||
@@ -66,16 +65,16 @@ async def load_chats(
|
||||
cids.append(r["chat"]["id"])
|
||||
for cid in cids:
|
||||
async with lock:
|
||||
c = await redis.execute("GET", f"chats/{cid}")
|
||||
print(f"[resolvers.load] redis GET by {cid}: {c}")
|
||||
if c:
|
||||
c: Chat = json.loads(c)
|
||||
chat_str = await redis.execute("GET", f"chats/{cid}")
|
||||
print(f"[resolvers.load] redis GET by {cid}: {chat_str}")
|
||||
if chat_str:
|
||||
c: ChatPayload = json.loads(chat_str)
|
||||
c["messages"] = await load_messages(cid, 5, 0)
|
||||
c["unread"] = await get_unread_counter(cid, author_id)
|
||||
member_ids = c["members"].copy()
|
||||
c["members"] = []
|
||||
for member_id in member_ids:
|
||||
a = await get_author(member_id)
|
||||
a: ChatMember = await get_author(member_id)
|
||||
if a:
|
||||
a["online"] = a.get("id") in members_online
|
||||
c["members"].append(a)
|
||||
@@ -88,9 +87,7 @@ async def load_chats(
|
||||
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))
|
||||
) or []
|
||||
user_chats = (await redis.execute("SMEMBERS", "chats_by_author/" + str(author_id))) or []
|
||||
user_chats = [c for c in user_chats]
|
||||
if user_chats:
|
||||
messages = []
|
||||
|
Reference in New Issue
Block a user