ThreadManager.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 { ObjectId, getCollection } = require('../../mongodb')
  16. const logger = require('logger-sharelatex')
  17. const metrics = require('metrics-sharelatex')
  18. const roomsCollectionPromise = getCollection('rooms')
  19. module.exports = ThreadManager = {
  20. GLOBAL_THREAD: 'GLOBAL',
  21. findOrCreateThread(project_id, thread_id, callback) {
  22. let query, update
  23. if (callback == null) {
  24. callback = function (error, thread) {}
  25. }
  26. project_id = ObjectId(project_id.toString())
  27. if (thread_id !== ThreadManager.GLOBAL_THREAD) {
  28. thread_id = ObjectId(thread_id.toString())
  29. }
  30. if (thread_id === ThreadManager.GLOBAL_THREAD) {
  31. query = {
  32. project_id,
  33. thread_id: { $exists: false }
  34. }
  35. update = {
  36. project_id
  37. }
  38. } else {
  39. query = {
  40. project_id,
  41. thread_id
  42. }
  43. update = {
  44. project_id,
  45. thread_id
  46. }
  47. }
  48. roomsCollectionPromise.then((rooms) =>
  49. rooms.updateOne(query, { $set: update }, { upsert: true }, function (
  50. error
  51. ) {
  52. if (error != null) {
  53. return callback(error)
  54. }
  55. rooms.findOne(query, callback)
  56. })
  57. )
  58. },
  59. findAllThreadRooms(project_id, callback) {
  60. if (callback == null) {
  61. callback = function (error, rooms) {}
  62. }
  63. roomsCollectionPromise.then((rooms) =>
  64. rooms
  65. .find(
  66. {
  67. project_id: ObjectId(project_id.toString()),
  68. thread_id: { $exists: true }
  69. },
  70. {
  71. thread_id: 1,
  72. resolved: 1
  73. }
  74. )
  75. .toArray(callback)
  76. )
  77. },
  78. resolveThread(project_id, thread_id, user_id, callback) {
  79. if (callback == null) {
  80. callback = function (error) {}
  81. }
  82. roomsCollectionPromise.then((rooms) =>
  83. rooms.updateOne(
  84. {
  85. project_id: ObjectId(project_id.toString()),
  86. thread_id: ObjectId(thread_id.toString())
  87. },
  88. {
  89. $set: {
  90. resolved: {
  91. user_id,
  92. ts: new Date()
  93. }
  94. }
  95. },
  96. callback
  97. )
  98. )
  99. },
  100. reopenThread(project_id, thread_id, callback) {
  101. if (callback == null) {
  102. callback = function (error) {}
  103. }
  104. roomsCollectionPromise.then((rooms) =>
  105. rooms.updateOne(
  106. {
  107. project_id: ObjectId(project_id.toString()),
  108. thread_id: ObjectId(thread_id.toString())
  109. },
  110. {
  111. $unset: {
  112. resolved: true
  113. }
  114. },
  115. callback
  116. )
  117. )
  118. },
  119. deleteThread(project_id, thread_id, callback) {
  120. if (callback == null) {
  121. callback = function (error, room_id) {}
  122. }
  123. return this.findOrCreateThread(project_id, thread_id, function (
  124. error,
  125. room
  126. ) {
  127. if (error != null) {
  128. return callback(error)
  129. }
  130. roomsCollectionPromise.then((rooms) =>
  131. rooms.deleteOne(
  132. {
  133. _id: room._id
  134. },
  135. function (error) {
  136. if (error != null) {
  137. return callback(error)
  138. }
  139. return callback(null, room._id)
  140. }
  141. )
  142. )
  143. })
  144. }
  145. }
  146. ;[
  147. 'findOrCreateThread',
  148. 'findAllThreadRooms',
  149. 'resolveThread',
  150. 'reopenThread',
  151. 'deleteThread'
  152. ].map((method) =>
  153. metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)
  154. )