2020-05-06 06:12:36 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2020-05-06 06:12:47 -04:00
|
|
|
let DocUpdaterClient
|
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const rclient = require('redis-sharelatex').createClient(
|
|
|
|
Settings.redis.documentupdater
|
|
|
|
)
|
|
|
|
const keys = Settings.redis.documentupdater.key_schema
|
|
|
|
const request = require('request').defaults({ jar: false })
|
|
|
|
const async = require('async')
|
|
|
|
|
|
|
|
const rclient_sub = require('redis-sharelatex').createClient(
|
|
|
|
Settings.redis.pubsub
|
|
|
|
)
|
|
|
|
rclient_sub.subscribe('applied-ops')
|
|
|
|
rclient_sub.setMaxListeners(0)
|
|
|
|
|
|
|
|
module.exports = DocUpdaterClient = {
|
|
|
|
randomId() {
|
2020-05-20 16:06:42 -04:00
|
|
|
let str = ''
|
|
|
|
for (let i = 0; i < 24; i++) {
|
|
|
|
str += Math.floor(Math.random() * 16).toString(16)
|
|
|
|
}
|
|
|
|
return str
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
subscribeToAppliedOps(callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
rclient_sub.on('message', callback)
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
sendUpdate(project_id, doc_id, update, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
rclient.rpush(
|
2020-05-06 06:12:47 -04:00
|
|
|
keys.pendingUpdates({ doc_id }),
|
|
|
|
JSON.stringify(update),
|
|
|
|
(error) => {
|
2020-05-20 15:53:06 -04:00
|
|
|
if (error) {
|
2020-05-06 06:12:47 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const doc_key = `${project_id}:${doc_id}`
|
2020-05-20 15:54:36 -04:00
|
|
|
rclient.sadd('DocsWithPendingUpdates', doc_key, (error) => {
|
2020-05-20 15:53:06 -04:00
|
|
|
if (error) {
|
2020-05-06 06:12:47 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2020-05-20 15:54:36 -04:00
|
|
|
rclient.rpush('pending-updates-list', doc_key, callback)
|
2020-05-06 06:12:17 -04:00
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
sendUpdates(project_id, doc_id, updates, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
DocUpdaterClient.preloadDoc(project_id, doc_id, (error) => {
|
2020-05-20 15:53:06 -04:00
|
|
|
if (error) {
|
2020-05-06 06:12:47 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2020-05-20 16:06:42 -04:00
|
|
|
const jobs = updates.map((update) => (callback) => {
|
|
|
|
DocUpdaterClient.sendUpdate(project_id, doc_id, update, callback)
|
|
|
|
})
|
2020-05-20 16:08:03 -04:00
|
|
|
async.series(jobs, (err) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2020-05-06 06:12:47 -04:00
|
|
|
DocUpdaterClient.waitForPendingUpdates(project_id, doc_id, callback)
|
2020-05-20 16:08:03 -04:00
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
waitForPendingUpdates(project_id, doc_id, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
async.retry(
|
2020-05-06 06:12:47 -04:00
|
|
|
{ times: 30, interval: 100 },
|
|
|
|
(cb) =>
|
|
|
|
rclient.llen(keys.pendingUpdates({ doc_id }), (err, length) => {
|
2020-05-20 16:08:03 -04:00
|
|
|
if (err) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
2020-05-06 06:12:47 -04:00
|
|
|
if (length > 0) {
|
2020-05-20 15:54:36 -04:00
|
|
|
cb(new Error('updates still pending'))
|
2020-05-06 06:12:47 -04:00
|
|
|
} else {
|
2020-05-20 15:54:36 -04:00
|
|
|
cb()
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getDoc(project_id, doc_id, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.get(
|
2020-05-06 06:12:47 -04:00
|
|
|
`http://localhost:3003/project/${project_id}/doc/${doc_id}`,
|
|
|
|
(error, res, body) => {
|
|
|
|
if (body != null && res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
body = JSON.parse(body)
|
|
|
|
}
|
2020-05-20 15:54:36 -04:00
|
|
|
callback(error, res, body)
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getDocAndRecentOps(project_id, doc_id, fromVersion, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.get(
|
2020-05-06 06:12:47 -04:00
|
|
|
`http://localhost:3003/project/${project_id}/doc/${doc_id}?fromVersion=${fromVersion}`,
|
|
|
|
(error, res, body) => {
|
|
|
|
if (body != null && res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
body = JSON.parse(body)
|
|
|
|
}
|
2020-05-20 15:54:36 -04:00
|
|
|
callback(error, res, body)
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
preloadDoc(project_id, doc_id, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
DocUpdaterClient.getDoc(project_id, doc_id, callback)
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
flushDoc(project_id, doc_id, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.post(
|
2020-05-06 06:12:47 -04:00
|
|
|
`http://localhost:3003/project/${project_id}/doc/${doc_id}/flush`,
|
|
|
|
(error, res, body) => callback(error, res, body)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
setDocLines(project_id, doc_id, lines, source, user_id, undoing, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.post(
|
2020-05-06 06:12:47 -04:00
|
|
|
{
|
|
|
|
url: `http://localhost:3003/project/${project_id}/doc/${doc_id}`,
|
|
|
|
json: {
|
|
|
|
lines,
|
|
|
|
source,
|
|
|
|
user_id,
|
|
|
|
undoing
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(error, res, body) => callback(error, res, body)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteDoc(project_id, doc_id, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.del(
|
2020-05-06 06:12:47 -04:00
|
|
|
`http://localhost:3003/project/${project_id}/doc/${doc_id}`,
|
|
|
|
(error, res, body) => callback(error, res, body)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
flushProject(project_id, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.post(`http://localhost:3003/project/${project_id}/flush`, callback)
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteProject(project_id, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.del(`http://localhost:3003/project/${project_id}`, callback)
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteProjectOnShutdown(project_id, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.del(
|
2020-05-06 06:12:47 -04:00
|
|
|
`http://localhost:3003/project/${project_id}?background=true&shutdown=true`,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
flushOldProjects(callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.get(
|
2020-05-06 06:12:47 -04:00
|
|
|
'http://localhost:3003/flush_queued_projects?min_delete_age=1',
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
acceptChange(project_id, doc_id, change_id, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.post(
|
2020-05-06 06:12:47 -04:00
|
|
|
`http://localhost:3003/project/${project_id}/doc/${doc_id}/change/${change_id}/accept`,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
removeComment(project_id, doc_id, comment, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.del(
|
2020-05-06 06:12:47 -04:00
|
|
|
`http://localhost:3003/project/${project_id}/doc/${doc_id}/comment/${comment}`,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getProjectDocs(project_id, projectStateHash, callback) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.get(
|
2020-05-06 06:12:47 -04:00
|
|
|
`http://localhost:3003/project/${project_id}/doc?state=${projectStateHash}`,
|
|
|
|
(error, res, body) => {
|
|
|
|
if (body != null && res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
body = JSON.parse(body)
|
|
|
|
}
|
2020-05-20 15:54:36 -04:00
|
|
|
callback(error, res, body)
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
sendProjectUpdate(
|
|
|
|
project_id,
|
|
|
|
userId,
|
|
|
|
docUpdates,
|
|
|
|
fileUpdates,
|
|
|
|
version,
|
|
|
|
callback
|
|
|
|
) {
|
2020-05-20 15:54:36 -04:00
|
|
|
request.post(
|
2020-05-06 06:12:47 -04:00
|
|
|
{
|
|
|
|
url: `http://localhost:3003/project/${project_id}`,
|
|
|
|
json: { userId, docUpdates, fileUpdates, version }
|
|
|
|
},
|
|
|
|
(error, res, body) => callback(error, res, body)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|