ThreadManager.js 3.5 KB

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