Don't read from comment thread if no global message thread exists

This commit is contained in:
James Allen 2017-02-24 16:20:06 +01:00
parent cb47ec464e
commit 2f30732011
2 changed files with 77 additions and 50 deletions

View file

@ -6,21 +6,33 @@ module.exports = ThreadManager =
GLOBAL_THREAD: "GLOBAL"
findOrCreateThread: (project_id, thread_id, callback = (error, thread) ->) ->
query =
project_id: ObjectId(project_id.toString())
project_id = ObjectId(project_id.toString())
if thread_id != ThreadManager.GLOBAL_THREAD
thread_id = ObjectId(thread_id.toString())
if thread_id? and thread_id != ThreadManager.GLOBAL_THREAD
query.thread_id = ObjectId(thread_id.toString())
# Threads used to be called rooms, and still are in the DB
db.rooms.findOne query, (error, thread) ->
return callback(error) if error?
if thread?
callback null, thread
if thread_id == ThreadManager.GLOBAL_THREAD
query = {
project_id: project_id
thread_id: { $exists: false }
}
update = {
project_id: project_id
}
else
db.rooms.save query, (error, thread) ->
query = {
project_id: project_id
thread_id: thread_id
}
update = {
project_id: project_id
thread_id: thread_id
}
db.rooms.update query, update, { upsert: true }, (error) ->
return callback(error) if error?
callback null, thread
db.rooms.find query, (error, rooms = []) ->
return callback(error) if error?
return callback null, rooms[0]
findAllThreadRooms: (project_id, callback = (error, rooms) ->) ->
db.rooms.find {

View file

@ -5,12 +5,11 @@ ChatClient = require "./helpers/ChatClient"
describe "Sending a message", ->
before ->
@project_id = ObjectId().toString()
@user_id = ObjectId().toString()
@thread_id = ObjectId().toString()
describe "globally", ->
before (done) ->
@project_id = ObjectId().toString()
@user_id = ObjectId().toString()
@content = "global message"
ChatClient.sendGlobalMessage @project_id, @user_id, @content, (error, response, body) =>
expect(error).to.be.null
@ -30,6 +29,9 @@ describe "Sending a message", ->
describe "to a thread", ->
before (done) ->
@project_id = ObjectId().toString()
@user_id = ObjectId().toString()
@thread_id = ObjectId().toString()
@content = "thread message"
ChatClient.sendMessage @project_id, @thread_id, @user_id, @content, (error, response, body) =>
expect(error).to.be.null
@ -47,6 +49,19 @@ describe "Sending a message", ->
expect(threads[@thread_id].messages[0].content).to.equal @content
done()
it "should not appear in the global messages", (done) ->
ChatClient.getGlobalMessages @project_id, (error, response, messages) =>
expect(error).to.be.null
expect(response.statusCode).to.equal 200
expect(messages.length).to.equal 0
done()
describe "failure cases", ->
before () ->
@project_id = ObjectId().toString()
@user_id = ObjectId().toString()
@thread_id = ObjectId().toString()
describe "with a malformed user_id", ->
it "should return a graceful error", (done) ->
ChatClient.sendMessage @project_id, @thread_id, "malformed-user", "content", (error, response, body) =>