Files
inbox/resolvers/search.py

76 lines
2.8 KiB
Python
Raw Normal View History

2023-12-23 09:11:04 +03:00
import time
2023-12-17 20:13:17 +03:00
from typing import Any, Dict, List, Union
2023-10-14 15:59:43 +03:00
from resolvers.load import load_messages
2023-10-03 17:15:17 +03:00
from services.auth import login_required
2023-12-23 09:11:04 +03:00
from services.core import CacheStorage
2023-10-14 17:55:51 +03:00
from services.rediscache import redis
2023-10-05 00:43:07 +03:00
from services.schema import query
2023-10-03 17:15:17 +03:00
2024-04-08 09:30:57 +03:00
@query.field("search_recipients")
2023-10-03 17:15:17 +03:00
@login_required
async def search_recipients(_, info, text: str, limit: int = 50, offset: int = 0):
2024-01-25 12:25:52 +03:00
result = set()
2023-10-14 09:38:12 +03:00
2023-10-03 17:15:17 +03:00
# TODO: maybe redis scan?
2023-10-14 15:59:43 +03:00
2024-04-08 09:30:57 +03:00
author_id = info.context["author_id"]
2023-12-19 17:24:52 +03:00
2024-04-08 09:30:57 +03:00
existed_chats = await redis.execute("SMEMBERS", f"/chats_by_author/{author_id}")
2024-01-23 23:13:49 +03:00
if isinstance(existed_chats, set):
chats_list = list(existed_chats)
2024-01-24 00:13:14 +03:00
for chat_id in chats_list[offset : (offset + limit)]:
2024-04-08 09:30:57 +03:00
members_ids = await redis.execute("SMEMBERS", f"/chats/{chat_id}/members")
2024-01-23 23:13:49 +03:00
if isinstance(members_ids, set):
2023-12-23 09:11:04 +03:00
for member_id in members_ids:
2024-01-24 13:06:47 +03:00
author = CacheStorage.authors_by_id.get(str(member_id))
2023-12-23 09:11:04 +03:00
if author:
2024-04-08 09:30:57 +03:00
if author["name"].startswith(text):
2023-12-23 09:11:04 +03:00
result.add(author)
2023-10-03 17:15:17 +03:00
more_amount = limit - len(result)
2023-10-04 23:42:39 +03:00
if more_amount > 0:
2023-12-23 09:11:04 +03:00
result.update(CacheStorage.authors[0:more_amount])
2024-04-08 09:30:57 +03:00
return {"members": list(result), "error": None}
2023-10-03 17:15:17 +03:00
2024-04-08 09:30:57 +03:00
@query.field("search_messages")
2023-10-03 17:15:17 +03:00
@login_required
2023-11-28 11:33:50 +03:00
async def search_messages(
2023-10-14 09:38:12 +03:00
_, info, by: Dict[str, Union[str, int]], limit: int, offset: int
) -> Dict[str, Union[List[Dict[str, Any]], None]]:
2024-01-25 12:25:52 +03:00
messages_set = set()
2024-04-08 09:30:57 +03:00
author_id = info.context["author_id"]
lookup_chats = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
2024-01-24 00:13:14 +03:00
if isinstance(lookup_chats, set):
2023-12-23 09:11:04 +03:00
# pre-filter lookup chats
2024-04-08 09:30:57 +03:00
by_member = by.get("author")
2023-10-03 17:15:17 +03:00
if by_member:
2023-12-23 09:11:04 +03:00
lookup_chats = filter(
2024-04-08 09:30:57 +03:00
lambda ca: by_member in ca["members"],
2023-12-23 09:11:04 +03:00
list(lookup_chats),
2023-10-14 09:38:12 +03:00
)
2023-10-03 17:15:17 +03:00
2023-12-23 09:11:04 +03:00
# load the messages from lookup chats
for c in lookup_chats:
chat_id = c.decode()
2024-01-25 12:25:52 +03:00
fltr = None
now = int(time.time())
2023-12-23 09:11:04 +03:00
if by_member:
2024-04-08 09:30:57 +03:00
fltr = lambda mx: mx and mx["created_by"] == by_member # noqa E731
body_like = by.get("body") or ""
2023-12-23 09:11:04 +03:00
if isinstance(body_like, str):
2024-04-08 09:30:57 +03:00
fltr = lambda mx: mx and body_like in mx["body"] # noqa E731
days_ago = int(by.get("days") or "0")
2023-12-23 09:11:04 +03:00
if days_ago:
2024-01-25 12:25:52 +03:00
ts = days_ago * 24 * 60 * 60
2024-04-08 09:30:57 +03:00
fltr = lambda mx: mx and now - mx["created_by"] < ts # noqa E731
2024-01-25 12:25:52 +03:00
if fltr:
2024-01-24 00:13:14 +03:00
mmm = await load_messages(chat_id, limit, offset)
if isinstance(mmm, list):
2024-01-25 12:25:52 +03:00
mmm = list(filter(fltr, mmm))
2024-01-24 00:13:14 +03:00
messages_set |= set(mmm)
2023-10-03 17:15:17 +03:00
2024-04-08 09:30:57 +03:00
return {"messages": sorted(messages_set), "error": None}