inbox-debug-2
This commit is contained in:
parent
636b5446a2
commit
6e8c084816
|
@ -65,44 +65,49 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni
|
||||||
"""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"]
|
||||||
chats = []
|
chats = []
|
||||||
if author_id:
|
try:
|
||||||
logger.debug("got author", author_id)
|
if author_id:
|
||||||
cids = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
|
logger.debug("got author", author_id)
|
||||||
logger.debug("got cids", cids)
|
cids = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
|
||||||
members_online = (await redis.execute("SMEMBERS", "authors-online")) or [] # to show online status
|
logger.debug("got cids", cids)
|
||||||
logger.debug("members online", members_online)
|
members_online = (await redis.execute("SMEMBERS", "authors-online")) or [] # to show online status
|
||||||
if isinstance(cids, set):
|
logger.debug("members online", members_online)
|
||||||
# TODO: add sort by chat.created_at with in-memory caching chats service
|
if isinstance(cids, set):
|
||||||
cids = list(cids)[offset : (offset + limit)]
|
# TODO: add sort by chat.created_at with in-memory caching chats service
|
||||||
lock = asyncio.Lock()
|
cids = list(cids)[offset : (offset + limit)]
|
||||||
if len(cids) == 0:
|
lock = asyncio.Lock()
|
||||||
logger.debug(f"no chats for user with id={author_id}")
|
if len(cids) == 0:
|
||||||
r = await create_chat(None, info, members=[2]) # member with id = 2 is discours
|
logger.debug(f"no chats for user with id={author_id}")
|
||||||
logger.debug(f"created chat: {r['chat_id']}")
|
r = await create_chat(None, info, members=[2]) # member with id = 2 is discours
|
||||||
cids.append(r["chat"]["id"])
|
logger.debug(f"created chat: {r['chat_id']}")
|
||||||
|
cids.append(r["chat"]["id"])
|
||||||
|
|
||||||
logger.debug(f"getting data for {len(cids)} user's chats")
|
logger.debug(f"getting data for {len(cids)} user's chats")
|
||||||
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 = await redis.execute("GET", f"chats/{cid}")
|
||||||
if isinstance(chat_str, str):
|
if isinstance(chat_str, str):
|
||||||
logger.debug(f"redis GET by {cid}: {chat_str}")
|
logger.debug(f"redis GET by {cid}: {chat_str}")
|
||||||
c: ChatPayload = json.loads(chat_str)
|
c: ChatPayload = json.loads(chat_str)
|
||||||
c["messages"] = (await load_messages(cid, 5, 0)) or []
|
c["messages"] = (await load_messages(cid, 5, 0)) or []
|
||||||
c["unread"] = await get_unread_counter(cid, author_id)
|
c["unread"] = await get_unread_counter(cid, author_id)
|
||||||
member_ids = c["members"].copy()
|
member_ids = c["members"].copy()
|
||||||
c["members"] = []
|
c["members"] = []
|
||||||
for member_id in member_ids:
|
for member_id in member_ids:
|
||||||
a = CacheStorage.authors_by_id.get(member_id)
|
a = CacheStorage.authors_by_id.get(member_id)
|
||||||
if a:
|
if a:
|
||||||
a["online"] = a.get("id") in members_online
|
a["online"] = a.get("id") in members_online
|
||||||
c["members"].append(a)
|
c["members"].append(a)
|
||||||
else:
|
else:
|
||||||
logger.error(f"cant find author by id {member_id}")
|
logger.error(f"cant find author by id {member_id}")
|
||||||
chats.append(c)
|
chats.append(c)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logger.error(f"cant find chat by id {cid}")
|
logger.error(f"cant find chat by id {cid}")
|
||||||
|
except Exception as error:
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
traceback.print_exc()
|
||||||
return {"chats": chats, "error": None}
|
return {"chats": chats, "error": None}
|
||||||
|
|
||||||
|
|
||||||
|
@ -111,24 +116,30 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni
|
||||||
async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
|
async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
|
||||||
"""load :limit messages of :chat_id with :offset"""
|
"""load :limit messages of :chat_id with :offset"""
|
||||||
author_id = info.context["author_id"]
|
author_id = info.context["author_id"]
|
||||||
author_chats = await redis.execute("SMEMBERS", "chats_by_author/" + str(author_id))
|
author_chats = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
|
||||||
if isinstance(author_chats, set):
|
try:
|
||||||
author_chats = list(author_chats)
|
if isinstance(author_chats, set):
|
||||||
messages = []
|
author_chats = list(author_chats)
|
||||||
by_chat = by.get("chat")
|
messages = []
|
||||||
if by_chat in author_chats:
|
by_chat = by.get("chat")
|
||||||
chat = await redis.execute("GET", f"chats/{by_chat}")
|
if by_chat in author_chats:
|
||||||
if not chat:
|
chat = await redis.execute("GET", f"chats/{by_chat}")
|
||||||
return {"messages": [], "error": "chat not exist"}
|
if not chat:
|
||||||
# everyone's messages in filtered chat
|
return {"messages": [], "error": "chat not exist"}
|
||||||
messages = await load_messages(by_chat, limit, offset)
|
# everyone's messages in filtered chat
|
||||||
if isinstance(messages, list):
|
messages = await load_messages(by_chat, limit, offset)
|
||||||
sorted_messages = [m for m in messages if m and m.get("created_at")]
|
if isinstance(messages, list):
|
||||||
return {
|
sorted_messages = [m for m in messages if m and m.get("created_at")]
|
||||||
"messages": sorted(
|
return {
|
||||||
sorted_messages,
|
"messages": sorted(
|
||||||
key=lambda m: m.get("created_at"),
|
sorted_messages,
|
||||||
),
|
key=lambda m: m.get("created_at"),
|
||||||
"error": None,
|
),
|
||||||
}
|
"error": None,
|
||||||
|
}
|
||||||
|
|
||||||
|
except Exception as error:
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
traceback.print_exc()
|
||||||
return {"error": "Cannot get messages of this chat"}
|
return {"error": "Cannot get messages of this chat"}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user