core/resolvers/inbox/load.py

103 lines
3.1 KiB
Python
Raw Normal View History

2022-11-12 07:13:51 +00:00
import json
2022-11-15 02:36:30 +00:00
from datetime import datetime, timedelta
2022-11-16 06:35:51 +00:00
from auth.authenticate import login_required
2022-11-12 07:13:51 +00:00
from base.redis import redis
2022-11-15 02:36:30 +00:00
from base.resolvers import query
2022-11-12 07:13:51 +00:00
async def get_unread_counter(chat_id: str, user_slug: str):
try:
2022-11-16 06:35:51 +00:00
unread = await redis.execute("LLEN", f"chats/{chat_id}/unread/{user_slug}")
if unread:
return unread
2022-11-12 07:13:51 +00:00
except Exception:
return 0
async def get_total_unread_counter(user_slug: str):
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
2022-11-16 06:48:32 +00:00
unread = 0
if chats:
2022-11-16 06:47:08 +00:00
chats = json.loads(chats)
for chat_id in chats:
n = await get_unread_counter(chat_id, user_slug)
unread += n
2022-11-12 07:13:51 +00:00
return unread
2022-11-16 07:32:24 +00:00
async def load_messages(chatId: str, limit: int, offset: int):
''' load :limit messages for :chatId with :offset '''
2022-11-15 02:36:30 +00:00
messages = []
message_ids = await redis.lrange(
2022-11-16 07:32:24 +00:00
f"chats/{chatId}/message_ids", 0 - offset - limit, 0 - offset
2022-11-15 02:36:30 +00:00
)
if message_ids:
message_keys = [
f"chats/{chatId}/messages/{mid}" for mid in message_ids
]
messages = await redis.mget(*message_keys)
messages = [json.loads(msg) for msg in messages]
return {
"messages": messages,
"error": None
}
2022-11-12 07:13:51 +00:00
2022-11-15 02:36:30 +00:00
@query.field("loadChats")
@login_required
2022-11-16 07:32:24 +00:00
async def load_chats(_, info, limit: int, offset: int):
""" load :limit chats of current user with :offset """
2022-11-15 02:36:30 +00:00
user = info.context["request"].user
chats = await redis.execute("GET", f"chats_by_user/{user.slug}")
2022-11-12 07:13:51 +00:00
if chats:
2022-11-16 07:32:24 +00:00
chats = list(json.loads(chats))[offset:offset + limit]
2022-11-12 07:13:51 +00:00
if not chats:
chats = []
for c in chats:
2022-11-16 07:32:24 +00:00
c['messages'] = await load_messages(c['id'], limit, offset)
2022-11-15 02:36:30 +00:00
c['unread'] = await get_unread_counter(c['id'], user.slug)
2022-11-12 07:13:51 +00:00
return {
"chats": chats,
"error": None
}
2022-11-15 02:36:30 +00:00
@query.field("loadMessagesBy")
@login_required
2022-11-16 07:32:24 +00:00
async def load_messages_by(_, info, by, limit: int = 50, offset: int = 0):
''' load :amolimitunt messages of :chat_id with :offset '''
2022-11-15 02:36:30 +00:00
user = info.context["request"].user
my_chats = await redis.execute("GET", f"chats_by_user/{user.slug}")
chat_id = by.get('chat')
if chat_id:
chat = await redis.execute("GET", f"chats/{chat_id}")
if not chat:
return {
"error": "chat not exist"
}
2022-11-16 07:32:24 +00:00
messages = await load_messages(chat_id, limit, offset)
2022-11-15 02:36:30 +00:00
user_id = by.get('author')
if user_id:
chats = await redis.execute("GET", f"chats_by_user/{user_id}")
our_chats = list(set(chats) & set(my_chats))
for c in our_chats:
2022-11-16 07:32:24 +00:00
messages += await load_messages(c, limit, offset)
2022-11-15 02:36:30 +00:00
body_like = by.get('body')
if body_like:
for c in my_chats:
2022-11-16 07:32:24 +00:00
mmm = await load_messages(c, limit, offset)
2022-11-15 02:36:30 +00:00
for m in mmm:
if body_like in m["body"]:
messages.append(m)
days = by.get("days")
if days:
messages = filter(
lambda m: datetime.now() - int(m["createdAt"]) < timedelta(days=by.get("days")),
messages
)
2022-11-12 07:13:51 +00:00
return {
"messages": messages,
"error": None
}