2023-10-03 14:15:17 +00:00
|
|
|
import json
|
2024-01-25 09:25:52 +00:00
|
|
|
import logging
|
2023-11-28 09:05:39 +00:00
|
|
|
import time
|
2023-12-17 17:13:17 +00:00
|
|
|
import uuid
|
2023-10-14 12:59:43 +00:00
|
|
|
|
2023-12-17 17:13:17 +00:00
|
|
|
from models.chat import Chat, ChatUpdate
|
2023-10-03 14:15:17 +00:00
|
|
|
from services.auth import login_required
|
2023-12-17 17:13:17 +00:00
|
|
|
from services.presence import notify_chat
|
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-25 09:25:52 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger('[resolvers.chats] ')
|
2024-01-23 20:13:49 +00:00
|
|
|
logger.setLevel(logging.DEBUG)
|
2023-11-22 12:09:24 +00:00
|
|
|
|
2024-01-23 21:13:14 +00:00
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
@mutation.field('update_chat')
|
2023-10-03 14:15:17 +00:00
|
|
|
@login_required
|
2023-10-14 14:55:51 +00:00
|
|
|
async def update_chat(_, info, chat_new: ChatUpdate):
|
2023-10-03 14:15:17 +00:00
|
|
|
"""
|
|
|
|
updating chat
|
2023-10-14 14:55:51 +00:00
|
|
|
requires info.context["author_id"] to be in chat["admins"]
|
|
|
|
:param _: not used
|
2023-10-03 14:15:17 +00:00
|
|
|
:param info: GraphQLInfo with request
|
|
|
|
:param chat_new: dict with chat data
|
|
|
|
:return: Result { error chat }
|
|
|
|
"""
|
2024-01-25 09:25:52 +00:00
|
|
|
logger.info('update_chat')
|
|
|
|
author_id = info.context['author_id']
|
|
|
|
chat_id = chat_new['id']
|
|
|
|
chat_str = await redis.execute('GET', f'chats/{chat_id}')
|
2023-10-14 14:55:51 +00:00
|
|
|
if not chat_str:
|
2024-01-25 09:25:52 +00:00
|
|
|
return {'error': 'chat not exist'}
|
2024-01-23 20:13:49 +00:00
|
|
|
elif isinstance(chat_str, str):
|
2023-10-14 14:55:51 +00:00
|
|
|
chat: Chat = json.loads(chat_str)
|
2024-01-25 09:25:52 +00:00
|
|
|
if author_id in chat['admins']:
|
2023-10-14 14:55:51 +00:00
|
|
|
chat.update(
|
|
|
|
{
|
2024-01-25 09:25:52 +00:00
|
|
|
'title': chat_new.get('title', chat['title']),
|
|
|
|
'description': chat_new.get('description', chat['description']),
|
|
|
|
'updated_at': int(time.time()),
|
|
|
|
'admins': chat_new.get('admins', chat.get('admins') or []),
|
|
|
|
'members': chat_new.get('members', chat['members']),
|
2023-10-14 14:55:51 +00:00
|
|
|
}
|
|
|
|
)
|
2023-11-16 14:52:39 +00:00
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
await redis.execute('SET', f"chats/{chat['id']}", json.dumps(chat))
|
|
|
|
for member_id in chat['members']:
|
|
|
|
await notify_chat(chat, member_id, 'update')
|
2023-10-03 14:15:17 +00:00
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
return {'error': None, 'chat': chat}
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
@mutation.field('create_chat')
|
2023-10-03 14:15:17 +00:00
|
|
|
@login_required
|
2024-01-25 09:25:52 +00:00
|
|
|
async def create_chat(_, info, title='', members=None):
|
|
|
|
logger.info('create_chat')
|
2023-12-19 09:46:57 +00:00
|
|
|
members = members or []
|
2024-01-25 09:25:52 +00:00
|
|
|
author_id = info.context['author_id']
|
2024-01-23 20:13:49 +00:00
|
|
|
chat: Chat
|
2023-12-19 14:24:52 +00:00
|
|
|
if author_id:
|
|
|
|
if author_id not in members:
|
|
|
|
members.append(int(author_id))
|
|
|
|
|
|
|
|
# NOTE: private chats has no title
|
|
|
|
# reuse private chat created before if exists
|
2024-01-25 09:25:52 +00:00
|
|
|
if len(members) == 2 and title == '':
|
|
|
|
chatset1 = await redis.execute('SMEMBERS', f'chats_by_author/{members[0]}')
|
|
|
|
chatset2 = await redis.execute('SMEMBERS', f'chats_by_author/{members[1]}')
|
2024-01-23 21:13:14 +00:00
|
|
|
for c in chatset1.intersection(chatset2):
|
2024-01-25 09:25:52 +00:00
|
|
|
chat = await redis.execute('GET', f'chats/{c}')
|
|
|
|
if chat['title'] == '':
|
|
|
|
logger.info('[inbox] createChat found old chat')
|
|
|
|
return {'chat': chat, 'error': 'existed'}
|
2023-12-19 14:24:52 +00:00
|
|
|
|
|
|
|
chat_id = str(uuid.uuid4())
|
|
|
|
chat: Chat = {
|
2024-01-25 09:25:52 +00:00
|
|
|
'id': chat_id,
|
|
|
|
'members': members,
|
|
|
|
'title': title,
|
|
|
|
'description': '',
|
|
|
|
'created_by': author_id,
|
|
|
|
'created_at': int(time.time()),
|
|
|
|
'updated_at': int(time.time()),
|
|
|
|
'admins': members if (len(members) == 2 and title == '') else [],
|
2023-12-19 14:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for member_id in members:
|
2024-01-25 09:25:52 +00:00
|
|
|
await redis.execute('SADD', f'chats_by_author/{member_id}', chat_id)
|
|
|
|
await notify_chat(chat, member_id, 'create')
|
2023-12-19 14:24:52 +00:00
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
print(f'\n\n[resolvers.chats] creating: {chat}\n\n')
|
2023-12-19 14:24:52 +00:00
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
await redis.execute('SET', f'chats/{chat_id}', json.dumps(chat))
|
|
|
|
await redis.execute('SET', f'chats/{chat_id}/next_message_id', str(0))
|
2023-10-13 08:31:14 +00:00
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
return {'error': None, 'chat': chat}
|
|
|
|
return {'error': 'no chat was created'}
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
|
|
|
2024-01-25 09:25:52 +00:00
|
|
|
@mutation.field('delete_chat')
|
2023-10-03 14:15:17 +00:00
|
|
|
@login_required
|
|
|
|
async def delete_chat(_, info, chat_id: str):
|
2024-01-25 09:25:52 +00:00
|
|
|
logger.info('delete_chat')
|
|
|
|
author_id = info.context['author_id']
|
|
|
|
chat_str = await redis.execute('GET', f'chats/{chat_id}')
|
2024-01-23 20:13:49 +00:00
|
|
|
if isinstance(chat_str, str):
|
2023-10-14 14:55:51 +00:00
|
|
|
chat: Chat = json.loads(chat_str)
|
2024-01-25 09:25:52 +00:00
|
|
|
if author_id in chat['admins']:
|
|
|
|
await redis.execute('DEL', f'chats/{chat_id}')
|
|
|
|
await redis.execute('SREM', f'chats_by_author/{author_id}', chat_id)
|
|
|
|
for member_id in chat['members']:
|
|
|
|
await notify_chat(chat, member_id, 'delete')
|
|
|
|
return {'error': 'chat not exist'}
|