inbox/resolvers/chats.py

111 lines
3.6 KiB
Python
Raw Normal View History

2023-10-03 14:15:17 +00:00
import json
import uuid
from datetime import datetime, timezone
2023-10-14 11:52:04 +00:00
from validators.inbox import Chat
2023-10-03 14:15:17 +00:00
from services.auth import login_required
from services.redis import redis
2023-10-04 21:43:07 +00:00
from services.schema import mutation
2023-10-03 14:15:17 +00:00
@mutation.field("updateChat")
@login_required
2023-10-14 06:38:12 +00:00
async def update_chat(_, info, chat_new: Chat):
2023-10-03 14:15:17 +00:00
"""
updating chat
requires info["request"].user.slug to be in chat["admins"]
:param info: GraphQLInfo with request
:param chat_new: dict with chat data
:return: Result { error chat }
"""
author_id = info.context["author_id"]
chat_id = chat_new["id"]
2023-10-14 06:38:12 +00:00
chat = await redis.execute("GET", f"chats/{chat_id}")
2023-10-03 14:15:17 +00:00
if not chat:
return {"error": "chat not exist"}
2023-10-13 16:45:30 +00:00
else:
2023-10-14 06:38:12 +00:00
chat: Chat = json.loads(chat)
2023-10-03 14:15:17 +00:00
if author_id in chat["admins"]:
chat.update(
{
"title": chat_new.get("title", chat["title"]),
"description": chat_new.get("description", chat["description"]),
"updatedAt": int(datetime.now(tz=timezone.utc).timestamp()),
"admins": chat_new.get("admins", chat.get("admins") or []),
2023-10-14 06:38:12 +00:00
"members": chat_new.get("members", chat["members"]),
2023-10-03 14:15:17 +00:00
}
)
await redis.execute("SET", f"chats/{chat['id']}", json.dumps(chat))
return {"error": None, "chat": chat}
@mutation.field("createChat")
@login_required
2023-10-11 19:47:25 +00:00
async def create_chat(_, info, title="", members=None):
if members is None:
members = []
2023-10-03 14:15:17 +00:00
chat = None
author_id = info.context["author_id"]
print("create_chat members: %r" % members)
if author_id not in members:
members.append(int(author_id))
2023-10-13 08:31:14 +00:00
2023-10-03 14:15:17 +00:00
# NOTE: private chats has no title
# reuse private chat created before if exists
if len(members) == 2 and title == "":
chatset1 = set(
(await redis.execute("SMEMBERS", f"chats_by_author/{members[0]}")) or []
)
chatset2 = set(
(await redis.execute("SMEMBERS", f"chats_by_author/{members[1]}")) or []
)
for c in chatset1.intersection(chatset2):
chat = await redis.execute("GET", f"chats/{c.decode('utf-8')}")
if chat:
chat = json.loads(chat)
if chat["title"] == "":
print("[inbox] createChat found old chat")
print(chat)
break
if chat:
return {"chat": chat, "error": "existed"}
chat_id = str(uuid.uuid4())
2023-10-14 06:38:12 +00:00
chat: Chat = {
2023-10-03 14:15:17 +00:00
"id": chat_id,
"members": members,
"title": title,
"createdBy": author_id,
"createdAt": int(datetime.now(tz=timezone.utc).timestamp()),
"updatedAt": int(datetime.now(tz=timezone.utc).timestamp()),
"admins": members if (len(members) == 2 and title == "") else [],
}
2023-10-14 06:38:12 +00:00
for member_id in members:
await redis.execute("SADD", f"chats_by_author/{member_id}", chat_id)
2023-10-14 11:52:04 +00:00
print(f"[resolvers.chatss] creating: {chat}")
2023-10-03 14:15:17 +00:00
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
await redis.execute("SET", f"chats/{chat_id}/next_message_id", str(0))
2023-10-13 08:31:14 +00:00
2023-10-03 14:15:17 +00:00
return {"error": None, "chat": chat}
@mutation.field("deleteChat")
@login_required
async def delete_chat(_, info, chat_id: str):
author_id = info.context["author_id"]
chat = await redis.execute("GET", f"/chats/{chat_id}")
if chat:
2023-10-14 06:38:12 +00:00
chat: Chat = json.loads(chat)
2023-10-03 14:15:17 +00:00
if author_id in chat["admins"]:
await redis.execute("DEL", f"chats/{chat_id}")
await redis.execute("SREM", f"chats_by_author/{author_id}", chat_id)
else:
return {"error": "chat not exist"}