mirror of
https://github.com/ENSL/ensl_gathers.git
synced 2024-11-10 15:21:56 +00:00
96 lines
2.2 KiB
JavaScript
96 lines
2.2 KiB
JavaScript
"use strict";
|
|
|
|
/*
|
|
* Chatroom Controller
|
|
*
|
|
* Server API
|
|
* message:append - New message to be added to history
|
|
* message:refresh - Reload all messages
|
|
*
|
|
* Client API
|
|
* message:new - New message has been created
|
|
* message:refresh - Retrieve most recent messages
|
|
* message:delete - Deletes message by ID (Admin only)
|
|
*
|
|
*/
|
|
|
|
var mongoose = require("mongoose");
|
|
var Message = mongoose.model("Message");
|
|
var winston = require("winston");
|
|
|
|
module.exports = namespace => {
|
|
|
|
var broadcastUpdate = message => {
|
|
namespace.emit("message:append", message);
|
|
};
|
|
|
|
var refreshMessages = socket => {
|
|
Message.list({}, (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
|
|
});
|
|
});
|
|
};
|
|
|
|
var retrievePreviousMessages = (options, socket) => {
|
|
Message.list({
|
|
before: options.before
|
|
}, (error, messages) => {
|
|
if (error) {
|
|
winston.error("Unable to retrieve messages. Error:", error);
|
|
return;
|
|
}
|
|
socket.emit("message:append", {
|
|
messages: messages
|
|
});
|
|
});
|
|
}
|
|
|
|
namespace.on('connection', socket => {
|
|
|
|
socket.on('message:new', data => {
|
|
Message.create({
|
|
author: {
|
|
username: socket._user.username,
|
|
avatar: socket._user.avatar
|
|
},
|
|
content: data.content
|
|
}, (error, newMessage) => {
|
|
if (error) {
|
|
winston.error("Unable to store message. Error:", error);
|
|
return;
|
|
}
|
|
winston.info("New Message", JSON.stringify(newMessage));
|
|
broadcastUpdate(newMessage);
|
|
});
|
|
});
|
|
|
|
socket.on('message:delete', data => {
|
|
var id = data.id;
|
|
if (id === undefined || !socket._user.admin) return;
|
|
|
|
Message.update({_id: id}, {deleted: true}, (error, message) => {
|
|
if (error) {
|
|
winston.error("An error occurred when trying to delete message:", error);
|
|
return;
|
|
}
|
|
winston.info("Deleted message", JSON.stringify(data));
|
|
refreshMessages();
|
|
});
|
|
});
|
|
|
|
socket.on('message:refresh', data => {
|
|
if (data && data.before) {
|
|
return retrievePreviousMessages(data, socket);
|
|
}
|
|
refreshMessages(socket);
|
|
});
|
|
});
|
|
};
|