2021-02-01 12:51:07 +01:00
|
|
|
from flask import request
|
2020-03-26 16:14:09 +01:00
|
|
|
from flask_login import current_user
|
2021-11-16 15:23:57 +01:00
|
|
|
from flask_socketio import join_room
|
2021-09-07 13:50:23 +02:00
|
|
|
from .. import socketio
|
|
|
|
from ..decorators import socketio_login_required
|
|
|
|
from ..models import User
|
2019-09-02 10:59:13 +02:00
|
|
|
|
2019-08-28 17:27:43 +02:00
|
|
|
|
2019-08-29 10:42:53 +02:00
|
|
|
'''
|
2021-09-22 13:52:51 +02:00
|
|
|
' A list containing session ids of Socket.IO sessions, to keep track
|
2021-02-01 12:51:07 +01:00
|
|
|
' of all connected sessions, which can be used to determine the runtimes of
|
2019-11-07 10:44:02 +01:00
|
|
|
' associated background tasks.
|
2019-08-29 10:42:53 +02:00
|
|
|
'''
|
2021-09-07 13:50:23 +02:00
|
|
|
sessions = []
|
2019-08-27 10:40:29 +02:00
|
|
|
|
|
|
|
|
2021-08-18 15:11:11 +02:00
|
|
|
###############################################################################
|
|
|
|
# Socket.IO event handlers #
|
|
|
|
###############################################################################
|
2019-08-27 10:40:29 +02:00
|
|
|
@socketio.on('connect')
|
2021-02-01 12:51:07 +01:00
|
|
|
@socketio_login_required
|
|
|
|
def socketio_connect():
|
2019-09-02 09:17:00 +02:00
|
|
|
'''
|
2019-11-07 10:44:02 +01:00
|
|
|
' The Socket.IO module creates a session id (sid) for each request.
|
2021-09-22 13:52:51 +02:00
|
|
|
' On connect the sid is saved in the sessions list.
|
2019-09-02 09:17:00 +02:00
|
|
|
'''
|
2021-09-07 13:50:23 +02:00
|
|
|
sessions.append(request.sid)
|
2021-11-16 15:23:57 +01:00
|
|
|
# return {'code': 200, 'msg': 'OK'}
|
2019-08-28 17:27:43 +02:00
|
|
|
|
|
|
|
|
2019-11-06 14:35:33 +01:00
|
|
|
@socketio.on('disconnect')
|
2021-02-01 12:51:07 +01:00
|
|
|
def socketio_disconnect():
|
2019-11-06 14:35:33 +01:00
|
|
|
'''
|
2021-09-22 13:52:51 +02:00
|
|
|
' On disconnect the session id gets removed from the sessions list.
|
2019-11-06 14:35:33 +01:00
|
|
|
'''
|
2021-08-23 16:43:47 +02:00
|
|
|
try:
|
2021-09-07 13:50:23 +02:00
|
|
|
sessions.remove(request.sid)
|
2021-08-23 16:43:47 +02:00
|
|
|
except ValueError:
|
|
|
|
pass
|
2021-11-16 15:23:57 +01:00
|
|
|
# return {'code': 200, 'msg': 'OK'}
|
2019-11-06 14:35:33 +01:00
|
|
|
|
|
|
|
|
2020-12-15 14:38:52 +01:00
|
|
|
@socketio.on('start_user_session')
|
2020-03-26 16:14:09 +01:00
|
|
|
@socketio_login_required
|
2021-02-01 12:51:07 +01: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 10:44:02 +01:00
|
|
|
|
|
|
|
|
2021-09-22 13:52:51 +02:00
|
|
|
@socketio.on('users.request')
|
2021-02-01 12:51:07 +01:00
|
|
|
@socketio_login_required
|
2021-09-22 13:52:51 +02:00
|
|
|
def socketio_start_session(user_id):
|
2021-02-01 12:51:07 +01:00
|
|
|
user = User.query.get(user_id)
|
|
|
|
if user is None:
|
|
|
|
response = {'code': 404, 'msg': 'Not found'}
|
|
|
|
elif not (user == current_user or current_user.is_administrator):
|
|
|
|
response = {'code': 403, 'msg': 'Forbidden'}
|
|
|
|
else:
|
2021-09-22 13:52:51 +02:00
|
|
|
response = {'code': 200, 'msg': 'OK', 'payload': user.to_dict()}
|
|
|
|
join_room('users.{}'.format(user.id))
|
|
|
|
return response
|