refac: async db
This commit is contained in:
@@ -333,7 +333,7 @@ async def connect(sid, environ, auth):
|
||||
data = decode_token(auth['token'])
|
||||
|
||||
if data is not None and 'id' in data:
|
||||
user = Users.get_user_by_id(data['id'])
|
||||
user = await Users.get_user_by_id(data['id'])
|
||||
|
||||
if user:
|
||||
SESSION_POOL[sid] = {
|
||||
@@ -361,7 +361,7 @@ async def user_join(sid, data):
|
||||
if data is None or 'id' not in data:
|
||||
return
|
||||
|
||||
user = Users.get_user_by_id(data['id'])
|
||||
user = await Users.get_user_by_id(data['id'])
|
||||
if not user:
|
||||
return
|
||||
|
||||
@@ -381,8 +381,8 @@ async def user_join(sid, data):
|
||||
await sio.enter_room(sid, f'user:{user.id}')
|
||||
|
||||
# Join all the channels only if user has channels permission
|
||||
if user.role == 'admin' or has_permission(user.id, 'features.channels'):
|
||||
channels = Channels.get_channels_by_user_id(user.id)
|
||||
if user.role == 'admin' or await has_permission(user.id, 'features.channels'):
|
||||
channels = await Channels.get_channels_by_user_id(user.id)
|
||||
log.debug(f'{channels=}')
|
||||
for channel in channels:
|
||||
await sio.enter_room(sid, f'channel:{channel.id}')
|
||||
@@ -395,7 +395,7 @@ async def heartbeat(sid, data):
|
||||
user = SESSION_POOL.get(sid)
|
||||
if user:
|
||||
SESSION_POOL[sid] = {**user, 'last_seen_at': int(time.time())}
|
||||
await asyncio.to_thread(Users.update_last_active_by_id, user['id'])
|
||||
await Users.update_last_active_by_id(user['id'])
|
||||
|
||||
|
||||
@sio.on('join-channels')
|
||||
@@ -408,13 +408,13 @@ async def join_channel(sid, data):
|
||||
if data is None or 'id' not in data:
|
||||
return
|
||||
|
||||
user = Users.get_user_by_id(data['id'])
|
||||
user = await Users.get_user_by_id(data['id'])
|
||||
if not user:
|
||||
return
|
||||
|
||||
# Join all the channels only if user has channels permission
|
||||
if user.role == 'admin' or has_permission(user.id, 'features.channels'):
|
||||
channels = Channels.get_channels_by_user_id(user.id)
|
||||
if user.role == 'admin' or await has_permission(user.id, 'features.channels'):
|
||||
channels = await Channels.get_channels_by_user_id(user.id)
|
||||
log.debug(f'{channels=}')
|
||||
for channel in channels:
|
||||
await sio.enter_room(sid, f'channel:{channel.id}')
|
||||
@@ -430,11 +430,11 @@ async def join_note(sid, data):
|
||||
if token_data is None or 'id' not in token_data:
|
||||
return
|
||||
|
||||
user = Users.get_user_by_id(token_data['id'])
|
||||
user = await Users.get_user_by_id(token_data['id'])
|
||||
if not user:
|
||||
return
|
||||
|
||||
note = Notes.get_note_by_id(data['note_id'])
|
||||
note = await Notes.get_note_by_id(data['note_id'])
|
||||
if not note:
|
||||
log.error(f'Note {data["note_id"]} not found for user {user.id}')
|
||||
return
|
||||
@@ -442,7 +442,7 @@ async def join_note(sid, data):
|
||||
if (
|
||||
user.role != 'admin'
|
||||
and user.id != note.user_id
|
||||
and not AccessGrants.has_access(
|
||||
and not await AccessGrants.has_access(
|
||||
user_id=user.id,
|
||||
resource_type='note',
|
||||
resource_id=note.id,
|
||||
@@ -488,7 +488,7 @@ async def channel_events(sid, data):
|
||||
room=room,
|
||||
)
|
||||
elif event_type == 'last_read_at':
|
||||
Channels.update_member_last_read_at(data['channel_id'], user['id'])
|
||||
await Channels.update_member_last_read_at(data['channel_id'], user['id'])
|
||||
|
||||
|
||||
@sio.on('events:chat')
|
||||
@@ -501,7 +501,7 @@ async def chat_events(sid, data):
|
||||
event_type = event_data.get('type')
|
||||
|
||||
if event_type == 'last_read_at':
|
||||
await asyncio.to_thread(Chats.update_chat_last_read_at_by_id, data['chat_id'], user['id'])
|
||||
await Chats.update_chat_last_read_at_by_id(data['chat_id'], user['id'])
|
||||
|
||||
|
||||
def normalize_document_id(document_id: str) -> str:
|
||||
@@ -529,7 +529,7 @@ async def ydoc_document_join(sid, data):
|
||||
|
||||
if document_id.startswith('note:'):
|
||||
note_id = document_id.split(':')[1]
|
||||
note = Notes.get_note_by_id(note_id)
|
||||
note = await Notes.get_note_by_id(note_id)
|
||||
if not note:
|
||||
log.error(f'Note {note_id} not found')
|
||||
return
|
||||
@@ -537,7 +537,7 @@ async def ydoc_document_join(sid, data):
|
||||
if (
|
||||
user.get('role') != 'admin'
|
||||
and user.get('id') != note.user_id
|
||||
and not AccessGrants.has_access(
|
||||
and not await AccessGrants.has_access(
|
||||
user_id=user.get('id'),
|
||||
resource_type='note',
|
||||
resource_id=note.id,
|
||||
@@ -602,7 +602,7 @@ async def document_save_handler(document_id, data, user):
|
||||
|
||||
if document_id.startswith('note:'):
|
||||
note_id = document_id.split(':')[1]
|
||||
note = Notes.get_note_by_id(note_id)
|
||||
note = await Notes.get_note_by_id(note_id)
|
||||
if not note:
|
||||
log.error(f'Note {note_id} not found')
|
||||
return
|
||||
@@ -610,7 +610,7 @@ async def document_save_handler(document_id, data, user):
|
||||
if (
|
||||
user.get('role') != 'admin'
|
||||
and user.get('id') != note.user_id
|
||||
and not AccessGrants.has_access(
|
||||
and not await AccessGrants.has_access(
|
||||
user_id=user.get('id'),
|
||||
resource_type='note',
|
||||
resource_id=note.id,
|
||||
@@ -620,7 +620,7 @@ async def document_save_handler(document_id, data, user):
|
||||
log.error(f'User {user.get("id")} does not have write access to note {note_id}')
|
||||
return
|
||||
|
||||
Notes.update_note_by_id(note_id, NoteUpdateForm(data=data))
|
||||
await Notes.update_note_by_id(note_id, NoteUpdateForm(data=data))
|
||||
|
||||
|
||||
@sio.on('ydoc:document:state')
|
||||
@@ -793,7 +793,7 @@ async def disconnect(sid):
|
||||
# print(f"Unknown session ID {sid} disconnected")
|
||||
|
||||
|
||||
def get_event_emitter(request_info, update_db=True):
|
||||
async def get_event_emitter(request_info, update_db=True):
|
||||
async def __event_emitter__(event_data):
|
||||
user_id = request_info['user_id']
|
||||
chat_id = request_info['chat_id']
|
||||
@@ -813,16 +813,14 @@ def get_event_emitter(request_info, update_db=True):
|
||||
event_type = event_data.get('type')
|
||||
|
||||
if event_type == 'status':
|
||||
await asyncio.to_thread(
|
||||
Chats.add_message_status_to_chat_by_id_and_message_id,
|
||||
await Chats.add_message_status_to_chat_by_id_and_message_id(
|
||||
request_info['chat_id'],
|
||||
request_info['message_id'],
|
||||
event_data.get('data', {}),
|
||||
)
|
||||
|
||||
elif event_type == 'message':
|
||||
message = await asyncio.to_thread(
|
||||
Chats.get_message_by_id_and_message_id,
|
||||
message = await Chats.get_message_by_id_and_message_id(
|
||||
request_info['chat_id'],
|
||||
request_info['message_id'],
|
||||
)
|
||||
@@ -831,8 +829,7 @@ def get_event_emitter(request_info, update_db=True):
|
||||
content = message.get('content', '')
|
||||
content += event_data.get('data', {}).get('content', '')
|
||||
|
||||
await asyncio.to_thread(
|
||||
Chats.upsert_message_to_chat_by_id_and_message_id,
|
||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||
request_info['chat_id'],
|
||||
request_info['message_id'],
|
||||
{
|
||||
@@ -843,8 +840,7 @@ def get_event_emitter(request_info, update_db=True):
|
||||
elif event_type == 'replace':
|
||||
content = event_data.get('data', {}).get('content', '')
|
||||
|
||||
await asyncio.to_thread(
|
||||
Chats.upsert_message_to_chat_by_id_and_message_id,
|
||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||
request_info['chat_id'],
|
||||
request_info['message_id'],
|
||||
{
|
||||
@@ -853,8 +849,7 @@ def get_event_emitter(request_info, update_db=True):
|
||||
)
|
||||
|
||||
elif event_type == 'embeds':
|
||||
message = await asyncio.to_thread(
|
||||
Chats.get_message_by_id_and_message_id,
|
||||
message = await Chats.get_message_by_id_and_message_id(
|
||||
request_info['chat_id'],
|
||||
request_info['message_id'],
|
||||
)
|
||||
@@ -862,8 +857,7 @@ def get_event_emitter(request_info, update_db=True):
|
||||
embeds = event_data.get('data', {}).get('embeds', [])
|
||||
embeds.extend(message.get('embeds', []))
|
||||
|
||||
await asyncio.to_thread(
|
||||
Chats.upsert_message_to_chat_by_id_and_message_id,
|
||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||
request_info['chat_id'],
|
||||
request_info['message_id'],
|
||||
{
|
||||
@@ -872,8 +866,7 @@ def get_event_emitter(request_info, update_db=True):
|
||||
)
|
||||
|
||||
elif event_type == 'files':
|
||||
message = await asyncio.to_thread(
|
||||
Chats.get_message_by_id_and_message_id,
|
||||
message = await Chats.get_message_by_id_and_message_id(
|
||||
request_info['chat_id'],
|
||||
request_info['message_id'],
|
||||
)
|
||||
@@ -881,8 +874,7 @@ def get_event_emitter(request_info, update_db=True):
|
||||
files = event_data.get('data', {}).get('files', [])
|
||||
files.extend(message.get('files', []))
|
||||
|
||||
await asyncio.to_thread(
|
||||
Chats.upsert_message_to_chat_by_id_and_message_id,
|
||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||
request_info['chat_id'],
|
||||
request_info['message_id'],
|
||||
{
|
||||
@@ -893,8 +885,7 @@ def get_event_emitter(request_info, update_db=True):
|
||||
elif event_type in ('source', 'citation'):
|
||||
data = event_data.get('data', {})
|
||||
if data.get('type') is None:
|
||||
message = await asyncio.to_thread(
|
||||
Chats.get_message_by_id_and_message_id,
|
||||
message = await Chats.get_message_by_id_and_message_id(
|
||||
request_info['chat_id'],
|
||||
request_info['message_id'],
|
||||
)
|
||||
@@ -902,8 +893,7 @@ def get_event_emitter(request_info, update_db=True):
|
||||
sources = message.get('sources', [])
|
||||
sources.append(data)
|
||||
|
||||
await asyncio.to_thread(
|
||||
Chats.upsert_message_to_chat_by_id_and_message_id,
|
||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||
request_info['chat_id'],
|
||||
request_info['message_id'],
|
||||
{
|
||||
@@ -917,7 +907,7 @@ def get_event_emitter(request_info, update_db=True):
|
||||
return None
|
||||
|
||||
|
||||
def get_event_call(request_info):
|
||||
async def get_event_call(request_info):
|
||||
async def __event_caller__(event_data):
|
||||
response = await sio.call(
|
||||
'events',
|
||||
|
||||
Reference in New Issue
Block a user