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

81 lines
3.3 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"
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 = { version: false }, callback = (error, doc) ->) ->
2016-09-02 08:32:50 -04:00
MongoManager.findDoc project_id, doc_id, (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
2014-04-30 08:06:12 -04:00
getAllDocs: (project_id, callback = (error, docs) ->) ->
2015-06-02 14:55:22 -04:00
DocArchive.unArchiveAllDocs project_id, (error) ->
2015-06-01 18:36:26 -04:00
MongoManager.getProjectsDocs project_id, (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
updateDoc: (project_id, doc_id, lines, version, callback = (error, modified, rev) ->) ->
if !lines? or !version?
return callback(new Error("no lines or version provided"))
DocManager.getDoc project_id, doc_id, {version: 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)
2014-04-28 12:43:19 -04:00
isNewDoc = lines.length == 0
linesAreSame = _.isEqual(doc?.lines, lines)
versionsAreSame = (doc?.version == version)
if linesAreSame and versionsAreSame and !isNewDoc
logger.log project_id: project_id, doc_id: doc_id, rev: doc?.rev, "doc lines have not changed - not updating"
return callback null, false, doc?.rev
else
oldRev = doc?.rev || 0
logger.log {
project_id: project_id
doc_id: doc_id,
oldDocLines: doc?.lines
newDocLines: lines
rev: oldRev
2016-11-29 11:49:35 -05:00
oldVersion: doc?.version
newVersion: version
}, "updating doc lines"
MongoManager.upsertIntoDocCollection project_id, doc_id, {lines}, (error)->
return callback(callback) if error?
MongoManager.setDocVersion doc_id, version, (error) ->
return callback(error) if error?
callback null, true, oldRev + 1 # rev will have been incremented in mongo by MongoManager.updateDoc
2014-04-29 10:07:22 -04:00
deleteDoc: (project_id, doc_id, callback = (error) ->) ->
2016-11-30 10:29:43 -05:00
DocManager.getDoc project_id, doc_id, { version: false }, (error, doc) ->
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 !doc?
2016-12-02 10:02:54 -05:00
MongoManager.markDocAsDeleted project_id, doc_id, callback