2022-11-11 21:27:17 +00:00
|
|
|
import json
|
|
|
|
import uuid
|
2022-11-23 14:09:35 +00:00
|
|
|
from datetime import datetime, 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
|
2022-11-11 21:27:17 +00:00
|
|
|
from base.redis import redis
|
2022-11-21 08:13:57 +00:00
|
|
|
from base.resolvers import mutation
|
2022-11-11 21:27:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("updateChat")
|
|
|
|
@login_required
|
|
|
|
async def update_chat(_, info, chat_new: dict):
|
|
|
|
"""
|
|
|
|
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 }
|
|
|
|
"""
|
2022-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
2022-11-11 21:27:17 +00:00
|
|
|
chat_id = chat_new["id"]
|
|
|
|
chat = await redis.execute("GET", f"chats/{chat_id}")
|
|
|
|
if not chat:
|
|
|
|
return {
|
|
|
|
"error": "chat not exist"
|
|
|
|
}
|
|
|
|
chat = dict(json.loads(chat))
|
2022-12-01 14:45:19 +00:00
|
|
|
|
|
|
|
# TODO
|
|
|
|
if auth.user_id in chat["admins"]:
|
2022-11-11 21:27:17 +00:00
|
|
|
chat.update({
|
|
|
|
"title": chat_new.get("title", chat["title"]),
|
|
|
|
"description": chat_new.get("description", chat["description"]),
|
2022-11-23 14:09:35 +00:00
|
|
|
"updatedAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
2022-11-27 13:39:16 +00:00
|
|
|
"admins": chat_new.get("admins", chat.get("admins") or []),
|
2022-11-11 21:27:17 +00:00
|
|
|
"users": chat_new.get("users", chat["users"])
|
|
|
|
})
|
|
|
|
await redis.execute("SET", f"chats/{chat.id}", json.dumps(chat))
|
2022-11-25 22:35:42 +00:00
|
|
|
await redis.execute("COMMIT")
|
2022-11-11 21:27:17 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
"error": None,
|
|
|
|
"chat": chat
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("createChat")
|
|
|
|
@login_required
|
|
|
|
async def create_chat(_, info, title="", members=[]):
|
2022-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
2022-11-26 14:02:20 +00:00
|
|
|
chat = {}
|
2022-12-01 14:45:19 +00:00
|
|
|
|
|
|
|
if auth.user_id not in members:
|
|
|
|
members.append(auth.user_id)
|
2022-11-26 14:02:20 +00:00
|
|
|
|
|
|
|
# reuse chat craeted before if exists
|
|
|
|
if len(members) == 2 and title == "":
|
|
|
|
chats1 = await redis.execute("SMEMBERS", f"chats_by_user/{members[0].slug}")
|
|
|
|
chats2 = await redis.execute("SMEMBERS", f"chats_by_user/{members[1].slug}")
|
|
|
|
chat = None
|
|
|
|
for c in chats1.intersection(chats2):
|
|
|
|
chat = await redis.execute("GET", f"chats/{c.decode('utf-8')}")
|
|
|
|
if chat:
|
|
|
|
chat = json.loads(chat)
|
|
|
|
if chat.title == "":
|
|
|
|
break
|
|
|
|
if chat:
|
|
|
|
return {
|
|
|
|
"chat": chat,
|
|
|
|
"error": "existed"
|
|
|
|
}
|
|
|
|
|
|
|
|
chat_id = str(uuid.uuid4())
|
2022-11-11 21:27:17 +00:00
|
|
|
chat = {
|
2022-11-26 14:02:20 +00:00
|
|
|
"id": chat_id,
|
|
|
|
"users": members,
|
2022-11-11 21:27:17 +00:00
|
|
|
"title": title,
|
2022-12-01 14:45:19 +00:00
|
|
|
"createdBy": auth.user_id,
|
2022-11-23 14:09:35 +00:00
|
|
|
"createdAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
|
|
|
"updatedAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
2022-11-27 13:39:16 +00:00
|
|
|
"admins": []
|
2022-11-11 21:27:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 22:35:42 +00:00
|
|
|
for m in members:
|
|
|
|
await redis.execute("SADD", f"chats_by_user/{m}", chat_id)
|
2022-11-11 21:27: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))
|
2022-11-25 22:35:42 +00:00
|
|
|
await redis.execute("COMMIT")
|
2022-11-11 21:27:17 +00:00
|
|
|
return {
|
|
|
|
"error": None,
|
|
|
|
"chat": chat
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("deleteChat")
|
|
|
|
@login_required
|
|
|
|
async def delete_chat(_, info, chat_id: str):
|
2022-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
|
|
|
|
2022-11-11 21:27:17 +00:00
|
|
|
chat = await redis.execute("GET", f"/chats/{chat_id}")
|
|
|
|
if chat:
|
|
|
|
chat = dict(json.loads(chat))
|
2022-12-01 14:45:19 +00:00
|
|
|
if auth.user_id in chat['admins']:
|
2022-11-11 21:27:17 +00:00
|
|
|
await redis.execute("DEL", f"chats/{chat_id}")
|
2022-12-01 14:45:19 +00:00
|
|
|
await redis.execute("SREM", "chats_by_user/" + str(auth.user_id), chat_id)
|
2022-11-25 22:35:42 +00:00
|
|
|
await redis.execute("COMMIT")
|
2022-11-11 21:27:17 +00:00
|
|
|
else:
|
|
|
|
return {
|
|
|
|
"error": "chat not exist"
|
|
|
|
}
|