LabelsManager.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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: new 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: new ObjectId(projectId),
  63. comment,
  64. version,
  65. user_id: new 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 deleteLabelForUser(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: new ObjectId(labelId),
  90. project_id: new ObjectId(projectId),
  91. user_id: new ObjectId(userId),
  92. },
  93. callback
  94. )
  95. }
  96. )
  97. }
  98. export function deleteLabel(projectId, labelId, callback) {
  99. return _toObjectId(projectId, labelId, function (error, projectId, labelId) {
  100. if (error != null) {
  101. return callback(OError.tag(error))
  102. }
  103. return db.projectHistoryLabels.deleteOne(
  104. {
  105. _id: new ObjectId(labelId),
  106. project_id: new ObjectId(projectId),
  107. },
  108. callback
  109. )
  110. })
  111. }
  112. export function transferLabels(fromUserId, toUserId, callback) {
  113. return _toObjectId(
  114. fromUserId,
  115. toUserId,
  116. function (error, fromUserId, toUserId) {
  117. if (error != null) {
  118. return callback(OError.tag(error))
  119. }
  120. return db.projectHistoryLabels.updateMany(
  121. {
  122. user_id: fromUserId,
  123. },
  124. {
  125. $set: { user_id: toUserId },
  126. },
  127. callback
  128. )
  129. }
  130. )
  131. }
  132. function _toObjectId(...args1) {
  133. const adjustedLength = Math.max(args1.length, 1)
  134. const args = args1.slice(0, adjustedLength - 1)
  135. const callback = args1[adjustedLength - 1]
  136. try {
  137. const ids = args.map(id => new ObjectId(id))
  138. return callback(null, ...Array.from(ids))
  139. } catch (error) {
  140. return callback(error)
  141. }
  142. }
  143. function _formatLabel(label) {
  144. return {
  145. id: label._id,
  146. comment: label.comment,
  147. version: label.version,
  148. user_id: label.user_id,
  149. created_at: label.created_at,
  150. }
  151. }
  152. function _validateChunkExistsForVersion(projectId, version, callback) {
  153. return UpdatesProcessor.processUpdatesForProject(projectId, function (error) {
  154. if (error != null) {
  155. return callback(error)
  156. }
  157. return WebApiManager.getHistoryId(projectId, function (error, historyId) {
  158. if (error != null) {
  159. return callback(error)
  160. }
  161. return HistoryStoreManager.getChunkAtVersion(
  162. projectId,
  163. historyId,
  164. version,
  165. function (error) {
  166. if (error != null) {
  167. return callback(error)
  168. }
  169. return callback()
  170. }
  171. )
  172. })
  173. })
  174. }