ThreadManager.coffee 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. query =
  8. project_id: ObjectId(project_id.toString())
  9. if thread_id? and thread_id != ThreadManager.GLOBAL_THREAD
  10. query.thread_id = ObjectId(thread_id.toString())
  11. # Threads used to be called rooms, and still are in the DB
  12. db.rooms.findOne query, (error, thread) ->
  13. return callback(error) if error?
  14. if thread?
  15. callback null, thread
  16. else
  17. db.rooms.save query, (error, thread) ->
  18. return callback(error) if error?
  19. callback null, thread
  20. findAllThreadRooms: (project_id, callback = (error, rooms) ->) ->
  21. db.rooms.find {
  22. project_id: ObjectId(project_id.toString())
  23. thread_id: { $exists: true }
  24. }, {
  25. thread_id: 1,
  26. resolved: 1
  27. }, callback
  28. resolveThread: (project_id, thread_id, user_id, callback = (error) ->) ->
  29. db.rooms.update {
  30. project_id: ObjectId(project_id.toString())
  31. thread_id: ObjectId(thread_id.toString())
  32. }, {
  33. $set: {
  34. resolved: {
  35. user_id: user_id
  36. ts: new Date()
  37. }
  38. }
  39. }, callback
  40. reopenThread: (project_id, thread_id, callback = (error) ->) ->
  41. db.rooms.update {
  42. project_id: ObjectId(project_id.toString())
  43. thread_id: ObjectId(thread_id.toString())
  44. }, {
  45. $unset: {
  46. resolved: true
  47. }
  48. }, callback
  49. deleteThread: (project_id, thread_id, callback = (error, room_id) ->) ->
  50. @findOrCreateThread project_id, thread_id, (error, room) ->
  51. return callback(error) if error?
  52. db.rooms.remove {
  53. _id: room._id
  54. }, (error) ->
  55. return callback(error) if error?
  56. return callback null, room._id