Project.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import mongoose from '../infrastructure/Mongoose.mjs'
  2. import _ from 'lodash'
  3. import settings from '@overleaf/settings'
  4. import { FolderSchema } from './Folder.mjs'
  5. import Errors from '../Features/Errors/Errors.js'
  6. const ConcreteObjectId = mongoose.Types.ObjectId
  7. const { Schema } = mongoose
  8. const { ObjectId } = Schema
  9. const DeletedDocSchema = new Schema({
  10. name: String,
  11. deletedAt: { type: Date },
  12. })
  13. export const ProjectSchema = new Schema(
  14. {
  15. name: { type: String, default: 'new project' },
  16. lastUpdated: {
  17. type: Date,
  18. default() {
  19. return new Date()
  20. },
  21. },
  22. lastUpdatedBy: { type: ObjectId, ref: 'User' },
  23. lastOpened: { type: Date },
  24. active: { type: Boolean, default: true },
  25. readOnly: { type: Boolean, default: false },
  26. owner_ref: { type: ObjectId, ref: 'User' },
  27. collaberator_refs: [{ type: ObjectId, ref: 'User' }],
  28. reviewer_refs: [{ type: ObjectId, ref: 'User' }],
  29. readOnly_refs: [{ type: ObjectId, ref: 'User' }],
  30. pendingEditor_refs: [{ type: ObjectId, ref: 'User' }],
  31. pendingReviewer_refs: [{ type: ObjectId, ref: 'User' }],
  32. rootDoc_id: { type: ObjectId },
  33. rootFolder: [FolderSchema],
  34. mainBibliographyDoc_id: { type: ObjectId },
  35. version: { type: Number }, // incremented for every change in the project structure (folders and filenames)
  36. publicAccesLevel: { type: String, default: 'private' },
  37. compiler: { type: String, default: settings.defaultLatexCompiler },
  38. spellCheckLanguage: { type: String, default: 'en' },
  39. deletedByExternalDataSource: { type: Boolean, default: false },
  40. description: { type: String, default: '' },
  41. archived: { type: Schema.Types.Mixed },
  42. trashed: [{ type: ObjectId, ref: 'User' }],
  43. deletedDocs: [DeletedDocSchema],
  44. imageName: { type: String },
  45. brandVariationId: { type: String },
  46. track_changes: { type: Object },
  47. tokens: {
  48. readOnly: {
  49. type: String,
  50. index: {
  51. unique: true,
  52. partialFilterExpression: { 'tokens.readOnly': { $exists: true } },
  53. },
  54. },
  55. readAndWrite: {
  56. type: String,
  57. index: {
  58. unique: true,
  59. partialFilterExpression: { 'tokens.readAndWrite': { $exists: true } },
  60. },
  61. },
  62. readAndWritePrefix: {
  63. type: String,
  64. index: {
  65. unique: true,
  66. partialFilterExpression: {
  67. 'tokens.readAndWritePrefix': { $exists: true },
  68. },
  69. },
  70. },
  71. },
  72. tokenAccessReadOnly_refs: [{ type: ObjectId, ref: 'User' }],
  73. tokenAccessReadAndWrite_refs: [{ type: ObjectId, ref: 'User' }],
  74. fromV1TemplateId: { type: Number },
  75. fromV1TemplateVersionId: { type: Number },
  76. overleaf: {
  77. id: { type: Number },
  78. imported_at_ver_id: { type: Number },
  79. token: { type: String },
  80. read_token: { type: String },
  81. history: {
  82. id: { type: Schema.Types.Mixed },
  83. display: { type: Boolean },
  84. upgradedAt: { type: Date },
  85. allowDowngrade: { type: Boolean },
  86. zipFileArchivedInProject: { type: Boolean },
  87. rangesSupportEnabled: { type: Boolean },
  88. otMigrationStage: { type: Number },
  89. lastResyncedAt: { type: Date },
  90. },
  91. isDebugCopyOf: { type: ObjectId },
  92. },
  93. collabratecUsers: [
  94. {
  95. user_id: { type: ObjectId, ref: 'User' },
  96. collabratec_document_id: { type: String },
  97. collabratec_privategroup_id: { type: String },
  98. added_at: {
  99. type: Date,
  100. default() {
  101. return new Date()
  102. },
  103. },
  104. },
  105. ],
  106. deferredTpdsFlushCounter: { type: Number },
  107. },
  108. { minimize: false }
  109. )
  110. ProjectSchema.statics.getProject = function (projectOrId, fields, callback) {
  111. if (projectOrId._id != null) {
  112. callback(null, projectOrId)
  113. } else {
  114. try {
  115. // eslint-disable-next-line no-new
  116. new ConcreteObjectId(projectOrId.toString())
  117. } catch (e) {
  118. return callback(new Errors.NotFoundError(e.message))
  119. }
  120. this.findById(projectOrId, fields, callback)
  121. }
  122. }
  123. function applyToAllFilesRecursivly(folder, fun) {
  124. _.forEach(folder.fileRefs, file => fun(file))
  125. _.forEach(folder.folders, folder => applyToAllFilesRecursivly(folder, fun))
  126. }
  127. ProjectSchema.statics.applyToAllFilesRecursivly = applyToAllFilesRecursivly
  128. export const Project = mongoose.model('Project', ProjectSchema)