core/resolvers/inbox/messages.py

166 lines
5.3 KiB
Python
Raw Normal View History

2022-11-09 11:46:00 +00:00
import asyncio
2022-09-04 17:20:38 +00:00
import json
2022-01-23 11:02:57 +00:00
from datetime import datetime
from auth.authenticate import login_required
2022-08-11 05:53:14 +00:00
from base.redis import redis
2022-11-15 02:36:30 +00:00
from base.resolvers import mutation, subscription
2022-11-09 11:46:00 +00:00
from services.inbox import ChatFollowing, MessageResult, MessagesStorage
2022-06-11 14:35:10 +00:00
2021-07-01 18:26:04 +00:00
@mutation.field("createMessage")
@login_required
2022-11-16 16:53:36 +00:00
async def create_message(_, info, chat: str, body: str, replyTo=None):
2022-11-11 21:27:17 +00:00
""" create message with :body for :chat_id replying to :replyTo optionally """
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
2022-11-16 16:53:36 +00:00
chat = await redis.execute("GET", f"chats/{chat}")
2022-09-03 10:50:14 +00:00
if not chat:
2022-11-02 10:00:04 +00:00
return {
2022-11-02 13:36:10 +00:00
"error": "chat not exist"
}
2022-11-16 17:13:12 +00:00
else:
2022-11-16 17:18:09 +00:00
chat = dict(json.loads(chat))
message_id = await redis.execute("GET", f"chats/{chat['id']}/next_message_id")
2022-11-16 17:13:12 +00:00
message_id = int(message_id)
new_message = {
2022-11-16 17:18:09 +00:00
"chatId": chat['id'],
2022-11-16 17:13:12 +00:00
"id": message_id,
"author": user.slug,
"body": body,
"replyTo": replyTo,
"createdAt": int(datetime.now().timestamp()),
}
2022-09-03 10:50:14 +00:00
await redis.execute(
2022-11-16 17:18:09 +00:00
"SET", f"chats/{chat['id']}/messages/{message_id}", json.dumps(new_message)
2022-09-03 10:50:14 +00:00
)
2022-11-16 17:18:09 +00:00
await redis.execute("LPUSH", f"chats/{chat['id']}/message_ids", str(message_id))
await redis.execute("SET", f"chats/{chat['id']}/next_message_id", str(message_id + 1))
2022-11-16 17:13:12 +00:00
chat = json.loads(chat)
users = chat["users"]
for user_slug in users:
await redis.execute(
2022-11-16 17:18:09 +00:00
"LPUSH", f"chats/{chat['id']}/unread/{user_slug}", str(message_id)
2022-11-16 17:13:12 +00:00
)
2022-01-23 11:02:57 +00:00
2022-11-16 17:13:12 +00:00
result = MessageResult("NEW", new_message)
await MessagesStorage.put(result)
2022-04-13 11:43:22 +00:00
2022-11-16 17:13:12 +00:00
return {
"message": new_message,
"error": None
}
2022-01-24 14:02:44 +00:00
2021-07-01 18:26:04 +00:00
@mutation.field("updateMessage")
@login_required
2022-11-02 13:36:10 +00:00
async def update_message(_, info, chat_id: str, message_id: int, body: str):
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
2022-11-02 13:36:10 +00:00
chat = await redis.execute("GET", f"chats/{chat_id}")
2022-09-03 10:50:14 +00:00
if not chat:
return {"error": "chat not exist"}
2022-01-24 11:56:55 +00:00
2022-11-02 13:36:10 +00:00
message = await redis.execute("GET", f"chats/{chat_id}/messages/{message_id}")
2022-09-03 10:50:14 +00:00
if not message:
return {"error": "message not exist"}
2022-01-24 11:56:55 +00:00
2022-09-03 10:50:14 +00:00
message = json.loads(message)
if message["author"] != user.slug:
return {"error": "access denied"}
2022-01-24 11:56:55 +00:00
2022-09-03 10:50:14 +00:00
message["body"] = body
2022-11-11 07:12:09 +00:00
message["updatedAt"] = int(datetime.now().timestamp())
2022-01-25 11:16:13 +00:00
2022-11-02 13:36:10 +00:00
await redis.execute("SET", f"chats/{chat_id}/messages/{message_id}", json.dumps(message))
2022-01-24 11:56:55 +00:00
2022-09-03 10:50:14 +00:00
result = MessageResult("UPDATED", message)
await MessagesStorage.put(result)
2022-01-24 11:56:55 +00:00
2022-11-02 10:00:04 +00:00
return {
"message": message,
"error": None
}
2022-01-24 11:56:55 +00:00
2021-07-01 18:26:04 +00:00
@mutation.field("deleteMessage")
@login_required
2022-11-02 13:36:10 +00:00
async def delete_message(_, info, chat_id: str, message_id: int):
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
2022-11-02 13:36:10 +00:00
chat = await redis.execute("GET", f"chats/{chat_id}")
2022-09-03 10:50:14 +00:00
if not chat:
return {"error": "chat not exist"}
2022-11-02 13:36:10 +00:00
chat = json.loads(chat)
2022-01-24 11:56:55 +00:00
2022-11-02 13:36:10 +00:00
message = await redis.execute("GET", f"chats/{chat_id}/messages/{str(message_id)}")
2022-09-03 10:50:14 +00:00
if not message:
return {"error": "message not exist"}
message = json.loads(message)
if message["author"] != user.slug:
return {"error": "access denied"}
2022-01-24 11:56:55 +00:00
2022-11-02 13:36:10 +00:00
await redis.execute("LREM", f"chats/{chat_id}/message_ids", 0, str(message_id))
await redis.execute("DEL", f"chats/{chat_id}/messages/{str(message_id)}")
2022-01-24 11:56:55 +00:00
2022-09-03 10:50:14 +00:00
users = chat["users"]
for user_slug in users:
2022-11-02 13:36:10 +00:00
await redis.execute("LREM", f"chats/{chat_id}/unread/{user_slug}", 0, str(message_id))
2022-01-24 11:56:55 +00:00
2022-09-03 10:50:14 +00:00
result = MessageResult("DELETED", message)
await MessagesStorage.put(result)
2022-04-13 11:43:22 +00:00
2022-09-03 10:50:14 +00:00
return {}
2022-01-24 14:02:44 +00:00
2021-07-02 09:16:43 +00:00
2022-04-13 11:43:22 +00:00
@mutation.field("markAsRead")
@login_required
2022-11-02 13:36:10 +00:00
async def mark_as_read(_, info, chat_id: str, messages: [int]):
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
2022-04-13 11:43:22 +00:00
2022-11-02 13:36:10 +00:00
chat = await redis.execute("GET", f"chats/{chat_id}")
2022-09-03 10:50:14 +00:00
if not chat:
return {"error": "chat not exist"}
2022-04-13 11:43:22 +00:00
2022-09-03 10:50:14 +00:00
chat = json.loads(chat)
users = set(chat["users"])
2022-09-04 17:20:38 +00:00
if user.slug not in users:
2022-09-03 10:50:14 +00:00
return {"error": "access denied"}
2022-04-13 11:43:22 +00:00
2022-11-02 13:36:10 +00:00
for message_id in messages:
await redis.execute("LREM", f"chats/{chat_id}/unread/{user.slug}", 0, str(message_id))
2022-09-03 10:50:14 +00:00
2022-11-02 10:00:04 +00:00
return {
"error": None
}
2022-04-13 11:43:22 +00:00
2022-11-02 13:36:10 +00:00
@subscription.source("newMessage")
2022-08-12 12:50:59 +00:00
@login_required
2022-11-02 13:36:10 +00:00
async def message_generator(obj, info):
2022-09-03 10:50:14 +00:00
try:
2022-11-02 13:36:10 +00:00
user = info.context["request"].user
2022-11-12 07:13:51 +00:00
user_following_chats = await redis.execute("GET", f"chats_by_user/{user.slug}")
if user_following_chats:
user_following_chats = list(json.loads(user_following_chats)) # chat ids
else:
user_following_chats = []
2022-11-02 13:36:10 +00:00
tasks = []
updated = {}
for chat_id in user_following_chats:
chat = await redis.execute("GET", f"chats/{chat_id}")
updated[chat_id] = chat['updatedAt']
user_following_chats_sorted = sorted(user_following_chats, key=lambda x: updated[x], reverse=True)
for chat_id in user_following_chats_sorted:
following_chat = ChatFollowing(chat_id)
await MessagesStorage.register_chat(following_chat)
chat_task = following_chat.queue.get()
tasks.append(chat_task)
2022-09-03 10:50:14 +00:00
while True:
2022-11-02 13:36:10 +00:00
msg = await asyncio.gather(*tasks)
2022-11-16 16:53:36 +00:00
print('[inbox] %d new messages' % len(tasks))
2022-09-03 10:50:14 +00:00
yield msg
finally:
await MessagesStorage.remove_chat(following_chat)