precached-authors-fix
Some checks are pending
deploy / deploy (push) Waiting to run

This commit is contained in:
2023-12-19 11:25:06 +03:00
parent b2040099a8
commit c3a6ecd3ae
4 changed files with 43 additions and 48 deletions

View File

@@ -4,7 +4,7 @@ from typing import Any, Dict, List, Union
from resolvers.load import load_messages
from services.auth import login_required
from services.core import get_all_authors
from services.core import authors_by_id
from services.rediscache import redis
from services.schema import query
@@ -12,27 +12,24 @@ from services.schema import query
@query.field("search_recipients")
@login_required
async def search_recipients(_, info, text: str, limit: int = 50, offset: int = 0):
result = []
result = set([])
# TODO: maybe redis scan?
author_id = info.context["author_id"]
existed_chats = await redis.execute("SMEMBERS", f"/chats_by_author/{author_id}")
authors = await get_all_authors()
members = {a["id"]: a for a in authors}
if existed_chats:
for chat_id in list(json.loads(existed_chats))[offset : (offset + limit)]:
members_ids = await redis.execute("GET", f"/chats/{chat_id}/members")
for member_id in members_ids:
author = members.get(member_id)
author = authors_by_id.get(member_id)
if author:
if author["name"].startswith(text):
if author not in result:
result.append(author)
result.add(author)
more_amount = limit - len(result)
if more_amount > 0:
result += authors[0:more_amount]
result.update(authors_by_id.values()[0:more_amount])
return {"members": list(result), "error": None}