This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import json
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import time
|
||||
from typing import Any, Dict, List, Union
|
||||
|
||||
from resolvers.load import load_messages
|
||||
from services.auth import login_required
|
||||
from services.core import get_all_authors
|
||||
from services.core import CacheStorage
|
||||
from services.rediscache import redis
|
||||
from services.schema import query
|
||||
|
||||
@@ -18,22 +18,21 @@ async def search_recipients(_, info, text: str, limit: int = 50, offset: int = 0
|
||||
|
||||
author_id = info.context["author_id"]
|
||||
|
||||
authors = get_all_authors()
|
||||
authors_by_id = {a["id"]: a for a in authors}
|
||||
|
||||
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)]:
|
||||
if isinstance(existed_chats, str):
|
||||
chats_list = list(json.loads(existed_chats))
|
||||
for chat_id in chats_list[offset : (offset + limit)]:
|
||||
members_ids = await redis.execute("GET", f"/chats/{chat_id}/members")
|
||||
for member_id in members_ids:
|
||||
author = authors_by_id.get(member_id)
|
||||
if author:
|
||||
if author["name"].startswith(text):
|
||||
result.add(author)
|
||||
if isinstance(members_ids, list):
|
||||
for member_id in members_ids:
|
||||
author = CacheStorage.authors_by_id.get(member_id)
|
||||
if author:
|
||||
if author["name"].startswith(text):
|
||||
result.add(author)
|
||||
|
||||
more_amount = limit - len(result)
|
||||
if more_amount > 0:
|
||||
result.update(authors_by_id.values()[0:more_amount])
|
||||
result.update(CacheStorage.authors[0:more_amount])
|
||||
return {"members": list(result), "error": None}
|
||||
|
||||
|
||||
@@ -42,39 +41,35 @@ async def search_recipients(_, info, text: str, limit: int = 50, offset: int = 0
|
||||
async def search_messages(
|
||||
_, info, by: Dict[str, Union[str, int]], limit: int, offset: int
|
||||
) -> Dict[str, Union[List[Dict[str, Any]], None]]:
|
||||
author_id = info.context["author_id"]
|
||||
lookup_chats = set((await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")) or [])
|
||||
messages_set = set([])
|
||||
author_id = info.context["author_id"]
|
||||
lookup_chats = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
|
||||
if isinstance(lookup_chats, list):
|
||||
lookup_chats = set(lookup_chats)
|
||||
|
||||
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:
|
||||
chat_id = c.decode()
|
||||
mmm = await load_messages(chat_id, limit, offset)
|
||||
# pre-filter lookup chats
|
||||
by_member = by.get("author")
|
||||
if by_member:
|
||||
mmm = list(filter(lambda mx: mx["author"] == by_member, mmm))
|
||||
if body_like:
|
||||
mmm = list(filter(lambda mx: body_like in mx["body"], mmm))
|
||||
if days_ago:
|
||||
mmm = list(
|
||||
filter(
|
||||
lambda msg: int(datetime.now(tz=timezone.utc)) - int(msg["created_at"])
|
||||
< int(timedelta(days=days_ago)),
|
||||
mmm,
|
||||
)
|
||||
lookup_chats = filter(
|
||||
lambda ca: by_member in ca["members"],
|
||||
list(lookup_chats),
|
||||
)
|
||||
|
||||
messages_set.union(set(mmm))
|
||||
# load the messages from lookup chats
|
||||
for c in lookup_chats:
|
||||
chat_id = c.decode()
|
||||
mmm = await load_messages(chat_id, limit, offset)
|
||||
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:
|
||||
mmm = list(filter(filter_method, mmm))
|
||||
messages_set |= set(mmm)
|
||||
|
||||
messages_sorted = sorted(list(messages_set))
|
||||
return {"messages": messages_sorted, "error": None}
|
||||
return {"messages": sorted(list(messages_set)), "error": None}
|
||||
|
Reference in New Issue
Block a user