overleaf/services/web/app/coffee/Features/Editor/EditorUpdatesController.coffee

62 lines
2.6 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
logger = require "logger-sharelatex"
metrics = require('../../infrastructure/Metrics')
Settings = require 'settings-sharelatex'
2014-09-26 12:04:33 -04:00
redis = require("redis-sharelatex")
rclient = redis.createClient(Settings.redis.web)
2014-02-12 05:23:40 -05:00
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
EditorRealTimeController = require("./EditorRealTimeController")
module.exports = EditorUpdatesController =
_applyUpdate: (client, project_id, doc_id, update, callback = (error) ->) ->
metrics.inc "editor.doc-update", 0.3
metrics.set "editor.active-projects", project_id, 0.3
client.get "user_id", (error, user_id) ->
metrics.set "editor.active-users", user_id, 0.3
2014-03-14 07:56:15 -04:00
logger.log doc_id: doc_id, project_id: project_id, client_id: update.meta?.source, version: update.v, "sending update to doc updater"
2014-02-12 05:23:40 -05:00
DocumentUpdaterHandler.queueChange project_id, doc_id, update, (error) ->
if error?
2014-03-14 07:56:15 -04:00
logger.error err:error, project_id: project_id, doc_id: doc_id, client_id: update.meta?.source, version: update.v, "document was not available for update"
2014-02-12 05:23:40 -05:00
client.disconnect()
callback(error)
applyOtUpdate: (client, project_id, doc_id, update) ->
update.meta ||= {}
update.meta.source = client.id
client.get "user_id", (error, user_id) ->
update.meta.user_id = user_id
EditorUpdatesController._applyUpdate client, project_id, doc_id, update
listenForUpdatesFromDocumentUpdater: () ->
rclient.subscribe "applied-ops"
rclient.on "message", @_processMessageFromDocumentUpdater.bind(@)
_processMessageFromDocumentUpdater: (channel, message) ->
message = JSON.parse message
if message.op?
@_applyUpdateFromDocumentUpdater(message.doc_id, message.op)
else if message.error?
@_processErrorFromDocumentUpdater(message.doc_id, message.error, message)
_applyUpdateFromDocumentUpdater: (doc_id, update) ->
io = require('../../infrastructure/Server').io
for client in io.sockets.clients(doc_id)
if client.id == update.meta.source
2014-03-17 08:50:30 -04:00
logger.log doc_id: doc_id, version: update.v, source: update.meta?.source, "distributing update to sender"
2014-02-12 05:23:40 -05:00
client.emit "otUpdateApplied", v: update.v, doc: update.doc
else
2014-03-17 08:50:30 -04:00
logger.log doc_id: doc_id, version: update.v, source: update.meta?.source, client_id: client.id, "distributing update to collaborator"
2014-02-12 05:23:40 -05:00
client.emit "otUpdateApplied", update
_processErrorFromDocumentUpdater: (doc_id, error, message) ->
io = require('../../infrastructure/Server').io
logger.error err: error, doc_id: doc_id, "error from document updater"
for client in io.sockets.clients(doc_id)
client.emit "otUpdateError", error, message
client.disconnect()