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,34 +53,35 @@ 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}")
members_online = (await redis.execute("SMEMBERS", "authors-online")) or []
cids = list(cids)[offset : (offset + limit)]
chats = [] chats = []
lock = asyncio.Lock() if cids:
if len(cids) == 0: members_online = (await redis.execute("SMEMBERS", "authors-online")) or []
print(f"[resolvers.load] no chats for user with id={author_id}") cids = list(cids)[offset : (offset + limit)]
r = await create_chat(None, info, members=[1]) # member with id = 1 is discours lock = asyncio.Lock()
print(f"[resolvers.load] created chat: {r['chat_id']}") if len(cids) == 0:
cids.append(r["chat"]["id"]) print(f"[resolvers.load] no chats for user with id={author_id}")
all_authors: List[ChatMember] = await get_all_authors() r = await create_chat(None, info, members=[1]) # member with id = 1 is discours
authors = {a["id"]: a for a in all_authors} print(f"[resolvers.load] created chat: {r['chat_id']}")
for cid in cids: cids.append(r["chat"]["id"])
async with lock: all_authors: List[ChatMember] = await get_all_authors()
chat_str = await redis.execute("GET", f"chats/{cid}") authors = {a["id"]: a for a in all_authors}
print(f"[resolvers.load] redis GET by {cid}: {chat_str}") for cid in cids:
if chat_str: async with lock:
c: ChatPayload = json.loads(chat_str) chat_str: str = await redis.execute("GET", f"chats/{cid}")
c["messages"] = await load_messages(cid, 5, 0) print(f"[resolvers.load] redis GET by {cid}: {chat_str}")
c["unread"] = await get_unread_counter(cid, author_id) if chat_str:
member_ids = c["members"].copy() c: ChatPayload = json.loads(chat_str)
c["members"] = [] c["messages"] = await load_messages(cid, 5, 0)
for member_id in member_ids: c["unread"] = await get_unread_counter(cid, author_id)
a = authors.get(member_id) member_ids = c["members"].copy()
if a: c["members"] = []
a["online"] = a.get("id") in members_online for member_id in member_ids:
c["members"].append(a) a = authors.get(member_id)
chats.append(c) if a:
a["online"] = a.get("id") in members_online
c["members"].append(a)
chats.append(c)
return {"chats": chats, "error": None} return {"chats": chats, "error": None}
@ -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 = ""):