load-by pattern, shoutscache removed
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import json
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from base.redis import redis
|
||||
from base.resolvers import query
|
||||
from auth.authenticate import login_required
|
||||
|
||||
|
||||
async def get_unread_counter(chat_id: str, user_slug: str):
|
||||
@@ -24,23 +26,6 @@ async def get_total_unread_counter(user_slug: str):
|
||||
return unread
|
||||
|
||||
|
||||
async def load_user_chats(slug, offset: int, amount: int):
|
||||
""" load :amount chats of :slug user with :offset """
|
||||
|
||||
chats = await redis.execute("GET", f"chats_by_user/{slug}")
|
||||
if chats:
|
||||
chats = list(json.loads(chats))[offset:offset + amount]
|
||||
if not chats:
|
||||
chats = []
|
||||
for c in chats:
|
||||
c['messages'] = await load_messages(c['id'])
|
||||
c['unread'] = await get_unread_counter(c['id'], slug)
|
||||
return {
|
||||
"chats": chats,
|
||||
"error": None
|
||||
}
|
||||
|
||||
|
||||
async def load_messages(chatId: str, offset: int, amount: int):
|
||||
''' load :amount messages for :chatId with :offset '''
|
||||
messages = []
|
||||
@@ -57,3 +42,61 @@ async def load_messages(chatId: str, offset: int, amount: int):
|
||||
"messages": messages,
|
||||
"error": None
|
||||
}
|
||||
|
||||
|
||||
@query.field("loadChats")
|
||||
@login_required
|
||||
async def load_chats(_, info, offset: int, amount: int):
|
||||
""" load :amount chats of current user with :offset """
|
||||
user = info.context["request"].user
|
||||
chats = await redis.execute("GET", f"chats_by_user/{user.slug}")
|
||||
if chats:
|
||||
chats = list(json.loads(chats))[offset:offset + amount]
|
||||
if not chats:
|
||||
chats = []
|
||||
for c in chats:
|
||||
c['messages'] = await load_messages(c['id'])
|
||||
c['unread'] = await get_unread_counter(c['id'], user.slug)
|
||||
return {
|
||||
"chats": chats,
|
||||
"error": None
|
||||
}
|
||||
|
||||
|
||||
@query.field("loadMessagesBy")
|
||||
@login_required
|
||||
async def load_messages_by(_, info, by, offset: int = 0, amount: int = 50):
|
||||
''' load :amount messages of :chat_id with :offset '''
|
||||
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"
|
||||
}
|
||||
messages = await load_messages(chat_id, offset, amount)
|
||||
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:
|
||||
messages += await load_messages(c, offset, amount)
|
||||
body_like = by.get('body')
|
||||
if body_like:
|
||||
for c in my_chats:
|
||||
mmm = await load_messages(c, offset, amount)
|
||||
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
|
||||
)
|
||||
return {
|
||||
"messages": messages,
|
||||
"error": None
|
||||
}
|
||||
|
Reference in New Issue
Block a user