2022-09-02 11:07:30 +00:00
|
|
|
from flask_login import current_user
|
|
|
|
from flask_socketio import join_room, leave_room
|
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
|
|
|
|
|
|
|
|
|
2022-12-22 12:57:55 +00:00
|
|
|
@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'}
|
2023-05-09 13:32:09 +00:00
|
|
|
if not (user == current_user or current_user.is_administrator()):
|
2022-12-22 12:57:55 +00:00
|
|
|
return {'status': 403, 'statusText': 'Forbidden'}
|
|
|
|
return {
|
|
|
|
'body': user.to_json_serializeable(
|
|
|
|
backrefs=backrefs,
|
|
|
|
relationships=relationships
|
|
|
|
),
|
|
|
|
'status': 200,
|
|
|
|
'statusText': 'OK',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-08 09:46:47 +00:00
|
|
|
@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:
|
2022-12-22 12:57:55 +00:00
|
|
|
return {'status': 404, 'statusText': 'Not found'}
|
2023-05-09 13:32:09 +00:00
|
|
|
if not (user == current_user or current_user.is_administrator()):
|
2022-12-22 12:57:55 +00:00
|
|
|
return {'status': 403, 'statusText': 'Forbidden'}
|
2022-07-08 09:46:47 +00:00
|
|
|
join_room(f'/users/{user.hashid}')
|
2022-12-22 12:57:55 +00:00
|
|
|
return {'status': 200, 'statusText': 'OK'}
|
2022-07-08 09:46:47 +00:00
|
|
|
|
|
|
|
|
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:
|
2022-12-22 12:57:55 +00:00
|
|
|
return {'status': 404, 'statusText': 'Not found'}
|
2023-05-09 13:32:09 +00:00
|
|
|
if not (user == current_user or current_user.is_administrator()):
|
2022-12-22 12:57:55 +00:00
|
|
|
return {'status': 403, 'statusText': 'Forbidden'}
|
2022-07-04 12:09:17 +00:00
|
|
|
leave_room(f'/users/{user.hashid}')
|
2022-12-22 12:57:55 +00:00
|
|
|
return {'status': 200, 'statusText': 'OK'}
|
2023-05-09 13:32:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
# @socketio.on('GET User')
|
|
|
|
# @socketio_login_required
|
|
|
|
# def n_get_user(user_hashid):
|
|
|
|
# # This constructs a JSON response which can easily be converted to a Response object
|
|
|
|
# # Ref: https://developer.mozilla.org/en-US/docs/Web/API/Response/Response
|
|
|
|
# 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'}}
|
|
|
|
# body = {
|
|
|
|
# 'id': user.hashid,
|
|
|
|
# # ...
|
|
|
|
# 'relationships': {
|
|
|
|
# 'corpora': {corpus.hashid for corpus in user.corpora},
|
|
|
|
# 'jobs': [job.hashid for job in user.jobs]
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# return {
|
|
|
|
# 'body': user.to_json_serializable(),
|
|
|
|
# 'options': {
|
|
|
|
# 'status': 200,
|
|
|
|
# 'statusText': 'OK',
|
|
|
|
# 'headers': {'Content-Type: application/json'}
|
|
|
|
# }
|
|
|
|
# }
|