2023-10-03 14:15:17 +00:00
|
|
|
|
import json
|
2024-01-25 09:25:52 +00:00
|
|
|
|
import logging
|
2023-11-22 12:09:24 +00:00
|
|
|
|
import time
|
2023-10-14 12:59:43 +00:00
|
|
|
|
|
2023-12-17 17:13:17 +00:00
|
|
|
|
from models.chat import Message
|
2023-10-03 14:15:17 +00:00
|
|
|
|
from services.auth import login_required
|
2023-10-04 17:14:06 +00:00
|
|
|
|
from services.presence import notify_message
|
2023-10-14 14:55:51 +00:00
|
|
|
|
from services.rediscache import redis
|
2023-10-04 21:43:07 +00:00
|
|
|
|
from services.schema import mutation
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-23 21:13:14 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
logger = logging.getLogger('[resolvers.messages] ')
|
2024-01-23 21:13:14 +00:00
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
@mutation.field('create_message')
|
2023-10-03 14:15:17 +00:00
|
|
|
|
@login_required
|
2023-11-16 15:16:51 +00:00
|
|
|
|
async def create_message(_, info, chat_id: str, body: str, reply_to=None):
|
2023-10-16 18:19:38 +00:00
|
|
|
|
"""Создание сообщения с телом :body для чата :chat_id с возможным ответом на :reply_to"""
|
2024-01-25 09:25:52 +00:00
|
|
|
|
author_id = info.context['author_id']
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2023-10-16 18:19:38 +00:00
|
|
|
|
# Получение данных чата из Redis
|
2024-01-25 09:25:52 +00:00
|
|
|
|
chat_data = await redis.execute('GET', f'chats/{chat_id}')
|
|
|
|
|
logger.debug(f'chat data: {chat_data}')
|
2023-10-16 18:19:38 +00:00
|
|
|
|
|
|
|
|
|
# Если данных чата нет, возвращаем ошибку
|
2023-10-13 17:55:13 +00:00
|
|
|
|
if not chat_data:
|
2024-01-25 09:25:52 +00:00
|
|
|
|
return {'error': 'chat is not exist'}
|
2024-01-23 20:13:49 +00:00
|
|
|
|
elif isinstance(chat_data, str):
|
2023-10-16 18:19:38 +00:00
|
|
|
|
# Преобразование данных чата из строки JSON в словарь
|
2023-10-13 17:55:13 +00:00
|
|
|
|
chat_dict = json.loads(chat_data)
|
2024-01-25 09:25:52 +00:00
|
|
|
|
chat_id = chat_dict['id']
|
2023-10-16 18:19:38 +00:00
|
|
|
|
|
|
|
|
|
# Получение ID следующего сообщения
|
2024-01-25 09:25:52 +00:00
|
|
|
|
message_id = await redis.execute('GET', f"chats/{chat_dict['id']}/next_message_id")
|
2024-01-23 20:13:49 +00:00
|
|
|
|
if isinstance(message_id, str) or isinstance(message_id, int):
|
|
|
|
|
message_id = int(message_id) if message_id else 0
|
|
|
|
|
# Создание нового сообщения
|
|
|
|
|
new_message: Message = {
|
2024-01-25 09:25:52 +00:00
|
|
|
|
'chat_id': chat_id,
|
|
|
|
|
'id': message_id,
|
|
|
|
|
'created_by': author_id,
|
|
|
|
|
'body': body,
|
|
|
|
|
'created_at': int(time.time()),
|
|
|
|
|
'updated_at': None,
|
|
|
|
|
'reply_to': None,
|
2024-01-23 20:13:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Если есть ответ, добавляем его в сообщение
|
|
|
|
|
if reply_to:
|
2024-01-25 09:25:52 +00:00
|
|
|
|
new_message['reply_to'] = reply_to
|
2024-01-23 20:13:49 +00:00
|
|
|
|
|
|
|
|
|
# Обновление времени последнего обновления чата
|
2024-01-25 09:25:52 +00:00
|
|
|
|
chat_dict['updated_at'] = new_message['created_at']
|
2024-01-23 20:13:49 +00:00
|
|
|
|
|
|
|
|
|
# Запись обновленных данных чата обратно в Redis
|
2024-01-25 09:25:52 +00:00
|
|
|
|
await redis.execute('SET', f'chats/{chat_id}', json.dumps(chat_dict))
|
|
|
|
|
logger.debug(f'creating message {new_message}')
|
2024-01-23 20:13:49 +00:00
|
|
|
|
|
|
|
|
|
# Запись нового сообщения в Redis
|
|
|
|
|
await redis.execute(
|
2024-01-25 09:25:52 +00:00
|
|
|
|
'SET',
|
|
|
|
|
f'chats/{chat_id}/messages/{message_id}',
|
2024-01-23 20:13:49 +00:00
|
|
|
|
json.dumps(new_message),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Добавление ID нового сообщения в список ID сообщений чата
|
2024-01-25 09:25:52 +00:00
|
|
|
|
await redis.execute('LPUSH', f'chats/{chat_id}/message_ids', str(message_id))
|
2024-01-23 20:13:49 +00:00
|
|
|
|
|
|
|
|
|
# Обновление ID следующего сообщения
|
2024-01-25 09:25:52 +00:00
|
|
|
|
await redis.execute('SET', f'chats/{chat_id}/next_message_id', str(message_id + 1))
|
2024-01-23 20:13:49 +00:00
|
|
|
|
|
|
|
|
|
# Добавление нового сообщения в список непрочитанных сообщений для каждого участника чата
|
2024-01-25 09:25:52 +00:00
|
|
|
|
members = chat_dict['members']
|
2024-01-23 20:13:49 +00:00
|
|
|
|
for member_id in members:
|
2024-01-25 09:25:52 +00:00
|
|
|
|
await redis.execute('LPUSH', f"chats/{chat_dict['id']}/unread/{member_id}", str(message_id))
|
2024-01-23 20:13:49 +00:00
|
|
|
|
|
|
|
|
|
# Отправка уведомления о новом сообщении
|
2024-01-25 09:25:52 +00:00
|
|
|
|
new_message['chat_id'] = chat_id
|
|
|
|
|
await notify_message(new_message, 'create')
|
2024-01-23 20:13:49 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
return {'message': new_message, 'error': None}
|
|
|
|
|
return {'error': 'cannot create message'}
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
@mutation.field('update_message')
|
2023-10-03 14:15:17 +00:00
|
|
|
|
@login_required
|
2023-11-17 08:51:15 +00:00
|
|
|
|
async def update_message(_, info, message):
|
2024-01-25 09:25:52 +00:00
|
|
|
|
author_id = info.context['author_id']
|
|
|
|
|
chat_id = message.get('chat_id')
|
|
|
|
|
chat_str = ''
|
2023-11-17 08:51:15 +00:00
|
|
|
|
if chat_id:
|
2024-01-25 09:25:52 +00:00
|
|
|
|
chat_str = await redis.execute('GET', f'chats/{chat_id}')
|
2023-11-17 08:51:15 +00:00
|
|
|
|
if not chat_str:
|
2024-01-25 09:25:52 +00:00
|
|
|
|
return {'error': 'chat not exist'}
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
message_id = message.get('id')
|
|
|
|
|
body = message.get('body')
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2023-11-17 08:51:15 +00:00
|
|
|
|
if message_id:
|
2024-01-25 09:25:52 +00:00
|
|
|
|
message = await redis.execute('GET', f'chats/{chat_id}/messages/{message_id}')
|
2024-01-23 21:13:14 +00:00
|
|
|
|
if isinstance(message, str):
|
2024-01-23 20:13:49 +00:00
|
|
|
|
message = json.loads(message)
|
2024-01-25 09:25:52 +00:00
|
|
|
|
if message['created_by'] != author_id:
|
|
|
|
|
return {'error': 'access denied'}
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-23 20:13:49 +00:00
|
|
|
|
if body:
|
2024-01-25 09:25:52 +00:00
|
|
|
|
message['body'] = body
|
|
|
|
|
message['updated_at'] = int(time.time())
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
await redis.execute('SET', f'chats/{chat_id}/messages/{message_id}', json.dumps(message))
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-23 20:13:49 +00:00
|
|
|
|
# Отправка уведомления
|
2024-01-25 09:25:52 +00:00
|
|
|
|
message['chat_id'] = chat_id
|
|
|
|
|
await notify_message(message, 'update')
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
return {'message': message, 'error': None}
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
return {'message': message, 'error': 'cannot update'}
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
@mutation.field('delete_message')
|
2023-10-03 14:15:17 +00:00
|
|
|
|
@login_required
|
|
|
|
|
async def delete_message(_, info, chat_id: str, message_id: int):
|
2024-01-25 09:25:52 +00:00
|
|
|
|
author_id = info.context['author_id']
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
chat_str = await redis.execute('GET', f'chats/{chat_id}')
|
2024-01-23 21:13:14 +00:00
|
|
|
|
if isinstance(chat_str, str):
|
2024-01-23 20:13:49 +00:00
|
|
|
|
chat = json.loads(chat_str)
|
2024-01-25 09:25:52 +00:00
|
|
|
|
message_data = await redis.execute('GET', f'chats/{chat_id}/messages/{str(message_id)}')
|
2024-01-23 21:13:14 +00:00
|
|
|
|
if isinstance(message_data, str):
|
2024-01-23 20:13:49 +00:00
|
|
|
|
message: Message = json.loads(message_data)
|
2024-01-25 09:25:52 +00:00
|
|
|
|
if message['created_by'] != author_id:
|
|
|
|
|
return {'error': 'access denied'}
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +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)}')
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
members = chat['members']
|
2024-01-23 20:13:49 +00:00
|
|
|
|
for member_id in members:
|
2024-01-25 09:25:52 +00:00
|
|
|
|
await redis.execute('LREM', f'chats/{chat_id}/unread/{member_id}', 0, str(message_id))
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
message['chat_id'] = chat_id
|
|
|
|
|
await notify_message(message, 'delete')
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
@mutation.field('mark_as_read')
|
2023-10-03 14:15:17 +00:00
|
|
|
|
@login_required
|
2023-11-16 15:16:51 +00:00
|
|
|
|
async def mark_as_read(_, info, chat_id: str, message_id: int):
|
2024-01-25 09:25:52 +00:00
|
|
|
|
author_id = info.context['author_id']
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
chat_str = await redis.execute('GET', f'chats/{chat_id}')
|
2024-01-23 20:13:49 +00:00
|
|
|
|
if isinstance(chat_str, str):
|
|
|
|
|
chat = json.loads(chat_str)
|
2024-01-25 09:25:52 +00:00
|
|
|
|
members = set(chat['members'])
|
2024-01-23 20:13:49 +00:00
|
|
|
|
if author_id not in members:
|
2024-01-25 09:25:52 +00:00
|
|
|
|
return {'error': 'access denied'}
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
await redis.execute('LREM', f'chats/{chat_id}/unread/{author_id}', 0, str(message_id))
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
message_data = await redis.execute('GET', f'chats/{chat_id}/messages/{str(message_id)}')
|
2024-01-23 21:13:14 +00:00
|
|
|
|
if isinstance(message_data, str):
|
2024-01-23 20:13:49 +00:00
|
|
|
|
message: Message = json.loads(message_data)
|
2023-11-22 12:09:24 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
await notify_message(message, 'seen')
|
2023-10-19 14:43:20 +00:00
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
|
return {'error': None}
|