core/resolvers/inbox/load.py

110 lines
3.4 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-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-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-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
2022-11-23 09:57:58 +00:00
if user:
chats = await redis.execute("GET", f"chats_by_user/{user.slug}")
if chats:
chats = list(json.loads(chats))[offset:offset + limit]
if not chats:
chats = []
for c in chats:
c['messages'] = await load_messages(c['id'], limit, offset)
c['unread'] = await get_unread_counter(c['id'], user.slug)
return {
"chats": chats,
"error": None
}
else:
return {
"error": "please login",
"chats": []
}
2022-11-12 07:13:51 +00:00
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
}
2022-11-21 08:13:57 +00:00
@query.field("loadRecipients")
async def load_recipients(_, info, limit=50, offset=0):
chat_users = []
user = info.context["request"].user
try:
chat_users += await followed_authors(user.slug)
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
}