2014-03-05 10:59:40 -05:00
|
|
|
UpdatesManager = require "./UpdatesManager"
|
2014-03-04 10:27:03 -05:00
|
|
|
DiffManager = require "./DiffManager"
|
2014-03-10 12:58:26 -04:00
|
|
|
RestoreManager = require "./RestoreManager"
|
2014-01-27 13:09:37 -05:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
|
|
|
|
module.exports = HttpController =
|
2014-02-26 06:34:56 -05:00
|
|
|
flushUpdatesWithLock: (req, res, next = (error) ->) ->
|
2014-01-27 13:09:37 -05:00
|
|
|
doc_id = req.params.doc_id
|
2014-02-26 06:34:56 -05:00
|
|
|
logger.log doc_id: doc_id, "compressing doc history"
|
2014-03-05 10:59:40 -05:00
|
|
|
UpdatesManager.processUncompressedUpdatesWithLock doc_id, (error) ->
|
2014-01-27 13:09:37 -05:00
|
|
|
return next(error) if error?
|
|
|
|
res.send 204
|
2014-03-04 10:27:03 -05:00
|
|
|
|
|
|
|
getDiff: (req, res, next = (error) ->) ->
|
|
|
|
doc_id = req.params.doc_id
|
|
|
|
project_id = req.params.project_id
|
|
|
|
|
|
|
|
if req.query.from?
|
|
|
|
from = parseInt(req.query.from, 10)
|
|
|
|
else
|
|
|
|
from = null
|
|
|
|
if req.query.to?
|
|
|
|
to = parseInt(req.query.to, 10)
|
|
|
|
else
|
|
|
|
to = null
|
|
|
|
|
|
|
|
logger.log project_id, doc_id: doc_id, from: from, to: to, "getting diff"
|
|
|
|
DiffManager.getDiff project_id, doc_id, from, to, (error, diff) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.send JSON.stringify(diff: diff)
|
|
|
|
|
2014-03-05 10:06:46 -05:00
|
|
|
getUpdates: (req, res, next = (error) ->) ->
|
|
|
|
doc_id = req.params.doc_id
|
|
|
|
project_id = req.params.project_id
|
2014-03-05 10:59:40 -05:00
|
|
|
|
|
|
|
if req.query.to?
|
|
|
|
to = parseInt(req.query.to, 10)
|
|
|
|
if req.query.limit?
|
|
|
|
limit = parseInt(req.query.limit, 10)
|
|
|
|
|
2014-03-06 13:04:00 -05:00
|
|
|
UpdatesManager.getUpdatesWithUserInfo doc_id, to: to, limit: limit, (error, updates) ->
|
2014-03-05 10:59:40 -05:00
|
|
|
return next(error) if error?
|
2014-03-11 13:39:40 -04:00
|
|
|
res.send JSON.stringify updates: HttpController._buildUpdatesView(updates)
|
|
|
|
|
|
|
|
TIME_BETWEEN_DISTINCT_UPDATES: fiveMinutes = 5 * 60 * 1000
|
|
|
|
_buildUpdatesView: (updates) ->
|
|
|
|
view = []
|
|
|
|
for update in updates.slice().reverse()
|
|
|
|
lastUpdate = view[view.length - 1]
|
|
|
|
if lastUpdate and update.meta.start_ts - lastUpdate.meta.end_ts < @TIME_BETWEEN_DISTINCT_UPDATES
|
|
|
|
if update.meta.user?
|
|
|
|
userExists = false
|
|
|
|
for user in lastUpdate.meta.users
|
|
|
|
if user.id == update.meta.user.id
|
|
|
|
userExists = true
|
|
|
|
break
|
|
|
|
if !userExists
|
|
|
|
lastUpdate.meta.users.push update.meta.user
|
|
|
|
lastUpdate.meta.start_ts = Math.min(lastUpdate.meta.start_ts, update.meta.start_ts)
|
|
|
|
lastUpdate.meta.end_ts = Math.max(lastUpdate.meta.end_ts, update.meta.end_ts)
|
|
|
|
lastUpdate.toV = update.v
|
|
|
|
else
|
|
|
|
newUpdate =
|
|
|
|
meta:
|
|
|
|
users: []
|
|
|
|
start_ts: update.meta.start_ts
|
|
|
|
end_ts: update.meta.end_ts
|
|
|
|
fromV: update.v
|
|
|
|
toV: update.v
|
|
|
|
|
|
|
|
if update.meta.user?
|
|
|
|
newUpdate.meta.users.push update.meta.user
|
|
|
|
|
|
|
|
view.push newUpdate
|
|
|
|
|
|
|
|
return view.reverse()
|
2014-03-10 12:58:26 -04:00
|
|
|
|
|
|
|
restore: (req, res, next = (error) ->) ->
|
|
|
|
{doc_id, project_id, version} = req.params
|
2014-03-11 09:01:07 -04:00
|
|
|
user_id = req.headers["x-user-id"]
|
2014-03-10 12:58:26 -04:00
|
|
|
version = parseInt(version, 10)
|
2014-03-11 09:01:07 -04:00
|
|
|
RestoreManager.restoreToBeforeVersion project_id, doc_id, version, user_id, (error) ->
|
2014-03-10 12:58:26 -04:00
|
|
|
return next(error) if error?
|
|
|
|
res.send 204
|