2023-10-03 14:15:17 +00:00
|
|
|
import json
|
|
|
|
|
2023-10-11 19:12:55 +00:00
|
|
|
from services.core import get_author, get_network
|
2023-10-03 14:15:17 +00:00
|
|
|
from services.redis import redis
|
|
|
|
from services.auth import login_required
|
2023-10-04 21:43:07 +00:00
|
|
|
from services.schema import query
|
2023-10-03 14:15:17 +00:00
|
|
|
from .chats import create_chat
|
|
|
|
from .unread import get_unread_counter
|
|
|
|
|
|
|
|
|
|
|
|
# NOTE: not an API handler
|
2023-10-11 19:12:55 +00:00
|
|
|
async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=None):
|
2023-10-03 14:15:17 +00:00
|
|
|
"""load :limit messages for :chat_id with :offset"""
|
2023-10-11 19:12:55 +00:00
|
|
|
if ids is None:
|
|
|
|
ids = []
|
2023-10-03 14:15:17 +00:00
|
|
|
messages = []
|
|
|
|
try:
|
|
|
|
message_ids = [] + ids
|
|
|
|
if limit:
|
|
|
|
mids = await redis.lrange(
|
|
|
|
f"chats/{chat_id}/message_ids", offset, offset + limit
|
|
|
|
)
|
|
|
|
mids = [mid.decode("utf-8") for mid in mids]
|
|
|
|
message_ids += mids
|
|
|
|
if message_ids:
|
|
|
|
message_keys = [f"chats/{chat_id}/messages/{mid}" for mid in message_ids]
|
|
|
|
messages = await redis.mget(*message_keys)
|
|
|
|
messages = [json.loads(msg.decode("utf-8")) for msg in messages]
|
|
|
|
replies = []
|
|
|
|
for m in messages:
|
|
|
|
rt = m.get("replyTo")
|
|
|
|
if rt:
|
|
|
|
rt = int(rt)
|
|
|
|
if rt not in message_ids:
|
|
|
|
replies.append(rt)
|
|
|
|
if replies:
|
2023-10-11 19:12:55 +00:00
|
|
|
messages += await load_messages(chat_id, offset, limit, replies)
|
2023-10-03 14:15:17 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
return messages
|
|
|
|
|
|
|
|
|
|
|
|
@query.field("loadChats")
|
|
|
|
@login_required
|
|
|
|
async def load_chats(_, info, limit: int = 50, offset: int = 0):
|
|
|
|
"""load :limit chats of current user with :offset"""
|
|
|
|
author_id = info.context["author_id"]
|
2023-10-12 23:20:10 +00:00
|
|
|
cids = (list(await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")) or []
|
|
|
|
)[offset:(offset + limit)]
|
2023-10-03 14:15:17 +00:00
|
|
|
members_online = (await redis.execute("SMEMBERS", "authors-online")) or []
|
|
|
|
chats = []
|
|
|
|
if len(cids) == 0:
|
2023-10-12 12:28:12 +00:00
|
|
|
print(f"[resolvers.load] no chats for user with id={author_id}, create one with Discours (id=2)")
|
2023-10-12 23:20:10 +00:00
|
|
|
r = await create_chat(None, info, members=[2]) # member with id = 2 is discours
|
2023-10-03 14:15:17 +00:00
|
|
|
cids.append(r["chat"]["id"])
|
2023-10-04 20:52:43 +00:00
|
|
|
for cid in cids:
|
2023-10-11 18:34:45 +00:00
|
|
|
c = await redis.execute("GET", f"chats/{cid}")
|
2023-10-04 20:52:43 +00:00
|
|
|
if c:
|
|
|
|
c = dict(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)
|
2023-10-03 14:15:17 +00:00
|
|
|
return {"chats": chats, "error": None}
|
|
|
|
|
|
|
|
|
|
|
|
@query.field("loadMessagesBy")
|
|
|
|
@login_required
|
|
|
|
async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
|
|
|
|
"""load :limit messages of :chat_id with :offset"""
|
|
|
|
author_id = info.context["author_id"]
|
|
|
|
user_chats = await redis.execute("SMEMBERS", "chats_by_author/" + str(author_id))
|
|
|
|
user_chats = [c.decode("utf-8") for c in user_chats]
|
|
|
|
if user_chats:
|
|
|
|
messages = []
|
|
|
|
by_chat = by.get("chat")
|
|
|
|
if by_chat in user_chats:
|
|
|
|
chat = await redis.execute("GET", f"chats/{by_chat}")
|
|
|
|
if not chat:
|
|
|
|
return {"messages": [], "error": "chat not exist"}
|
|
|
|
# everyone's messages in filtered chat
|
|
|
|
messages = await load_messages(by_chat, limit, offset)
|
|
|
|
return {
|
|
|
|
"messages": sorted(list(messages), key=lambda m: m["createdAt"]),
|
|
|
|
"error": None,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return {"error": "Cannot access messages of this chat"}
|
|
|
|
|
|
|
|
|
|
|
|
@query.field("loadRecipients")
|
|
|
|
async def load_recipients(_, _info, limit=50, offset=0):
|
|
|
|
"""load possible chat participants"""
|
|
|
|
onliners = (await redis.execute("SMEMBERS", "authors-online")) or []
|
|
|
|
members = []
|
2023-10-11 19:12:55 +00:00
|
|
|
all_authors = await get_network(limit, offset)
|
|
|
|
for a in all_authors:
|
|
|
|
members.append(
|
|
|
|
{
|
|
|
|
"id": a.id,
|
|
|
|
"slug": a.slug,
|
|
|
|
"userpic": a.userpic,
|
|
|
|
"name": a.name,
|
|
|
|
"lastSeen": a.lastSeen,
|
|
|
|
"online": a.id in onliners,
|
|
|
|
}
|
|
|
|
)
|
2023-10-03 14:15:17 +00:00
|
|
|
|
2023-10-11 19:12:55 +00:00
|
|
|
# NOTE: maybe sort members here
|
2023-10-03 14:15:17 +00:00
|
|
|
|
2023-10-11 19:12:55 +00:00
|
|
|
return {"members": members, "error": None}
|