unify get_user event via socketio

This commit is contained in:
Patrick Jentsch 2023-07-24 10:02:35 +02:00
parent 87e2c2b484
commit 656eef17db
3 changed files with 11 additions and 14 deletions

View File

@ -75,9 +75,9 @@ def create_app(config: Config = Config) -> Flask:
from .corpora import bp as corpora_blueprint from .corpora import bp as corpora_blueprint
from .corpora.cqi_over_sio import CQiNamespace from .corpora.cqi_over_sio import CQiNamespace
socketio.on_namespace(CQiNamespace('/cqi_over_sio'))
default_breadcrumb_root(corpora_blueprint, '.corpora') default_breadcrumb_root(corpora_blueprint, '.corpora')
app.register_blueprint(corpora_blueprint, cli_group='corpus', url_prefix='/corpora') app.register_blueprint(corpora_blueprint, cli_group='corpus', url_prefix='/corpora')
socketio.on_namespace(CQiNamespace('/cqi_over_sio'))
from .errors import bp as errors_bp from .errors import bp as errors_bp
app.register_blueprint(errors_bp) app.register_blueprint(errors_bp)

View File

@ -8,19 +8,19 @@ class App {
this.socket.on('PATCH', (patch) => {this.onPatch(patch);}); this.socket.on('PATCH', (patch) => {this.onPatch(patch);});
} }
getUser(userId, backrefs=true, relationships=true) { getUser(userId) {
if (userId in this.data.promises.getUser) { if (userId in this.data.promises.getUser) {
return this.data.promises.getUser[userId]; return this.data.promises.getUser[userId];
} }
this.data.promises.getUser[userId] = new Promise((resolve, reject) => { this.data.promises.getUser[userId] = new Promise((resolve, reject) => {
this.socket.emit('GET /users/<user_id>', userId, backrefs, relationships, (response) => { this.socket.emit('GET /users/<user_id>', userId, (response) => {
if (response.status !== 200) { if (response.status === 200) {
reject(response);
return;
}
this.data.users[userId] = response.body; this.data.users[userId] = response.body;
resolve(this.data.users[userId]); resolve(this.data.users[userId]);
} else {
reject(`[${response.status}] ${response.statusText}`);
}
}); });
}); });

View File

@ -7,7 +7,7 @@ from app.models import User
@socketio.on('GET /users/<user_id>') @socketio.on('GET /users/<user_id>')
@socketio_login_required @socketio_login_required
def get_user(user_hashid, backrefs=False, relationships=False): def get_user(user_hashid):
user_id = hashids.decode(user_hashid) user_id = hashids.decode(user_hashid)
user = User.query.get(user_id) user = User.query.get(user_id)
if user is None: if user is None:
@ -15,12 +15,9 @@ def get_user(user_hashid, backrefs=False, relationships=False):
if not (user == current_user or current_user.is_administrator()): if not (user == current_user or current_user.is_administrator()):
return {'status': 403, 'statusText': 'Forbidden'} return {'status': 403, 'statusText': 'Forbidden'}
return { return {
'body': user.to_json_serializeable( 'body': user.to_json_serializeable(backrefs=True, relationships=True),
backrefs=backrefs,
relationships=relationships
),
'status': 200, 'status': 200,
'statusText': 'OK', 'statusText': 'OK'
} }