mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 01:05:42 +00:00
76 lines
2.6 KiB
Python
76 lines
2.6 KiB
Python
from flask import request
|
|
from flask_login import current_user
|
|
from flask_socketio import join_room, leave_room
|
|
from . import socketio
|
|
from .decorators import socketio_login_required
|
|
from .models import User
|
|
|
|
|
|
'''
|
|
' A list containing session ids of connected Socket.IO sessions, to keep track
|
|
' of all connected sessions, which can be used to determine the runtimes of
|
|
' associated background tasks.
|
|
'''
|
|
socketio_sessions = []
|
|
|
|
|
|
###############################################################################
|
|
# Socket.IO event handlers #
|
|
###############################################################################
|
|
@socketio.on('connect')
|
|
@socketio_login_required
|
|
def socketio_connect():
|
|
'''
|
|
' The Socket.IO module creates a session id (sid) for each request.
|
|
' On connect the sid is saved in the connected sessions list.
|
|
'''
|
|
socketio_sessions.append(request.sid)
|
|
|
|
|
|
@socketio.on('disconnect')
|
|
def socketio_disconnect():
|
|
'''
|
|
' On disconnect the session id gets removed from the connected sessions
|
|
' list.
|
|
'''
|
|
try:
|
|
socketio_sessions.remove(request.sid)
|
|
except ValueError:
|
|
pass
|
|
|
|
|
|
@socketio.on('start_user_session')
|
|
@socketio_login_required
|
|
def socketio_start_user_session(user_id):
|
|
user = User.query.get(user_id)
|
|
if user is None:
|
|
response = {'code': 404, 'msg': 'Not found'}
|
|
socketio.emit('start_user_session', response, room=request.sid)
|
|
elif not (user == current_user or current_user.is_administrator):
|
|
response = {'code': 403, 'msg': 'Forbidden'}
|
|
socketio.emit('start_user_session', response, room=request.sid)
|
|
else:
|
|
response = {'code': 200, 'msg': 'OK'}
|
|
socketio.emit('start_user_session', response, room=request.sid)
|
|
socketio.emit('user_{}_init'.format(user.id), user.to_dict(),
|
|
room=request.sid)
|
|
room = 'user_{}'.format(user.id)
|
|
join_room(room)
|
|
|
|
|
|
@socketio.on('stop_user_session')
|
|
@socketio_login_required
|
|
def socketio_stop_user_session(user_id):
|
|
user = User.query.get(user_id)
|
|
if user is None:
|
|
response = {'code': 404, 'msg': 'Not found'}
|
|
socketio.emit('stop_user_session', response, room=request.sid)
|
|
elif not (user == current_user or current_user.is_administrator):
|
|
response = {'code': 403, 'msg': 'Forbidden'}
|
|
socketio.emit('stop_user_session', response, room=request.sid)
|
|
else:
|
|
response = {'code': 200, 'msg': 'OK'}
|
|
socketio.emit('stop_user_session', response, room=request.sid)
|
|
room = 'user_{}'.format(user.id)
|
|
leave_room(room)
|