inbox/resolvers/search.py

80 lines
2.6 KiB
Python
Raw Normal View History

2023-10-03 14:15:17 +00:00
import json
from datetime import datetime, timezone, timedelta
2023-10-14 12:59:43 +00:00
from typing import Dict, Union, List, Any
from resolvers.load import load_messages
2023-10-03 14:15:17 +00:00
from services.auth import login_required
2023-11-14 18:31:02 +00:00
from services.core import get_all_authors
2023-10-14 14:55:51 +00:00
from services.rediscache import redis
2023-10-04 21:43:07 +00:00
from services.schema import query
2023-10-03 14:15:17 +00:00
@query.field("searchRecipients")
@login_required
async def search_recipients(_, info, text: str, limit: int = 50, offset: int = 0):
result = []
2023-10-14 06:38:12 +00:00
2023-10-03 14:15:17 +00:00
# TODO: maybe redis scan?
2023-10-14 12:59:43 +00:00
2023-10-04 20:42:39 +00:00
author_id = info.context["author_id"]
2023-10-16 15:54:31 +00:00
existed_chats = await redis.execute("SMEMBERS", f"/chats_by_author/{author_id}")
2023-11-14 18:31:02 +00:00
authors = await get_all_authors()
members = {a["id"]: a for a in authors}
2023-10-16 15:54:31 +00:00
if existed_chats:
for chat_id in list(json.loads(existed_chats))[offset : (offset + limit)]:
members_ids = await redis.execute("GET", f"/chats/{chat_id}/members")
for member_id in members_ids:
2023-11-14 18:31:02 +00:00
author = members.get(member_id)
if author:
2023-11-14 15:56:37 +00:00
if author["name"].startswith(text):
if author not in result:
result.append(author)
2023-10-03 14:15:17 +00:00
more_amount = limit - len(result)
2023-10-04 20:42:39 +00:00
if more_amount > 0:
2023-11-14 18:31:02 +00:00
result += authors[0:more_amount]
2023-10-03 14:15:17 +00:00
return {"members": list(result), "error": None}
@query.field("searchMessages")
@login_required
2023-10-14 06:38:12 +00:00
async def search_in_chats(
_, info, by: Dict[str, Union[str, int]], limit: int, offset: int
) -> Dict[str, Union[List[Dict[str, Any]], None]]:
2023-10-03 14:15:17 +00:00
author_id = info.context["author_id"]
2023-10-14 14:55:51 +00:00
lookup_chats = set((await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")) or [])
2023-10-03 14:15:17 +00:00
messages_set = set([])
by_member = by.get("author")
body_like = by.get("body")
days_ago = by.get("days")
# pre-filter lookup chats
if by_member:
lookup_chats = filter(
lambda ca: by_member in ca["members"],
list(lookup_chats),
)
# load the messages from lookup chats
for c in lookup_chats:
2023-10-13 16:45:30 +00:00
chat_id = c.decode()
2023-10-03 14:15:17 +00:00
mmm = await load_messages(chat_id, limit, offset)
if by_member:
2023-10-13 16:45:30 +00:00
mmm = list(filter(lambda mx: mx["author"] == by_member, mmm))
2023-10-03 14:15:17 +00:00
if body_like:
2023-10-13 16:45:30 +00:00
mmm = list(filter(lambda mx: body_like in mx["body"], mmm))
2023-10-03 14:15:17 +00:00
if days_ago:
2023-10-14 06:38:12 +00:00
mmm = list(
filter(
2023-10-14 14:55:51 +00:00
lambda msg: int(datetime.now(tz=timezone.utc)) - int(msg["createdAt"])
< int(timedelta(days=days_ago)),
2023-10-14 06:38:12 +00:00
mmm,
)
)
2023-10-03 14:15:17 +00:00
messages_set.union(set(mmm))
2023-10-13 16:45:30 +00:00
messages_sorted = sorted(list(messages_set))
return {"messages": messages_sorted, "error": None}