This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import json
|
||||
import time
|
||||
import uuid
|
||||
from typing import List
|
||||
|
||||
from models.chat import Chat, ChatUpdate
|
||||
from models.member import ChatMember
|
||||
from services.auth import login_required
|
||||
from services.core import authors_by_user
|
||||
from services.presence import notify_chat
|
||||
from services.rediscache import redis
|
||||
from services.schema import mutation
|
||||
@@ -22,7 +21,9 @@ async def update_chat(_, info, chat_new: ChatUpdate):
|
||||
:param chat_new: dict with chat data
|
||||
:return: Result { error chat }
|
||||
"""
|
||||
author_id = info.context["author_id"]
|
||||
user_id = info.context["user_id"]
|
||||
author = authors_by_user[user_id]
|
||||
author_id = author["id"]
|
||||
chat_id = chat_new["id"]
|
||||
chat_str = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if not chat_str:
|
||||
@@ -51,45 +52,48 @@ async def update_chat(_, info, chat_new: ChatUpdate):
|
||||
@login_required
|
||||
async def create_chat(_, info, title="", members=None):
|
||||
members = members or []
|
||||
print("create_chat context: %r" % info.context)
|
||||
author_id = info.context["author_id"]
|
||||
print("create_chat members: %r" % members)
|
||||
if author_id not in members:
|
||||
members.append(int(author_id))
|
||||
print("create_chat context: %r" % info.context)
|
||||
user_id = info.context["user_id"]
|
||||
author = authors_by_user[user_id]
|
||||
author_id = author["id"]
|
||||
if author_id:
|
||||
if author_id not in members:
|
||||
members.append(int(author_id))
|
||||
|
||||
# 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"}
|
||||
# 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"}
|
||||
|
||||
chat_id = str(uuid.uuid4())
|
||||
chat: Chat = {
|
||||
"id": chat_id,
|
||||
"members": members,
|
||||
"title": title,
|
||||
"description": "",
|
||||
"created_by": author_id,
|
||||
"created_at": int(time.time()),
|
||||
"updated_at": int(time.time()),
|
||||
"admins": members if (len(members) == 2 and title == "") else [],
|
||||
}
|
||||
chat_id = str(uuid.uuid4())
|
||||
chat: Chat = {
|
||||
"id": chat_id,
|
||||
"members": members,
|
||||
"title": title,
|
||||
"description": "",
|
||||
"created_by": author_id,
|
||||
"created_at": int(time.time()),
|
||||
"updated_at": int(time.time()),
|
||||
"admins": members if (len(members) == 2 and title == "") else [],
|
||||
}
|
||||
|
||||
for member_id in members:
|
||||
await redis.execute("SADD", f"chats_by_author/{member_id}", chat_id)
|
||||
await notify_chat(chat, member_id, "create")
|
||||
for member_id in members:
|
||||
await redis.execute("SADD", f"chats_by_author/{member_id}", chat_id)
|
||||
await notify_chat(chat, member_id, "create")
|
||||
|
||||
print(f"\n\n[resolvers.chats] creating: {chat}\n\n")
|
||||
print(f"\n\n[resolvers.chats] creating: {chat}\n\n")
|
||||
|
||||
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
|
||||
await redis.execute("SET", f"chats/{chat_id}/next_message_id", str(0))
|
||||
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}
|
||||
|
||||
@@ -97,8 +101,9 @@ async def create_chat(_, info, title="", members=None):
|
||||
@mutation.field("delete_chat")
|
||||
@login_required
|
||||
async def delete_chat(_, info, chat_id: str):
|
||||
author_id = info.context["author_id"]
|
||||
|
||||
user_id = info.context["user_id"]
|
||||
author = authors_by_user[user_id]
|
||||
author_id = author["id"]
|
||||
chat_str = await redis.execute("GET", f"chats/{chat_id}")
|
||||
if chat_str:
|
||||
chat: Chat = json.loads(chat_str)
|
||||
|
Reference in New Issue
Block a user