core/resolvers/inbox/search.py

97 lines
3.3 KiB
Python
Raw Normal View History

2023-10-26 21:07:35 +00:00
import json
2023-10-30 21:00:55 +00:00
from datetime import datetime, timedelta, timezone
2022-11-11 21:27:17 +00:00
from auth.authenticate import login_required
2022-12-01 14:45:19 +00:00
from auth.credentials import AuthCredentials
2023-10-30 21:00:55 +00:00
from base.orm import local_session
2022-11-11 21:27:17 +00:00
from base.redis import redis
2022-11-12 07:13:51 +00:00
from base.resolvers import query
2022-11-29 12:36:46 +00:00
from orm.user import AuthorFollower, User
2022-12-05 09:48:21 +00:00
from resolvers.inbox.load import load_messages
2022-11-11 21:27:17 +00:00
2022-11-21 08:13:57 +00:00
@query.field("searchRecipients")
2022-11-11 21:27:17 +00:00
@login_required
2022-11-21 08:13:57 +00:00
async def search_recipients(_, info, query: str, limit: int = 50, offset: int = 0):
2022-11-11 21:27:17 +00:00
result = []
# TODO: maybe redis scan?
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
talk_before = await redis.execute("GET", f"/chats_by_user/{auth.user_id}")
2022-11-11 21:27:17 +00:00
if talk_before:
2023-10-30 21:00:55 +00:00
talk_before = list(json.loads(talk_before))[offset : offset + limit]
2022-11-11 21:27:17 +00:00
for chat_id in talk_before:
members = await redis.execute("GET", f"/chats/{chat_id}/users")
if members:
members = list(json.loads(members))
for member in members:
if member.startswith(query):
if member not in result:
result.append(member)
2022-11-16 07:32:24 +00:00
more_amount = limit - len(result)
2022-11-11 21:27:17 +00:00
2022-11-12 07:13:51 +00:00
with local_session() as session:
# followings
2023-10-30 21:00:55 +00:00
result += (
session.query(AuthorFollower.author)
.join(User, User.id == AuthorFollower.follower)
.where(User.slug.startswith(query))
.offset(offset + len(result))
.limit(more_amount)
)
2022-11-11 21:27:17 +00:00
2022-11-16 07:32:24 +00:00
more_amount = limit
2022-11-12 07:13:51 +00:00
# followers
2023-10-30 21:00:55 +00:00
result += (
session.query(AuthorFollower.follower)
.join(User, User.id == AuthorFollower.author)
.where(User.slug.startswith(query))
.offset(offset + len(result))
.limit(offset + len(result) + limit)
)
return {"members": list(result), "error": None}
2022-12-05 09:48:21 +00:00
@query.field("searchMessages")
@login_required
async def search_user_chats(by, messages, user_id: int, limit, offset):
cids = set([])
cids.union(set(await redis.execute("SMEMBERS", "chats_by_user/" + str(user_id))))
messages = []
2023-10-30 21:00:55 +00:00
by_author = by.get("author")
2022-12-05 09:48:21 +00:00
if by_author:
# all author's messages
cids.union(set(await redis.execute("SMEMBERS", f"chats_by_user/{by_author}")))
# author's messages in filtered chat
messages.union(set(filter(lambda m: m["author"] == by_author, list(messages))))
for c in cids:
2023-10-30 21:00:55 +00:00
c = c.decode("utf-8")
2022-12-05 09:48:21 +00:00
messages = await load_messages(c, limit, offset)
2023-10-30 21:00:55 +00:00
body_like = by.get("body")
2022-12-05 09:48:21 +00:00
if body_like:
# search in all messages in all user's chats
for c in cids:
# FIXME: use redis scan here
2023-10-30 21:00:55 +00:00
c = c.decode("utf-8")
2022-12-05 09:48:21 +00:00
mmm = await load_messages(c, limit, offset)
for m in mmm:
if body_like in m["body"]:
messages.add(m)
else:
# search in chat's messages
messages.extend(filter(lambda m: body_like in m["body"], list(messages)))
days = by.get("days")
if days:
2023-10-30 21:00:55 +00:00
messages.extend(
filter(
list(messages),
key=lambda m: (
datetime.now(tz=timezone.utc) - int(m["createdAt"]) < timedelta(days=by["days"])
),
2022-12-05 09:48:21 +00:00
)
2023-10-30 21:00:55 +00:00
)
return {"messages": messages, "error": None}