This commit is contained in:
2023-10-13 19:45:30 +03:00
parent 1c46fc1d48
commit 84e6841331
8 changed files with 84 additions and 73 deletions

View File

@@ -1,5 +1,7 @@
import json
from datetime import datetime, timezone
from typing import List
from validators.chat import Message
from services.auth import login_required
from services.presence import notify_message
from services.redis import redis
@@ -17,15 +19,15 @@ async def create_message(_, info, chat: str, body: str, reply_to=None):
"""
author_id = info.context["author_id"]
chat = await redis.execute("GET", f"chats/{chat}")
if not chat:
chat_dict = await redis.execute("GET", f"chats/{chat}")
if not chat_dict:
return {"error": "chat is not exist"}
else:
chat = vars(json.loads(chat))
message_id = await redis.execute("GET", f"chats/{chat['id']}/next_message_id")
chat_dict = vars(json.loads(chat))
message_id = (await redis.execute("GET", f"chats/{chat_dict['id']}/next_message_id")) or 0
message_id = int(message_id)
new_message = {
"chat": chat["id"],
"chat": chat_dict["id"],
"id": message_id,
"author": author_id,
"body": body,
@@ -33,28 +35,28 @@ async def create_message(_, info, chat: str, body: str, reply_to=None):
}
if reply_to:
new_message["replyTo"] = reply_to
chat["updatedAt"] = new_message["createdAt"]
await redis.execute("SET", f"chats/{chat['id']}", json.dumps(chat))
chat_dict["updatedAt"] = new_message["createdAt"]
await redis.execute("SET", f"chats/{chat_dict['id']}", json.dumps(chat))
print(f"[inbox] creating message {new_message}")
await redis.execute(
"SET", f"chats/{chat['id']}/messages/{message_id}", json.dumps(new_message)
"SET", f"chats/{chat_dict['id']}/messages/{message_id}", json.dumps(new_message)
)
await redis.execute("LPUSH", f"chats/{chat['id']}/message_ids", str(message_id))
await redis.execute("LPUSH", f"chats/{chat_dict['id']}/message_ids", str(message_id))
await redis.execute(
"SET", f"chats/{chat['id']}/next_message_id", str(message_id + 1)
"SET", f"chats/{chat_dict['id']}/next_message_id", str(message_id + 1)
)
members = chat["members"]
members = chat_dict["members"]
for member_id in members:
await redis.execute(
"LPUSH", f"chats/{chat['id']}/unread/{member_id}", str(message_id)
"LPUSH", f"chats/{chat_dict['id']}/unread/{member_id}", str(message_id)
)
# result = FollowingResult("NEW", "chat", new_message)
# await FollowingManager.push("chat", result)
# subscribe on updates
await notify_message(new_message, chat["id"])
await notify_message(new_message, chat_dict["id"])
return {"message": new_message, "error": None}
@@ -103,7 +105,7 @@ async def delete_message(_, info, chat_id: str, message_id: int):
message = await redis.execute("GET", f"chats/{chat_id}/messages/{str(message_id)}")
if not message:
return {"error": "message not exist"}
message = json.loads(message)
message: Message = json.loads(message)
if message["author"] != author_id:
return {"error": "access denied"}
@@ -125,7 +127,7 @@ async def delete_message(_, info, chat_id: str, message_id: int):
@mutation.field("markAsRead")
@login_required
async def mark_as_read(_, info, chat_id: str, messages: [int]):
async def mark_as_read(_, info, chat_id: str, messages: List[int]):
author_id = info.context["author_id"]
chat = await redis.execute("GET", f"chats/{chat_id}")