2023-10-03 14:15:17 +00:00
|
|
|
import json
|
2023-12-17 17:13:17 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
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-19 08:25:06 +00:00
|
|
|
from services.core import authors_by_id
|
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-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}")
|
|
|
|
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-12-19 08:25:06 +00:00
|
|
|
author = authors_by_id.get(member_id)
|
2023-11-14 18:31:02 +00:00
|
|
|
if author:
|
2023-11-14 15:56:37 +00:00
|
|
|
if author["name"].startswith(text):
|
2023-12-19 08:25:06 +00:00
|
|
|
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-19 08:25:06 +00:00
|
|
|
result.update(authors_by_id.values()[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
|
|
|
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-11-16 14:58:14 +00:00
|
|
|
lambda msg: int(datetime.now(tz=timezone.utc)) - int(msg["created_at"])
|
2023-10-14 14:55:51 +00:00
|
|
|
< 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}
|