2014-11-14 10:30:18 -05:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
settings = require 'settings-sharelatex'
|
|
|
|
redis = require("redis-sharelatex")
|
2017-05-02 10:51:17 -04:00
|
|
|
rclient = redis.createClient(settings.redis.documentupdater)
|
2015-12-01 06:05:49 -05:00
|
|
|
SafeJsonParse = require "./SafeJsonParse"
|
2014-11-14 10:30:18 -05:00
|
|
|
|
2015-11-30 10:40:03 -05:00
|
|
|
MESSAGE_SIZE_LOG_LIMIT = 1024 * 1024 # 1Mb
|
2015-11-30 10:25:09 -05:00
|
|
|
|
2014-11-14 10:30:18 -05:00
|
|
|
module.exports = DocumentUpdaterController =
|
|
|
|
# DocumentUpdaterController is responsible for updates that come via Redis
|
|
|
|
# Pub/Sub from the document updater.
|
|
|
|
|
|
|
|
listenForUpdatesFromDocumentUpdater: (io) ->
|
|
|
|
rclient.subscribe "applied-ops"
|
|
|
|
rclient.on "message", (channel, message) ->
|
|
|
|
DocumentUpdaterController._processMessageFromDocumentUpdater(io, channel, message)
|
|
|
|
|
|
|
|
_processMessageFromDocumentUpdater: (io, channel, message) ->
|
2015-12-01 06:05:49 -05:00
|
|
|
SafeJsonParse.parse message, (error, message) ->
|
|
|
|
if error?
|
|
|
|
logger.error {err: error, channel}, "error parsing JSON"
|
|
|
|
return
|
|
|
|
if message.op?
|
|
|
|
DocumentUpdaterController._applyUpdateFromDocumentUpdater(io, message.doc_id, message.op)
|
|
|
|
else if message.error?
|
|
|
|
DocumentUpdaterController._processErrorFromDocumentUpdater(io, message.doc_id, message.error, message)
|
2014-11-14 10:30:18 -05:00
|
|
|
|
|
|
|
_applyUpdateFromDocumentUpdater: (io, doc_id, update) ->
|
|
|
|
for client in io.sockets.clients(doc_id)
|
|
|
|
if client.id == update.meta.source
|
|
|
|
logger.log doc_id: doc_id, version: update.v, source: update.meta?.source, "distributing update to sender"
|
|
|
|
client.emit "otUpdateApplied", v: update.v, doc: update.doc
|
2015-11-19 05:58:28 -05:00
|
|
|
else if !update.dup # Duplicate ops should just be sent back to sending client for acknowledgement
|
2014-11-14 10:30:18 -05:00
|
|
|
logger.log doc_id: doc_id, version: update.v, source: update.meta?.source, client_id: client.id, "distributing update to collaborator"
|
|
|
|
client.emit "otUpdateApplied", update
|
|
|
|
|
|
|
|
_processErrorFromDocumentUpdater: (io, doc_id, error, message) ->
|
|
|
|
for client in io.sockets.clients(doc_id)
|
2016-11-21 10:33:13 -05:00
|
|
|
logger.error err: error, doc_id: doc_id, client_id: client.id, "error from document updater, disconnecting client"
|
2014-11-14 10:30:18 -05:00
|
|
|
client.emit "otUpdateError", error, message
|
|
|
|
client.disconnect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|