overleaf/services/track-changes/app/coffee/DocumentUpdaterManager.coffee

40 lines
1.6 KiB
CoffeeScript
Raw Normal View History

request = require "request"
logger = require "logger-sharelatex"
Settings = require "settings-sharelatex"
module.exports = DocumentUpdaterManager =
getDocument: (project_id, doc_id, callback = (error, content, version) ->) ->
url = "#{Settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}"
logger.log project_id:project_id, doc_id: doc_id, "getting doc from document updater"
request.get url, (error, res, body)->
if error?
return callback(error)
if res.statusCode >= 200 and res.statusCode < 300
try
body = JSON.parse(body)
catch error
return callback(error)
2014-03-10 16:03:03 +00:00
callback null, body.lines.join("\n"), body.version
2014-03-10 16:58:26 +00:00
else
error = new Error("doc updater returned a non-success status code: #{res.statusCode}")
logger.error err: error, project_id:project_id, doc_id:doc_id, url: url, "error accessing doc updater"
callback error
setDocument: (project_id, doc_id, content, user_id, callback = (error) ->) ->
2014-03-10 16:58:26 +00:00
url = "#{Settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}"
logger.log project_id:project_id, doc_id: doc_id, "setting doc in document updater"
request.post {
url: url
json:
lines: content.split("\n")
source: "restore"
user_id: user_id
2014-03-10 16:58:26 +00:00
}, (error, res, body)->
if error?
return callback(error)
if res.statusCode >= 200 and res.statusCode < 300
callback null
else
error = new Error("doc updater returned a non-success status code: #{res.statusCode}")
logger.error err: error, project_id:project_id, doc_id:doc_id, url: url, "error accessing doc updater"
callback error