2019-09-02 08:42:28 +00:00
|
|
|
from flask import current_app, request
|
2020-03-26 15:14:09 +00:00
|
|
|
from flask_login import current_user
|
2020-04-30 09:28:55 +00:00
|
|
|
from . import db, socketio
|
2020-03-26 15:14:09 +00:00
|
|
|
from .decorators import socketio_admin_required, socketio_login_required
|
2019-11-06 12:22:18 +00:00
|
|
|
from .models import User
|
2019-08-27 08:40:29 +00:00
|
|
|
import json
|
2019-08-29 08:42:53 +00:00
|
|
|
import jsonpatch
|
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
|
|
|
|
' of all connected sessions, which is used to determine the runtimes of
|
|
|
|
' associated background tasks.
|
2019-08-29 08:42:53 +00:00
|
|
|
'''
|
2019-11-06 13:35:33 +00:00
|
|
|
connected_sessions = []
|
2019-08-27 08:40:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@socketio.on('connect')
|
|
|
|
def 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
|
|
|
'''
|
2019-11-06 13:35:33 +00:00
|
|
|
connected_sessions.append(request.sid)
|
2019-08-28 15:27:43 +00:00
|
|
|
|
|
|
|
|
2019-11-06 13:35:33 +00:00
|
|
|
@socketio.on('disconnect')
|
|
|
|
def disconnect():
|
|
|
|
'''
|
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
|
|
|
'''
|
|
|
|
connected_sessions.remove(request.sid)
|
|
|
|
|
|
|
|
|
2020-12-15 13:38:52 +00:00
|
|
|
@socketio.on('start_user_session')
|
2020-03-26 15:14:09 +00:00
|
|
|
@socketio_login_required
|
2020-12-15 13:38:52 +00:00
|
|
|
def start_user_session(user_id):
|
|
|
|
if not (current_user.id == user_id or current_user.is_administrator):
|
|
|
|
return
|
|
|
|
socketio.start_background_task(user_session,
|
2019-11-07 09:44:02 +00:00
|
|
|
current_app._get_current_object(),
|
2020-12-15 13:38:52 +00:00
|
|
|
user_id, request.sid)
|
2019-11-07 09:44:02 +00:00
|
|
|
|
|
|
|
|
2020-12-15 13:38:52 +00:00
|
|
|
def user_session(app, user_id, session_id):
|
2019-09-18 09:33:06 +00:00
|
|
|
'''
|
2020-12-15 13:38:52 +00:00
|
|
|
' Sends initial user data to the client. Afterwards it checks every 3s if
|
|
|
|
' changes to the initial values appeared. If changes are detected, a
|
|
|
|
' RFC 6902 compliant JSON patch gets send.
|
2019-09-18 09:33:06 +00:00
|
|
|
'''
|
2020-12-15 13:38:52 +00:00
|
|
|
init_event = 'user_{}_init'.format(user_id)
|
|
|
|
patch_event = 'user_{}_patch'.format(user_id)
|
2019-09-18 09:33:06 +00:00
|
|
|
with app.app_context():
|
2019-11-07 09:44:02 +00:00
|
|
|
# Gather current values from database.
|
2019-11-15 12:09:12 +00:00
|
|
|
user = User.query.get(user_id)
|
2020-04-30 09:28:55 +00:00
|
|
|
user_dict = user.to_dict()
|
2020-04-30 10:56:59 +00:00
|
|
|
# Send initial values to the client.
|
2020-04-30 09:28:55 +00:00
|
|
|
socketio.emit(init_event, json.dumps(user_dict), room=session_id)
|
2019-11-06 13:35:33 +00:00
|
|
|
while session_id in connected_sessions:
|
2019-11-07 09:44:02 +00:00
|
|
|
# Get new values from the database
|
2020-04-30 09:28:55 +00:00
|
|
|
db.session.refresh(user)
|
|
|
|
new_user_dict = user.to_dict()
|
2019-11-07 09:44:02 +00:00
|
|
|
# Compute JSON patches.
|
2020-04-30 09:28:55 +00:00
|
|
|
user_patch = jsonpatch.JsonPatch.from_diff(user_dict,
|
|
|
|
new_user_dict)
|
2020-04-30 10:56:59 +00:00
|
|
|
# In case there are patches, send them to the client.
|
2020-04-30 09:28:55 +00:00
|
|
|
if user_patch:
|
2020-12-15 13:38:52 +00:00
|
|
|
socketio.emit(patch_event, user_patch.to_string(),
|
2019-09-18 09:33:06 +00:00
|
|
|
room=session_id)
|
2019-11-07 09:44:02 +00:00
|
|
|
# Set new values as references for the next iteration.
|
2020-04-30 09:28:55 +00:00
|
|
|
user_dict = new_user_dict
|
2019-09-18 09:33:06 +00:00
|
|
|
socketio.sleep(3)
|