2014-02-12 05:23:40 -05:00
|
|
|
request = require 'request'
|
|
|
|
request = request.defaults()
|
|
|
|
settings = require 'settings-sharelatex'
|
|
|
|
_ = require 'underscore'
|
|
|
|
async = require 'async'
|
|
|
|
logger = require('logger-sharelatex')
|
2017-04-03 11:18:30 -04:00
|
|
|
metrics = require('metrics-sharelatex')
|
2014-02-12 05:23:40 -05:00
|
|
|
Project = require("../../models/Project").Project
|
|
|
|
ProjectLocator = require('../../Features/Project/ProjectLocator')
|
|
|
|
|
2014-06-18 11:37:18 -04:00
|
|
|
module.exports = DocumentUpdaterHandler =
|
2014-10-15 09:11:02 -04:00
|
|
|
flushProjectToMongo: (project_id, callback = (error) ->)->
|
|
|
|
logger.log project_id:project_id, "flushing project from document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
timer = new metrics.Timer("flushing.mongo.project")
|
|
|
|
url = "#{settings.apis.documentupdater.url}/project/#{project_id}/flush"
|
|
|
|
request.post url, (error, res, body)->
|
|
|
|
if error?
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.error err: error, project_id: project_id, "error flushing project from document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error)
|
|
|
|
else if res.statusCode >= 200 and res.statusCode < 300
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log project_id: project_id, "flushed project from document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(null)
|
|
|
|
else
|
|
|
|
error = new Error("document updater returned a failure status code: #{res.statusCode}")
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.error err: error, project_id: project_id, "document updater returned failure status code: #{res.statusCode}"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error)
|
|
|
|
|
2014-06-18 11:37:18 -04:00
|
|
|
flushMultipleProjectsToMongo: (project_ids, callback = (error) ->) ->
|
|
|
|
jobs = []
|
|
|
|
for project_id in project_ids
|
|
|
|
do (project_id) ->
|
|
|
|
jobs.push (callback) ->
|
|
|
|
DocumentUpdaterHandler.flushProjectToMongo project_id, callback
|
|
|
|
async.series jobs, callback
|
|
|
|
|
2014-10-15 09:11:02 -04:00
|
|
|
flushProjectToMongoAndDelete: (project_id, callback = ()->) ->
|
|
|
|
logger.log project_id:project_id, "deleting project from document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
timer = new metrics.Timer("delete.mongo.project")
|
|
|
|
url = "#{settings.apis.documentupdater.url}/project/#{project_id}"
|
|
|
|
request.del url, (error, res, body)->
|
|
|
|
if error?
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.error err: error, project_id: project_id, "error deleting project from document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error)
|
|
|
|
else if res.statusCode >= 200 and res.statusCode < 300
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log project_id: project_id, "deleted project from document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(null)
|
|
|
|
else
|
|
|
|
error = new Error("document updater returned a failure status code: #{res.statusCode}")
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.error err: error, project_id: project_id, "document updater returned failure status code: #{res.statusCode}"
|
2014-05-08 10:47:50 -04:00
|
|
|
return callback(error)
|
|
|
|
|
|
|
|
flushDocToMongo: (project_id, doc_id, callback = (error) ->) ->
|
|
|
|
logger.log project_id:project_id, doc_id: doc_id, "flushing doc from document updater"
|
|
|
|
timer = new metrics.Timer("flushing.mongo.doc")
|
|
|
|
url = "#{settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}/flush"
|
|
|
|
request.post url, (error, res, body)->
|
|
|
|
if error?
|
|
|
|
logger.error err: error, project_id: project_id, doc_id: doc_id, "error flushing doc from document updater"
|
|
|
|
return callback(error)
|
|
|
|
else if res.statusCode >= 200 and res.statusCode < 300
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "flushed doc from document updater"
|
|
|
|
return callback(null)
|
|
|
|
else
|
|
|
|
error = new Error("document updater returned a failure status code: #{res.statusCode}")
|
|
|
|
logger.error err: error, project_id: project_id, doc_id: doc_id, "document updater returned failure status code: #{res.statusCode}"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error)
|
|
|
|
|
2014-10-15 09:11:02 -04:00
|
|
|
deleteDoc : (project_id, doc_id, callback = ()->)->
|
|
|
|
logger.log project_id:project_id, doc_id: doc_id, "deleting doc from document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
timer = new metrics.Timer("delete.mongo.doc")
|
|
|
|
url = "#{settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}"
|
|
|
|
request.del url, (error, res, body)->
|
|
|
|
if error?
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.error err: error, project_id: project_id, doc_id: doc_id, "error deleting doc from document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error)
|
|
|
|
else if res.statusCode >= 200 and res.statusCode < 300
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "deleted doc from document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(null)
|
|
|
|
else
|
|
|
|
error = new Error("document updater returned a failure status code: #{res.statusCode}")
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.error err: error, project_id: project_id, doc_id: doc_id, "document updater returned failure status code: #{res.statusCode}"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error)
|
|
|
|
|
2016-12-08 09:09:06 -05:00
|
|
|
getDocument: (project_id, doc_id, fromVersion, callback = (error, doclines, version, ranges, ops) ->) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
timer = new metrics.Timer("get-document")
|
|
|
|
url = "#{settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}?fromVersion=#{fromVersion}"
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log project_id:project_id, doc_id: doc_id, "getting doc from document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
request.get url, (error, res, body)->
|
|
|
|
timer.done()
|
|
|
|
if error?
|
|
|
|
logger.error err:error, url:url, project_id:project_id, doc_id:doc_id, "error getting doc from doc updater"
|
|
|
|
return callback(error)
|
|
|
|
if res.statusCode >= 200 and res.statusCode < 300
|
|
|
|
logger.log project_id:project_id, doc_id:doc_id, "got doc from document document updater"
|
|
|
|
try
|
|
|
|
body = JSON.parse(body)
|
|
|
|
catch error
|
|
|
|
return callback(error)
|
2016-12-08 09:09:06 -05:00
|
|
|
callback null, body.lines, body.version, body.ranges, body.ops
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
|
|
|
logger.error project_id:project_id, doc_id:doc_id, url: url, "doc updater returned a non-success status code: #{res.statusCode}"
|
|
|
|
callback new Error("doc updater returned a non-success status code: #{res.statusCode}")
|
|
|
|
|
2016-02-04 09:26:50 -05:00
|
|
|
setDocument : (project_id, doc_id, user_id, docLines, source, callback = (error) ->)->
|
2014-02-12 05:23:40 -05:00
|
|
|
timer = new metrics.Timer("set-document")
|
|
|
|
url = "#{settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}"
|
|
|
|
body =
|
|
|
|
url: url
|
|
|
|
json:
|
|
|
|
lines: docLines
|
2014-10-15 11:37:14 -04:00
|
|
|
source: source
|
2016-02-04 09:26:50 -05:00
|
|
|
user_id: user_id
|
|
|
|
logger.log project_id:project_id, doc_id: doc_id, source: source, user_id: user_id, "setting doc in document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
request.post body, (error, res, body)->
|
|
|
|
timer.done()
|
|
|
|
if error?
|
|
|
|
logger.error err:error, url:url, project_id:project_id, doc_id:doc_id, "error setting doc in doc updater"
|
|
|
|
return callback(error)
|
|
|
|
if res.statusCode >= 200 and res.statusCode < 300
|
2014-10-15 09:11:02 -04:00
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "set doc in document updater"
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(null)
|
|
|
|
else
|
|
|
|
logger.error project_id:project_id, doc_id:doc_id, url: url, "doc updater returned a non-success status code: #{res.statusCode}"
|
|
|
|
callback new Error("doc updater returned a non-success status code: #{res.statusCode}")
|
|
|
|
|
2017-08-07 09:45:04 -04:00
|
|
|
getProjectDocsIfMatch: (project_id, projectStateHash, callback = (error, docs) ->) ->
|
2017-08-09 11:00:11 -04:00
|
|
|
# If the project state hasn't changed, we can get all the latest
|
|
|
|
# docs from redis via the docupdater. Otherwise we will need to
|
|
|
|
# fall back to getting them from mongo.
|
2017-08-01 09:38:34 -04:00
|
|
|
timer = new metrics.Timer("get-project-docs")
|
2017-10-11 10:50:21 -04:00
|
|
|
url = "#{settings.apis.documentupdater.url}/project/#{project_id}/get_and_flush_if_old?state=#{projectStateHash}"
|
2017-08-01 09:38:34 -04:00
|
|
|
logger.log project_id:project_id, "getting project docs from document updater"
|
2017-10-11 10:50:21 -04:00
|
|
|
request.post url, (error, res, body)->
|
2017-08-01 09:38:34 -04:00
|
|
|
timer.done()
|
|
|
|
if error?
|
|
|
|
logger.error err:error, url:url, project_id:project_id, "error getting project docs from doc updater"
|
|
|
|
return callback(error)
|
2017-08-09 11:00:11 -04:00
|
|
|
if res.statusCode is 409 # HTTP response code "409 Conflict"
|
|
|
|
# Docupdater has checked the projectStateHash and found that
|
|
|
|
# it has changed. This means that the docs currently in redis
|
|
|
|
# aren't the only change to the project and the full set of
|
|
|
|
# docs/files should be retreived from docstore/filestore
|
|
|
|
# instead.
|
2017-08-07 09:45:04 -04:00
|
|
|
return callback()
|
|
|
|
else if res.statusCode >= 200 and res.statusCode < 300
|
2017-08-01 09:38:34 -04:00
|
|
|
logger.log project_id:project_id, "got project docs from document document updater"
|
|
|
|
try
|
|
|
|
docs = JSON.parse(body)
|
|
|
|
catch error
|
|
|
|
return callback(error)
|
|
|
|
callback null, docs
|
|
|
|
else
|
|
|
|
logger.error project_id:project_id, url: url, "doc updater returned a non-success status code: #{res.statusCode}"
|
|
|
|
callback new Error("doc updater returned a non-success status code: #{res.statusCode}")
|
|
|
|
|
2017-09-08 10:57:29 -04:00
|
|
|
clearProjectState: (project_id, callback = (error) ->) ->
|
|
|
|
timer = new metrics.Timer("clear-project-state")
|
2017-09-12 06:40:00 -04:00
|
|
|
url = "#{settings.apis.documentupdater.url}/project/#{project_id}/clearState"
|
2017-09-08 10:57:29 -04:00
|
|
|
logger.log project_id:project_id, "clearing project state from document updater"
|
|
|
|
request.post url, (error, res)->
|
|
|
|
timer.done()
|
|
|
|
if error?
|
|
|
|
logger.error err:error, url:url, project_id:project_id, "error clearing project state from doc updater"
|
|
|
|
return callback(error)
|
|
|
|
else if res.statusCode is 200
|
|
|
|
logger.log project_id:project_id, "cleared project state from doc updater"
|
|
|
|
callback()
|
|
|
|
else
|
|
|
|
logger.error project_id:project_id, url: url, "doc updater returned a non-success status code: #{res.statusCode}"
|
|
|
|
callback new Error("doc updater returned a non-success status code: #{res.statusCode}")
|
|
|
|
|
2017-05-05 10:19:31 -04:00
|
|
|
acceptChanges: (project_id, doc_id, change_ids = [], callback = (error) ->) ->
|
|
|
|
timer = new metrics.Timer("accept-changes")
|
2017-05-04 10:33:47 -04:00
|
|
|
reqSettings =
|
|
|
|
url: "#{settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}/change/accept"
|
|
|
|
json:
|
|
|
|
change_ids: change_ids
|
2017-05-05 10:19:31 -04:00
|
|
|
logger.log {project_id, doc_id }, "accepting #{ change_ids.length } changes"
|
2017-05-04 10:33:47 -04:00
|
|
|
request.post reqSettings, (error, res, body)->
|
|
|
|
timer.done()
|
|
|
|
if error?
|
|
|
|
logger.error {err:error, project_id, doc_id }, "error accepting #{ change_ids.length } changes in doc updater"
|
|
|
|
return callback(error)
|
|
|
|
if res.statusCode >= 200 and res.statusCode < 300
|
|
|
|
logger.log {project_id, doc_id }, "accepted #{ change_ids.length } changes in document updater"
|
|
|
|
return callback(null)
|
|
|
|
else
|
|
|
|
logger.error {project_id, doc_id }, "doc updater returned a non-success status code: #{res.statusCode}"
|
|
|
|
callback new Error("doc updater returned a non-success status code: #{res.statusCode}")
|
|
|
|
|
2017-01-24 10:18:49 -05:00
|
|
|
deleteThread: (project_id, doc_id, thread_id, callback = (error) ->) ->
|
|
|
|
timer = new metrics.Timer("delete-thread")
|
|
|
|
url = "#{settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}/comment/#{thread_id}"
|
|
|
|
logger.log {project_id, doc_id, thread_id}, "deleting comment range in document updater"
|
|
|
|
request.del url, (error, res, body)->
|
|
|
|
timer.done()
|
|
|
|
if error?
|
|
|
|
logger.error {err:error, project_id, doc_id, thread_id}, "error deleting comment range in doc updater"
|
|
|
|
return callback(error)
|
|
|
|
if res.statusCode >= 200 and res.statusCode < 300
|
|
|
|
logger.log {project_id, doc_id, thread_id}, "deleted comment rangee in document updater"
|
|
|
|
return callback(null)
|
|
|
|
else
|
|
|
|
logger.error {project_id, doc_id, thread_id}, "doc updater returned a non-success status code: #{res.statusCode}"
|
|
|
|
callback new Error("doc updater returned a non-success status code: #{res.statusCode}")
|
|
|
|
|
2017-11-30 10:46:37 -05:00
|
|
|
updateProjectStructure : (project_id, userId, changes, callback = (error) ->)->
|
2017-11-01 13:54:58 -04:00
|
|
|
return callback() if !settings.apis.project_history?.enabled
|
|
|
|
|
2017-11-30 10:46:37 -05:00
|
|
|
docUpdates = DocumentUpdaterHandler._getRenameUpdates('doc', changes.oldDocs, changes.newDocs)
|
|
|
|
fileUpdates = DocumentUpdaterHandler._getRenameUpdates('file', changes.oldFiles, changes.newFiles)
|
2017-11-01 13:54:58 -04:00
|
|
|
|
|
|
|
timer = new metrics.Timer("set-document")
|
|
|
|
url = "#{settings.apis.documentupdater.url}/project/#{project_id}"
|
|
|
|
body =
|
|
|
|
url: url
|
2017-11-06 10:11:33 -05:00
|
|
|
json: { docUpdates, fileUpdates, userId }
|
2017-11-01 13:54:58 -04:00
|
|
|
|
2017-11-06 10:11:33 -05:00
|
|
|
return callback() if (docUpdates.length + fileUpdates.length) < 1
|
2017-11-01 13:54:58 -04:00
|
|
|
|
|
|
|
request.post body, (error, res, body)->
|
|
|
|
timer.done()
|
|
|
|
if error?
|
|
|
|
logger.error {error, url, project_id}, "error update project structure in doc updater"
|
|
|
|
callback(error)
|
|
|
|
else if res.statusCode >= 200 and res.statusCode < 300
|
|
|
|
logger.error {project_id}, "updated project structure in doc updater"
|
|
|
|
callback(null)
|
|
|
|
else
|
|
|
|
logger.error {project_id, url}, "doc updater returned a non-success status code: #{res.statusCode}"
|
|
|
|
callback new Error("doc updater returned a non-success status code: #{res.statusCode}")
|
|
|
|
|
2017-11-06 10:11:33 -05:00
|
|
|
_getRenameUpdates: (entityType, oldEntities, newEntities) ->
|
2017-11-30 10:46:37 -05:00
|
|
|
oldEntities ||= []
|
|
|
|
newEntities ||= []
|
2017-11-06 10:11:33 -05:00
|
|
|
updates = []
|
|
|
|
|
2017-11-14 11:47:15 -05:00
|
|
|
oldEntitiesHash = _.indexBy oldEntities, (entity) -> entity[entityType]._id.toString()
|
|
|
|
newEntitiesHash = _.indexBy newEntities, (entity) -> entity[entityType]._id.toString()
|
2017-11-06 10:11:33 -05:00
|
|
|
|
2017-11-14 11:47:15 -05:00
|
|
|
for id, newEntity of newEntitiesHash
|
|
|
|
oldEntity = oldEntitiesHash[id]
|
2017-11-10 10:47:12 -05:00
|
|
|
|
|
|
|
if !oldEntity?
|
2017-11-15 10:12:59 -05:00
|
|
|
# entity added
|
2017-11-10 10:47:12 -05:00
|
|
|
updates.push
|
|
|
|
id: id
|
|
|
|
pathname: newEntity.path
|
|
|
|
docLines: newEntity.docLines
|
|
|
|
url: newEntity.url
|
2017-11-15 10:12:59 -05:00
|
|
|
else if newEntity.path != oldEntity.path
|
|
|
|
# entity renamed
|
|
|
|
updates.push
|
|
|
|
id: id
|
|
|
|
pathname: oldEntity.path
|
|
|
|
newPathname: newEntity.path
|
2017-11-10 10:47:12 -05:00
|
|
|
|
2017-11-06 10:11:33 -05:00
|
|
|
updates
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
PENDINGUPDATESKEY = "PendingUpdates"
|
|
|
|
DOCLINESKEY = "doclines"
|
|
|
|
DOCIDSWITHPENDINGUPDATES = "DocsWithPendingUpdates"
|
|
|
|
|
|
|
|
keys =
|
|
|
|
pendingUpdates : (op) -> "#{PENDINGUPDATESKEY}:#{op.doc_id}"
|
|
|
|
docsWithPendingUpdates: DOCIDSWITHPENDINGUPDATES
|
|
|
|
docLines : (op) -> "#{DOCLINESKEY}:#{op.doc_id}"
|
|
|
|
combineProjectIdAndDocId: (project_id, doc_id) -> "#{project_id}:#{doc_id}"
|
|
|
|
|
|
|
|
|