ThreadManager.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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) { callback = function(error, thread) {}; }
  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. return db.rooms.update(query, update, { upsert: true }, function(error) {
  48. if (error != null) { return callback(error); }
  49. return db.rooms.find(query, function(error, rooms) {
  50. if (rooms == null) { rooms = []; }
  51. if (error != null) { return callback(error); }
  52. return callback(null, rooms[0]);
  53. });
  54. });
  55. },
  56. findAllThreadRooms(project_id, callback) {
  57. if (callback == null) { callback = function(error, rooms) {}; }
  58. return db.rooms.find({
  59. project_id: ObjectId(project_id.toString()),
  60. thread_id: { $exists: true }
  61. }, {
  62. thread_id: 1,
  63. resolved: 1
  64. }, callback);
  65. },
  66. resolveThread(project_id, thread_id, user_id, callback) {
  67. if (callback == null) { callback = function(error) {}; }
  68. return db.rooms.update({
  69. project_id: ObjectId(project_id.toString()),
  70. thread_id: ObjectId(thread_id.toString())
  71. }, {
  72. $set: {
  73. resolved: {
  74. user_id,
  75. ts: new Date()
  76. }
  77. }
  78. }, callback);
  79. },
  80. reopenThread(project_id, thread_id, callback) {
  81. if (callback == null) { callback = function(error) {}; }
  82. return db.rooms.update({
  83. project_id: ObjectId(project_id.toString()),
  84. thread_id: ObjectId(thread_id.toString())
  85. }, {
  86. $unset: {
  87. resolved: true
  88. }
  89. }, callback);
  90. },
  91. deleteThread(project_id, thread_id, callback) {
  92. if (callback == null) { callback = function(error, room_id) {}; }
  93. return this.findOrCreateThread(project_id, thread_id, function(error, room) {
  94. if (error != null) { return callback(error); }
  95. return db.rooms.remove({
  96. _id: room._id
  97. }, function(error) {
  98. if (error != null) { return callback(error); }
  99. return callback(null, room._id);
  100. });
  101. });
  102. }
  103. });
  104. [
  105. 'findOrCreateThread',
  106. 'findAllThreadRooms',
  107. 'resolveThread',
  108. 'reopenThread',
  109. 'deleteThread',
  110. ].map(method => metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger));