mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-14 16:55:42 +00:00
Some more events (unregistered for now)
This commit is contained in:
parent
fab259522e
commit
5cc07f2e13
45
app/corpora/events.py
Normal file
45
app/corpora/events.py
Normal file
@ -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/<corpus_id>')
|
||||
@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/<corpus_id>')
|
||||
@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'}}
|
@ -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'}
|
||||
# }
|
||||
# }
|
||||
|
114
app/users/nevents.py
Normal file
114
app/users/nevents.py
Normal file
@ -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/<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_serializable(),
|
||||
'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 {'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/<user_id>')
|
||||
@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/<user_id>')
|
||||
@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'}}
|
Loading…
Reference in New Issue
Block a user