2022-11-12 07:13:51 +00:00
|
|
|
import json
|
2022-11-23 14:09:35 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
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-12 07:13:51 +00:00
|
|
|
from base.redis import redis
|
2022-11-21 08:13:57 +00:00
|
|
|
from base.orm import local_session
|
2022-11-15 02:36:30 +00:00
|
|
|
from base.resolvers import query
|
2022-11-27 13:14:17 +00:00
|
|
|
from base.exceptions import ObjectNotExist, Unauthorized
|
2022-11-21 08:13:57 +00:00
|
|
|
from orm.user import User
|
|
|
|
from resolvers.zine.profile import followed_authors
|
|
|
|
from .unread import get_unread_counter
|
2022-11-12 07:13:51 +00:00
|
|
|
|
|
|
|
|
2022-11-25 22:35:42 +00:00
|
|
|
async def load_messages(chat_id: str, limit: int, offset: int):
|
|
|
|
''' load :limit messages for :chat_id with :offset '''
|
2022-11-15 02:36:30 +00:00
|
|
|
messages = []
|
|
|
|
message_ids = await redis.lrange(
|
2022-11-25 22:35:42 +00:00
|
|
|
f"chats/{chat_id}/message_ids", offset + limit, offset
|
2022-11-15 02:36:30 +00:00
|
|
|
)
|
|
|
|
if message_ids:
|
|
|
|
message_keys = [
|
2022-11-25 22:35:42 +00:00
|
|
|
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)
|
|
|
|
messages = [json.loads(msg) for msg in messages]
|
2022-11-25 22:35:42 +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
|
2022-11-25 22:35:42 +00:00
|
|
|
async def load_chats(_, info, limit: int = 50, offset: int = 0):
|
2022-11-16 07:32:24 +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))
|
2022-11-25 22:35:42 +00:00
|
|
|
if cids:
|
|
|
|
cids = list(cids)[offset:offset + limit]
|
|
|
|
if not cids:
|
|
|
|
print('[inbox.load] no chats were found')
|
|
|
|
cids = []
|
|
|
|
chats = []
|
|
|
|
for cid in cids:
|
|
|
|
c = await redis.execute("GET", "chats/" + cid.decode("utf-8"))
|
|
|
|
if c:
|
2022-11-27 12:12:08 +00:00
|
|
|
c = dict(json.loads(c))
|
2022-11-27 13:39:16 +00:00
|
|
|
c['messages'] = await load_messages(cid, 5, 0)
|
2022-12-01 14:45:19 +00:00
|
|
|
c['unread'] = await get_unread_counter(cid, auth.user_id)
|
2022-11-27 12:12:08 +00:00
|
|
|
with local_session() as session:
|
|
|
|
c['members'] = []
|
2022-11-27 13:14:17 +00:00
|
|
|
for userslug in c["users"]:
|
|
|
|
a = session.query(User).where(User.slug == userslug).first().dict()
|
2022-11-27 12:12:08 +00:00
|
|
|
c['members'].append({
|
2022-11-27 13:14:17 +00:00
|
|
|
"slug": userslug,
|
2022-11-27 12:12:08 +00:00
|
|
|
"userpic": a["userpic"],
|
|
|
|
"name": a["name"],
|
|
|
|
"lastSeen": a["lastSeen"],
|
|
|
|
})
|
2022-11-25 22:35:42 +00:00
|
|
|
chats.append(c)
|
|
|
|
return {
|
|
|
|
"chats": chats,
|
|
|
|
"error": None
|
|
|
|
}
|
2022-11-12 07:13:51 +00:00
|
|
|
|
|
|
|
|
2022-12-01 14:45:19 +00:00
|
|
|
async def search_user_chats(by, messages: set, user_id: int, limit, offset):
|
2022-11-27 13:39:16 +00:00
|
|
|
cids = set([])
|
|
|
|
by_author = by.get('author')
|
|
|
|
body_like = by.get('body')
|
2022-12-01 14:45:19 +00:00
|
|
|
cids.unioin(set(await redis.execute("SMEMBERS", "chats_by_user/" + str(user_id))))
|
2022-11-27 13:39:16 +00:00
|
|
|
if by_author:
|
|
|
|
# all author's messages
|
|
|
|
cids.union(set(await redis.execute("SMEMBERS", f"chats_by_user/{by_author}")))
|
|
|
|
# author's messages in filtered chat
|
|
|
|
messages.union(set(filter(lambda m: m["author"] == by_author, list(messages))))
|
|
|
|
for c in cids:
|
|
|
|
messages.union(set(await load_messages(c, limit, offset)))
|
|
|
|
if body_like:
|
|
|
|
# search in all messages in all user's chats
|
|
|
|
for c in cids:
|
2022-11-27 19:04:03 +00:00
|
|
|
# FIXME: user redis scan here
|
2022-11-27 13:39:16 +00:00
|
|
|
mmm = set(await load_messages(c, limit, offset))
|
|
|
|
for m in mmm:
|
|
|
|
if body_like in m["body"]:
|
|
|
|
messages.add(m)
|
|
|
|
else:
|
|
|
|
# search in chat's messages
|
|
|
|
messages.union(set(filter(lambda m: body_like in m["body"], list(messages))))
|
|
|
|
return messages
|
|
|
|
|
|
|
|
|
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):
|
2022-11-27 13:14:17 +00:00
|
|
|
''' load :limit messages of :chat_id with :offset '''
|
2022-11-27 12:12:08 +00:00
|
|
|
messages = set([])
|
2022-11-25 22:35:42 +00:00
|
|
|
by_chat = by.get('chat')
|
|
|
|
if by_chat:
|
|
|
|
chat = await redis.execute("GET", f"chats/{by_chat}")
|
2022-11-15 02:36:30 +00:00
|
|
|
if not chat:
|
2022-11-25 22:35:42 +00:00
|
|
|
raise ObjectNotExist("Chat not exists")
|
2022-11-27 12:12:08 +00:00
|
|
|
# everyone's messages in filtered chat
|
|
|
|
messages.union(set(await load_messages(by_chat, limit, offset)))
|
|
|
|
|
2022-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
|
|
|
|
|
|
|
if len(messages) == 0:
|
|
|
|
# FIXME
|
|
|
|
messages.union(search_user_chats(by, messages, auth.user_id, limit, offset))
|
2022-11-27 12:12:08 +00:00
|
|
|
|
2022-11-15 02:36:30 +00:00
|
|
|
days = by.get("days")
|
|
|
|
if days:
|
2022-11-27 12:12:08 +00:00
|
|
|
messages.union(set(filter(
|
2022-11-23 14:09:35 +00:00
|
|
|
lambda m: datetime.now(tz=timezone.utc) - int(m["createdAt"]) < timedelta(days=by.get("days")),
|
2022-11-27 13:39:16 +00:00
|
|
|
list(messages)
|
2022-11-27 12:12:08 +00:00
|
|
|
)))
|
2022-11-12 07:13:51 +00:00
|
|
|
return {
|
2022-11-27 13:39:16 +00:00
|
|
|
"messages": sorted(
|
|
|
|
lambda m: m.createdAt,
|
|
|
|
list(messages)
|
|
|
|
),
|
2022-11-12 07:13:51 +00:00
|
|
|
"error": None
|
|
|
|
}
|
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
|
|
|
|
|
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)
|
|
|
|
return {
|
|
|
|
"members": chat_users,
|
|
|
|
"error": None
|
|
|
|
}
|