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
|
|
|
|
|
|
|
|
2024-01-25 09:25:52 +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):
|
2024-01-25 09:25:52 +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
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
author_id = info.context['author_id']
|
2023-12-19 14:24:52 +00:00
|
|
|
|
2024-01-25 09:25:52 +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-25 09:25:52 +00:00
|
|
|
members_ids = await redis.execute('SMEMBERS', f'/chats/{chat_id}/members')
|
2024-01-23 20:13:49 +00:00
|
|
|
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:
|
2024-01-25 09:25:52 +00:00
|
|
|
if author['name'].startswith(text):
|
2023-12-23 06:11:04 +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-23 06:11:04 +00:00
|
|
|
result.update(CacheStorage.authors[0:more_amount])
|
2024-01-25 09:25:52 +00:00
|
|
|
return {'members': list(result), 'error': None}
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
|
|
|
2024-01-25 09:25:52 +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]]:
|
2024-01-25 09:25:52 +00:00
|
|
|
messages_set = set()
|
|
|
|
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-12-23 06:11:04 +00:00
|
|
|
# pre-filter lookup chats
|
2024-01-25 09:25:52 +00:00
|
|
|
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(
|
2024-01-25 09:25:52 +00:00
|
|
|
lambda ca: by_member in ca['members'],
|
2023-12-23 06:11:04 +00:00
|
|
|
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()
|
2024-01-25 09:25:52 +00:00
|
|
|
fltr = None
|
|
|
|
now = int(time.time())
|
2023-12-23 06:11:04 +00:00
|
|
|
if by_member:
|
2024-01-25 09:25:52 +00:00
|
|
|
fltr = lambda mx: mx and mx['created_by'] == by_member # noqa E731
|
|
|
|
body_like = by.get('body') or ''
|
2023-12-23 06:11:04 +00:00
|
|
|
if isinstance(body_like, str):
|
2024-01-25 09:25:52 +00:00
|
|
|
fltr = lambda mx: mx and body_like in mx['body'] # noqa E731
|
|
|
|
days_ago = int(by.get('days') or '0')
|
2023-12-23 06:11:04 +00:00
|
|
|
if days_ago:
|
2024-01-25 09:25:52 +00:00
|
|
|
ts = days_ago * 24 * 60 * 60
|
|
|
|
fltr = lambda mx: mx and now - mx['created_by'] < ts # noqa E731
|
|
|
|
if fltr:
|
2024-01-23 21:13:14 +00:00
|
|
|
mmm = await load_messages(chat_id, limit, offset)
|
|
|
|
if isinstance(mmm, list):
|
2024-01-25 09:25:52 +00:00
|
|
|
mmm = list(filter(fltr, mmm))
|
2024-01-23 21:13:14 +00:00
|
|
|
messages_set |= set(mmm)
|
2023-10-03 14:15:17 +00:00
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
return {'messages': sorted(messages_set), 'error': None}
|