LabelsManager.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import OError from '@overleaf/o-error'
  2. import { db, ObjectId } from './mongodb.js'
  3. import * as HistoryStoreManager from './HistoryStoreManager.js'
  4. import * as UpdatesProcessor from './UpdatesProcessor.js'
  5. import * as WebApiManager from './WebApiManager.js'
  6. export function getLabels(projectId, callback) {
  7. _toObjectId(projectId, function (error, projectId) {
  8. if (error) {
  9. return callback(OError.tag(error))
  10. }
  11. db.projectHistoryLabels
  12. .find({ project_id: new ObjectId(projectId) })
  13. .toArray(function (error, labels) {
  14. if (error) {
  15. return callback(OError.tag(error))
  16. }
  17. const formattedLabels = labels.map(_formatLabel)
  18. callback(null, formattedLabels)
  19. })
  20. })
  21. }
  22. export function createLabel(
  23. projectId,
  24. userId,
  25. version,
  26. comment,
  27. createdAt,
  28. shouldValidateExists,
  29. callback
  30. ) {
  31. const validateVersionExists = function (callback) {
  32. if (shouldValidateExists === false) {
  33. callback()
  34. } else {
  35. _validateChunkExistsForVersion(projectId.toString(), version, callback)
  36. }
  37. }
  38. _toObjectId(projectId, userId, function (error, projectId, userId) {
  39. if (error) {
  40. return callback(OError.tag(error))
  41. }
  42. validateVersionExists(function (error) {
  43. if (error) {
  44. return callback(OError.tag(error))
  45. }
  46. createdAt = createdAt != null ? new Date(createdAt) : new Date()
  47. const label = {
  48. project_id: new ObjectId(projectId),
  49. comment,
  50. version,
  51. created_at: createdAt,
  52. }
  53. if (userId) {
  54. label.user_id = userId
  55. }
  56. db.projectHistoryLabels.insertOne(label, function (error, confirmation) {
  57. if (error) {
  58. return callback(OError.tag(error))
  59. }
  60. label._id = confirmation.insertedId
  61. callback(null, _formatLabel(label))
  62. })
  63. })
  64. })
  65. }
  66. export function deleteLabelForUser(projectId, userId, labelId, callback) {
  67. _toObjectId(
  68. projectId,
  69. userId,
  70. labelId,
  71. function (error, projectId, userId, labelId) {
  72. if (error) {
  73. return callback(OError.tag(error))
  74. }
  75. db.projectHistoryLabels.deleteOne(
  76. {
  77. _id: new ObjectId(labelId),
  78. project_id: new ObjectId(projectId),
  79. user_id: new ObjectId(userId),
  80. },
  81. callback
  82. )
  83. }
  84. )
  85. }
  86. export function deleteLabel(projectId, labelId, callback) {
  87. _toObjectId(projectId, labelId, function (error, projectId, labelId) {
  88. if (error) {
  89. return callback(OError.tag(error))
  90. }
  91. db.projectHistoryLabels.deleteOne(
  92. {
  93. _id: new ObjectId(labelId),
  94. project_id: new ObjectId(projectId),
  95. },
  96. callback
  97. )
  98. })
  99. }
  100. export function transferLabels(fromUserId, toUserId, callback) {
  101. _toObjectId(fromUserId, toUserId, function (error, fromUserId, toUserId) {
  102. if (error) {
  103. return callback(OError.tag(error))
  104. }
  105. db.projectHistoryLabels.updateMany(
  106. {
  107. user_id: fromUserId,
  108. },
  109. {
  110. $set: { user_id: toUserId },
  111. },
  112. callback
  113. )
  114. })
  115. }
  116. function _toObjectId(...args1) {
  117. const adjustedLength = Math.max(args1.length, 1)
  118. const args = args1.slice(0, adjustedLength - 1)
  119. const callback = args1[adjustedLength - 1]
  120. try {
  121. const ids = args.map(id => {
  122. if (id) {
  123. return new ObjectId(id)
  124. } else {
  125. return undefined
  126. }
  127. })
  128. callback(null, ...ids)
  129. } catch (error) {
  130. callback(error)
  131. }
  132. }
  133. function _formatLabel(label) {
  134. return {
  135. id: label._id,
  136. comment: label.comment,
  137. version: label.version,
  138. user_id: label.user_id,
  139. created_at: label.created_at,
  140. }
  141. }
  142. function _validateChunkExistsForVersion(projectId, version, callback) {
  143. UpdatesProcessor.processUpdatesForProject(projectId, function (error) {
  144. if (error) {
  145. return callback(error)
  146. }
  147. WebApiManager.getHistoryId(projectId, function (error, historyId) {
  148. if (error) {
  149. return callback(error)
  150. }
  151. HistoryStoreManager.getChunkAtVersion(
  152. projectId,
  153. historyId,
  154. version,
  155. function (error) {
  156. if (error) {
  157. return callback(error)
  158. }
  159. callback()
  160. }
  161. )
  162. })
  163. })
  164. }