2022-01-24 14:08:30 +00:00
|
|
|
from orm import User
|
2021-08-05 16:49:08 +00:00
|
|
|
from orm.base import local_session
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2022-02-22 11:44:01 +00:00
|
|
|
from resolvers_base import mutation, query, subscription
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2021-07-01 18:26:04 +00:00
|
|
|
from auth.authenticate import login_required
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2022-01-23 11:02:57 +00:00
|
|
|
import asyncio, uuid, json
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from redis import redis
|
2021-07-02 09:16:43 +00:00
|
|
|
|
2022-01-24 14:02:44 +00:00
|
|
|
class MessageSubscription:
|
|
|
|
queue = asyncio.Queue()
|
|
|
|
|
|
|
|
def __init__(self, chat_id):
|
|
|
|
self.chat_id = chat_id
|
|
|
|
|
2022-06-21 12:21:02 +00:00
|
|
|
class MessagesStorage:
|
2021-11-24 07:36:06 +00:00
|
|
|
lock = asyncio.Lock()
|
|
|
|
subscriptions = []
|
2021-07-08 07:58:49 +00:00
|
|
|
|
2021-11-24 07:36:06 +00:00
|
|
|
@staticmethod
|
|
|
|
async def register_subscription(subs):
|
2022-06-21 12:21:02 +00:00
|
|
|
async with MessagesStorage.lock:
|
|
|
|
MessagesStorage.subscriptions.append(subs)
|
2021-07-08 07:58:49 +00:00
|
|
|
|
2021-11-24 07:36:06 +00:00
|
|
|
@staticmethod
|
|
|
|
async def del_subscription(subs):
|
2022-06-21 12:21:02 +00:00
|
|
|
async with MessagesStorage.lock:
|
|
|
|
MessagesStorage.subscriptions.remove(subs)
|
2021-11-24 07:36:06 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-01-24 14:02:44 +00:00
|
|
|
async def put(message_result):
|
2022-06-21 12:21:02 +00:00
|
|
|
async with MessagesStorage.lock:
|
|
|
|
for subs in MessagesStorage.subscriptions:
|
2022-01-24 14:02:44 +00:00
|
|
|
if message_result.message["chatId"] == subs.chat_id:
|
|
|
|
subs.queue.put_nowait(message_result)
|
2021-11-24 07:36:06 +00:00
|
|
|
|
|
|
|
class MessageResult:
|
|
|
|
def __init__(self, status, message):
|
|
|
|
self.status = status
|
|
|
|
self.message = message
|
2021-07-08 07:58:49 +00:00
|
|
|
|
2022-04-13 12:11:06 +00:00
|
|
|
async def get_total_unread_messages_for_user(user_slug):
|
|
|
|
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
|
|
|
|
if not chats:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
chats = json.loads(chats)
|
|
|
|
total = 0
|
|
|
|
for chat_id in chats:
|
|
|
|
n = await redis.execute("LLEN", f"chats/{chat_id}/unread/{user_slug}")
|
|
|
|
total += n
|
|
|
|
|
|
|
|
return total
|
|
|
|
|
2022-04-13 11:43:22 +00:00
|
|
|
async def add_user_to_chat(user_slug, chat_id, chat = None):
|
|
|
|
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
|
|
|
|
if not chats:
|
|
|
|
chats = set()
|
|
|
|
else:
|
|
|
|
chats = set(json.loads(chats))
|
|
|
|
chats.add(str(chat_id))
|
|
|
|
chats = list(chats)
|
|
|
|
await redis.execute("SET", f"chats_by_user/{user_slug}", json.dumps(chats))
|
|
|
|
|
|
|
|
if chat:
|
|
|
|
users = set(chat["users"])
|
|
|
|
users.add(user_slug)
|
|
|
|
chat["users"] = list(users)
|
|
|
|
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
|
|
|
|
|
2022-01-23 11:02:57 +00:00
|
|
|
@mutation.field("createChat")
|
|
|
|
@login_required
|
|
|
|
async def create_chat(_, info, description):
|
|
|
|
user = info.context["request"].user
|
|
|
|
|
|
|
|
chat_id = uuid.uuid4()
|
|
|
|
chat = {
|
|
|
|
"description" : description,
|
|
|
|
"createdAt" : str(datetime.now),
|
|
|
|
"createdBy" : user.slug,
|
2022-04-13 11:43:22 +00:00
|
|
|
"id" : str(chat_id),
|
|
|
|
"users" : [user.slug]
|
2022-01-23 11:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
|
2022-01-24 11:56:55 +00:00
|
|
|
await redis.execute("SET", f"chats/{chat_id}/next_message_id", 0)
|
2022-01-23 11:02:57 +00:00
|
|
|
|
2022-04-13 11:43:22 +00:00
|
|
|
await add_user_to_chat(user.slug, chat_id)
|
|
|
|
|
2022-01-23 11:02:57 +00:00
|
|
|
return { "chatId" : chat_id }
|
|
|
|
|
2022-01-28 12:52:14 +00:00
|
|
|
async def load_messages(chatId, size, page):
|
|
|
|
message_ids = await redis.lrange(f"chats/{chatId}/message_ids",
|
|
|
|
size * (page -1), size * page - 1)
|
|
|
|
messages = []
|
|
|
|
if message_ids:
|
|
|
|
message_keys = [f"chats/{chatId}/messages/{id.decode('UTF-8')}" for id in message_ids]
|
|
|
|
messages = await redis.mget(*message_keys)
|
|
|
|
messages = [json.loads(msg) for msg in messages]
|
|
|
|
return messages
|
|
|
|
|
2022-06-11 14:35:10 +00:00
|
|
|
@query.field("userChats")
|
|
|
|
@login_required
|
|
|
|
async def user_chats(_, info):
|
|
|
|
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))
|
|
|
|
|
|
|
|
return {"chats" : chats}
|
|
|
|
|
2022-01-23 11:02:57 +00:00
|
|
|
@query.field("enterChat")
|
|
|
|
@login_required
|
2022-01-28 12:52:14 +00:00
|
|
|
async def enter_chat(_, info, chatId, size):
|
2022-04-13 11:43:22 +00:00
|
|
|
user = info.context["request"].user
|
|
|
|
|
2022-01-23 11:02:57 +00:00
|
|
|
chat = await redis.execute("GET", f"chats/{chatId}")
|
|
|
|
if not chat:
|
|
|
|
return { "error" : "chat not exist" }
|
|
|
|
chat = json.loads(chat)
|
|
|
|
|
2022-01-28 12:52:14 +00:00
|
|
|
messages = await load_messages(chatId, size, 1)
|
2022-01-23 11:02:57 +00:00
|
|
|
|
2022-04-13 11:43:22 +00:00
|
|
|
await add_user_to_chat(user.slug, chatId, chat)
|
|
|
|
|
2022-01-23 11:02:57 +00:00
|
|
|
return {
|
|
|
|
"chat" : chat,
|
|
|
|
"messages" : messages
|
|
|
|
}
|
2021-07-02 09:16:43 +00:00
|
|
|
|
2021-07-01 18:26:04 +00:00
|
|
|
@mutation.field("createMessage")
|
|
|
|
@login_required
|
2022-01-23 11:02:57 +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-24 11:56:55 +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
|
|
|
new_message = {
|
|
|
|
"chatId" : chatId,
|
2022-01-24 11:56:55 +00:00
|
|
|
"id" : message_id,
|
2022-01-23 11:02:57 +00:00
|
|
|
"author" : user.slug,
|
|
|
|
"body" : body,
|
2022-01-25 11:16:13 +00:00
|
|
|
"replyTo" : replyTo,
|
|
|
|
"createdAt" : datetime.now().isoformat()
|
2022-01-23 11:02:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 11:56:55 +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-04-13 11:43:22 +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-24 14:02:44 +00:00
|
|
|
result = MessageResult("NEW", new_message)
|
2022-06-21 12:21:02 +00:00
|
|
|
await MessagesStorage.put(result)
|
2022-01-24 14:02:44 +00:00
|
|
|
|
2021-08-04 13:38:56 +00:00
|
|
|
return {"message" : new_message}
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2021-07-01 18:26:04 +00:00
|
|
|
@query.field("getMessages")
|
|
|
|
@login_required
|
2022-01-28 12:52:14 +00:00
|
|
|
async def get_messages(_, info, chatId, size, page):
|
|
|
|
chat = await redis.execute("GET", f"chats/{chatId}")
|
|
|
|
if not chat:
|
|
|
|
return { "error" : "chat not exist" }
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2022-01-28 12:52:14 +00:00
|
|
|
messages = await load_messages(chatId, size, page)
|
|
|
|
|
|
|
|
return messages
|
2021-06-28 09:08:09 +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):
|
|
|
|
user = info.context["request"].user
|
|
|
|
|
|
|
|
chat = await redis.execute("GET", f"chats/{chatId}")
|
|
|
|
if not chat:
|
|
|
|
return { "error" : "chat not exist" }
|
|
|
|
|
|
|
|
message = await redis.execute("GET", f"chats/{chatId}/messages/{id}")
|
|
|
|
if not message:
|
|
|
|
return { "error" : "message not exist" }
|
|
|
|
|
|
|
|
message = json.loads(message)
|
2022-01-25 11:16:13 +00:00
|
|
|
if message["author"] != user.slug:
|
|
|
|
return { "error" : "access denied" }
|
|
|
|
|
2022-01-24 11:56:55 +00:00
|
|
|
message["body"] = body
|
2022-01-25 11:16:13 +00:00
|
|
|
message["updatedAt"] = datetime.now().isoformat()
|
2022-01-24 11:56:55 +00:00
|
|
|
|
|
|
|
await redis.execute("SET", f"chats/{chatId}/messages/{id}", json.dumps(message))
|
|
|
|
|
2022-01-24 14:02:44 +00:00
|
|
|
result = MessageResult("UPDATED", message)
|
2022-06-21 12:21:02 +00:00
|
|
|
await MessagesStorage.put(result)
|
2022-01-24 11:56:55 +00:00
|
|
|
|
2021-08-04 13:38:56 +00:00
|
|
|
return {"message" : message}
|
2021-06-28 09:08:09 +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):
|
|
|
|
user = info.context["request"].user
|
|
|
|
|
|
|
|
chat = await redis.execute("GET", f"chats/{chatId}")
|
|
|
|
if not chat:
|
|
|
|
return { "error" : "chat not exist" }
|
|
|
|
|
2022-01-24 14:02:44 +00:00
|
|
|
message = await redis.execute("GET", f"chats/{chatId}/messages/{id}")
|
|
|
|
if not message:
|
|
|
|
return { "error" : "message not exist" }
|
|
|
|
message = json.loads(message)
|
2022-01-25 11:16:13 +00:00
|
|
|
if message["author"] != user.slug:
|
|
|
|
return { "error" : "access denied" }
|
2022-01-24 11:56:55 +00:00
|
|
|
|
2022-01-24 14:02:44 +00:00
|
|
|
await redis.execute("LREM", f"chats/{chatId}/message_ids", 0, str(id))
|
2022-01-24 11:56:55 +00:00
|
|
|
await redis.execute("DEL", f"chats/{chatId}/messages/{id}")
|
|
|
|
|
2022-04-13 11:43:22 +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 14:02:44 +00:00
|
|
|
result = MessageResult("DELETED", message)
|
2022-06-21 12:21:02 +00:00
|
|
|
await MessagesStorage.put(result)
|
2022-01-24 14:02:44 +00:00
|
|
|
|
2021-08-04 13:38:56 +00:00
|
|
|
return {}
|
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):
|
|
|
|
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)
|
|
|
|
users = set(chat["users"])
|
|
|
|
if not user.slug in users:
|
|
|
|
return { "error" : "access denied" }
|
|
|
|
|
|
|
|
for id in ids:
|
|
|
|
await redis.execute("LREM", f"chats/{chatId}/unread/{user.slug}", 0, str(id))
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
2022-01-24 14:02:44 +00:00
|
|
|
@subscription.source("chatUpdated")
|
|
|
|
async def message_generator(obj, info, chatId):
|
2022-01-25 11:16:13 +00:00
|
|
|
|
|
|
|
#TODO: send AUTH header
|
|
|
|
#auth = info.context["request"].auth
|
|
|
|
#if not auth.logged_in:
|
|
|
|
# yield {"error" : auth.error_message or "Please login"}
|
|
|
|
|
2021-11-24 07:36:06 +00:00
|
|
|
try:
|
2022-01-24 14:02:44 +00:00
|
|
|
subs = MessageSubscription(chatId)
|
2022-06-21 12:21:02 +00:00
|
|
|
await MessagesStorage.register_subscription(subs)
|
2021-11-24 07:36:06 +00:00
|
|
|
while True:
|
2022-01-24 14:02:44 +00:00
|
|
|
msg = await subs.queue.get()
|
2021-11-24 07:36:06 +00:00
|
|
|
yield msg
|
|
|
|
finally:
|
2022-06-21 12:21:02 +00:00
|
|
|
await MessagesStorage.del_subscription(subs)
|
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):
|
2021-07-02 09:16:43 +00:00
|
|
|
return message
|