nopaque/app/events/socketio.py
2021-11-30 16:22:16 +01:00

29 lines
1.0 KiB
Python

from app import hashids
from flask_login import current_user
from flask_socketio import join_room
from .. import socketio
from ..decorators import socketio_login_required
from ..models import User
###############################################################################
# Socket.IO event handlers #
###############################################################################
@socketio.on('users.user.get')
@socketio_login_required
def users_user_get(user_hashid):
user_id = hashids.decode(user_hashid)[0]
user = User.query.get(user_id)
if user is None:
response = {'code': 404, 'msg': 'Not found'}
elif not (user == current_user or current_user.is_administrator):
response = {'code': 403, 'msg': 'Forbidden'}
else:
response = {
'code': 200,
'msg': 'OK',
'payload': user.to_dict(backrefs=True, relationships=True)
}
join_room(f'users.{user.hashid}')
return response