ThreadManager.coffee 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. mongojs = require("../../mongojs")
  2. db = mongojs.db
  3. ObjectId = mongojs.ObjectId
  4. logger = require('logger-sharelatex')
  5. metrics = require('metrics-sharelatex')
  6. module.exports = ThreadManager =
  7. GLOBAL_THREAD: "GLOBAL"
  8. findOrCreateThread: (project_id, thread_id, callback = (error, thread) ->) ->
  9. project_id = ObjectId(project_id.toString())
  10. if thread_id != ThreadManager.GLOBAL_THREAD
  11. thread_id = ObjectId(thread_id.toString())
  12. if thread_id == ThreadManager.GLOBAL_THREAD
  13. query = {
  14. project_id: project_id
  15. thread_id: { $exists: false }
  16. }
  17. update = {
  18. project_id: project_id
  19. }
  20. else
  21. query = {
  22. project_id: project_id
  23. thread_id: thread_id
  24. }
  25. update = {
  26. project_id: project_id
  27. thread_id: thread_id
  28. }
  29. db.rooms.update query, update, { upsert: true }, (error) ->
  30. return callback(error) if error?
  31. db.rooms.find query, (error, rooms = []) ->
  32. return callback(error) if error?
  33. return callback null, rooms[0]
  34. findAllThreadRooms: (project_id, callback = (error, rooms) ->) ->
  35. db.rooms.find {
  36. project_id: ObjectId(project_id.toString())
  37. thread_id: { $exists: true }
  38. }, {
  39. thread_id: 1,
  40. resolved: 1
  41. }, callback
  42. resolveThread: (project_id, thread_id, user_id, callback = (error) ->) ->
  43. db.rooms.update {
  44. project_id: ObjectId(project_id.toString())
  45. thread_id: ObjectId(thread_id.toString())
  46. }, {
  47. $set: {
  48. resolved: {
  49. user_id: user_id
  50. ts: new Date()
  51. }
  52. }
  53. }, callback
  54. reopenThread: (project_id, thread_id, callback = (error) ->) ->
  55. db.rooms.update {
  56. project_id: ObjectId(project_id.toString())
  57. thread_id: ObjectId(thread_id.toString())
  58. }, {
  59. $unset: {
  60. resolved: true
  61. }
  62. }, callback
  63. deleteThread: (project_id, thread_id, callback = (error, room_id) ->) ->
  64. @findOrCreateThread project_id, thread_id, (error, room) ->
  65. return callback(error) if error?
  66. db.rooms.remove {
  67. _id: room._id
  68. }, (error) ->
  69. return callback(error) if error?
  70. return callback null, room._id
  71. [
  72. 'findOrCreateThread',
  73. 'findAllThreadRooms',
  74. 'resolveThread',
  75. 'reopenThread',
  76. 'deleteThread',
  77. ].map (method) ->
  78. metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)