ProjectGetter.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS202: Simplify dynamic range loops
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const { db } = require('../../infrastructure/mongodb')
  17. const { normalizeQuery } = require('../Helpers/Mongo')
  18. const OError = require('@overleaf/o-error')
  19. const metrics = require('@overleaf/metrics')
  20. const async = require('async')
  21. const { promisifyAll } = require('../../util/promises')
  22. const { Project } = require('../../models/Project')
  23. const logger = require('logger-sharelatex')
  24. const LockManager = require('../../infrastructure/LockManager')
  25. const { DeletedProject } = require('../../models/DeletedProject')
  26. const ProjectGetter = {
  27. EXCLUDE_DEPTH: 8,
  28. getProjectWithoutDocLines(project_id, callback) {
  29. if (callback == null) {
  30. callback = function(error, project) {}
  31. }
  32. const excludes = {}
  33. for (
  34. let i = 1, end = ProjectGetter.EXCLUDE_DEPTH, asc = end >= 1;
  35. asc ? i <= end : i >= end;
  36. asc ? i++ : i--
  37. ) {
  38. excludes[`rootFolder${Array(i).join('.folders')}.docs.lines`] = 0
  39. }
  40. return ProjectGetter.getProject(project_id, excludes, callback)
  41. },
  42. getProjectWithOnlyFolders(project_id, callback) {
  43. if (callback == null) {
  44. callback = function(error, project) {}
  45. }
  46. const excludes = {}
  47. for (
  48. let i = 1, end = ProjectGetter.EXCLUDE_DEPTH, asc = end >= 1;
  49. asc ? i <= end : i >= end;
  50. asc ? i++ : i--
  51. ) {
  52. excludes[`rootFolder${Array(i).join('.folders')}.docs`] = 0
  53. excludes[`rootFolder${Array(i).join('.folders')}.fileRefs`] = 0
  54. }
  55. return ProjectGetter.getProject(project_id, excludes, callback)
  56. },
  57. getProject(project_id, projection, callback) {
  58. if (typeof projection === 'function' && callback == null) {
  59. callback = projection
  60. projection = {}
  61. }
  62. if (project_id == null) {
  63. return callback(new Error('no project_id provided'))
  64. }
  65. if (typeof projection !== 'object') {
  66. return callback(new Error('projection is not an object'))
  67. }
  68. if (
  69. (projection != null ? projection.rootFolder : undefined) ||
  70. Object.keys(projection).length === 0
  71. ) {
  72. const ProjectEntityMongoUpdateHandler = require('./ProjectEntityMongoUpdateHandler')
  73. return LockManager.runWithLock(
  74. ProjectEntityMongoUpdateHandler.LOCK_NAMESPACE,
  75. project_id,
  76. cb => ProjectGetter.getProjectWithoutLock(project_id, projection, cb),
  77. callback
  78. )
  79. } else {
  80. return ProjectGetter.getProjectWithoutLock(
  81. project_id,
  82. projection,
  83. callback
  84. )
  85. }
  86. },
  87. getProjectWithoutLock(project_id, projection, callback) {
  88. if (typeof projection === 'function' && callback == null) {
  89. callback = projection
  90. projection = {}
  91. }
  92. if (project_id == null) {
  93. return callback(new Error('no project_id provided'))
  94. }
  95. if (typeof projection !== 'object') {
  96. return callback(new Error('projection is not an object'))
  97. }
  98. let query
  99. try {
  100. query = normalizeQuery(project_id)
  101. } catch (err) {
  102. return callback(err)
  103. }
  104. return db.projects.findOne(query, { projection }, function(err, project) {
  105. if (err != null) {
  106. OError.tag(err, 'error getting project', {
  107. query,
  108. projection
  109. })
  110. return callback(err)
  111. }
  112. return callback(null, project)
  113. })
  114. },
  115. getProjectIdByReadAndWriteToken(token, callback) {
  116. if (callback == null) {
  117. callback = function(err, project_id) {}
  118. }
  119. return Project.findOne(
  120. { 'tokens.readAndWrite': token },
  121. { _id: 1 },
  122. function(err, project) {
  123. if (err != null) {
  124. return callback(err)
  125. }
  126. if (project == null) {
  127. return callback()
  128. }
  129. return callback(null, project._id)
  130. }
  131. )
  132. },
  133. getProjectByV1Id(v1_id, callback) {
  134. if (callback == null) {
  135. callback = function(err, v1_id) {}
  136. }
  137. return Project.findOne({ 'overleaf.id': v1_id }, { _id: 1 }, function(
  138. err,
  139. project
  140. ) {
  141. if (err != null) {
  142. return callback(err)
  143. }
  144. if (project == null) {
  145. return callback()
  146. }
  147. return callback(null, project._id)
  148. })
  149. },
  150. findAllUsersProjects(user_id, fields, callback) {
  151. if (callback == null) {
  152. callback = function(error, projects) {
  153. if (projects == null) {
  154. projects = {
  155. owned: [],
  156. readAndWrite: [],
  157. readOnly: [],
  158. tokenReadAndWrite: [],
  159. tokenReadOnly: []
  160. }
  161. }
  162. }
  163. }
  164. const CollaboratorsGetter = require('../Collaborators/CollaboratorsGetter')
  165. return Project.find({ owner_ref: user_id }, fields, function(
  166. error,
  167. ownedProjects
  168. ) {
  169. if (error != null) {
  170. return callback(error)
  171. }
  172. return CollaboratorsGetter.getProjectsUserIsMemberOf(
  173. user_id,
  174. fields,
  175. function(error, projects) {
  176. if (error != null) {
  177. return callback(error)
  178. }
  179. const result = {
  180. owned: ownedProjects || [],
  181. readAndWrite: projects.readAndWrite || [],
  182. readOnly: projects.readOnly || [],
  183. tokenReadAndWrite: projects.tokenReadAndWrite || [],
  184. tokenReadOnly: projects.tokenReadOnly || []
  185. }
  186. return callback(null, result)
  187. }
  188. )
  189. })
  190. },
  191. getUsersDeletedProjects(user_id, callback) {
  192. DeletedProject.find(
  193. {
  194. 'deleterData.deletedProjectOwnerId': user_id
  195. },
  196. callback
  197. )
  198. }
  199. }
  200. ;['getProject', 'getProjectWithoutDocLines'].map(method =>
  201. metrics.timeAsyncMethod(ProjectGetter, method, 'mongo.ProjectGetter', logger)
  202. )
  203. ProjectGetter.promises = promisifyAll(ProjectGetter)
  204. module.exports = ProjectGetter