2016-03-10 10:15:57 -05:00
|
|
|
async = require 'async'
|
|
|
|
zlib = require 'zlib'
|
2014-03-04 10:27:03 -05:00
|
|
|
request = require "request"
|
|
|
|
rclient = require("redis").createClient() # Only works locally for now
|
|
|
|
{db, ObjectId} = require "../../../../app/js/mongojs"
|
2015-09-03 07:36:32 -04:00
|
|
|
Settings = require "settings-sharelatex"
|
2014-03-04 10:27:03 -05:00
|
|
|
|
|
|
|
module.exports = TrackChangesClient =
|
2014-03-19 12:14:17 -04:00
|
|
|
flushAndGetCompressedUpdates: (project_id, doc_id, callback = (error, updates) ->) ->
|
2014-03-21 11:57:17 -04:00
|
|
|
TrackChangesClient.flushDoc project_id, doc_id, (error) ->
|
2014-03-20 08:10:04 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
TrackChangesClient.getCompressedUpdates doc_id, callback
|
|
|
|
|
2014-03-21 11:57:17 -04:00
|
|
|
flushDoc: (project_id, doc_id, callback = (error) ->) ->
|
2014-03-04 10:27:03 -05:00
|
|
|
request.post {
|
2014-03-19 12:14:17 -04:00
|
|
|
url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/flush"
|
2014-03-04 10:27:03 -05:00
|
|
|
}, (error, response, body) =>
|
|
|
|
response.statusCode.should.equal 204
|
2014-03-20 08:10:04 -04:00
|
|
|
callback(error)
|
|
|
|
|
2014-03-21 11:57:17 -04:00
|
|
|
flushProject: (project_id, callback = (error) ->) ->
|
|
|
|
request.post {
|
|
|
|
url: "http://localhost:3015/project/#{project_id}/flush"
|
|
|
|
}, (error, response, body) =>
|
|
|
|
response.statusCode.should.equal 204
|
|
|
|
callback(error)
|
|
|
|
|
2014-03-20 08:10:04 -04:00
|
|
|
getCompressedUpdates: (doc_id, callback = (error, updates) ->) ->
|
|
|
|
db.docHistory
|
|
|
|
.find(doc_id: ObjectId(doc_id))
|
|
|
|
.sort("meta.end_ts": 1)
|
|
|
|
.toArray callback
|
2014-03-04 10:27:03 -05:00
|
|
|
|
2014-03-28 12:01:34 -04:00
|
|
|
getProjectMetaData: (project_id, callback = (error, updates) ->) ->
|
|
|
|
db.projectHistoryMetaData
|
|
|
|
.find {
|
|
|
|
project_id: ObjectId(project_id)
|
|
|
|
},
|
|
|
|
(error, projects) ->
|
|
|
|
callback error, projects[0]
|
|
|
|
|
|
|
|
setPreserveHistoryForProject: (project_id, callback = (error) ->) ->
|
|
|
|
db.projectHistoryMetaData.update {
|
|
|
|
project_id: ObjectId(project_id)
|
|
|
|
}, {
|
|
|
|
$set: { preserveHistory: true }
|
|
|
|
}, {
|
|
|
|
upsert: true
|
|
|
|
}, callback
|
|
|
|
|
2014-03-21 09:17:58 -04:00
|
|
|
pushRawUpdates: (project_id, doc_id, updates, callback = (error) ->) ->
|
|
|
|
rclient.sadd "DocsWithHistoryOps:#{project_id}", doc_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
rclient.rpush "UncompressedHistoryOps:#{doc_id}", (JSON.stringify(u) for u in updates)..., callback
|
2014-03-04 10:27:03 -05:00
|
|
|
|
|
|
|
getDiff: (project_id, doc_id, from, to, callback = (error, diff) ->) ->
|
|
|
|
request.get {
|
|
|
|
url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/diff?from=#{from}&to=#{to}"
|
2014-03-05 10:59:40 -05:00
|
|
|
}, (error, response, body) =>
|
|
|
|
response.statusCode.should.equal 200
|
|
|
|
callback null, JSON.parse(body)
|
|
|
|
|
2014-03-20 08:10:04 -04:00
|
|
|
getUpdates: (project_id, options, callback = (error, body) ->) ->
|
2014-03-05 10:59:40 -05:00
|
|
|
request.get {
|
2014-03-20 08:10:04 -04:00
|
|
|
url: "http://localhost:3015/project/#{project_id}/updates?before=#{options.before}&min_count=#{options.min_count}"
|
2014-03-04 10:27:03 -05:00
|
|
|
}, (error, response, body) =>
|
|
|
|
response.statusCode.should.equal 200
|
2014-03-10 12:58:26 -04:00
|
|
|
callback null, JSON.parse(body)
|
|
|
|
|
2014-03-11 09:01:07 -04:00
|
|
|
restoreDoc: (project_id, doc_id, version, user_id, callback = (error) ->) ->
|
2014-03-10 12:58:26 -04:00
|
|
|
request.post {
|
|
|
|
url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/version/#{version}/restore"
|
2014-03-11 09:01:07 -04:00
|
|
|
headers:
|
|
|
|
"X-User-Id": user_id
|
2014-03-10 12:58:26 -04:00
|
|
|
}, (error, response, body) =>
|
|
|
|
response.statusCode.should.equal 204
|
2015-09-02 17:47:34 -04:00
|
|
|
callback null
|
|
|
|
|
2016-03-09 11:56:49 -05:00
|
|
|
pushDocHistory: (project_id, doc_id, callback = (error) ->) ->
|
2015-10-01 10:55:07 -04:00
|
|
|
request.post {
|
2016-03-09 11:56:49 -05:00
|
|
|
url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/push"
|
2015-09-02 17:47:34 -04:00
|
|
|
}, (error, response, body) =>
|
|
|
|
response.statusCode.should.equal 204
|
|
|
|
callback(error)
|
|
|
|
|
2016-03-09 11:56:49 -05:00
|
|
|
pullDocHistory: (project_id, doc_id, callback = (error) ->) ->
|
2015-10-01 10:55:07 -04:00
|
|
|
request.post {
|
2016-03-09 11:56:49 -05:00
|
|
|
url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/pull"
|
2015-09-02 17:47:34 -04:00
|
|
|
}, (error, response, body) =>
|
|
|
|
response.statusCode.should.equal 204
|
2015-09-03 07:36:32 -04:00
|
|
|
callback(error)
|
|
|
|
|
|
|
|
buildS3Options: (content, key)->
|
|
|
|
return {
|
|
|
|
aws:
|
2016-03-09 11:56:49 -05:00
|
|
|
key: Settings.trackchanges.s3.key
|
|
|
|
secret: Settings.trackchanges.s3.secret
|
|
|
|
bucket: Settings.trackchanges.stores.doc_history
|
2015-09-03 07:36:32 -04:00
|
|
|
timeout: 30 * 1000
|
|
|
|
json: content
|
2016-03-09 11:56:49 -05:00
|
|
|
uri:"https://#{Settings.trackchanges.stores.doc_history}.s3.amazonaws.com/#{key}"
|
2015-09-03 07:36:32 -04:00
|
|
|
}
|
|
|
|
|
2016-03-10 10:15:57 -05:00
|
|
|
getS3Doc: (project_id, doc_id, pack_id, callback = (error, body) ->) ->
|
|
|
|
options = TrackChangesClient.buildS3Options(true, project_id+"/changes-"+doc_id+"/pack-"+pack_id)
|
|
|
|
options.encoding = null
|
|
|
|
request.get options, (err, res, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
zlib.gunzip body, (err, result) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
callback(null, JSON.parse(result.toString()))
|
2015-09-03 07:36:32 -04:00
|
|
|
|
|
|
|
removeS3Doc: (project_id, doc_id, callback = (error, res, body) ->) ->
|
2016-03-10 10:15:57 -05:00
|
|
|
options = TrackChangesClient.buildS3Options(true, "?prefix=" + project_id + "/changes-" +doc_id)
|
|
|
|
request.get options, (error, res, body) ->
|
|
|
|
keys = body.match /[0-9a-f]{24}\/changes-[0-9a-f]{24}\/pack-[0-9a-f]{24}/g
|
|
|
|
async.eachSeries keys, (key, cb) ->
|
|
|
|
options = TrackChangesClient.buildS3Options(true, key)
|
|
|
|
request.del options, cb
|
|
|
|
, callback
|