2021-02-01 11:51:07 +00:00
|
|
|
from flask import request
|
2020-03-26 15:14:09 +00:00
|
|
|
from flask_login import current_user
|
2021-02-01 11:51:07 +00:00
|
|
|
from flask_socketio import join_room, leave_room
|
|
|
|
from . import socketio
|
|
|
|
from .decorators import socketio_login_required
|
2019-11-06 12:22:18 +00:00
|
|
|
from .models import User
|
2019-09-02 08:59:13 +00:00
|
|
|
|
2019-08-28 15:27:43 +00:00
|
|
|
|
2019-08-29 08:42:53 +00:00
|
|
|
'''
|
2019-11-07 09:44:02 +00:00
|
|
|
' A list containing session ids of connected Socket.IO sessions, to keep track
|
2021-02-01 11:51:07 +00:00
|
|
|
' of all connected sessions, which can be used to determine the runtimes of
|
2019-11-07 09:44:02 +00:00
|
|
|
' associated background tasks.
|
2019-08-29 08:42:53 +00:00
|
|
|
'''
|
2021-02-01 11:51:07 +00:00
|
|
|
socketio_sessions = []
|
2019-08-27 08:40:29 +00:00
|
|
|
|
|
|
|
|
2021-08-18 13:11:11 +00:00
|
|
|
###############################################################################
|
|
|
|
# Socket.IO event handlers #
|
|
|
|
###############################################################################
|
2019-08-27 08:40:29 +00:00
|
|
|
@socketio.on('connect')
|
2021-02-01 11:51:07 +00:00
|
|
|
@socketio_login_required
|
|
|
|
def socketio_connect():
|
2019-09-02 07:17:00 +00:00
|
|
|
'''
|
2019-11-07 09:44:02 +00:00
|
|
|
' The Socket.IO module creates a session id (sid) for each request.
|
|
|
|
' On connect the sid is saved in the connected sessions list.
|
2019-09-02 07:17:00 +00:00
|
|
|
'''
|
2021-02-01 11:51:07 +00:00
|
|
|
socketio_sessions.append(request.sid)
|
2019-08-28 15:27:43 +00:00
|
|
|
|
|
|
|
|
2019-11-06 13:35:33 +00:00
|
|
|
@socketio.on('disconnect')
|
2021-02-01 11:51:07 +00:00
|
|
|
def socketio_disconnect():
|
2019-11-06 13:35:33 +00:00
|
|
|
'''
|
2019-11-07 09:44:02 +00:00
|
|
|
' On disconnect the session id gets removed from the connected sessions
|
|
|
|
' list.
|
2019-11-06 13:35:33 +00:00
|
|
|
'''
|
2021-02-01 11:51:07 +00:00
|
|
|
socketio_sessions.remove(request.sid)
|
2019-11-06 13:35:33 +00:00
|
|
|
|
|
|
|
|
2020-12-15 13:38:52 +00:00
|
|
|
@socketio.on('start_user_session')
|
2020-03-26 15:14:09 +00:00
|
|
|
@socketio_login_required
|
2021-02-01 11:51:07 +00:00
|
|
|
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)
|
2019-11-07 09:44:02 +00:00
|
|
|
|
|
|
|
|
2021-02-01 11:51:07 +00:00
|
|
|
@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)
|