nopaque/app/events/socketio.py

28 lines
1016 B
Python
Raw Normal View History

from app import hashids, socketio
from app.decorators import socketio_login_required
from app.models import User
from flask_login import current_user
2021-11-16 15:23:57 +01:00
from flask_socketio import join_room
2019-09-02 10:59:13 +02:00
###############################################################################
# Socket.IO event handlers #
###############################################################################
2021-11-30 16:22:16 +01:00
@socketio.on('users.user.get')
@socketio_login_required
2021-11-30 16:22:16 +01:00
def users_user_get(user_hashid):
2021-12-08 11:25:33 +01:00
user_id = hashids.decode(user_hashid)
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:
2021-11-30 16:22:16 +01:00
response = {
'code': 200,
'msg': 'OK',
'payload': user.to_dict(backrefs=True, relationships=True)
}
join_room(f'users.{user.hashid}')
return response