ThreadManager.coffee 1.9 KB

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