2014-04-28 11:45:59 -04:00
|
|
|
MongoManager = require "./MongoManager"
|
2014-04-28 12:43:19 -04:00
|
|
|
Errors = require "./Errors"
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
_ = require "underscore"
|
2014-04-28 11:45:59 -04:00
|
|
|
|
|
|
|
module.exports = DocManager =
|
2014-04-28 12:43:19 -04:00
|
|
|
getDoc: (project_id, doc_id, callback = (error, doc, mongoPath) ->) ->
|
2014-04-28 11:45:59 -04:00
|
|
|
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) ->
|
2014-04-28 11:45:59 -04:00
|
|
|
return callback(error) if error?
|
2014-04-28 12:43:19 -04:00
|
|
|
return callback new Errors.NotFoundError("No such doc: #{project_id}") if !doc?
|
|
|
|
return callback null, doc, mongoPath
|
|
|
|
|
2014-04-29 06:49:09 -04:00
|
|
|
updateDoc: (project_id, doc_id, lines, callback = (error, modified) ->) ->
|
2014-04-28 12:43:19 -04:00
|
|
|
DocManager.getDoc project_id, doc_id, (error, doc, mongoPath) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback new Errors.NotFoundError("No such project/doc: #{project_id}/#{doc_id}") if !doc?
|
|
|
|
|
|
|
|
if _.isEqual(doc.lines, lines)
|
|
|
|
logger.log {
|
|
|
|
project_id: project_id, doc_id: doc_id, rev: doc.rev
|
|
|
|
}, "doc lines have not changed"
|
2014-04-29 06:49:09 -04:00
|
|
|
return callback null, false
|
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
|
|
|
|
}, "updating doc lines"
|
|
|
|
MongoManager.updateDoc project_id, mongoPath, lines, (error) ->
|
|
|
|
return callback(error) if error?
|
2014-04-29 06:49:09 -04:00
|
|
|
callback null, true
|
2014-04-28 11:45:59 -04:00
|
|
|
|
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.insertDoc project_id, doc_id, { lines: doc.lines, deleted: true }, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback()
|
|
|
|
|
2014-04-28 11:45:59 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
_findDocInFolder: (folder, doc_id, currentPath) ->
|
|
|
|
for doc, i in folder.docs or []
|
|
|
|
if 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?
|
|
|
|
|
|
|
|
return null
|