2022-11-09 11:46:00 +00:00
|
|
|
import asyncio
|
2022-09-04 17:20:38 +00:00
|
|
|
import json
|
2022-11-23 14:09:35 +00:00
|
|
|
from datetime import datetime, timezone
|
2022-09-17 18:12:14 +00:00
|
|
|
|
|
|
|
from auth.authenticate import login_required
|
2022-12-01 14:45:19 +00:00
|
|
|
from auth.credentials import AuthCredentials
|
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-12-04 14:03:55 +00:00
|
|
|
from services.inbox.helpers import ChatFollowing, MessageResult
|
|
|
|
from services.inbox.storage import 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-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
|
|
|
|
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-12-04 14:03:55 +00:00
|
|
|
"error": "chat is not exist"
|
2022-11-02 13:36:10 +00:00
|
|
|
}
|
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,
|
2022-12-01 14:45:19 +00:00
|
|
|
"author": auth.user_id,
|
2022-11-16 17:13:12 +00:00
|
|
|
"body": body,
|
|
|
|
"replyTo": replyTo,
|
2022-11-23 14:09:35 +00:00
|
|
|
"createdAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
2022-11-16 17:13:12 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
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-06-28 09:08:09 +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-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
2022-09-03 10:50:14 +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-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)
|
2022-12-01 14:45:19 +00:00
|
|
|
if message["author"] != auth.user_id:
|
2022-09-03 10:50:14 +00:00
|
|
|
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-23 14:09:35 +00:00
|
|
|
message["updatedAt"] = int(datetime.now(tz=timezone.utc).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-06-28 09:08:09 +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-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
2022-09-03 10:50:14 +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-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)
|
2022-12-01 14:45:19 +00:00
|
|
|
if message["author"] != auth.user_id:
|
2022-09-03 10:50:14 +00:00
|
|
|
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"]
|
2022-12-01 14:45:19 +00:00
|
|
|
for user_id in users:
|
|
|
|
await redis.execute("LREM", f"chats/{chat_id}/unread/{user_id}", 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-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
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-12-01 14:45:19 +00:00
|
|
|
if auth.user_id 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:
|
2022-12-01 14:45:19 +00:00
|
|
|
await redis.execute("LREM", f"chats/{chat_id}/unread/{auth.user_id}", 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-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
|
|
|
|
|
|
|
user_following_chats = await redis.execute("GET", f"chats_by_user/{auth.user_id}")
|
2022-11-12 07:13:51 +00:00
|
|
|
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)
|
2022-12-04 14:03:55 +00:00
|
|
|
|
2022-11-02 13:36:10 +00:00
|
|
|
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)
|