core/resolvers/inbox.py

258 lines
7.3 KiB
Python
Raw Normal View History

2022-09-04 17:20:38 +00:00
import json
import uuid
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
from base.resolvers import mutation, query, subscription
2022-11-02 09:23:14 +00:00
from services.inbox import MessageResult, MessagesStorage, ChatFollowing
2022-09-03 10:50:14 +00:00
2021-07-08 07:58:49 +00:00
2022-08-12 12:50:59 +00:00
async def get_unread_counter(user_slug):
2022-09-03 10:50:14 +00:00
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
if not chats:
return 0
chats = json.loads(chats)
unread = 0
for chat_id in chats:
n = await redis.execute("LLEN", f"chats/{chat_id}/unread/{user_slug}")
unread += n
return unread
2022-11-02 09:23:14 +00:00
async def add_user_to_chat(user_slug: str, chat_id: int, chat=None):
2022-09-03 10:50:14 +00:00
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
2022-11-02 09:23:14 +00:00
if chats:
chats = list(json.loads(chats))
2022-09-03 10:50:14 +00:00
else:
2022-11-02 09:23:14 +00:00
chats = []
if chat_id not in chats:
chats.append(chat_id)
2022-09-03 10:50:14 +00:00
await redis.execute("SET", f"chats_by_user/{user_slug}", json.dumps(chats))
2022-11-02 09:23:14 +00:00
if user_slug not in chat["users"]:
chat["users"].append(user_slug)
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
2022-09-03 10:50:14 +00:00
2022-04-13 11:43:22 +00:00
2022-10-03 23:43:08 +00:00
@mutation.field("inviteChat")
2022-10-03 23:25:11 +00:00
async def invite_to_chat(_, info, invited, chat_id):
user = info.context["request"].user
chat = await redis.execute("GET", f"chats/{chat_id}")
if user.slug in chat['users']:
add_user_to_chat(invited, chat_id, chat)
2022-01-23 11:02:57 +00:00
@mutation.field("createChat")
@login_required
2022-11-02 09:23:14 +00:00
async def create_chat(_, info, description="", title=""):
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
chat_id = uuid.uuid4()
chat = {
2022-10-03 23:25:11 +00:00
"title": title,
2022-09-03 10:50:14 +00:00
"description": description,
2022-11-02 09:23:14 +00:00
"createdAt": str(datetime.now().timestamp()),
"updatedAt": str(datetime.now().timestamp()),
2022-09-03 10:50:14 +00:00
"createdBy": user.slug,
"id": str(chat_id),
"users": [user.slug],
}
2022-01-23 11:02:57 +00:00
2022-09-03 10:50:14 +00:00
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
await redis.execute("SET", f"chats/{chat_id}/next_message_id", 0)
await add_user_to_chat(user.slug, chat_id)
2022-01-23 11:02:57 +00:00
2022-11-02 09:23:14 +00:00
return chat
2022-04-13 11:43:22 +00:00
2022-01-23 11:02:57 +00:00
2022-11-02 09:23:14 +00:00
async def load_messages(chatId: int, size: int, page: int):
2022-09-03 10:50:14 +00:00
message_ids = await redis.lrange(
f"chats/{chatId}/message_ids", size * (page - 1), size * page - 1
)
messages = []
if message_ids:
message_keys = [
2022-11-02 09:23:14 +00:00
f"chats/{chatId}/messages/{mid}" for mid in message_ids
2022-09-03 10:50:14 +00:00
]
messages = await redis.mget(*message_keys)
messages = [json.loads(msg) for msg in messages]
return messages
2022-01-28 12:52:14 +00:00
2022-10-03 23:25:11 +00:00
@query.field("myChats")
2022-06-11 14:35:10 +00:00
@login_required
async def user_chats(_, info):
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
chats = await redis.execute("GET", f"chats_by_user/{user.slug}")
if not chats:
chats = list()
else:
chats = list(json.loads(chats))
2022-10-03 23:25:11 +00:00
for c in chats:
c['messages'] = await load_messages(c['id'], 50, 1)
return chats
2022-06-11 14:35:10 +00:00
2022-01-23 11:02:57 +00:00
@query.field("enterChat")
@login_required
2022-10-03 23:25:11 +00:00
async def enter_chat(_, info, chatId):
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
chat = await redis.execute("GET", f"chats/{chatId}")
if not chat:
return {"error": "chat not exist"}
chat = json.loads(chat)
await add_user_to_chat(user.slug, chatId, chat)
2022-10-03 23:25:11 +00:00
chat['messages'] = await load_messages(chatId, 50, 1)
return chat
2022-04-13 11:43:22 +00:00
2021-07-02 09:16:43 +00:00
2021-07-01 18:26:04 +00:00
@mutation.field("createMessage")
@login_required
2022-09-03 10:50:14 +00:00
async def create_message(_, info, chatId, body, replyTo=None):
user = info.context["request"].user
chat = await redis.execute("GET", f"chats/{chatId}")
if not chat:
return {"error": "chat not exist"}
2022-01-23 11:02:57 +00:00
2022-09-03 10:50:14 +00:00
message_id = await redis.execute("GET", f"chats/{chatId}/next_message_id")
message_id = int(message_id)
2022-01-23 11:02:57 +00:00
2022-09-03 10:50:14 +00:00
new_message = {
"chatId": chatId,
"id": message_id,
"author": user.slug,
"body": body,
"replyTo": replyTo,
"createdAt": datetime.now().isoformat(),
}
2022-01-24 11:56:55 +00:00
2022-09-03 10:50:14 +00:00
await redis.execute(
"SET", f"chats/{chatId}/messages/{message_id}", json.dumps(new_message)
)
await redis.execute("LPUSH", f"chats/{chatId}/message_ids", str(message_id))
await redis.execute("SET", f"chats/{chatId}/next_message_id", str(message_id + 1))
2022-01-23 11:02:57 +00:00
2022-09-03 10:50:14 +00:00
chat = json.loads(chat)
users = chat["users"]
for user_slug in users:
await redis.execute(
"LPUSH", f"chats/{chatId}/unread/{user_slug}", str(message_id)
)
2022-01-23 11:02:57 +00:00
2022-09-03 10:50:14 +00:00
result = MessageResult("NEW", new_message)
await MessagesStorage.put(result)
2022-04-13 11:43:22 +00:00
2022-09-03 10:50:14 +00:00
return {"message": new_message}
2022-01-24 14:02:44 +00:00
2022-10-03 23:25:11 +00:00
@query.field("loadChat")
2021-07-01 18:26:04 +00:00
@login_required
2022-01-28 12:52:14 +00:00
async def get_messages(_, info, chatId, size, page):
2022-09-03 10:50:14 +00:00
chat = await redis.execute("GET", f"chats/{chatId}")
if not chat:
return {"error": "chat not exist"}
2022-09-03 10:50:14 +00:00
messages = await load_messages(chatId, size, page)
return messages
2022-01-28 12:52:14 +00:00
2021-07-01 18:26:04 +00:00
@mutation.field("updateMessage")
@login_required
2022-01-24 11:56:55 +00:00
async def update_message(_, info, chatId, id, body):
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
chat = await redis.execute("GET", f"chats/{chatId}")
if not chat:
return {"error": "chat not exist"}
2022-01-24 11:56:55 +00:00
2022-09-03 10:50:14 +00:00
message = await redis.execute("GET", f"chats/{chatId}/messages/{id}")
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
message["updatedAt"] = datetime.now().isoformat()
2022-01-25 11:16:13 +00:00
2022-09-03 10:50:14 +00:00
await redis.execute("SET", f"chats/{chatId}/messages/{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-09-03 10:50:14 +00:00
return {"message": message}
2022-01-24 11:56:55 +00:00
2021-07-01 18:26:04 +00:00
@mutation.field("deleteMessage")
@login_required
2022-01-24 11:56:55 +00:00
async def delete_message(_, info, chatId, id):
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
chat = await redis.execute("GET", f"chats/{chatId}")
if not chat:
return {"error": "chat not exist"}
2022-01-24 11:56:55 +00:00
2022-09-03 10:50:14 +00:00
message = await redis.execute("GET", f"chats/{chatId}/messages/{id}")
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-09-03 10:50:14 +00:00
await redis.execute("LREM", f"chats/{chatId}/message_ids", 0, str(id))
await redis.execute("DEL", f"chats/{chatId}/messages/{id}")
2022-01-24 11:56:55 +00:00
2022-09-03 10:50:14 +00:00
chat = json.loads(chat)
users = chat["users"]
for user_slug in users:
await redis.execute("LREM", f"chats/{chatId}/unread/{user_slug}", 0, str(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
async def mark_as_read(_, info, chatId, ids):
2022-09-03 10:50:14 +00:00
user = info.context["request"].user
2022-04-13 11:43:22 +00:00
2022-09-03 10:50:14 +00:00
chat = await redis.execute("GET", f"chats/{chatId}")
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-09-03 10:50:14 +00:00
for id in ids:
await redis.execute("LREM", f"chats/{chatId}/unread/{user.slug}", 0, str(id))
return {}
2022-04-13 11:43:22 +00:00
2022-01-24 14:02:44 +00:00
@subscription.source("chatUpdated")
2022-08-12 12:50:59 +00:00
@login_required
2022-01-24 14:02:44 +00:00
async def message_generator(obj, info, chatId):
2022-09-03 10:50:14 +00:00
try:
following_chat = ChatFollowing(chatId)
await MessagesStorage.register_chat(following_chat)
while True:
msg = await following_chat.queue.get()
yield msg
finally:
await MessagesStorage.remove_chat(following_chat)
2021-11-24 07:36:06 +00:00
2022-01-24 14:02:44 +00:00
@subscription.field("chatUpdated")
def message_resolver(message, info, chatId):
2022-09-03 10:50:14 +00:00
return message