fixes...
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
from typing import Dict, Union, List, Any
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from services.auth import login_required
|
||||
from services.core import get_network
|
||||
@@ -33,9 +34,9 @@ async def search_recipients(_, info, text: str, limit: int = 50, offset: int = 0
|
||||
|
||||
@query.field("searchMessages")
|
||||
@login_required
|
||||
async def search_in_chats(_, info, by, limit, offset):
|
||||
async def search_in_chats(_, 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}"))
|
||||
lookup_chats = set((await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")) or [])
|
||||
messages_set = set([])
|
||||
|
||||
by_member = by.get("author")
|
||||
@@ -44,7 +45,6 @@ async def search_in_chats(_, info, by, limit, offset):
|
||||
|
||||
# pre-filter lookup chats
|
||||
if by_member:
|
||||
# all author's chats where reqeusting author is participating
|
||||
lookup_chats = filter(
|
||||
lambda ca: by_member in ca["members"],
|
||||
list(lookup_chats),
|
||||
@@ -52,27 +52,27 @@ async def search_in_chats(_, info, by, limit, offset):
|
||||
|
||||
# load the messages from lookup chats
|
||||
for c in lookup_chats:
|
||||
chat_id = c.decode("utf-8")
|
||||
chat_id = c.decode()
|
||||
mmm = await load_messages(chat_id, limit, offset)
|
||||
if by_member:
|
||||
mmm = filter(lambda mx: mx["author"] == by_member, mmm)
|
||||
mmm = list(filter(lambda mx: mx["author"] == by_member, mmm))
|
||||
if body_like:
|
||||
mmm = filter(lambda mx: body_like in mx["body"], mmm)
|
||||
mmm = list(filter(lambda mx: body_like in mx["body"], mmm))
|
||||
if days_ago:
|
||||
mmm = filter(
|
||||
lambda msg: datetime.now(tz=timezone.utc) - int(msg["createdAt"])
|
||||
mmm = list(filter(
|
||||
lambda msg: int(datetime.now(tz=timezone.utc)) - int(msg["createdAt"])
|
||||
< timedelta(days=days_ago),
|
||||
mmm,
|
||||
)
|
||||
))
|
||||
|
||||
messages_set.union(set(mmm))
|
||||
messages_sorted = list(messages_set).sort()
|
||||
return {"messages": messages_sorted, "error": None}
|
||||
|
||||
messages_sorted = sorted(list(messages_set))
|
||||
return {"messages": messages_sorted, "error": None}
|
||||
|
||||
search_resolvers = {
|
||||
"Query": {
|
||||
"searchMessages": search_in_chats,
|
||||
"searchRecipients": search_recipients,
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user