This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import json
|
||||
import time
|
||||
import uuid
|
||||
import logging
|
||||
|
||||
from models.chat import Chat, ChatUpdate
|
||||
from services.auth import login_required
|
||||
@@ -8,6 +9,8 @@ from services.presence import notify_chat
|
||||
from services.rediscache import redis
|
||||
from services.schema import mutation
|
||||
|
||||
logger = logging.getLogger("[resolvers.chats] ")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
@mutation.field("update_chat")
|
||||
@login_required
|
||||
@@ -25,7 +28,7 @@ async def update_chat(_, info, chat_new: ChatUpdate):
|
||||
chat_str = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if not chat_str:
|
||||
return {"error": "chat not exist"}
|
||||
else:
|
||||
elif isinstance(chat_str, str):
|
||||
chat: Chat = json.loads(chat_str)
|
||||
if author_id in chat["admins"]:
|
||||
chat.update(
|
||||
@@ -50,7 +53,7 @@ async def update_chat(_, info, chat_new: ChatUpdate):
|
||||
async def create_chat(_, info, title="", members=None):
|
||||
members = members or []
|
||||
author_id = info.context["author_id"]
|
||||
chat = None
|
||||
chat: Chat
|
||||
if author_id:
|
||||
if author_id not in members:
|
||||
members.append(int(author_id))
|
||||
@@ -58,15 +61,19 @@ 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 [])
|
||||
for c in chatset1.intersection(chatset2):
|
||||
chat_data = await redis.execute("GET", f"chats/{c}")
|
||||
if chat_data:
|
||||
chat = json.loads(chat_data)
|
||||
if chat["title"] == "":
|
||||
print("[inbox] createChat found old chat")
|
||||
return {"chat": chat, "error": "existed"}
|
||||
chatdata1 = await redis.execute("SMEMBERS", f"chats_by_author/{members[0]}")
|
||||
chatdata2 = await redis.execute("SMEMBERS", f"chats_by_author/{members[1]}")
|
||||
if isinstance(chatdata1, list) and isinstance(chatdata2, list):
|
||||
chatset1 = set(chatdata1)
|
||||
chatset2 = set(chatdata2)
|
||||
|
||||
for c in chatset1.intersection(chatset2):
|
||||
chat_data = await redis.execute("GET", f"chats/{c}")
|
||||
if isinstance(chat_data, str):
|
||||
chat = json.loads(chat_data)
|
||||
if chat["title"] == "":
|
||||
logger.info("[inbox] createChat found old chat")
|
||||
return {"chat": chat, "error": "existed"}
|
||||
|
||||
chat_id = str(uuid.uuid4())
|
||||
chat: Chat = {
|
||||
@@ -89,7 +96,8 @@ async def create_chat(_, info, title="", members=None):
|
||||
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
|
||||
await redis.execute("SET", f"chats/{chat_id}/next_message_id", str(0))
|
||||
|
||||
return {"error": None, "chat": chat}
|
||||
return {"error": None, "chat": chat}
|
||||
return {"error": "no chat was created"}
|
||||
|
||||
|
||||
@mutation.field("delete_chat")
|
||||
@@ -97,12 +105,11 @@ async def create_chat(_, info, title="", members=None):
|
||||
async def delete_chat(_, info, chat_id: str):
|
||||
author_id = info.context["author_id"]
|
||||
chat_str = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if chat_str:
|
||||
if isinstance(chat_str, 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)
|
||||
for member_id in chat["members"]:
|
||||
await notify_chat(chat, member_id, "delete")
|
||||
else:
|
||||
return {"error": "chat not exist"}
|
||||
return {"error": "chat not exist"}
|
||||
|
Reference in New Issue
Block a user