From 5f43b3e78eb32f1efcaa0121926a5b2e0d2bd452 Mon Sep 17 00:00:00 2001 From: khlieng Date: Sat, 31 Jan 2015 23:35:38 +0100 Subject: [PATCH] Added nick change handling, the client now shows nick and quit messages, the userlist gets hidden when the chat window is not a channel --- client/src/js/actions/channel.js | 5 +++++ client/src/js/actions/message.js | 11 ++++++++++- client/src/js/components/UserList.jsx | 10 ++++++++-- client/src/js/stores/channel.js | 16 ++++++++++++++++ client/src/js/stores/message.js | 16 ++++++++++++++++ irc.go | 1 + json_types.go | 6 ++++++ message_handler.go | 11 +++++++++++ storage/channel.go | 12 ++++++++++++ 9 files changed, 85 insertions(+), 3 deletions(-) diff --git a/client/src/js/actions/channel.js b/client/src/js/actions/channel.js index 9d522d8f..e917910d 100644 --- a/client/src/js/actions/channel.js +++ b/client/src/js/actions/channel.js @@ -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); }); diff --git a/client/src/js/actions/message.js b/client/src/js/actions/message.js index 67f5ceeb..802eb4d7 100644 --- a/client/src/js/actions/message.js +++ b/client/src/js/actions/message.js @@ -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({ diff --git a/client/src/js/components/UserList.jsx b/client/src/js/components/UserList.jsx index a149c842..1b7e3588 100644 --- a/client/src/js/components/UserList.jsx +++ b/client/src/js/components/UserList.jsx @@ -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 ( -
{users}
+
{users}
); } }); diff --git a/client/src/js/stores/channel.js b/client/src/js/stores/channel.js index 235acb4b..cb1bbd7f 100644 --- a/client/src/js/stores/channel.js +++ b/client/src/js/stores/channel.js @@ -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; } diff --git a/client/src/js/stores/message.js b/client/src/js/stores/message.js index a0dadccf..8bb0bcd4 100644 --- a/client/src/js/stores/message.js +++ b/client/src/js/stores/message.js @@ -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; } diff --git a/irc.go b/irc.go index 9c0c08e2..056fd727 100644 --- a/irc.go +++ b/irc.go @@ -12,6 +12,7 @@ import ( const ( PING = "PING" + NICK = "NICK" JOIN = "JOIN" PART = "PART" MODE = "MODE" diff --git a/json_types.go b/json_types.go index 9b44858e..3f30f8d1 100644 --- a/json_types.go +++ b/json_types.go @@ -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"` diff --git a/message_handler.go b/message_handler.go index a0bb89e4..607e81d2 100644 --- a/message_handler.go +++ b/message_handler.go @@ -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 diff --git a/storage/channel.go b/storage/channel.go index ccfb1873..4d5959e3 100644 --- a/storage/channel.go +++ b/storage/channel.go @@ -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, "@+")