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

105 lines
3.9 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"
async = require "async"
module.exports = DocManager =
getDoc: (project_id, doc_id, options, callback = (error, doc, mongoPath) ->) ->
if typeof(options) == "function"
callback = options
options.include_deleted = false
MongoManager.findProject project_id, (error, project) ->
return callback(error) if error?
2014-04-28 12:43:19 -04:00
return callback new Errors.NotFoundError("No such project: #{project_id}") if !project?
DocManager.findDocInProject project, doc_id, (error, doc, mongoPath) ->
return callback(error) if error?
2014-06-05 08:29:50 -04:00
if doc?
return callback null, doc, mongoPath
else
if options.include_deleted
MongoManager.findDoc doc_id, (error, doc) ->
return callback(error) if error?
return callback new Errors.NotFoundError("No such doc: #{project_id}") if !doc?
return callback null, doc
else
return callback new Errors.NotFoundError("No such doc: #{project_id}")
2014-04-28 12:43:19 -04:00
2014-04-30 08:06:12 -04:00
getAllDocs: (project_id, callback = (error, docs) ->) ->
MongoManager.findProject project_id, (error, project) ->
return callback(error) if error?
return callback new Errors.NotFoundError("No such project: #{project_id}") if !project?
DocManager.findAllDocsInProject project, (error, docs) ->
return callback(error) if error?
return callback null, docs
2014-05-16 08:06:35 -04:00
updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) ->
2014-04-28 12:43:19 -04:00
DocManager.getDoc project_id, doc_id, (error, doc, mongoPath) ->
return callback(error) if error?
2014-06-05 08:29:50 -04:00
return callback new Errors.NotFoundError("No such project/doc: #{project_id}/#{doc_id}") if !doc? or !mongoPath?
2014-04-28 12:43:19 -04:00
2014-05-16 08:06:35 -04:00
if _.isEqual(doc.lines, lines)
2014-04-28 12:43:19 -04:00
logger.log {
2014-05-16 08:06:35 -04:00
project_id: project_id, doc_id: doc_id, rev: doc.rev
}, "doc lines have not changed"
return callback null, false, doc.rev
2014-04-28 12:43:19 -04:00
else
logger.log {
project_id: project_id
doc_id: doc_id,
oldDocLines: doc.lines
newDocLines: lines
rev: doc.rev
2014-04-28 12:43:19 -04:00
}, "updating doc lines"
async.series [
(cb)->
# project collection is still primary so that needs to be successful first
MongoManager.updateDoc project_id, mongoPath, lines, cb
(cb)->
MongoManager.upsertIntoDocCollection project_id, doc_id, lines, doc.rev, cb
], (error)->
2014-04-28 12:43:19 -04:00
return callback(error) if error?
callback null, true, doc.rev + 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) ->) ->
DocManager.getDoc project_id, doc_id, (error, doc) ->
return callback(error) if error?
return callback new Errors.NotFoundError("No such project/doc: #{project_id}/#{doc_id}") if !doc?
MongoManager.upsertIntoDocCollection project_id, doc_id, doc.lines, doc.rev, (error) ->
2015-01-21 07:31:06 -05:00
return callback(error) if error?
MongoManager.markDocAsDeleted doc_id, (error) ->
return callback(error) if error?
callback()
2014-04-29 10:07:22 -04:00
2014-04-30 08:06:12 -04:00
findAllDocsInProject: (project, callback = (error, docs) ->) ->
callback null, @_findAllDocsInFolder project.rootFolder[0]
findDocInProject: (project, doc_id, callback = (error, doc, mongoPath) ->) ->
result = @_findDocInFolder project.rootFolder[0], doc_id, "rootFolder.0"
if result?
callback null, result.doc, result.mongoPath
else
callback null, null, null
2014-05-29 09:21:46 -04:00
_findDocInFolder: (folder = {}, doc_id, currentPath) ->
for doc, i in folder.docs or []
2014-06-05 05:20:56 -04:00
if doc?._id? and doc._id.toString() == doc_id.toString()
return {
doc: doc
mongoPath: "#{currentPath}.docs.#{i}"
}
for childFolder, i in folder.folders or []
result = @_findDocInFolder childFolder, doc_id, "#{currentPath}.folders.#{i}"
return result if result?
2014-04-30 08:06:12 -04:00
return null
2014-05-29 09:21:46 -04:00
_findAllDocsInFolder: (folder = {}) ->
2014-04-30 08:06:12 -04:00
docs = folder.docs or []
for childFolder in folder.folders or []
docs = docs.concat @_findAllDocsInFolder childFolder
return docs