Project.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const mongoose = require('mongoose')
  15. const Settings = require('settings-sharelatex')
  16. const _ = require('underscore')
  17. const { FolderSchema } = require('./Folder.js')
  18. const logger = require('logger-sharelatex')
  19. const concreteObjectId = require('mongoose').Types.ObjectId
  20. const Errors = require('../Features/Errors/Errors')
  21. const { Schema } = mongoose
  22. const { ObjectId } = Schema
  23. const DeletedDocSchema = new Schema({
  24. name: String,
  25. deletedAt: { type: Date }
  26. })
  27. const DeletedFileSchema = new Schema({
  28. name: String,
  29. created: {
  30. type: Date
  31. },
  32. linkedFileData: { type: Schema.Types.Mixed },
  33. hash: {
  34. type: String
  35. },
  36. deletedAt: { type: Date }
  37. })
  38. const ProjectSchema = new Schema({
  39. name: { type: String, default: 'new project' },
  40. lastUpdated: {
  41. type: Date,
  42. default() {
  43. return new Date()
  44. }
  45. },
  46. lastUpdatedBy: { type: ObjectId, ref: 'User' },
  47. lastOpened: { type: Date },
  48. active: { type: Boolean, default: true },
  49. owner_ref: { type: ObjectId, ref: 'User' },
  50. collaberator_refs: [{ type: ObjectId, ref: 'User' }],
  51. readOnly_refs: [{ type: ObjectId, ref: 'User' }],
  52. rootDoc_id: { type: ObjectId },
  53. rootFolder: [FolderSchema],
  54. version: { type: Number }, // incremented for every change in the project structure (folders and filenames)
  55. publicAccesLevel: { type: String, default: 'private' },
  56. compiler: { type: String, default: 'pdflatex' },
  57. spellCheckLanguage: { type: String, default: 'en' },
  58. deletedByExternalDataSource: { type: Boolean, default: false },
  59. description: { type: String, default: '' },
  60. archived: { type: Boolean },
  61. deletedDocs: [DeletedDocSchema],
  62. deletedFiles: [DeletedFileSchema],
  63. imageName: { type: String },
  64. brandVariationId: { type: String },
  65. track_changes: { type: Object },
  66. tokens: {
  67. readOnly: {
  68. type: String,
  69. index: {
  70. unique: true,
  71. partialFilterExpression: { 'tokens.readOnly': { $exists: true } }
  72. }
  73. },
  74. readAndWrite: {
  75. type: String,
  76. index: {
  77. unique: true,
  78. partialFilterExpression: { 'tokens.readAndWrite': { $exists: true } }
  79. }
  80. },
  81. readAndWritePrefix: {
  82. type: String,
  83. index: {
  84. unique: true,
  85. partialFilterExpression: {
  86. 'tokens.readAndWritePrefix': { $exists: true }
  87. }
  88. }
  89. }
  90. },
  91. tokenAccessReadOnly_refs: [{ type: ObjectId, ref: 'User' }],
  92. tokenAccessReadAndWrite_refs: [{ type: ObjectId, ref: 'User' }],
  93. fromV1TemplateId: { type: Number },
  94. fromV1TemplateVersionId: { type: Number },
  95. overleaf: {
  96. id: { type: Number },
  97. imported_at_ver_id: { type: Number },
  98. token: { type: String },
  99. read_token: { type: String },
  100. history: {
  101. id: { type: Number },
  102. display: { type: Boolean },
  103. upgradedAt: { type: Date }
  104. }
  105. },
  106. collabratecUsers: [
  107. {
  108. user_id: { type: ObjectId, ref: 'User' },
  109. collabratec_document_id: { type: String },
  110. collabratec_privategroup_id: { type: String },
  111. added_at: {
  112. type: Date,
  113. default() {
  114. return new Date()
  115. }
  116. }
  117. }
  118. ]
  119. })
  120. ProjectSchema.statics.getProject = function(project_or_id, fields, callback) {
  121. if (project_or_id._id != null) {
  122. return callback(null, project_or_id)
  123. } else {
  124. try {
  125. concreteObjectId(project_or_id.toString())
  126. } catch (e) {
  127. return callback(new Errors.NotFoundError(e.message))
  128. }
  129. return this.findById(project_or_id, fields, callback)
  130. }
  131. }
  132. var applyToAllFilesRecursivly = (ProjectSchema.statics.applyToAllFilesRecursivly = function(
  133. folder,
  134. fun
  135. ) {
  136. _.each(folder.fileRefs, file => fun(file))
  137. return _.each(folder.folders, folder =>
  138. applyToAllFilesRecursivly(folder, fun)
  139. )
  140. })
  141. const conn = mongoose.createConnection(Settings.mongo.url, {
  142. server: { poolSize: Settings.mongo.poolSize || 10 },
  143. config: { autoIndex: false }
  144. })
  145. const Project = conn.model('Project', ProjectSchema)
  146. mongoose.model('Project', ProjectSchema)
  147. exports.Project = Project
  148. exports.ProjectSchema = ProjectSchema