ThreadManager.js 3.6 KB

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