LabelsManager.js 4.4 KB

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