nopaque/app/users/events.py

45 lines
1.5 KiB
Python
Raw Normal View History

2022-07-04 12:09:17 +00:00
from app import hashids, socketio
from app.decorators import socketio_login_required
from app.models import User
from flask_login import current_user
from flask_socketio import join_room, leave_room
@socketio.on('GET /users/<user_id>')
2022-07-04 12:09:17 +00:00
@socketio_login_required
2022-07-08 13:52:15 +00:00
def get_user(user_hashid):
2022-07-04 12:09:17 +00:00
user_id = hashids.decode(user_hashid)
user = User.query.get(user_id)
if user is None:
return {'code': 404, 'msg': 'Not found'}
if not (user == current_user or current_user.is_administrator):
return {'code': 403, 'msg': 'Forbidden'}
dict_user = user.to_dict(backrefs=True, relationships=True)
return {'code': 200, 'msg': 'OK', 'payload': dict_user}
@socketio.on('SUBSCRIBE /users/<user_id>')
@socketio_login_required
def subscribe_user(user_hashid):
user_id = hashids.decode(user_hashid)
user = User.query.get(user_id)
if user is None:
return {'code': 404, 'msg': 'Not found'}
if not (user == current_user or current_user.is_administrator):
return {'code': 403, 'msg': 'Forbidden'}
join_room(f'/users/{user.hashid}')
return {'code': 200, 'msg': 'OK'}
2022-07-04 12:09:17 +00:00
@socketio.on('UNSUBSCRIBE /users/<user_id>')
@socketio_login_required
2022-07-08 13:52:15 +00:00
def unsubscribe_user(user_hashid):
2022-07-04 12:09:17 +00:00
user_id = hashids.decode(user_hashid)
user = User.query.get(user_id)
if user is None:
return {'code': 404, 'msg': 'Not found'}
if not (user == current_user or current_user.is_administrator):
return {'code': 403, 'msg': 'Forbidden'}
leave_room(f'/users/{user.hashid}')
return {'code': 200, 'msg': 'OK'}