race-fix7

This commit is contained in:
Untone 2023-10-13 03:16:54 +03:00
parent 95bf812ae2
commit 55c75f2a2e

View File

@ -6,6 +6,9 @@ from services.auth import login_required
from services.schema import query
from .chats import create_chat
from .unread import get_unread_counter
import asyncio
lock = asyncio.Lock()
# NOTE: not an API handler
@ -55,35 +58,28 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0):
cids.append(r["chat"]["id"])
for cid in cids:
# Start a pipeline
pipeline = await redis.pipeline()
# Add the GET command to the pipeline
await pipeline.get(f"chats/{cid}")
# Execute the pipeline
result = await pipeline.execute()
# Get the result of the GET command
c = result[0]
print(f"got chat {c}")
if c:
c = json.loads(c)
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)
if a:
c["members"].append(
{
"id": a.id,
"slug": a.slug,
"userpic": a.userpic,
"name": a.name,
"lastSeen": a.lastSeen,
"online": a.id in members_online,
}
)
chats.append(c)
async with lock:
c = await redis.execute("GET", f"chats/{cid}")
if c:
c = json.loads(c)
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)
if a:
c["members"].append(
{
"id": a.id,
"slug": a.slug,
"userpic": a.userpic,
"name": a.name,
"lastSeen": a.lastSeen,
"online": a.id in members_online,
}
)
chats.append(c)
return {"chats": chats, "error": None}