2016-06-17 07:17:22 -04:00
|
|
|
Settings = require('settings-sharelatex')
|
2018-12-05 11:17:41 -05:00
|
|
|
rclient = require("redis-sharelatex").createClient(Settings.redis.documentupdater)
|
|
|
|
keys = Settings.redis.documentupdater.key_schema
|
2014-02-12 05:40:42 -05:00
|
|
|
request = require("request").defaults(jar: false)
|
|
|
|
async = require "async"
|
|
|
|
|
2018-12-05 11:17:41 -05:00
|
|
|
rclient_sub = require("redis-sharelatex").createClient(Settings.redis.documentupdater)
|
2016-06-21 09:31:20 -04:00
|
|
|
rclient_sub.subscribe "applied-ops"
|
|
|
|
rclient_sub.setMaxListeners(0)
|
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
module.exports = DocUpdaterClient =
|
|
|
|
randomId: () ->
|
2014-02-10 10:17:08 -05:00
|
|
|
chars = for i in [1..24]
|
|
|
|
Math.random().toString(16)[2]
|
|
|
|
return chars.join("")
|
2016-06-17 07:17:22 -04:00
|
|
|
|
|
|
|
subscribeToAppliedOps: (callback = (message) ->) ->
|
|
|
|
rclient_sub.on "message", callback
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
sendUpdate: (project_id, doc_id, update, callback = (error) ->) ->
|
2017-05-03 11:27:32 -04:00
|
|
|
rclient.rpush keys.pendingUpdates({doc_id}), JSON.stringify(update), (error)->
|
2014-02-12 05:40:42 -05:00
|
|
|
return callback(error) if error?
|
|
|
|
doc_key = "#{project_id}:#{doc_id}"
|
|
|
|
rclient.sadd "DocsWithPendingUpdates", doc_key, (error) ->
|
|
|
|
return callback(error) if error?
|
2014-08-07 06:45:19 -04:00
|
|
|
rclient.rpush "pending-updates-list", doc_key, callback
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
sendUpdates: (project_id, doc_id, updates, callback = (error) ->) ->
|
|
|
|
DocUpdaterClient.preloadDoc project_id, doc_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
jobs = []
|
|
|
|
for update in updates
|
|
|
|
do (update) ->
|
|
|
|
jobs.push (callback) ->
|
|
|
|
DocUpdaterClient.sendUpdate project_id, doc_id, update, callback
|
|
|
|
async.series jobs, callback
|
|
|
|
|
|
|
|
getDoc: (project_id, doc_id, callback = (error, res, body) ->) ->
|
|
|
|
request.get "http://localhost:3003/project/#{project_id}/doc/#{doc_id}", (error, res, body) ->
|
|
|
|
if body? and res.statusCode >= 200 and res.statusCode < 300
|
|
|
|
body = JSON.parse(body)
|
|
|
|
callback error, res, body
|
|
|
|
|
|
|
|
getDocAndRecentOps: (project_id, doc_id, fromVersion, callback = (error, res, body) ->) ->
|
|
|
|
request.get "http://localhost:3003/project/#{project_id}/doc/#{doc_id}?fromVersion=#{fromVersion}", (error, res, body) ->
|
|
|
|
if body? and res.statusCode >= 200 and res.statusCode < 300
|
|
|
|
body = JSON.parse(body)
|
|
|
|
callback error, res, body
|
|
|
|
|
|
|
|
preloadDoc: (project_id, doc_id, callback = (error) ->) ->
|
|
|
|
DocUpdaterClient.getDoc project_id, doc_id, callback
|
|
|
|
|
|
|
|
flushDoc: (project_id, doc_id, callback = (error) ->) ->
|
|
|
|
request.post "http://localhost:3003/project/#{project_id}/doc/#{doc_id}/flush", (error, res, body) ->
|
|
|
|
callback error, res, body
|
|
|
|
|
2017-03-21 07:20:38 -04:00
|
|
|
setDocLines: (project_id, doc_id, lines, source, user_id, undoing, callback = (error) ->) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
request.post {
|
|
|
|
url: "http://localhost:3003/project/#{project_id}/doc/#{doc_id}"
|
|
|
|
json:
|
|
|
|
lines: lines
|
2014-03-11 08:47:26 -04:00
|
|
|
source: source
|
|
|
|
user_id: user_id
|
2017-03-21 07:20:38 -04:00
|
|
|
undoing: undoing
|
2014-02-12 05:40:42 -05:00
|
|
|
}, (error, res, body) ->
|
|
|
|
callback error, res, body
|
|
|
|
|
|
|
|
deleteDoc: (project_id, doc_id, callback = (error) ->) ->
|
|
|
|
request.del "http://localhost:3003/project/#{project_id}/doc/#{doc_id}", (error, res, body) ->
|
|
|
|
callback error, res, body
|
|
|
|
|
|
|
|
flushProject: (project_id, callback = () ->) ->
|
|
|
|
request.post "http://localhost:3003/project/#{project_id}/flush", callback
|
|
|
|
|
|
|
|
deleteProject: (project_id, callback = () ->) ->
|
|
|
|
request.del "http://localhost:3003/project/#{project_id}", callback
|
2017-01-09 08:41:18 -05:00
|
|
|
|
|
|
|
acceptChange: (project_id, doc_id, change_id, callback = () ->) ->
|
2017-01-24 09:57:11 -05:00
|
|
|
request.post "http://localhost:3003/project/#{project_id}/doc/#{doc_id}/change/#{change_id}/accept", callback
|
|
|
|
|
|
|
|
removeComment: (project_id, doc_id, comment, callback = () ->) ->
|
|
|
|
request.del "http://localhost:3003/project/#{project_id}/doc/#{doc_id}/comment/#{comment}", callback
|
2017-08-11 11:55:31 -04:00
|
|
|
|
|
|
|
getProjectDocs: (project_id, projectStateHash, callback = () ->) ->
|
|
|
|
request.get "http://localhost:3003/project/#{project_id}/doc?state=#{projectStateHash}", (error, res, body) ->
|
|
|
|
if body? and res.statusCode >= 200 and res.statusCode < 300
|
|
|
|
body = JSON.parse(body)
|
|
|
|
callback error, res, body
|
2017-11-06 12:18:28 -05:00
|
|
|
|
2018-03-06 05:36:38 -05:00
|
|
|
sendProjectUpdate: (project_id, userId, docUpdates, fileUpdates, version, callback = (error) ->) ->
|
2017-11-06 12:18:28 -05:00
|
|
|
request.post {
|
|
|
|
url: "http://localhost:3003/project/#{project_id}"
|
2018-03-06 05:36:38 -05:00
|
|
|
json: { userId, docUpdates, fileUpdates, version }
|
2017-11-06 12:18:28 -05:00
|
|
|
}, (error, res, body) ->
|
|
|
|
callback error, res, body
|