load-recipients-fix
All checks were successful
deploy / deploy (push) Successful in 1m8s

This commit is contained in:
Untone 2023-12-18 20:59:20 +03:00
parent 83fba058ab
commit 93c1727be3
2 changed files with 33 additions and 34 deletions

View File

@ -53,10 +53,11 @@ async def load_messages(
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""" """load :limit chats of current user with :offset"""
author_id = info.context["author_id"] author_id = info.context["author_id"]
cids = (await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")) or [] cids = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
chats = []
if cids:
members_online = (await redis.execute("SMEMBERS", "authors-online")) 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() lock = asyncio.Lock()
if len(cids) == 0: if len(cids) == 0:
print(f"[resolvers.load] no chats for user with id={author_id}") print(f"[resolvers.load] no chats for user with id={author_id}")
@ -67,7 +68,7 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni
authors = {a["id"]: a for a in all_authors} authors = {a["id"]: a for a in all_authors}
for cid in cids: for cid in cids:
async with lock: async with lock:
chat_str = await redis.execute("GET", f"chats/{cid}") chat_str: str = await redis.execute("GET", f"chats/{cid}")
print(f"[resolvers.load] redis GET by {cid}: {chat_str}") print(f"[resolvers.load] redis GET by {cid}: {chat_str}")
if chat_str: if chat_str:
c: ChatPayload = json.loads(chat_str) c: ChatPayload = json.loads(chat_str)
@ -114,15 +115,15 @@ async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
@query.field("load_recipients") @query.field("load_recipients")
async def load_recipients(_, _info, limit=50, offset=0): async def load_recipients(_, _info, limit=50, offset=0):
"""load possible chat participants""" """load possible chat participants"""
onliners = (await redis.execute("SMEMBERS", "authors-online")) or [] onliners: List[int] = (await redis.execute("SMEMBERS", "authors-online")) or []
r = [] r = []
all_authors: List[ChatMember] = await get_all_authors() all_authors: List[ChatMember] = await get_all_authors(limit, offset)
my_followings: List[ChatMember] = await get_my_followed() my_followings: List[ChatMember] = await get_my_followed()
if all_authors: if all_authors:
if len(my_followings) < limit: if len(my_followings) < limit:
my_followings = my_followings + list(all_authors)[0 : limit - len(my_followings)] my_followings = my_followings + list(all_authors)[offset : limit - len(my_followings)]
for a in my_followings: for a in my_followings:
a["online"] = a["id"] in onliners a["online"] = bool(a["id"] in list(onliners))
r.append(a) r.append(a)
# NOTE: maybe sort members here # NOTE: maybe sort members here

View File

@ -38,9 +38,7 @@ async def get_my_followed() -> List[ChatMember]:
"variables": None, "variables": None,
} }
result = await _request_endpoint(query_name, gql) return await _request_endpoint(query_name, gql) or []
return result.get("authors", [])
async def get_author(user: str = ""): async def get_author(user: str = ""):