ThreadManager.coffee 899 B

12345678910111213141516171819202122232425262728293031323334
  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. }, callback