inbox/resolvers/search.py

76 lines
2.8 KiB
Python
Raw Normal View History

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