Added delete message method

This commit is contained in:
Chris Blanchard 2015-08-07 01:53:52 +01:00
parent c883fee668
commit e9e85036d5
2 changed files with 27 additions and 7 deletions

View file

@ -33,7 +33,6 @@ messageSchema.methods.toJson = function () {
};
};
messageSchema.statics.list = function (options, callback) {
return this.find({deleted: false}).sort({createdAt: -1}).limit(30).exec(callback);
};

View file

@ -10,6 +10,7 @@
* Client API
* message:new - New message has been created
* message:refresh - Retrieve most recent messages
* message:delete - Deletes message by ID (Admin only)
*
*/
@ -22,6 +23,21 @@ module.exports = function (namespace) {
namespace.emit("message:new", message.toJson());
};
var refreshMessages = function (socket) {
Message.list({}, function (error, messages) {
if (error) {
winston.error("Unable to retrieve messages. Error:", error);
return;
}
var receiver = (socket === undefined) ? namespace : socket;
receiver.emit("message:refresh", {
chatHistory: messages
});
});
};
namespace.on('connection', function (socket) {
socket.on('message:new', function (data) {
@ -40,16 +56,21 @@ module.exports = function (namespace) {
});
});
socket.on('message:refresh', function () {
Message.list({}, function (error, messages) {
socket.on('message:delete', function (data) {
var id = data.id;
if (id === undefined || !socket._user.admin) return;
Message.remove({_id: id}, function (error) {
if (error) {
winston.error("Unable to retrieve messages. Error:", error);
winston.error("An error occurred when trying to delete message:", error);
return;
}
socket.emit("message:refresh", {
chatHistory: messages
});
refreshMessages();
});
});
socket.on('message:refresh', function () {
refreshMessages(socket);
});
});
};