mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 01:05:42 +00:00
70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
from flask_login import current_user
|
|
from flask_socketio import join_room, leave_room
|
|
from app import hashids, socketio
|
|
from app.decorators import socketio_login_required
|
|
from app.models import User
|
|
|
|
|
|
@socketio.on('GET /users/<user_id>')
|
|
@socketio_login_required
|
|
def get_user(user_hashid, backrefs=False, relationships=False):
|
|
user_id = hashids.decode(user_hashid)
|
|
user = User.query.get(user_id)
|
|
if user is None:
|
|
return {'status': 404, 'statusText': 'Not found'}
|
|
if not (user == current_user or current_user.is_administrator):
|
|
return {'status': 403, 'statusText': 'Forbidden'}
|
|
return {
|
|
'body': user.to_json_serializeable(
|
|
backrefs=backrefs,
|
|
relationships=relationships
|
|
),
|
|
'status': 200,
|
|
'statusText': 'OK',
|
|
}
|
|
|
|
|
|
# @socketio.on('GET /users/<user_id>')
|
|
# @socketio_login_required
|
|
# def get_user(user_hashid):
|
|
# user_id = hashids.decode(user_hashid)
|
|
# user = User.query.get(user_id)
|
|
# if user is None:
|
|
# return {'options': {'status': 404, 'statusText': 'Not found'}}
|
|
# if not (user == current_user or current_user.is_administrator):
|
|
# return {'options': {'status': 403, 'statusText': 'Forbidden'}}
|
|
# return {
|
|
# 'body': user.to_json_serializable2(),
|
|
# 'options': {
|
|
# 'status': 200,
|
|
# 'statusText': 'OK',
|
|
# 'headers': {'Content-Type: application/json'}
|
|
# }
|
|
# }
|
|
|
|
|
|
@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 {'status': 404, 'statusText': 'Not found'}
|
|
if not (user == current_user or current_user.is_administrator):
|
|
return {'status': 403, 'statusText': 'Forbidden'}
|
|
join_room(f'/users/{user.hashid}')
|
|
return {'status': 200, 'statusText': 'OK'}
|
|
|
|
|
|
@socketio.on('UNSUBSCRIBE /users/<user_id>')
|
|
@socketio_login_required
|
|
def unsubscribe_user(user_hashid):
|
|
user_id = hashids.decode(user_hashid)
|
|
user = User.query.get(user_id)
|
|
if user is None:
|
|
return {'status': 404, 'statusText': 'Not found'}
|
|
if not (user == current_user or current_user.is_administrator):
|
|
return {'status': 403, 'statusText': 'Forbidden'}
|
|
leave_room(f'/users/{user.hashid}')
|
|
return {'status': 200, 'statusText': 'OK'}
|