ThreadManager.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(
  48. query,
  49. { $set: update },
  50. { upsert: true },
  51. function (error) {
  52. if (error != null) {
  53. return callback(error)
  54. }
  55. db.rooms.findOne(query, callback)
  56. }
  57. )
  58. },
  59. findAllThreadRooms(project_id, callback) {
  60. if (callback == null) {
  61. callback = function (error, rooms) {}
  62. }
  63. db.rooms
  64. .find(
  65. {
  66. project_id: ObjectId(project_id.toString()),
  67. thread_id: { $exists: true },
  68. },
  69. {
  70. thread_id: 1,
  71. resolved: 1,
  72. }
  73. )
  74. .toArray(callback)
  75. },
  76. resolveThread(project_id, thread_id, user_id, callback) {
  77. if (callback == null) {
  78. callback = function (error) {}
  79. }
  80. db.rooms.updateOne(
  81. {
  82. project_id: ObjectId(project_id.toString()),
  83. thread_id: ObjectId(thread_id.toString()),
  84. },
  85. {
  86. $set: {
  87. resolved: {
  88. user_id,
  89. ts: new Date(),
  90. },
  91. },
  92. },
  93. callback
  94. )
  95. },
  96. reopenThread(project_id, thread_id, callback) {
  97. if (callback == null) {
  98. callback = function (error) {}
  99. }
  100. db.rooms.updateOne(
  101. {
  102. project_id: ObjectId(project_id.toString()),
  103. thread_id: ObjectId(thread_id.toString()),
  104. },
  105. {
  106. $unset: {
  107. resolved: true,
  108. },
  109. },
  110. callback
  111. )
  112. },
  113. deleteThread(project_id, thread_id, callback) {
  114. if (callback == null) {
  115. callback = function (error, room_id) {}
  116. }
  117. return this.findOrCreateThread(
  118. project_id,
  119. thread_id,
  120. function (error, room) {
  121. if (error != null) {
  122. return callback(error)
  123. }
  124. db.rooms.deleteOne(
  125. {
  126. _id: room._id,
  127. },
  128. function (error) {
  129. if (error != null) {
  130. return callback(error)
  131. }
  132. return callback(null, room._id)
  133. }
  134. )
  135. }
  136. )
  137. },
  138. }
  139. ;[
  140. 'findOrCreateThread',
  141. 'findAllThreadRooms',
  142. 'resolveThread',
  143. 'reopenThread',
  144. 'deleteThread',
  145. ].map(method =>
  146. metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)
  147. )