Added nick change handling, the client now shows nick and quit messages, the userlist gets hidden when the chat window is not a channel

This commit is contained in:
khlieng 2015-01-31 23:35:38 +01:00
parent c421dc504b
commit 5f43b3e78e
9 changed files with 85 additions and 3 deletions

View File

@ -9,6 +9,7 @@ var channelActions = Reflux.createActions([
'addUser',
'removeUser',
'removeUserAll',
'renameUser',
'setUsers',
'setTopic',
'setMode',
@ -41,6 +42,10 @@ 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);
});

View File

@ -5,7 +5,8 @@ var socket = require('../socket');
var messageActions = Reflux.createActions([
'send',
'add'
'add',
'broadcast'
]);
messageActions.send.preEmit = function(message, to, server) {
@ -44,6 +45,14 @@ socket.on('part', function(data) {
});
});
socket.on('quit', function(data) {
messageActions.broadcast(data.user + ' has 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({

View File

@ -2,7 +2,7 @@ var React = require('react');
var Reflux = require('reflux');
var _ = require('lodash');
var UserListItem = require('../components/UserListItem.jsx');
var UserListItem = require('./UserListItem.jsx');
var channelStore = require('../stores/channel');
var selectedTabStore = require('../stores/selectedTab');
@ -23,6 +23,12 @@ var UserList = React.createClass({
var users = null;
var tab = this.state.selectedTab;
var style = {};
if (!tab.channel || tab.channel[0] !== '#') {
style.display = 'none';
}
if (tab.channel && this.state.channels[tab.server]) {
var channel = this.state.channels[tab.server][tab.channel];
if (channel) {
@ -33,7 +39,7 @@ var UserList = React.createClass({
}
return (
<div className="userlist">{users}</div>
<div className="userlist" style={style}>{users}</div>
);
}
});

View File

@ -110,6 +110,18 @@ var channelStore = Reflux.createStore({
this.trigger(channels);
},
renameUser: function(oldNick, newNick, server) {
_.each(channels[server], function(channel, channelName) {
var user = _.find(channel.users, { nick: oldNick });
if (user) {
user.nick = newNick;
updateRenderName(user);
sortUsers(server, channelName);
}
});
this.trigger(channels);
},
setUsers: function(users, server, channel) {
initChannel(server, channel);
var chan = channels[server][channel];
@ -170,6 +182,10 @@ var channelStore = Reflux.createStore({
}
},
getChannels: function(server) {
return channels[server];
},
getState: function() {
return channels;
}

View File

@ -1,6 +1,8 @@
var Reflux = require('reflux');
var _ = require('lodash');
var serverStore = require('./server');
var channelStore = require('./channel');
var actions = require('../actions/message');
var messages = {};
@ -45,6 +47,20 @@ var messageStore = Reflux.createStore({
this.trigger(messages);
},
broadcast: function(message, server) {
_.each(channelStore.getChannels(server), function(channel, channelName) {
addMessage({
server: server,
from: '',
to: channelName,
message: message,
type: 'info',
time: new Date()
}, channelName);
});
this.trigger(messages);
},
getState: function() {
return messages;
}

1
irc.go
View File

@ -12,6 +12,7 @@ import (
const (
PING = "PING"
NICK = "NICK"
JOIN = "JOIN"
PART = "PART"
MODE = "MODE"

View File

@ -21,6 +21,12 @@ type Connect struct {
Username string `json:"username"`
}
type Nick struct {
Server string `json:"server"`
Old string `json:"old"`
New string `json:"new"`
}
type Join struct {
Server string `json:"server"`
User string `json:"user"`

View File

@ -15,6 +15,17 @@ 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,
New: msg.Trailing,
})
channelStore.RenameUser(msg.Prefix, msg.Trailing, irc.Host)
case JOIN:
user := msg.Prefix

View File

@ -75,6 +75,12 @@ func (c *ChannelStore) RemoveUserAll(user, server string) {
c.userLock.Unlock()
}
func (c *ChannelStore) RenameUser(oldNick, newNick, server string) {
c.userLock.Lock()
c.renameAll(server, oldNick, newNick)
c.userLock.Unlock()
}
func (c *ChannelStore) SetMode(server, channel, user, add, remove string) {
c.userLock.Lock()
@ -118,6 +124,12 @@ func (c *ChannelStore) rename(server, channel, oldNick, newNick string) {
}
}
func (c *ChannelStore) renameAll(server, oldNick, newNick string) {
for channel, _ := range c.users[server] {
c.rename(server, channel, oldNick, newNick)
}
}
func (c *ChannelStore) removeUser(user, server, channel string) {
for i, u := range c.users[server][channel] {
u = strings.TrimLeft(u, "@+")