From ab790e8c9ecde760b1652f19a6a2c2e3d93ce7f1 Mon Sep 17 00:00:00 2001 From: Chris Blanchard Date: Fri, 7 Aug 2015 01:14:36 +0100 Subject: [PATCH] Added message persistence --- db/models/message.js | 2 +- index.js | 3 +++ lib/chat/chatroom.js | 33 --------------------------------- lib/chat/controller.js | 28 ++++++++++++++++++++++------ lib/chat/message.js | 22 ---------------------- spec/message.js | 2 +- 6 files changed, 27 insertions(+), 63 deletions(-) delete mode 100644 lib/chat/chatroom.js delete mode 100644 lib/chat/message.js diff --git a/db/models/message.js b/db/models/message.js index 96c6bd9..3185db8 100644 --- a/db/models/message.js +++ b/db/models/message.js @@ -32,7 +32,7 @@ messageSchema.methods.toJson = function () { }; -messageSchema.statics.list = function (callback) { +messageSchema.statics.list = function (options, callback) { return this.find().sort({createdAt: 1}).limit(30).exec(callback); }; diff --git a/index.js b/index.js index 5b7f64c..809cda6 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,9 @@ var server = require('http').Server(app); var io = require('socket.io')(server); var config = require(path.join(__dirname, "config/config.js")); +// Load Models +require(path.join(__dirname, "db/index")); + // Configure express require(path.join(__dirname, "config/express"))(app); diff --git a/lib/chat/chatroom.js b/lib/chat/chatroom.js deleted file mode 100644 index 8d98e2d..0000000 --- a/lib/chat/chatroom.js +++ /dev/null @@ -1,33 +0,0 @@ -"use strict"; - -var Message = require("./message"); - -function Chatroom (o) { - if (!(this instanceof Chatroom)) { - return new Chatroom(o); - } - - this.messages = []; -}; - -Chatroom.prototype.createMessage = function (options, callback) { - var message = new Message({ - author: options.author, - content: options.content - }) - this.messages.push(message); - - if (callback) { - return callback(message); - } -}; - -Chatroom.prototype.retrieveMessages = function (n) { - return this.messages - .slice(this.messages.length - n, this.messages.length) - .map(function (message) { - return message.toJson(); - }); -}; - -module.exports = Chatroom; diff --git a/lib/chat/controller.js b/lib/chat/controller.js index 0910fa9..7052506 100644 --- a/lib/chat/controller.js +++ b/lib/chat/controller.js @@ -13,7 +13,8 @@ * */ -var chatroom = require("./chatroom")(); +var mongoose = require("mongoose"); +var Message = mongoose.model("message"); module.exports = function (namespace) { @@ -24,15 +25,30 @@ module.exports = function (namespace) { namespace.on('connection', function (socket) { socket.on('message:new', function (data) { - chatroom.createMessage({ - author: socket._user, + Message.create({ + author: { + username: socket._user.username, + avatar: socket._user.avatar + }, content: data.content - }, broadcastUpdate); + }, function (error, newMessage) { + if (error) { + winston.error("Unable to store message. Error:", error); + return; + } + broadcastUpdate(newMessage) + }); }); socket.on('message:refresh', function () { - socket.emit("message:refresh", { - chatHistory: chatroom.retrieveMessages(20) + Message.list({}, function (error, messages) { + if (error) { + winston.error("Unable to retrieve messages. Error:", error); + return; + } + socket.emit("message:refresh", { + chatHistory: messages + }); }); }); }); diff --git a/lib/chat/message.js b/lib/chat/message.js deleted file mode 100644 index e246298..0000000 --- a/lib/chat/message.js +++ /dev/null @@ -1,22 +0,0 @@ -"use strict"; - -var Author = require("./author"); -var count = 0; - -function Message (o) { - this.id = count++; - this.author = Author(o.author); - this.content = o.content; - this.createdAt = new Date(); -}; - -Message.prototype.toJson = function () { - return { - id: this.id, - author: this.author, - content: this.content, - createdAt: this.createdAt - } -}; - -module.exports = Message; \ No newline at end of file diff --git a/spec/message.js b/spec/message.js index 8cd3034..8408fab 100644 --- a/spec/message.js +++ b/spec/message.js @@ -57,7 +57,7 @@ describe("Message Model", function () { async.series(instructions, done); }); it ("lists last 30 messages with oldest first", function (done) { - Message.list(function (error, messages) { + Message.list({}, function (error, messages) { if (error) return done(error); assert.equal(messages.length, 30); assert.isTrue(messages.reduce(function (acc, message, index, arr) {