Added message persistence

This commit is contained in:
Chris Blanchard 2015-08-07 01:14:36 +01:00
parent 6271d96133
commit ab790e8c9e
6 changed files with 27 additions and 63 deletions

View file

@ -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);
};

View file

@ -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);

View file

@ -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;

View file

@ -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
});
});
});
});

View file

@ -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;

View file

@ -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) {