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": {
"lodash": "3.0.0",
"reflux": "~0.2.2",
"reflux": "0.2.4",
"react-router": "~0.11.6",
"react": "~0.12.2"
}

View File

@ -8,12 +8,13 @@ var serverActions = Reflux.createActions([
'load'
]);
serverActions.connect.preEmit = function(server, nick, username, tls) {
serverActions.connect.preEmit = function(server, nick, username, tls, name) {
socket.send('connect', {
server: server,
nick: nick,
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.send('uuid', uuid);
serverActions.connect('irc.freenode.net', nick, 'username', true);
serverActions.connect('irc.quakenet.org', nick, 'username');
serverActions.connect('irc.freenode.net', nick, 'username', true, 'Freenode');
serverActions.connect('irc.quakenet.org', nick, 'username', false, 'QuakeNet');
channelActions.join(['#stuff'], 'irc.freenode.net');
channelActions.join(['#herp'], 'irc.quakenet.org');

View File

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

View File

@ -4,6 +4,7 @@ var _ = require('lodash');
var channelStore = require('../stores/channel');
var privateChatStore = require('../stores/privateChat');
var serverStore = require('../stores/server');
var selectedTabStore = require('../stores/selectedTab');
var tabActions = require('../actions/tab');
@ -11,14 +12,16 @@ var TabList = React.createClass({
mixins: [
Reflux.connect(channelStore, 'channels'),
Reflux.connect(privateChatStore, 'privateChats'),
Reflux.connect(selectedTabStore, 'selectedTab')
Reflux.connect(selectedTabStore, 'selectedTab'),
Reflux.connect(serverStore, 'servers')
],
getInitialState: function() {
return {
channels: channelStore.getState(),
privateChats: privateChatStore.getState(),
selectedTab: selectedTabStore.getState()
selectedTab: selectedTabStore.getState(),
servers: serverStore.getState()
};
},
@ -36,7 +39,13 @@ var TabList = React.createClass({
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) {
@ -46,8 +55,14 @@ var TabList = React.createClass({
} else {
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 &&
@ -57,7 +72,13 @@ var TabList = React.createClass({
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;
});

View File

@ -22,20 +22,14 @@ var UserList = React.createClass({
render: function() {
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) {
users = _.map(channel.users, function(user) {
return <UserListItem user={user} />;
});
}
} else {
users = _.map(channelStore.getUsers(tab.server, tab.channel), function(user) {
return <UserListItem key={user.nick} user={user} />;
});
}
return (

View File

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

View File

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

View File

@ -10,11 +10,12 @@ var serverStore = Reflux.createStore({
this.listenToMany(actions);
},
connect: function(server, nick, username) {
connect: function(server, nick, username, tls, name) {
servers[server] = {
address: server,
nick: nick,
username: username
username: username,
name: name || server
};
this.trigger(servers);
},
@ -30,6 +31,13 @@ var serverStore = Reflux.createStore({
return servers[server].nick;
},
getName: function(server) {
if (servers[server]) {
return servers[server].name;
}
return null;
},
getState: function() {
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) {
fmt.Fprint(i.conn, data+"\r\n")
i.conn.Write([]byte(data + "\r\n"))
}
func (i *IRC) writef(format string, a ...interface{}) {
@ -189,7 +189,7 @@ func (i *IRC) writef(format string, a ...interface{}) {
func (i *IRC) send() {
i.ready.Wait()
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 {
Server string `json:"server"`
TLS bool `json:"tls"`
Name string `json:"name"`
Nick string `json:"nick"`
Username string `json:"username"`
}

View File

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

View File

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

View File

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