core/resolvers/inbox/load.py

141 lines
4.9 KiB
Python
Raw Normal View History

2023-10-26 20:38:31 +00:00
from .unread import get_unread_counter
2022-11-16 06:35:51 +00:00
from auth.authenticate import login_required
2022-12-01 14:45:19 +00:00
from auth.credentials import AuthCredentials
2022-11-21 08:13:57 +00:00
from base.orm import local_session
2023-10-26 17:56:42 +00:00
from base.redis import redis
2022-11-15 02:36:30 +00:00
from base.resolvers import query
2022-11-21 08:13:57 +00:00
from orm.user import User
from resolvers.zine.profile import followed_authors
2023-10-26 17:56:42 +00:00
2023-10-26 20:38:31 +00:00
import json
2022-11-12 07:13:51 +00:00
2023-10-26 17:56:42 +00:00
# from datetime import datetime, timedelta, timezone
2022-11-12 07:13:51 +00:00
2022-12-12 06:50:53 +00:00
async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=[]):
2023-10-26 20:38:31 +00:00
"""load :limit messages for :chat_id with :offset"""
2022-11-15 02:36:30 +00:00
messages = []
2022-12-12 06:50:53 +00:00
message_ids = []
if ids:
message_ids += ids
2022-12-05 07:10:49 +00:00
try:
2022-12-12 06:50:53 +00:00
if limit:
2023-10-26 17:56:42 +00:00
mids = await redis.lrange(f"chats/{chat_id}/message_ids", offset, offset + limit)
2022-12-12 07:35:12 +00:00
mids = [mid.decode("utf-8") for mid in mids]
message_ids += mids
2022-12-05 07:10:49 +00:00
except Exception as e:
print(e)
2022-11-15 02:36:30 +00:00
if message_ids:
2022-12-12 07:35:12 +00:00
message_keys = [f"chats/{chat_id}/messages/{mid}" for mid in message_ids]
2022-11-15 02:36:30 +00:00
messages = await redis.mget(*message_keys)
2023-10-26 20:38:31 +00:00
messages = [json.loads(msg.decode("utf-8")) for msg in messages]
2022-12-12 06:50:53 +00:00
replies = []
for m in messages:
2023-10-26 20:38:31 +00:00
rt = m.get("replyTo")
2022-12-12 06:50:53 +00:00
if rt:
rt = int(rt)
if rt not in message_ids:
replies.append(rt)
if replies:
messages += await load_messages(chat_id, limit=0, ids=replies)
2022-12-05 07:10:49 +00:00
return messages
2022-11-15 02:36:30 +00:00
2022-11-12 07:13:51 +00:00
2022-11-15 02:36:30 +00:00
@query.field("loadChats")
@login_required
async def load_chats(_, info, limit: int = 50, offset: int = 0):
2023-10-26 17:56:42 +00:00
"""load :limit chats of current user with :offset"""
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
cids = await redis.execute("SMEMBERS", "chats_by_user/" + str(auth.user_id))
if cids:
2023-10-26 17:56:42 +00:00
cids = list(cids)[offset : offset + limit]
if not cids:
2023-10-26 20:38:31 +00:00
print("[inbox.load] no chats were found")
cids = []
2023-02-20 17:38:59 +00:00
onliners = await redis.execute("SMEMBERS", "users-online")
if not onliners:
onliners = []
chats = []
for cid in cids:
2022-12-05 07:10:49 +00:00
cid = cid.decode("utf-8")
c = await redis.execute("GET", "chats/" + cid)
if c:
2022-11-27 12:12:08 +00:00
c = dict(json.loads(c))
2023-10-26 20:38:31 +00:00
c["messages"] = await load_messages(cid, 5, 0)
c["unread"] = await get_unread_counter(cid, auth.user_id)
2022-11-27 12:12:08 +00:00
with local_session() as session:
2023-10-26 20:38:31 +00:00
c["members"] = []
2022-12-02 11:06:20 +00:00
for uid in c["users"]:
a = session.query(User).where(User.id == uid).first()
if a:
2023-10-26 20:38:31 +00:00
c["members"].append(
2023-10-26 17:56:42 +00:00
{
"id": a.id,
"slug": a.slug,
"userpic": a.userpic,
"name": a.name,
"lastSeen": a.lastSeen,
"online": a.id in onliners,
}
)
2022-12-04 10:18:56 +00:00
chats.append(c)
2023-10-26 17:56:42 +00:00
return {"chats": chats, "error": None}
2022-11-12 07:13:51 +00:00
2022-11-15 02:36:30 +00:00
@query.field("loadMessagesBy")
@login_required
2022-11-27 13:39:16 +00:00
async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
2023-10-26 20:38:31 +00:00
"""load :limit messages of :chat_id with :offset"""
2022-11-27 12:12:08 +00:00
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2022-12-05 07:10:49 +00:00
userchats = await redis.execute("SMEMBERS", "chats_by_user/" + str(auth.user_id))
2023-10-26 20:38:31 +00:00
userchats = [c.decode("utf-8") for c in userchats]
2022-12-05 07:10:49 +00:00
# print('[inbox] userchats: %r' % userchats)
if userchats:
# print('[inbox] loading messages by...')
messages = []
2023-10-26 20:38:31 +00:00
by_chat = by.get("chat")
2022-12-05 07:10:49 +00:00
if by_chat in userchats:
chat = await redis.execute("GET", f"chats/{by_chat}")
# print(chat)
if not chat:
2023-10-26 17:56:42 +00:00
return {"messages": [], "error": "chat not exist"}
2022-12-05 07:10:49 +00:00
# everyone's messages in filtered chat
messages = await load_messages(by_chat, limit, offset)
2023-10-26 20:38:31 +00:00
return {
"messages": sorted(list(messages), key=lambda m: m["createdAt"]),
"error": None,
}
2022-12-05 07:10:49 +00:00
else:
2023-10-26 17:56:42 +00:00
return {"error": "Cannot access messages of this chat"}
2022-11-21 08:13:57 +00:00
@query.field("loadRecipients")
async def load_recipients(_, info, limit=50, offset=0):
chat_users = []
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2023-02-20 17:38:59 +00:00
onliners = await redis.execute("SMEMBERS", "users-online")
if not onliners:
onliners = []
2022-11-21 08:13:57 +00:00
try:
2022-12-01 14:45:19 +00:00
chat_users += await followed_authors(auth.user_id)
2022-11-21 08:13:57 +00:00
limit = limit - len(chat_users)
except Exception:
pass
with local_session() as session:
chat_users += session.query(User).where(User.emailConfirmed).limit(limit).offset(offset)
members = []
for a in chat_users:
2023-10-26 17:56:42 +00:00
members.append(
{
"id": a.id,
"slug": a.slug,
"userpic": a.userpic,
"name": a.name,
"lastSeen": a.lastSeen,
"online": a.id in onliners,
}
)
return {"members": members, "error": None}