mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Move update summarizing logic into UpdatesManager
This commit is contained in:
parent
22a806a200
commit
81811d7cc5
4 changed files with 123 additions and 98 deletions
|
@ -38,42 +38,10 @@ module.exports = HttpController =
|
||||||
if req.query.limit?
|
if req.query.limit?
|
||||||
limit = parseInt(req.query.limit, 10)
|
limit = parseInt(req.query.limit, 10)
|
||||||
|
|
||||||
UpdatesManager.getUpdatesWithUserInfo doc_id, to: to, limit: limit, (error, updates) ->
|
UpdatesManager.getSummarizedUpdates doc_id, to: to, limit: limit, (error, updates) ->
|
||||||
return next(error) if error?
|
return next(error) if error?
|
||||||
res.send JSON.stringify updates: HttpController._buildUpdatesView(updates)
|
res.send JSON.stringify updates: 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()
|
|
||||||
|
|
||||||
restore: (req, res, next = (error) ->) ->
|
restore: (req, res, next = (error) ->) ->
|
||||||
{doc_id, project_id, version} = req.params
|
{doc_id, project_id, version} = req.params
|
||||||
|
|
|
@ -76,6 +76,11 @@ module.exports = UpdatesManager =
|
||||||
return callback(error) if error?
|
return callback(error) if error?
|
||||||
callback null, updates
|
callback null, updates
|
||||||
|
|
||||||
|
getSummarizedUpdates: (doc_id, options = {}, callback = (error, updates) ->) ->
|
||||||
|
UpdatesManager.getUpdatesWithUserInfo doc_id, options, (error, updates) ->
|
||||||
|
return callback(error) if error?
|
||||||
|
callback null, UpdatesManager._summarizeUpdates(updates)
|
||||||
|
|
||||||
fillUserInfo: (updates, callback = (error, updates) ->) ->
|
fillUserInfo: (updates, callback = (error, updates) ->) ->
|
||||||
users = {}
|
users = {}
|
||||||
for update in updates
|
for update in updates
|
||||||
|
@ -105,3 +110,37 @@ module.exports = UpdatesManager =
|
||||||
return false
|
return false
|
||||||
else
|
else
|
||||||
return !!user_id.match(/^[a-f0-9]{24}$/)
|
return !!user_id.match(/^[a-f0-9]{24}$/)
|
||||||
|
|
||||||
|
|
||||||
|
TIME_BETWEEN_DISTINCT_UPDATES: fiveMinutes = 5 * 60 * 1000
|
||||||
|
_summarizeUpdates: (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()
|
||||||
|
|
|
@ -74,77 +74,17 @@ describe "HttpController", ->
|
||||||
limit: @limit.toString()
|
limit: @limit.toString()
|
||||||
@res =
|
@res =
|
||||||
send: sinon.stub()
|
send: sinon.stub()
|
||||||
@rawUpdates = ["raw-updates"]
|
@updates = ["mock-summarized-updates"]
|
||||||
@updatesView = ["updates-view"]
|
@UpdatesManager.getSummarizedUpdates = sinon.stub().callsArgWith(2, null, @updates)
|
||||||
@HttpController._buildUpdatesView = sinon.stub().returns(@updatesView)
|
|
||||||
@UpdatesManager.getUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @rawUpdates)
|
|
||||||
@HttpController.getUpdates @req, @res, @next
|
@HttpController.getUpdates @req, @res, @next
|
||||||
|
|
||||||
it "should get the updates", ->
|
it "should get the updates", ->
|
||||||
@UpdatesManager.getUpdatesWithUserInfo
|
@UpdatesManager.getSummarizedUpdates
|
||||||
.calledWith(@doc_id, to: @to, limit: @limit)
|
.calledWith(@doc_id, to: @to, limit: @limit)
|
||||||
.should.equal true
|
.should.equal true
|
||||||
|
|
||||||
it "should build the updates view", ->
|
|
||||||
@HttpController._buildUpdatesView
|
|
||||||
.calledWith(@rawUpdates)
|
|
||||||
.should.equal true
|
|
||||||
|
|
||||||
it "should return the formatted updates", ->
|
it "should return the formatted updates", ->
|
||||||
@res.send.calledWith(JSON.stringify(updates: @updatesView)).should.equal true
|
@res.send.calledWith(JSON.stringify(updates: @updates)).should.equal true
|
||||||
|
|
||||||
describe "_buildUpdatesView", ->
|
|
||||||
it "should concat updates that are close in time", ->
|
|
||||||
expect(@HttpController._buildUpdatesView [{
|
|
||||||
meta:
|
|
||||||
user: @user_2 = { id: "mock-user-2" }
|
|
||||||
start_ts: @now + 20
|
|
||||||
end_ts: @now + 30
|
|
||||||
v: 5
|
|
||||||
}, {
|
|
||||||
meta:
|
|
||||||
user: @user_1 = { id: "mock-user-1" }
|
|
||||||
start_ts: @now
|
|
||||||
end_ts: @now + 10
|
|
||||||
v: 4
|
|
||||||
}]).to.deep.equal [{
|
|
||||||
meta:
|
|
||||||
users: [@user_1, @user_2]
|
|
||||||
start_ts: @now
|
|
||||||
end_ts: @now + 30
|
|
||||||
fromV: 4
|
|
||||||
toV: 5
|
|
||||||
}]
|
|
||||||
|
|
||||||
it "should leave updates that are far apart in time", ->
|
|
||||||
oneDay = 1000 * 60 * 60 * 24
|
|
||||||
expect(@HttpController._buildUpdatesView [{
|
|
||||||
meta:
|
|
||||||
user: @user_2 = { id: "mock-user-2" }
|
|
||||||
start_ts: @now + oneDay
|
|
||||||
end_ts: @now + oneDay + 10
|
|
||||||
v: 5
|
|
||||||
}, {
|
|
||||||
meta:
|
|
||||||
user: @user_1 = { id: "mock-user-2" }
|
|
||||||
start_ts: @now
|
|
||||||
end_ts: @now + 10
|
|
||||||
v: 4
|
|
||||||
}]).to.deep.equal [{
|
|
||||||
meta:
|
|
||||||
users: [@user_2]
|
|
||||||
start_ts: @now + oneDay
|
|
||||||
end_ts: @now + oneDay + 10
|
|
||||||
fromV: 5
|
|
||||||
toV: 5
|
|
||||||
}, {
|
|
||||||
meta:
|
|
||||||
users: [@user_1]
|
|
||||||
start_ts: @now
|
|
||||||
end_ts: @now + 10
|
|
||||||
fromV: 4
|
|
||||||
toV: 4
|
|
||||||
}]
|
|
||||||
|
|
||||||
describe "RestoreManager", ->
|
describe "RestoreManager", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
|
|
|
@ -242,6 +242,29 @@ describe "UpdatesManager", ->
|
||||||
it "shoudl return the updates with the filled details", ->
|
it "shoudl return the updates with the filled details", ->
|
||||||
@callback.calledWith(null, @updatesWithUserInfo).should.equal true
|
@callback.calledWith(null, @updatesWithUserInfo).should.equal true
|
||||||
|
|
||||||
|
describe "getSummarizedUpdates", ->
|
||||||
|
beforeEach ->
|
||||||
|
@to = 42
|
||||||
|
@limit = 10
|
||||||
|
@updates = ["mock-updates"]
|
||||||
|
@summarizedUpdates = ["summarized-updates"]
|
||||||
|
@UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
|
||||||
|
@UpdatesManager.getUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
|
||||||
|
@UpdatesManager.getSummarizedUpdates @doc_id, { to: @to, limit: @limit }, @callback
|
||||||
|
|
||||||
|
it "should get the updates", ->
|
||||||
|
@UpdatesManager.getUpdatesWithUserInfo
|
||||||
|
.calledWith(@doc_id, { to: @to, limit: @limit })
|
||||||
|
.should.equal true
|
||||||
|
|
||||||
|
it "should summarize the updates", ->
|
||||||
|
@UpdatesManager._summarizeUpdates
|
||||||
|
.calledWith(@updates)
|
||||||
|
.should.equal true
|
||||||
|
|
||||||
|
it "should call the callback with the summarized updates", ->
|
||||||
|
@callback.calledWith(null, @summarizedUpdates).should.equal true
|
||||||
|
|
||||||
describe "fillUserInfo", ->
|
describe "fillUserInfo", ->
|
||||||
describe "with valid users", ->
|
describe "with valid users", ->
|
||||||
beforeEach (done) ->
|
beforeEach (done) ->
|
||||||
|
@ -330,5 +353,60 @@ describe "UpdatesManager", ->
|
||||||
op: "mock-op-2"
|
op: "mock-op-2"
|
||||||
}]
|
}]
|
||||||
|
|
||||||
|
describe "_buildUpdatesView", ->
|
||||||
|
beforeEach ->
|
||||||
|
@now = Date.now()
|
||||||
|
|
||||||
|
it "should concat updates that are close in time", ->
|
||||||
|
expect(@UpdatesManager._summarizeUpdates [{
|
||||||
|
meta:
|
||||||
|
user: @user_2 = { id: "mock-user-2" }
|
||||||
|
start_ts: @now + 20
|
||||||
|
end_ts: @now + 30
|
||||||
|
v: 5
|
||||||
|
}, {
|
||||||
|
meta:
|
||||||
|
user: @user_1 = { id: "mock-user-1" }
|
||||||
|
start_ts: @now
|
||||||
|
end_ts: @now + 10
|
||||||
|
v: 4
|
||||||
|
}]).to.deep.equal [{
|
||||||
|
meta:
|
||||||
|
users: [@user_1, @user_2]
|
||||||
|
start_ts: @now
|
||||||
|
end_ts: @now + 30
|
||||||
|
fromV: 4
|
||||||
|
toV: 5
|
||||||
|
}]
|
||||||
|
|
||||||
|
it "should leave updates that are far apart in time", ->
|
||||||
|
oneDay = 1000 * 60 * 60 * 24
|
||||||
|
expect(@UpdatesManager._summarizeUpdates [{
|
||||||
|
meta:
|
||||||
|
user: @user_2 = { id: "mock-user-2" }
|
||||||
|
start_ts: @now + oneDay
|
||||||
|
end_ts: @now + oneDay + 10
|
||||||
|
v: 5
|
||||||
|
}, {
|
||||||
|
meta:
|
||||||
|
user: @user_1 = { id: "mock-user-2" }
|
||||||
|
start_ts: @now
|
||||||
|
end_ts: @now + 10
|
||||||
|
v: 4
|
||||||
|
}]).to.deep.equal [{
|
||||||
|
meta:
|
||||||
|
users: [@user_2]
|
||||||
|
start_ts: @now + oneDay
|
||||||
|
end_ts: @now + oneDay + 10
|
||||||
|
fromV: 5
|
||||||
|
toV: 5
|
||||||
|
}, {
|
||||||
|
meta:
|
||||||
|
users: [@user_1]
|
||||||
|
start_ts: @now
|
||||||
|
end_ts: @now + 10
|
||||||
|
fromV: 4
|
||||||
|
toV: 4
|
||||||
|
}]
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue