Allow editing/deleting of comments and threads

This commit is contained in:
James Allen 2017-01-24 15:44:32 +01:00
parent a997bf9c01
commit fa63eb6947
9 changed files with 184 additions and 4 deletions

View file

@ -8,6 +8,8 @@ module.exports = MessageFormatter =
content: message.content content: message.content
timestamp: message.timestamp timestamp: message.timestamp
user_id: message.user_id user_id: message.user_id
if message.edited_at?
formattedMessage.edited_at = message.edited_at
return formattedMessage return formattedMessage
formatMessagesForClientSide: (messages) -> formatMessagesForClientSide: (messages) ->

View file

@ -43,6 +43,34 @@ module.exports = MessageHttpController =
return next(error) if error? return next(error) if error?
res.send 204 # No content res.send 204 # No content
deleteThread: (req, res, next) ->
{project_id, thread_id} = req.params
logger.log {project_id, thread_id}, "deleting thread"
ThreadManager.deleteThread project_id, thread_id, (error, room_id) ->
return next(error) if error?
MessageManager.deleteAllMessagesInRoom room_id, (error) ->
return next(error) if error?
res.send 204 # No content
editMessage: (req, res, next) ->
{content} = req?.body
{project_id, thread_id, message_id} = req.params
logger.log {project_id, thread_id, message_id, content}, "editing message"
ThreadManager.findOrCreateThread project_id, thread_id, (error, room) ->
return next(error) if error?
MessageManager.updateMessage room._id, message_id, content, Date.now(), (error) ->
return next(error) if error?
res.send(200)
deleteMessage: (req, res, next) ->
{project_id, thread_id, message_id} = req.params
logger.log {project_id, thread_id, message_id}, "deleting message"
ThreadManager.findOrCreateThread project_id, thread_id, (error, room) ->
return next(error) if error?
MessageManager.deleteMessage room._id, message_id, (error, message) ->
return next(error) if error?
res.send(204)
_sendMessage: (client_thread_id, req, res, next) -> _sendMessage: (client_thread_id, req, res, next) ->
{user_id, content} = req?.body {user_id, content} = req?.body
{project_id} = req.params {project_id} = req.params

View file

@ -27,10 +27,41 @@ module.exports = MessageManager =
room_id: { $in: room_ids } room_id: { $in: room_ids }
}, callback }, callback
deleteAllMessagesInRoom: (room_id, callback = (error) ->) ->
db.messages.remove {
room_id: room_id
}, callback
updateMessage: (room_id, message_id, content, timestamp, callback = (error, message) ->) ->
query = @_ensureIdsAreObjectIds(
_id: message_id
room_id: room_id
)
db.messages.update query, {
$set:
content: content
edited_at: timestamp
}, (error) ->
return callback(error) if error?
db.messages.find query, (error, messages) ->
return callback(error) if error?
return callback null, messages[0]
deleteMessage: (room_id, message_id, callback = (error) ->) ->
query = @_ensureIdsAreObjectIds(
_id: message_id
room_id: room_id
)
db.messages.remove query, (error) ->
return callback(error) if error?
return callback()
_ensureIdsAreObjectIds: (query) -> _ensureIdsAreObjectIds: (query) ->
if query.user_id? and query.user_id not instanceof ObjectId if query.user_id? and query.user_id not instanceof ObjectId
query.user_id = ObjectId(query.user_id) query.user_id = ObjectId(query.user_id)
if query.room_id? and query.room_id not instanceof ObjectId if query.room_id? and query.room_id not instanceof ObjectId
query.room_id = ObjectId(query.room_id) query.room_id = ObjectId(query.room_id)
if query._id? and query._id not instanceof ObjectId
query._id = ObjectId(query._id)
return query return query

View file

@ -53,3 +53,12 @@ module.exports = ThreadManager =
resolved: true resolved: true
} }
}, callback }, callback
deleteThread: (project_id, thread_id, callback = (error, room_id) ->) ->
@findOrCreateThread project_id, thread_id, (error, room) ->
return callback(error) if error?
db.rooms.remove {
_id: room._id
}, (error) ->
return callback(error) if error?
return callback null, room._id

View file

@ -25,12 +25,12 @@ module.exports = Router =
app.post "/project/:project_id/thread/:thread_id/messages", MessageHttpController.sendThreadMessage app.post "/project/:project_id/thread/:thread_id/messages", MessageHttpController.sendThreadMessage
app.get "/project/:project_id/threads", MessageHttpController.getAllThreads app.get "/project/:project_id/threads", MessageHttpController.getAllThreads
# app.post "/project/:project_id/thread/:thread_id/messages/:message_id/edit", MessageHttpController.editMessage app.post "/project/:project_id/thread/:thread_id/messages/:message_id/edit", MessageHttpController.editMessage
# app.del "/project/:project_id/thread/:thread_id/messages/:message_id", MessageHttpController.deleteMessage app.del "/project/:project_id/thread/:thread_id/messages/:message_id", MessageHttpController.deleteMessage
app.post "/project/:project_id/thread/:thread_id/resolve", MessageHttpController.resolveThread app.post "/project/:project_id/thread/:thread_id/resolve", MessageHttpController.resolveThread
app.post "/project/:project_id/thread/:thread_id/reopen", MessageHttpController.reopenThread app.post "/project/:project_id/thread/:thread_id/reopen", MessageHttpController.reopenThread
# app.del "/project/:project_id/thread/:thread_id", MessageHttpController.deleteThread app.del "/project/:project_id/thread/:thread_id", MessageHttpController.deleteThread
app.get "/status", (req, res, next) -> app.get "/status", (req, res, next) ->
res.send("chat is alive") res.send("chat is alive")

View file

@ -0,0 +1,30 @@
{ObjectId} = require "../../../app/js/mongojs"
expect = require("chai").expect
ChatClient = require "./helpers/ChatClient"
describe "Deleting a message", ->
before ->
@project_id = ObjectId().toString()
@user_id = ObjectId().toString()
@thread_id = ObjectId().toString()
describe "in a thread", ->
before (done) ->
ChatClient.sendMessage @project_id, @thread_id, @user_id, "first message", (error, response, @message) =>
expect(error).to.be.null
expect(response.statusCode).to.equal 201
ChatClient.sendMessage @project_id, @thread_id, @user_id, "deleted message", (error, response, @message) =>
expect(error).to.be.null
expect(response.statusCode).to.equal 201
ChatClient.deleteMessage @project_id, @thread_id, @message.id, (error, response, body) =>
expect(error).to.be.null
expect(response.statusCode).to.equal 204
done()
it "should then remove the message from the threads", (done) ->
ChatClient.getThreads @project_id, (error, response, threads) =>
expect(error).to.be.null
expect(response.statusCode).to.equal 200
expect(threads[@thread_id].messages.length).to.equal 1
done()

View file

@ -0,0 +1,29 @@
{ObjectId} = require "../../../app/js/mongojs"
expect = require("chai").expect
crypto = require "crypto"
ChatClient = require "./helpers/ChatClient"
describe "Deleting a thread", ->
before ->
@project_id = ObjectId().toString()
@user_id = ObjectId().toString()
describe "with a thread that is deleted", ->
before (done) ->
@thread_id = ObjectId().toString()
@content = "deleted thread message"
ChatClient.sendMessage @project_id, @thread_id, @user_id, @content, (error, response, body) =>
expect(error).to.be.null
expect(response.statusCode).to.equal 201
ChatClient.deleteThread @project_id, @thread_id, (error, response, body) =>
expect(error).to.be.null
expect(response.statusCode).to.equal 204
done()
it "should then not list the thread for the project", (done) ->
ChatClient.getThreads @project_id, (error, response, threads) =>
expect(error).to.be.null
expect(response.statusCode).to.equal 200
expect(Object.keys(threads).length).to.equal 0
done()

View file

@ -0,0 +1,34 @@
{ObjectId} = require "../../../app/js/mongojs"
expect = require("chai").expect
ChatClient = require "./helpers/ChatClient"
describe "Editing a message", ->
before ->
@project_id = ObjectId().toString()
@user_id = ObjectId().toString()
@thread_id = ObjectId().toString()
describe "in a thread", ->
before (done) ->
@content = "thread message"
@new_content = "updated thread message"
ChatClient.sendMessage @project_id, @thread_id, @user_id, @content, (error, response, @message) =>
expect(error).to.be.null
expect(response.statusCode).to.equal 201
expect(@message.id).to.exist
expect(@message.content).to.equal @content
ChatClient.editMessage @project_id, @thread_id, @message.id, @new_content, (error, response, @new_message) =>
expect(error).to.be.null
expect(response.statusCode).to.equal 201
expect(@new_message.edited_at).to.exist
expect(@new_message.content).to.equal @new_content
done()
it "should then list the updated message in the threads", (done) ->
ChatClient.getThreads @project_id, (error, response, threads) =>
expect(error).to.be.null
expect(response.statusCode).to.equal 200
expect(threads[@thread_id].messages.length).to.equal 1
expect(threads[@thread_id].messages[0].content).to.equal @new_content
done()

View file

@ -41,3 +41,20 @@ module.exports =
request.post { request.post {
url: "/project/#{project_id}/thread/#{thread_id}/reopen", url: "/project/#{project_id}/thread/#{thread_id}/reopen",
}, callback }, callback
deleteThread: (project_id, thread_id, callback) ->
request.del {
url: "/project/#{project_id}/thread/#{thread_id}",
}, callback
editMessage: (project_id, thread_id, message_id, content, callback) ->
request.post {
url: "/project/#{project_id}/thread/#{thread_id}/messages/#{message_id}/edit"
json:
content: content
}, callback
deleteMessage: (project_id, thread_id, message_id, callback) ->
request.del {
url: "/project/#{project_id}/thread/#{thread_id}/messages/#{message_id}",
}, callback