ThreadManager.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS207: Consider shorter variations of null checks
  5. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  6. */
  7. let ThreadManager;
  8. const mongojs = require("../../mongojs");
  9. const { db } = mongojs;
  10. const { ObjectId } = mongojs;
  11. const logger = require('logger-sharelatex');
  12. const metrics = require('metrics-sharelatex');
  13. module.exports = (ThreadManager = {
  14. GLOBAL_THREAD: "GLOBAL",
  15. findOrCreateThread(project_id, thread_id, callback) {
  16. let query, update;
  17. if (callback == null) { callback = function(error, thread) {}; }
  18. project_id = ObjectId(project_id.toString());
  19. if (thread_id !== ThreadManager.GLOBAL_THREAD) {
  20. thread_id = ObjectId(thread_id.toString());
  21. }
  22. if (thread_id === ThreadManager.GLOBAL_THREAD) {
  23. query = {
  24. project_id,
  25. thread_id: { $exists: false }
  26. };
  27. update = {
  28. project_id
  29. };
  30. } else {
  31. query = {
  32. project_id,
  33. thread_id
  34. };
  35. update = {
  36. project_id,
  37. thread_id
  38. };
  39. }
  40. return db.rooms.update(query, update, { upsert: true }, function(error) {
  41. if (error != null) { return callback(error); }
  42. return db.rooms.find(query, function(error, rooms) {
  43. if (rooms == null) { rooms = []; }
  44. if (error != null) { return callback(error); }
  45. return callback(null, rooms[0]);
  46. });
  47. });
  48. },
  49. findAllThreadRooms(project_id, callback) {
  50. if (callback == null) { callback = function(error, rooms) {}; }
  51. return db.rooms.find({
  52. project_id: ObjectId(project_id.toString()),
  53. thread_id: { $exists: true }
  54. }, {
  55. thread_id: 1,
  56. resolved: 1
  57. }, callback);
  58. },
  59. resolveThread(project_id, thread_id, user_id, callback) {
  60. if (callback == null) { callback = function(error) {}; }
  61. return db.rooms.update({
  62. project_id: ObjectId(project_id.toString()),
  63. thread_id: ObjectId(thread_id.toString())
  64. }, {
  65. $set: {
  66. resolved: {
  67. user_id,
  68. ts: new Date()
  69. }
  70. }
  71. }, callback);
  72. },
  73. reopenThread(project_id, thread_id, callback) {
  74. if (callback == null) { callback = function(error) {}; }
  75. return db.rooms.update({
  76. project_id: ObjectId(project_id.toString()),
  77. thread_id: ObjectId(thread_id.toString())
  78. }, {
  79. $unset: {
  80. resolved: true
  81. }
  82. }, callback);
  83. },
  84. deleteThread(project_id, thread_id, callback) {
  85. if (callback == null) { callback = function(error, room_id) {}; }
  86. return this.findOrCreateThread(project_id, thread_id, function(error, room) {
  87. if (error != null) { return callback(error); }
  88. return db.rooms.remove({
  89. _id: room._id
  90. }, function(error) {
  91. if (error != null) { return callback(error); }
  92. return callback(null, room._id);
  93. });
  94. });
  95. }
  96. });
  97. [
  98. 'findOrCreateThread',
  99. 'findAllThreadRooms',
  100. 'resolveThread',
  101. 'reopenThread',
  102. 'deleteThread',
  103. ].map(method => metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger));