ThreadManager.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let ThreadManager
  15. const { db, ObjectId } = require('../../mongodb')
  16. const logger = require('logger-sharelatex')
  17. const metrics = require('@overleaf/metrics')
  18. module.exports = ThreadManager = {
  19. GLOBAL_THREAD: 'GLOBAL',
  20. findOrCreateThread(project_id, thread_id, callback) {
  21. let query, update
  22. if (callback == null) {
  23. callback = function (error, thread) {}
  24. }
  25. project_id = ObjectId(project_id.toString())
  26. if (thread_id !== ThreadManager.GLOBAL_THREAD) {
  27. thread_id = ObjectId(thread_id.toString())
  28. }
  29. if (thread_id === ThreadManager.GLOBAL_THREAD) {
  30. query = {
  31. project_id,
  32. thread_id: { $exists: false }
  33. }
  34. update = {
  35. project_id
  36. }
  37. } else {
  38. query = {
  39. project_id,
  40. thread_id
  41. }
  42. update = {
  43. project_id,
  44. thread_id
  45. }
  46. }
  47. db.rooms.updateOne(query, { $set: update }, { upsert: true }, function (
  48. error
  49. ) {
  50. if (error != null) {
  51. return callback(error)
  52. }
  53. db.rooms.findOne(query, callback)
  54. })
  55. },
  56. findAllThreadRooms(project_id, callback) {
  57. if (callback == null) {
  58. callback = function (error, rooms) {}
  59. }
  60. db.rooms
  61. .find(
  62. {
  63. project_id: ObjectId(project_id.toString()),
  64. thread_id: { $exists: true }
  65. },
  66. {
  67. thread_id: 1,
  68. resolved: 1
  69. }
  70. )
  71. .toArray(callback)
  72. },
  73. resolveThread(project_id, thread_id, user_id, callback) {
  74. if (callback == null) {
  75. callback = function (error) {}
  76. }
  77. db.rooms.updateOne(
  78. {
  79. project_id: ObjectId(project_id.toString()),
  80. thread_id: ObjectId(thread_id.toString())
  81. },
  82. {
  83. $set: {
  84. resolved: {
  85. user_id,
  86. ts: new Date()
  87. }
  88. }
  89. },
  90. callback
  91. )
  92. },
  93. reopenThread(project_id, thread_id, callback) {
  94. if (callback == null) {
  95. callback = function (error) {}
  96. }
  97. db.rooms.updateOne(
  98. {
  99. project_id: ObjectId(project_id.toString()),
  100. thread_id: ObjectId(thread_id.toString())
  101. },
  102. {
  103. $unset: {
  104. resolved: true
  105. }
  106. },
  107. callback
  108. )
  109. },
  110. deleteThread(project_id, thread_id, callback) {
  111. if (callback == null) {
  112. callback = function (error, room_id) {}
  113. }
  114. return this.findOrCreateThread(project_id, thread_id, function (
  115. error,
  116. room
  117. ) {
  118. if (error != null) {
  119. return callback(error)
  120. }
  121. db.rooms.deleteOne(
  122. {
  123. _id: room._id
  124. },
  125. function (error) {
  126. if (error != null) {
  127. return callback(error)
  128. }
  129. return callback(null, room._id)
  130. }
  131. )
  132. })
  133. }
  134. }
  135. ;[
  136. 'findOrCreateThread',
  137. 'findAllThreadRooms',
  138. 'resolveThread',
  139. 'reopenThread',
  140. 'deleteThread'
  141. ].map((method) =>
  142. metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)
  143. )