2019-04-11 08:25:03 -04:00
|
|
|
{db, ObjectId} = require "./mongojs"
|
|
|
|
|
|
|
|
module.exports = SnapshotManager =
|
2019-04-16 06:05:17 -04:00
|
|
|
recordSnapshot: (project_id, doc_id, version, pathname, lines, ranges, callback) ->
|
2019-04-11 08:25:03 -04:00
|
|
|
try
|
|
|
|
project_id = ObjectId(project_id)
|
|
|
|
doc_id = ObjectId(doc_id)
|
|
|
|
catch error
|
|
|
|
return callback(error)
|
|
|
|
db.docSnapshots.insert {
|
2019-04-16 06:05:17 -04:00
|
|
|
project_id, doc_id, version, lines, pathname,
|
2019-04-11 08:25:03 -04:00
|
|
|
ranges: SnapshotManager.jsonRangesToMongo(ranges),
|
|
|
|
ts: new Date()
|
|
|
|
}, callback
|
2019-04-16 06:05:17 -04:00
|
|
|
# Suggested indexes:
|
|
|
|
# db.docSnapshots.createIndex({project_id:1, doc_id:1})
|
|
|
|
# db.docSnapshots.createIndex({ts:1},{expiresAfterSeconds: 30*24*3600)) # expires after 30 days
|
2019-04-11 08:25:03 -04:00
|
|
|
|
|
|
|
jsonRangesToMongo: (ranges) ->
|
|
|
|
return null if !ranges?
|
|
|
|
|
|
|
|
updateMetadata = (metadata) ->
|
|
|
|
if metadata?.ts?
|
|
|
|
metadata.ts = new Date(metadata.ts)
|
|
|
|
if metadata?.user_id?
|
|
|
|
metadata.user_id = SnapshotManager._safeObjectId(metadata.user_id)
|
|
|
|
|
|
|
|
for change in ranges.changes or []
|
|
|
|
change.id = SnapshotManager._safeObjectId(change.id)
|
|
|
|
updateMetadata(change.metadata)
|
|
|
|
for comment in ranges.comments or []
|
|
|
|
comment.id = SnapshotManager._safeObjectId(comment.id)
|
|
|
|
if comment.op?.t?
|
|
|
|
comment.op.t = SnapshotManager._safeObjectId(comment.op.t)
|
|
|
|
updateMetadata(comment.metadata)
|
|
|
|
return ranges
|
|
|
|
|
|
|
|
_safeObjectId: (data) ->
|
|
|
|
try
|
|
|
|
return ObjectId(data)
|
|
|
|
catch error
|
|
|
|
return data
|