inbox fixes

This commit is contained in:
tonyrewin 2022-11-27 16:39:16 +03:00
parent 8377043286
commit d1caded052
2 changed files with 36 additions and 30 deletions

View File

@ -31,7 +31,7 @@ async def update_chat(_, info, chat_new: dict):
"title": chat_new.get("title", chat["title"]), "title": chat_new.get("title", chat["title"]),
"description": chat_new.get("description", chat["description"]), "description": chat_new.get("description", chat["description"]),
"updatedAt": int(datetime.now(tz=timezone.utc).timestamp()), "updatedAt": int(datetime.now(tz=timezone.utc).timestamp()),
"admins": chat_new.get("admins", chat["admins"]), "admins": chat_new.get("admins", chat.get("admins") or []),
"users": chat_new.get("users", chat["users"]) "users": chat_new.get("users", chat["users"])
}) })
await redis.execute("SET", f"chats/{chat.id}", json.dumps(chat)) await redis.execute("SET", f"chats/{chat.id}", json.dumps(chat))
@ -76,7 +76,7 @@ async def create_chat(_, info, title="", members=[]):
"createdBy": user.slug, "createdBy": user.slug,
"createdAt": int(datetime.now(tz=timezone.utc).timestamp()), "createdAt": int(datetime.now(tz=timezone.utc).timestamp()),
"updatedAt": int(datetime.now(tz=timezone.utc).timestamp()), "updatedAt": int(datetime.now(tz=timezone.utc).timestamp()),
# "admins": [user.slug, ] "admins": []
} }
for m in members: for m in members:

View File

@ -46,7 +46,7 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0):
c = await redis.execute("GET", "chats/" + cid.decode("utf-8")) c = await redis.execute("GET", "chats/" + cid.decode("utf-8"))
if c: if c:
c = dict(json.loads(c)) c = dict(json.loads(c))
c['messages'] = await load_messages(cid, 50, 0) c['messages'] = await load_messages(cid, 5, 0)
c['unread'] = await get_unread_counter(cid, user.slug) c['unread'] = await get_unread_counter(cid, user.slug)
with local_session() as session: with local_session() as session:
c['members'] = [] c['members'] = []
@ -65,9 +65,34 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0):
} }
async def search_user_chats(by, messages: set, slug: str, limit, offset):
cids = set([])
by_author = by.get('author')
body_like = by.get('body')
cids.unioin(set(await redis.execute("SMEMBERS", "chats_by_user/" + slug)))
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:
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
@query.field("loadMessagesBy") @query.field("loadMessagesBy")
@login_required @login_required
async def load_messages_by(_, info, by, limit: int = 50, offset: int = 0): async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
''' load :limit messages of :chat_id with :offset ''' ''' load :limit messages of :chat_id with :offset '''
messages = set([]) messages = set([])
by_chat = by.get('chat') by_chat = by.get('chat')
@ -78,40 +103,21 @@ async def load_messages_by(_, info, by, limit: int = 50, offset: int = 0):
# everyone's messages in filtered chat # everyone's messages in filtered chat
messages.union(set(await load_messages(by_chat, limit, offset))) messages.union(set(await load_messages(by_chat, limit, offset)))
cids = set([])
by_author = by.get('author')
body_like = by.get('body')
user = info.context["request"].user user = info.context["request"].user
if user: if user and len(messages) == 0:
cids.unioin(set(await redis.execute("SMEMBERS", "chats_by_user/" + user.slug))) messages.union(search_user_chats(by, messages, user.slug, limit, offset))
if len(messages) == 0:
if by_author:
# all author's messages
cids.union(set(await redis.execute("SMEMBERS", f"chats_by_user/{by_author}")))
if by_chat:
# 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:
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))))
days = by.get("days") days = by.get("days")
if days: if days:
messages.union(set(filter( messages.union(set(filter(
lambda m: datetime.now(tz=timezone.utc) - int(m["createdAt"]) < timedelta(days=by.get("days")), lambda m: datetime.now(tz=timezone.utc) - int(m["createdAt"]) < timedelta(days=by.get("days")),
messages list(messages)
))) )))
return { return {
"messages": sorted(list(messages)), "messages": sorted(
lambda m: m.createdAt,
list(messages)
),
"error": None "error": None
} }