diff --git a/app/corpora/events.py b/app/corpora/events.py new file mode 100644 index 00000000..6c32e2ba --- /dev/null +++ b/app/corpora/events.py @@ -0,0 +1,45 @@ +from flask_login import current_user +from flask_socketio import join_room +from app import hashids, socketio +from app.decorators import socketio_login_required +from app.models import Corpus + + +@socketio.on('GET /corpora/') +@socketio_login_required +def get_corpus(corpus_hashid): + corpus_id = hashids.decode(corpus_hashid) + corpus = Corpus.query.get(corpus_id) + if corpus is None: + return {'options': {'status': 404, 'statusText': 'Not found'}} + if not ( + corpus.is_public + or corpus.user == current_user + or current_user.is_administrator() + ): + return {'options': {'status': 403, 'statusText': 'Forbidden'}} + return { + 'body': corpus.to_json_serializable(), + 'options': { + 'status': 200, + 'statusText': 'OK', + 'headers': {'Content-Type: application/json'} + } + } + + +@socketio.on('SUBSCRIBE /corpora/') +@socketio_login_required +def subscribe_corpus(corpus_hashid): + corpus_id = hashids.decode(corpus_hashid) + corpus = Corpus.query.get(corpus_id) + if corpus is None: + return {'options': {'status': 404, 'statusText': 'Not found'}} + if not ( + corpus.is_public + or corpus.user == current_user + or current_user.is_administrator() + ): + return {'options': {'status': 403, 'statusText': 'Forbidden'}} + join_room(f'/corpora/{corpus.hashid}') + return {'options': {'status': 200, 'statusText': 'OK'}} diff --git a/app/users/events.py b/app/users/events.py index fd6dc6db..af490a01 100644 --- a/app/users/events.py +++ b/app/users/events.py @@ -48,32 +48,3 @@ def unsubscribe_user(user_hashid): return {'status': 403, 'statusText': 'Forbidden'} leave_room(f'/users/{user.hashid}') return {'status': 200, 'statusText': 'OK'} - - -# @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'} -# } -# } diff --git a/app/users/nevents.py b/app/users/nevents.py new file mode 100644 index 00000000..8205df03 --- /dev/null +++ b/app/users/nevents.py @@ -0,0 +1,114 @@ +from flask_login import current_user +from flask_socketio import join_room, leave_room +from app import hashids, socketio +from app.decorators import socketio_admin_required, socketio_login_required +from app.models import User + + +@socketio.on('GET /users') +@socketio_admin_required +def get_users(): + users = User.query.filter_by().all() + return { + 'body': [user.to_json_serializable() for user in users], + 'options': { + 'status': 200, + 'statusText': 'OK', + 'headers': {'Content-Type: application/json'} + } + } + + +@socketio.on('SUBSCRIBE /users') +@socketio_admin_required +def subscribe_users(): + join_room('/users') + return {'options': {'status': 200, 'statusText': 'OK'}} + + +@socketio.on('GET /users/') +@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_serializable(), + 'options': { + 'status': 200, + 'statusText': 'OK', + 'headers': {'Content-Type: application/json'} + } + } + + +@socketio.on('SUBSCRIBE /users/') +@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 {'options': {'status': 404, 'statusText': 'Not found'}} + if not (user == current_user or current_user.is_administrator()): + return {'options': {'status': 403, 'statusText': 'Forbidden'}} + join_room(f'/users/{user.hashid}') + return {'options': {'status': 200, 'statusText': 'OK'}} + + +@socketio.on('GET /public_users') +@socketio_login_required +def get_public_users(): + users = User.query.filter_by(is_public=True).all() + return { + 'body': [ + user.to_json_serializable(filter_by_privacy_settings=True) + for user in users + ], + 'options': { + 'status': 200, + 'statusText': 'OK', + 'headers': {'Content-Type: application/json'} + } + } + + +@socketio.on('SUBSCRIBE /users') +@socketio_admin_required +def subscribe_users(): + join_room('/public_users') + return {'options': {'status': 200, 'statusText': 'OK'}} + + +@socketio.on('GET /public_users/') +@socketio_login_required +def get_user(user_hashid): + user_id = hashids.decode(user_hashid) + user = User.query.filter_by(id=user_id, is_public=True).first() + 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_serializable(filter_by_privacy_settings=True), + 'options': { + 'status': 200, + 'statusText': 'OK', + 'headers': {'Content-Type: application/json'} + } + } + + +@socketio.on('SUBSCRIBE /public_users/') +@socketio_login_required +def subscribe_user(user_hashid): + user_id = hashids.decode(user_hashid) + user = User.query.filter_by(id=user_id, is_public=True).first() + 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'}} + join_room(f'/public_users/{user.hashid}') + return {'options': {'status': 200, 'statusText': 'OK'}}