Added IRC network naming, added some convenience getters to the stores

This commit is contained in:
khlieng 2015-02-02 01:54:26 +01:00
parent 1c30da5474
commit eec82a7dd9
14 changed files with 73 additions and 33 deletions

View File

@ -20,7 +20,7 @@
}, },
"dependencies": { "dependencies": {
"lodash": "3.0.0", "lodash": "3.0.0",
"reflux": "~0.2.2", "reflux": "0.2.4",
"react-router": "~0.11.6", "react-router": "~0.11.6",
"react": "~0.12.2" "react": "~0.12.2"
} }

View File

@ -8,12 +8,13 @@ var serverActions = Reflux.createActions([
'load' 'load'
]); ]);
serverActions.connect.preEmit = function(server, nick, username, tls) { serverActions.connect.preEmit = function(server, nick, username, tls, name) {
socket.send('connect', { socket.send('connect', {
server: server, server: server,
nick: nick, nick: nick,
username: username, username: username,
tls: tls || false tls: tls || false,
name: name || server
}); });
}; };

View File

@ -14,8 +14,8 @@ var nick = 'test' + Math.floor(Math.random() * 99999);
socket.on('connect', function() { socket.on('connect', function() {
socket.send('uuid', uuid); socket.send('uuid', uuid);
serverActions.connect('irc.freenode.net', nick, 'username', true); serverActions.connect('irc.freenode.net', nick, 'username', true, 'Freenode');
serverActions.connect('irc.quakenet.org', nick, 'username'); serverActions.connect('irc.quakenet.org', nick, 'username', false, 'QuakeNet');
channelActions.join(['#stuff'], 'irc.freenode.net'); channelActions.join(['#stuff'], 'irc.freenode.net');
channelActions.join(['#herp'], 'irc.quakenet.org'); channelActions.join(['#herp'], 'irc.quakenet.org');

View File

@ -19,25 +19,21 @@ var ChatTitle = React.createClass({
render: function() { render: function() {
var tab = this.state.selectedTab; var tab = this.state.selectedTab;
var title;
var topic; var topic;
var usercount; var usercount;
if (tab.channel && this.state.channels[tab.server]) { if (tab.channel && this.state.channels[tab.server]) {
var channel = this.state.channels[tab.server][tab.channel]; var channel = this.state.channels[tab.server][tab.channel];
title = tab.channel
if (channel) { if (channel) {
usercount = channel.users.length; usercount = channel.users.length;
topic = channel.topic || ''; topic = channel.topic || '';
} }
} else {
title = tab.server;
} }
return ( return (
<div className="chat-title-bar"> <div className="chat-title-bar">
<div> <div>
<span className="chat-title">{title}</span> <span className="chat-title">{tab.name}</span>
<span className="chat-topic" title={topic}>{topic}</span> <span className="chat-topic" title={topic}>{topic}</span>
</div> </div>
<span className="chat-usercount">{usercount}</span> <span className="chat-usercount">{usercount}</span>

View File

@ -4,6 +4,7 @@ var _ = require('lodash');
var channelStore = require('../stores/channel'); var channelStore = require('../stores/channel');
var privateChatStore = require('../stores/privateChat'); var privateChatStore = require('../stores/privateChat');
var serverStore = require('../stores/server');
var selectedTabStore = require('../stores/selectedTab'); var selectedTabStore = require('../stores/selectedTab');
var tabActions = require('../actions/tab'); var tabActions = require('../actions/tab');
@ -11,14 +12,16 @@ var TabList = React.createClass({
mixins: [ mixins: [
Reflux.connect(channelStore, 'channels'), Reflux.connect(channelStore, 'channels'),
Reflux.connect(privateChatStore, 'privateChats'), Reflux.connect(privateChatStore, 'privateChats'),
Reflux.connect(selectedTabStore, 'selectedTab') Reflux.connect(selectedTabStore, 'selectedTab'),
Reflux.connect(serverStore, 'servers')
], ],
getInitialState: function() { getInitialState: function() {
return { return {
channels: channelStore.getState(), channels: channelStore.getState(),
privateChats: privateChatStore.getState(), privateChats: privateChatStore.getState(),
selectedTab: selectedTabStore.getState() selectedTab: selectedTabStore.getState(),
servers: serverStore.getState()
}; };
}, },
@ -36,7 +39,13 @@ var TabList = React.createClass({
tabClass = ''; tabClass = '';
} }
return <p className={tabClass} onClick={tabActions.select.bind(null, address, name)}>{name}</p>; return (
<p
className={tabClass}
onClick={tabActions.select.bind(null, address, name)}>
{name}
</p>
);
}); });
_.each(self.state.privateChats[address], function(chat, nick) { _.each(self.state.privateChats[address], function(chat, nick) {
@ -46,8 +55,14 @@ var TabList = React.createClass({
} else { } else {
tabClass = ''; tabClass = '';
} }
channels.push(<p className={tabClass} onClick={tabActions.select.bind(null, address, nick)}>{nick}</p>); channels.push(
<p
className={tabClass}
onClick={tabActions.select.bind(null, address, nick)}>
{nick}
</p>
);
}); });
if (address === selected.server && if (address === selected.server &&
@ -57,7 +72,13 @@ var TabList = React.createClass({
tabClass = 'tab-server'; tabClass = 'tab-server';
} }
channels.unshift(<p className={tabClass} onClick={tabActions.select.bind(null, address, null)}>{address}</p>); channels.unshift(
<p
className={tabClass}
onClick={tabActions.select.bind(null, address, null)}>
{serverStore.getName(address)}
</p>
);
return channels; return channels;
}); });

View File

@ -22,20 +22,14 @@ var UserList = React.createClass({
render: function() { render: function() {
var users = null; var users = null;
var tab = this.state.selectedTab; var tab = this.state.selectedTab;
var style = {}; var style = {};
if (!tab.channel || tab.channel[0] !== '#') { if (!tab.channel || tab.channel[0] !== '#') {
style.display = 'none'; style.display = 'none';
} } else {
users = _.map(channelStore.getUsers(tab.server, tab.channel), function(user) {
if (tab.channel && this.state.channels[tab.server]) { return <UserListItem key={user.nick} user={user} />;
var channel = this.state.channels[tab.server][tab.channel]; });
if (channel) {
users = _.map(channel.users, function(user) {
return <UserListItem user={user} />;
});
}
} }
return ( return (

View File

@ -186,6 +186,13 @@ var channelStore = Reflux.createStore({
return channels[server]; return channels[server];
}, },
getUsers: function(server, channel) {
if (channels[server] && channels[server][channel]) {
return channels[server][channel].users;
}
return [];
},
getState: function() { getState: function() {
return channels; return channels;
} }

View File

@ -1,6 +1,7 @@
var Reflux = require('reflux'); var Reflux = require('reflux');
var _ = require('lodash'); var _ = require('lodash');
var serverStore = require('./server');
var actions = require('../actions/tab'); var actions = require('../actions/tab');
var channelActions = require('../actions/channel'); var channelActions = require('../actions/channel');
@ -20,6 +21,13 @@ var selectedTabStore = Reflux.createStore({
select: function(server, channel) { select: function(server, channel) {
selectedTab.server = server; selectedTab.server = server;
selectedTab.channel = channel; selectedTab.channel = channel;
if (channel) {
selectedTab.name = channel;
} else {
selectedTab.name = serverStore.getName(server);
}
this.trigger(selectedTab); this.trigger(selectedTab);
}, },
@ -49,8 +57,8 @@ var selectedTabStore = Reflux.createStore({
} }
}); });
selectedTabStore.listen(function(selected) { selectedTabStore.listen(function(selectedTab) {
localStorage.selectedTab = JSON.stringify(selected); localStorage.selectedTab = JSON.stringify(selectedTab);
}); });
module.exports = selectedTabStore; module.exports = selectedTabStore;

View File

@ -10,11 +10,12 @@ var serverStore = Reflux.createStore({
this.listenToMany(actions); this.listenToMany(actions);
}, },
connect: function(server, nick, username) { connect: function(server, nick, username, tls, name) {
servers[server] = { servers[server] = {
address: server, address: server,
nick: nick, nick: nick,
username: username username: username,
name: name || server
}; };
this.trigger(servers); this.trigger(servers);
}, },
@ -30,6 +31,13 @@ var serverStore = Reflux.createStore({
return servers[server].nick; return servers[server].nick;
}, },
getName: function(server) {
if (servers[server]) {
return servers[server].name;
}
return null;
},
getState: function() { getState: function() {
return servers; return servers;
} }

4
irc.go
View File

@ -179,7 +179,7 @@ func (i *IRC) Writef(format string, a ...interface{}) {
} }
func (i *IRC) write(data string) { func (i *IRC) write(data string) {
fmt.Fprint(i.conn, data+"\r\n") i.conn.Write([]byte(data + "\r\n"))
} }
func (i *IRC) writef(format string, a ...interface{}) { func (i *IRC) writef(format string, a ...interface{}) {
@ -189,7 +189,7 @@ func (i *IRC) writef(format string, a ...interface{}) {
func (i *IRC) send() { func (i *IRC) send() {
i.ready.Wait() i.ready.Wait()
for message := range i.out { for message := range i.out {
fmt.Fprint(i.conn, message) i.conn.Write([]byte(message))
} }
} }

View File

@ -17,6 +17,7 @@ type WSResponse struct {
type Connect struct { type Connect struct {
Server string `json:"server"` Server string `json:"server"`
TLS bool `json:"tls"` TLS bool `json:"tls"`
Name string `json:"name"`
Nick string `json:"nick"` Nick string `json:"nick"`
Username string `json:"username"` Username string `json:"username"`
} }

View File

@ -31,7 +31,7 @@ func main() {
sessions[user.UUID] = session sessions[user.UUID] = session
irc := NewIRC(server.Nick, server.Username) irc := NewIRC(server.Nick, server.Username)
irc.TLS = true irc.TLS = server.TLS
irc.Connect(server.Address) irc.Connect(server.Address)
session.setIRC(irc.Host, irc) session.setIRC(irc.Host, irc)

View File

@ -9,6 +9,8 @@ import (
type Server struct { type Server struct {
Address string `json:"address"` Address string `json:"address"`
TLS bool `json:"tls"`
Name string `json:"name"`
Nick string `json:"nick"` Nick string `json:"nick"`
Username string `json:"username"` Username string `json:"username"`
Realname string `json:"realname"` Realname string `json:"realname"`

View File

@ -86,6 +86,8 @@ func handleWS(ws *websocket.Conn) {
session.user.AddServer(storage.Server{ session.user.AddServer(storage.Server{
Address: irc.Host, Address: irc.Host,
TLS: data.TLS,
Name: data.Name,
Nick: data.Nick, Nick: data.Nick,
Username: data.Username, Username: data.Username,
}) })