LabelsManager.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. user_id: new ObjectId(userId),
  52. created_at: createdAt,
  53. }
  54. db.projectHistoryLabels.insertOne(label, function (error, confirmation) {
  55. if (error) {
  56. return callback(OError.tag(error))
  57. }
  58. label._id = confirmation.insertedId
  59. callback(null, _formatLabel(label))
  60. })
  61. })
  62. })
  63. }
  64. export function deleteLabelForUser(projectId, userId, labelId, callback) {
  65. _toObjectId(
  66. projectId,
  67. userId,
  68. labelId,
  69. function (error, projectId, userId, labelId) {
  70. if (error) {
  71. return callback(OError.tag(error))
  72. }
  73. db.projectHistoryLabels.deleteOne(
  74. {
  75. _id: new ObjectId(labelId),
  76. project_id: new ObjectId(projectId),
  77. user_id: new ObjectId(userId),
  78. },
  79. callback
  80. )
  81. }
  82. )
  83. }
  84. export function deleteLabel(projectId, labelId, callback) {
  85. _toObjectId(projectId, labelId, function (error, projectId, labelId) {
  86. if (error) {
  87. return callback(OError.tag(error))
  88. }
  89. db.projectHistoryLabels.deleteOne(
  90. {
  91. _id: new ObjectId(labelId),
  92. project_id: new ObjectId(projectId),
  93. },
  94. callback
  95. )
  96. })
  97. }
  98. export function transferLabels(fromUserId, toUserId, callback) {
  99. _toObjectId(fromUserId, toUserId, function (error, fromUserId, toUserId) {
  100. if (error) {
  101. return callback(OError.tag(error))
  102. }
  103. db.projectHistoryLabels.updateMany(
  104. {
  105. user_id: fromUserId,
  106. },
  107. {
  108. $set: { user_id: toUserId },
  109. },
  110. callback
  111. )
  112. })
  113. }
  114. function _toObjectId(...args1) {
  115. const adjustedLength = Math.max(args1.length, 1)
  116. const args = args1.slice(0, adjustedLength - 1)
  117. const callback = args1[adjustedLength - 1]
  118. try {
  119. const ids = args.map(id => new ObjectId(id))
  120. callback(null, ...ids)
  121. } catch (error) {
  122. callback(error)
  123. }
  124. }
  125. function _formatLabel(label) {
  126. return {
  127. id: label._id,
  128. comment: label.comment,
  129. version: label.version,
  130. user_id: label.user_id,
  131. created_at: label.created_at,
  132. }
  133. }
  134. function _validateChunkExistsForVersion(projectId, version, callback) {
  135. UpdatesProcessor.processUpdatesForProject(projectId, function (error) {
  136. if (error) {
  137. return callback(error)
  138. }
  139. WebApiManager.getHistoryId(projectId, function (error, historyId) {
  140. if (error) {
  141. return callback(error)
  142. }
  143. HistoryStoreManager.getChunkAtVersion(
  144. projectId,
  145. historyId,
  146. version,
  147. function (error) {
  148. if (error) {
  149. return callback(error)
  150. }
  151. callback()
  152. }
  153. )
  154. })
  155. })
  156. }