overleaf/services/docstore/app/coffee/DocManager.coffee

131 lines
4.8 KiB
CoffeeScript
Raw Normal View History

MongoManager = require "./MongoManager"
2014-04-28 12:43:19 -04:00
Errors = require "./Errors"
logger = require "logger-sharelatex"
_ = require "underscore"
2015-06-02 15:29:32 -04:00
DocArchive = require "./DocArchiveManager"
2016-12-05 09:21:49 -05:00
RangeManager = require "./RangeManager"
module.exports = DocManager =
# TODO: For historical reasons, the doc version is currently stored in the docOps
# collection (which is all that this collection contains). In future, we should
# migrate this version property to be part of the docs collection, to guarantee
# consitency between lines and version when writing/reading, and for a simpler schema.
_getDoc: (project_id, doc_id, filter = {}, callback = (error, doc) ->) ->
if filter.inS3 != true
return callback("must include inS3 when getting doc")
MongoManager.findDoc project_id, doc_id, filter, (err, doc)->
if err?
return callback(err)
else if !doc?
return callback new Errors.NotFoundError("No such doc: #{doc_id} in project #{project_id}")
else if doc?.inS3
2015-06-02 14:55:22 -04:00
DocArchive.unarchiveDoc project_id, doc_id, (err)->
if err?
2015-08-13 08:59:12 -04:00
logger.err err:err, project_id:project_id, doc_id:doc_id, "error unarchiving doc"
return callback(err)
DocManager._getDoc project_id, doc_id, filter, callback
else
if filter.version
MongoManager.getDocVersion doc_id, (error, version) ->
return callback(error) if error?
doc.version = version
callback err, doc
else
callback err, doc
2014-04-28 12:43:19 -04:00
checkDocExists: (project_id, doc_id, callback = (err, exists)->)->
DocManager._getDoc project_id, doc_id, {_id:1, inS3:true}, (err, doc)->
if err?
return callback(err)
callback(err, doc?)
getFullDoc: (project_id, doc_id, callback = (err, doc)->)->
DocManager._getDoc project_id, doc_id, {lines: true, rev: true, deleted: true, version: true, ranges: true, inS3:true}, (err, doc)->
if err?
return callback(err)
callback(err, doc)
getDocLines: (project_id, doc_id, callback = (err, doc)->)->
DocManager._getDoc project_id, doc_id, {lines:true, inS3:true}, (err, doc)->
if err?
return callback(err)
callback(err, doc)
2017-06-27 11:58:20 -04:00
getAllNonDeletedDocs: (project_id, filter, callback = (error, docs) ->) ->
DocArchive.unArchiveAllDocs project_id, (error) ->
if error?
return callback(error)
MongoManager.getProjectsDocs project_id, {include_deleted: false}, filter, (error, docs) ->
if err?
return callback(error)
else if !docs?
return callback new Errors.NotFoundError("No docs for project #{project_id}")
else
return callback(null, docs)
2014-04-30 08:06:12 -04:00
2016-12-05 09:21:49 -05:00
updateDoc: (project_id, doc_id, lines, version, ranges, callback = (error, modified, rev) ->) ->
if !lines? or !version? or !ranges?
return callback(new Error("no lines, version or ranges provided"))
DocManager._getDoc project_id, doc_id, {version: true, rev: true, lines: true, version: true, ranges: true, inS3:true}, (err, doc)->
2015-06-02 14:13:28 -04:00
if err? and !(err instanceof Errors.NotFoundError)
logger.err project_id: project_id, doc_id: doc_id, err:err, "error getting document for update"
return callback(err)
2016-12-05 09:21:49 -05:00
ranges = RangeManager.jsonRangesToMongo(ranges)
2014-04-28 12:43:19 -04:00
2016-12-05 09:21:49 -05:00
if !doc?
# If the document doesn't exist, we'll make sure to create/update all parts of it.
updateLines = true
updateVersion = true
updateRanges = true
else
2016-12-05 09:21:49 -05:00
updateLines = not _.isEqual(doc.lines, lines)
updateVersion = (doc.version != version)
updateRanges = RangeManager.shouldUpdateRanges(doc.ranges, ranges)
modified = false
rev = doc?.rev || 0
updateLinesAndRangesIfNeeded = (cb) ->
if updateLines or updateRanges
update = {}
if updateLines
update.lines = lines
if updateRanges
update.ranges = ranges
2017-09-20 03:52:28 -04:00
logger.log { project_id, doc_id }, "updating doc lines and ranges"
2016-12-05 09:21:49 -05:00
modified = true
rev += 1 # rev will be incremented in mongo by MongoManager.upsertIntoDocCollection
MongoManager.upsertIntoDocCollection project_id, doc_id, update, cb
else
logger.log { project_id, doc_id, }, "doc lines have not changed - not updating"
cb()
updateVersionIfNeeded = (cb) ->
if updateVersion
logger.log { project_id, doc_id, oldVersion: doc?.version, newVersion: version }, "updating doc version"
modified = true
MongoManager.setDocVersion doc_id, version, cb
else
logger.log { project_id, doc_id, version }, "doc version has not changed - not updating"
cb()
updateLinesAndRangesIfNeeded (error) ->
return callback(error) if error?
updateVersionIfNeeded (error) ->
return callback(error) if error?
2016-12-05 09:21:49 -05:00
callback null, modified, rev
2014-04-29 10:07:22 -04:00
deleteDoc: (project_id, doc_id, callback = (error) ->) ->
DocManager.checkDocExists project_id, doc_id, (error, exists) ->
2014-04-29 10:07:22 -04:00
return callback(error) if error?
return callback new Errors.NotFoundError("No such project/doc to delete: #{project_id}/#{doc_id}") if !exists
2016-12-02 10:02:54 -05:00
MongoManager.markDocAsDeleted project_id, doc_id, callback
2016-12-05 09:21:49 -05:00