dispatch/client/src/js/modules/socket.js

126 lines
3.5 KiB
JavaScript
Raw Normal View History

import { socketAction } from '../state/actions';
2017-06-06 23:03:35 +00:00
import { setConnected } from '../state/app';
import { broadcast, inform, print, addMessage, addMessages } from '../state/messages';
import { select } from '../state/tab';
2017-05-19 05:29:44 +00:00
import { normalizeChannel } from '../util';
import { replace } from '../util/router';
2015-12-28 23:34:32 +00:00
function withReason(message, reason) {
return message + (reason ? ` (${reason})` : '');
2015-02-21 06:48:25 +00:00
}
2015-01-17 01:37:21 +00:00
function findChannels(state, server, user) {
const channels = [];
state.channels.get(server).forEach((channel, channelName) => {
if (channel.get('users').find(u => u.nick === user)) {
channels.push(channelName);
2015-12-28 23:34:32 +00:00
}
});
return channels;
}
2017-05-19 05:29:44 +00:00
export default function handleSocket({ socket, store: { dispatch, getState } }) {
const handlers = {
message(message) {
dispatch(addMessage(message, message.server, message.to));
},
pm(message) {
dispatch(addMessage(message, message.server, message.from));
},
2017-05-02 21:21:25 +00:00
messages({ messages, server, to, prepend, next }) {
dispatch(addMessages(messages, server, to, prepend, next));
},
2017-05-02 21:21:25 +00:00
join({ user, server, channels }) {
const state = getState();
2017-05-02 21:21:25 +00:00
const tab = state.tab.selected;
const [joinedChannel] = channels;
if (tab.server && tab.name) {
const { nick } = state.servers.get(tab.server);
if (tab.server === server &&
nick === user &&
tab.name !== joinedChannel &&
normalizeChannel(tab.name) === normalizeChannel(joinedChannel)) {
dispatch(select(server, joinedChannel));
}
}
2017-05-02 21:21:25 +00:00
dispatch(inform(`${user} joined the channel`, server, joinedChannel));
},
servers(data) {
if (!data) {
dispatch(replace('/connect'));
}
},
part({ user, server, channel, reason }) {
dispatch(inform(withReason(`${user} left the channel`, reason), server, channel));
},
quit({ user, server, reason }) {
const channels = findChannels(getState(), server, user);
dispatch(broadcast(withReason(`${user} quit`, reason), server, channels));
},
nick({ server, oldNick, newNick }) {
const channels = findChannels(getState(), server, oldNick);
dispatch(broadcast(`${oldNick} changed nick to ${newNick}`, server, channels));
},
topic({ server, channel, topic, nick }) {
if (nick) {
if (topic) {
dispatch(inform(`${nick} changed the topic to:`, server, channel));
dispatch(print(topic, server, channel));
} else {
dispatch(inform(`${nick} cleared the topic`, server, channel));
}
}
},
motd({ content, server }) {
dispatch(addMessages(content.map(line => ({ content: line })), server));
},
whois(data) {
const tab = getState().tab.selected;
dispatch(print([
`Nick: ${data.nick}`,
`Username: ${data.username}`,
`Realname: ${data.realname}`,
`Host: ${data.host}`,
`Server: ${data.server}`,
`Channels: ${data.channels}`
], tab.server, tab.name));
},
print(message) {
const tab = getState().tab.selected;
dispatch(addMessage(message, tab.server, tab.name));
2017-05-29 04:16:24 +00:00
},
_connected(connected) {
2017-06-06 23:03:35 +00:00
dispatch(setConnected(connected));
2015-12-28 23:34:32 +00:00
}
};
2015-12-28 23:34:32 +00:00
socket.onMessage((type, data) => {
if (type in handlers) {
handlers[type](data);
2015-12-28 23:34:32 +00:00
}
type = socketAction(type);
if (Array.isArray(data)) {
dispatch({ type, data });
} else {
dispatch({ type, ...data });
}
2015-12-28 23:34:32 +00:00
});
}