inspected
This commit is contained in:
@@ -3,43 +3,42 @@ import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from services.auth import login_required
|
||||
from services.redis import redis
|
||||
from services.rediscache import redis
|
||||
from services.schema import mutation
|
||||
from validators.inbox import Chat
|
||||
from validators.chat import Chat, ChatUpdate
|
||||
|
||||
|
||||
@mutation.field("updateChat")
|
||||
@login_required
|
||||
async def update_chat(_, info, chat_new: Chat):
|
||||
async def update_chat(_, info, chat_new: ChatUpdate):
|
||||
"""
|
||||
updating chat
|
||||
requires info["request"].user.slug to be in chat["admins"]
|
||||
|
||||
requires info.context["author_id"] to be in chat["admins"]
|
||||
:param _: not used
|
||||
: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"]
|
||||
chat = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if not chat:
|
||||
chat_str = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if not chat_str:
|
||||
return {"error": "chat not exist"}
|
||||
else:
|
||||
chat: Chat = json.loads(chat)
|
||||
chat: Chat = json.loads(chat_str)
|
||||
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 []),
|
||||
"members": chat_new.get("members", chat["members"]),
|
||||
}
|
||||
)
|
||||
await redis.execute("SET", f"chats/{chat['id']}", json.dumps(chat))
|
||||
|
||||
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 []),
|
||||
"members": chat_new.get("members", chat["members"]),
|
||||
}
|
||||
)
|
||||
await redis.execute("SET", f"chats/{chat['id']}", json.dumps(chat))
|
||||
|
||||
return {"error": None, "chat": chat}
|
||||
return {"error": None, "chat": chat}
|
||||
|
||||
|
||||
@mutation.field("createChat")
|
||||
@@ -47,7 +46,6 @@ async def update_chat(_, info, chat_new: Chat):
|
||||
async def create_chat(_, info, title="", members=None):
|
||||
if members is None:
|
||||
members = []
|
||||
chat = None
|
||||
author_id = info.context["author_id"]
|
||||
print("create_chat members: %r" % members)
|
||||
if author_id not in members:
|
||||
@@ -56,28 +54,22 @@ async def create_chat(_, info, title="", members=None):
|
||||
# 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 []
|
||||
)
|
||||
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)
|
||||
chat_data = await redis.execute("GET", f"chats/{c.decode('utf-8')}")
|
||||
if chat_data:
|
||||
chat = json.loads(chat_data)
|
||||
if chat["title"] == "":
|
||||
print("[inbox] createChat found old chat")
|
||||
print(chat)
|
||||
break
|
||||
if chat:
|
||||
return {"chat": chat, "error": "existed"}
|
||||
return {"chat": chat, "error": "existed"}
|
||||
|
||||
chat_id = str(uuid.uuid4())
|
||||
chat: Chat = {
|
||||
"id": chat_id,
|
||||
"members": members,
|
||||
"title": title,
|
||||
"description": "",
|
||||
"createdBy": author_id,
|
||||
"createdAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
||||
"updatedAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
||||
@@ -100,9 +92,9 @@ async def create_chat(_, info, title="", members=None):
|
||||
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:
|
||||
chat: Chat = json.loads(chat)
|
||||
chat_str = await redis.execute("GET", f"/chats/{chat_id}")
|
||||
if chat_str:
|
||||
chat: Chat = json.loads(chat_str)
|
||||
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)
|
||||
|
Reference in New Issue
Block a user