Add messageActions.addAll()

This commit is contained in:
Ken-Håvard Lieng 2015-06-10 00:45:28 +02:00
parent 29c13f4f54
commit e51c9522de
4 changed files with 38 additions and 22 deletions

File diff suppressed because one or more lines are too long

View File

@ -5,6 +5,7 @@ var socket = require('../socket');
var messageActions = Reflux.createActions([ var messageActions = Reflux.createActions([
'send', 'send',
'add', 'add',
'addAll',
'broadcast', 'broadcast',
'inform', 'inform',
'command', 'command',

View File

@ -41,13 +41,13 @@ socket.on('pm', function(data) {
}); });
socket.on('motd', function(data) { socket.on('motd', function(data) {
_.each(data.content, function(line) { messageActions.addAll(_.map(data.content, line => {
messageActions.add({ return {
server: data.server, server: data.server,
to: data.server, to: data.server,
message: line message: line
}); };
}); }));
}); });
socket.on('users', function(data) { socket.on('users', function(data) {

View File

@ -22,7 +22,7 @@ var Message = Immutable.Record({
lines: [] lines: []
}); });
function addMessage(message, dest) { function addMessage(message, dest, mutable) {
message.time = new Date(); message.time = new Date();
if (message.message.indexOf('\x01ACTION') === 0) { if (message.message.indexOf('\x01ACTION') === 0) {
@ -32,7 +32,19 @@ function addMessage(message, dest) {
message.message = from + message.message.slice(7); message.message = from + message.message.slice(7);
} }
messages = messages.updateIn([message.server, dest], empty, list => list.push(new Message(message))); if (mutable) {
mutable.updateIn([message.server, dest], empty, list => list.push(new Message(message)));
} else {
messages = messages.updateIn([message.server, dest], empty, list => list.push(new Message(message)));
}
}
function getDest(message) {
var dest = message.to || message.from;
if (message.from && message.from.indexOf('.') !== -1) {
dest = message.server;
}
return dest;
} }
var messageStore = Reflux.createStore({ var messageStore = Reflux.createStore({
@ -54,15 +66,18 @@ var messageStore = Reflux.createStore({
}, },
add(message) { add(message) {
var dest = message.to || message.from; addMessage(message, getDest(message));
if (message.from && message.from.indexOf('.') !== -1) {
dest = message.server;
}
addMessage(message, dest);
this.trigger(messages); this.trigger(messages);
}, },
addAll(newMessages) {
messages = messages.withMutations(mutable => {
_.each(newMessages, message => {
addMessage(message, getDest(message), mutable);
});
});
},
broadcast(message, server, user) { broadcast(message, server, user) {
channelStore.getChannels(server).forEach((channel, channelName) => { channelStore.getChannels(server).forEach((channel, channelName) => {
if (!user || (user && channel.get('users').find(u => u.nick === user))) { if (!user || (user && channel.get('users').find(u => u.nick === user))) {