Moved websocket handling on the client into its own file, added reason to part and quit messages, global system messages about a user now only gets sent to channels where the user is/was

This commit is contained in:
khlieng 2015-02-01 01:56:56 +01:00
parent 3f335187c0
commit 736ef76bc0
9 changed files with 108 additions and 100 deletions

View File

@ -30,36 +30,4 @@ channelActions.part.preEmit = function(channels, server) {
});
};
socket.on('join', function(data) {
channelActions.addUser(data.user, data.server, data.channels[0]);
});
socket.on('part', function(data) {
channelActions.removeUser(data.user, data.server, data.channels[0]);
});
socket.on('quit', function(data) {
channelActions.removeUserAll(data.user, data.server);
});
socket.on('nick', function(data) {
channelActions.renameUser(data.old, data.new, data.server);
});
socket.on('users', function(data) {
channelActions.setUsers(data.users, data.server, data.channel);
});
socket.on('topic', function(data) {
channelActions.setTopic(data.topic, data.server, data.channel);
});
socket.on('mode', function(data) {
channelActions.setMode(data);
});
socket.on('channels', function(data) {
channelActions.load(data);
});
module.exports = channelActions;

View File

@ -6,7 +6,8 @@ var socket = require('../socket');
var messageActions = Reflux.createActions([
'send',
'add',
'broadcast'
'broadcast',
'inform'
]);
messageActions.send.preEmit = function(message, to, server) {
@ -17,48 +18,4 @@ messageActions.send.preEmit = function(message, to, server) {
});
};
socket.on('message', function(data) {
messageActions.add(data);
});
socket.on('pm', function(data) {
messageActions.add(data);
});
socket.on('join', function(data) {
messageActions.add({
server: data.server,
to: data.channels[0],
message: data.user + ' joined the channel',
type: 'info'
});
});
socket.on('part', function(data) {
messageActions.add({
server: data.server,
to: data.channels[0],
message: data.user + ' left the channel',
type: 'info'
});
});
socket.on('quit', function(data) {
messageActions.broadcast(data.user + ' quit', data.server);
});
socket.on('nick', function(data) {
messageActions.broadcast(data.old + ' changed nick to ' + data.new, data.server);
});
socket.on('motd', function(data) {
_.each(data.content.split('\n'), function(line) {
messageActions.add({
server: data.server,
to: data.server,
message: line
});
});
});
module.exports = messageActions;

View File

@ -21,8 +21,4 @@ serverActions.disconnect.preEmit = function(server) {
socket.send('quit', { server: server });
};
socket.on('servers', function(data) {
serverActions.load(data);
});
module.exports = serverActions;

View File

@ -1,5 +1,6 @@
var React = require('react');
require('./irc');
var socket = require('./socket');
var util = require('./util');
var App = require('./components/App.jsx');

68
client/src/js/irc.js Normal file
View File

@ -0,0 +1,68 @@
var _ = require('lodash');
var socket = require('./socket');
var channelActions = require('./actions/channel');
var messageActions = require('./actions/message');
var serverActions = require('./actions/server');
socket.on('join', function(data) {
channelActions.addUser(data.user, data.server, data.channels[0]);
messageActions.inform(data.user + ' joined the channel', data.server, data.channels[0]);
});
socket.on('part', function(data) {
channelActions.removeUser(data.user, data.server, data.channels[0]);
messageActions.inform(withReason(data.user + ' left the channel', data.reason), data.server, data.channels[0]);
});
socket.on('quit', function(data) {
messageActions.broadcast(withReason(data.user + ' quit', data.reason), data.server, data.user);
channelActions.removeUserAll(data.user, data.server);
});
socket.on('nick', function(data) {
messageActions.broadcast(data.old + ' changed nick to ' + data.new, data.server, data.old);
channelActions.renameUser(data.old, data.new, data.server);
});
socket.on('message', function(data) {
messageActions.add(data);
});
socket.on('pm', function(data) {
messageActions.add(data);
});
socket.on('motd', function(data) {
_.each(data.content.split('\n'), function(line) {
messageActions.add({
server: data.server,
to: data.server,
message: line
});
});
});
socket.on('users', function(data) {
channelActions.setUsers(data.users, data.server, data.channel);
});
socket.on('topic', function(data) {
channelActions.setTopic(data.topic, data.server, data.channel);
});
socket.on('mode', function(data) {
channelActions.setMode(data);
});
socket.on('servers', function(data) {
serverActions.load(data);
});
socket.on('channels', function(data) {
channelActions.load(data);
});
function withReason(message, reason) {
return message + (reason ? ' (' + reason + ')' : '');
}

View File

@ -8,6 +8,8 @@ var actions = require('../actions/message');
var messages = {};
function addMessage(message, dest) {
message.time = new Date();
if (!(message.server in messages)) {
messages[message.server] = {};
messages[message.server][dest] = [message];
@ -28,8 +30,7 @@ var messageStore = Reflux.createStore({
server: server,
from: serverStore.getNick(server),
to: to,
message: message,
time: new Date()
message: message
}, to);
this.trigger(messages);
@ -41,25 +42,34 @@ var messageStore = Reflux.createStore({
dest = message.server;
}
message.time = new Date();
addMessage(message, dest);
this.trigger(messages);
},
broadcast: function(message, server) {
broadcast: function(message, server, user) {
_.each(channelStore.getChannels(server), function(channel, channelName) {
addMessage({
server: server,
to: channelName,
message: message,
type: 'info',
time: new Date()
}, channelName);
if (!user || (user && _.find(channel.users, { nick: user }))) {
addMessage({
server: server,
to: channelName,
message: message,
type: 'info'
}, channelName);
}
});
this.trigger(messages);
},
inform: function(message, server, channel) {
addMessage({
server: server,
to: channel,
message: message,
type: 'info'
}, channel);
this.trigger(messages);
},
getState: function() {
return messages;
}

View File

@ -33,6 +33,11 @@ type Join struct {
Channels []string `json:"channels"`
}
type Part struct {
Join
Reason string `json:"reason,omitempty"`
}
type Mode struct {
Server string `json:"server"`
Channel string `json:"channel"`
@ -44,6 +49,7 @@ type Mode struct {
type Quit struct {
Server string `json:"server"`
User string `json:"user"`
Reason string `json:"reason,omitempty"`
}
type Chat struct {

View File

@ -16,8 +16,6 @@ func handleMessages(irc *IRC, session *Session) {
for msg := range irc.Messages {
switch msg.Command {
case NICK:
printMessage(msg, irc)
session.sendJSON("nick", Nick{
Server: irc.Host,
Old: msg.Prefix,
@ -47,10 +45,13 @@ func handleMessages(irc *IRC, session *Session) {
case PART:
user := msg.Prefix
session.sendJSON("part", Join{
Server: irc.Host,
User: user,
Channels: msg.Params,
session.sendJSON("part", Part{
Join: Join{
Server: irc.Host,
User: user,
Channels: msg.Params,
},
Reason: msg.Trailing,
})
channelStore.RemoveUser(user, irc.Host, msg.Params[0])
@ -94,6 +95,7 @@ func handleMessages(irc *IRC, session *Session) {
session.sendJSON("quit", Quit{
Server: irc.Host,
User: user,
Reason: msg.Trailing,
})
channelStore.RemoveUserAll(user, irc.Host)

View File

@ -104,7 +104,7 @@ func handleWS(ws *websocket.Conn) {
}
case "part":
var data Join
var data Part
json.Unmarshal(req.Request, &data)