2017-05-26 06:20:00 +00:00
|
|
|
import { createSelector } from 'reselect';
|
2018-04-25 03:36:27 +00:00
|
|
|
import has from 'lodash/has';
|
2018-04-05 23:46:22 +00:00
|
|
|
import {
|
|
|
|
findBreakpoints,
|
|
|
|
messageHeight,
|
|
|
|
linkify,
|
|
|
|
timestamp,
|
2018-12-14 13:24:23 +00:00
|
|
|
isChannel,
|
|
|
|
formatDate
|
2018-04-05 23:46:22 +00:00
|
|
|
} from 'utils';
|
2018-04-05 19:13:32 +00:00
|
|
|
import createReducer from 'utils/createReducer';
|
2017-06-06 23:03:35 +00:00
|
|
|
import { getApp } from './app';
|
2017-05-26 06:20:00 +00:00
|
|
|
import { getSelectedTab } from './tab';
|
|
|
|
import * as actions from './actions';
|
|
|
|
|
|
|
|
export const getMessages = state => state.messages;
|
|
|
|
|
|
|
|
export const getSelectedMessages = createSelector(
|
|
|
|
getSelectedTab,
|
|
|
|
getMessages,
|
2018-04-25 03:36:27 +00:00
|
|
|
(tab, messages) => {
|
|
|
|
const target = tab.name || tab.server;
|
|
|
|
if (has(messages, [tab.server, target])) {
|
|
|
|
return messages[tab.server][target];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
2017-05-26 06:20:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
export const getHasMoreMessages = createSelector(
|
|
|
|
getSelectedMessages,
|
|
|
|
messages => {
|
2018-04-25 03:36:27 +00:00
|
|
|
const first = messages[0];
|
2017-05-26 06:20:00 +00:00
|
|
|
return first && first.next;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
function init(state, server, tab) {
|
|
|
|
if (!state[server]) {
|
|
|
|
state[server] = {};
|
|
|
|
}
|
|
|
|
if (!state[server][tab]) {
|
|
|
|
state[server][tab] = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 13:24:23 +00:00
|
|
|
let nextID = 0;
|
|
|
|
|
|
|
|
function createDateMessage(date) {
|
|
|
|
const message = {
|
|
|
|
id: nextID,
|
|
|
|
type: 'date',
|
|
|
|
content: formatDate(date),
|
|
|
|
height: 40
|
|
|
|
};
|
|
|
|
|
|
|
|
nextID++;
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isSameDay(d1, d2) {
|
|
|
|
return (
|
|
|
|
d1.getDate() === d2.getDate() &&
|
|
|
|
d1.getMonth() === d2.getMonth() &&
|
|
|
|
d1.getFullYear() === d2.getFullYear()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function reducerAddMessage(message, server, tab, state, prepend) {
|
|
|
|
const messages = state[server][tab];
|
|
|
|
|
|
|
|
if (messages.length > 0) {
|
|
|
|
if (prepend) {
|
|
|
|
const firstMessage = messages[0];
|
|
|
|
if (firstMessage.date && !isSameDay(firstMessage.date, message.date)) {
|
|
|
|
messages.unshift(createDateMessage(firstMessage.date));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const lastMessage = messages[messages.length - 1];
|
|
|
|
if (lastMessage.date && !isSameDay(lastMessage.date, message.date)) {
|
|
|
|
messages.push(createDateMessage(message.date));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prepend) {
|
|
|
|
messages.unshift(message);
|
|
|
|
} else {
|
|
|
|
messages.push(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
export default createReducer(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
[actions.ADD_MESSAGE](state, { server, tab, message }) {
|
|
|
|
init(state, server, tab);
|
2018-12-14 13:24:23 +00:00
|
|
|
reducerAddMessage(message, server, tab, state);
|
2018-04-25 03:36:27 +00:00
|
|
|
},
|
2017-05-26 06:20:00 +00:00
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
[actions.ADD_MESSAGES](state, { server, tab, messages, prepend }) {
|
2017-05-26 06:20:00 +00:00
|
|
|
if (prepend) {
|
2018-04-25 03:36:27 +00:00
|
|
|
init(state, server, tab);
|
2018-12-14 13:24:23 +00:00
|
|
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
|
|
reducerAddMessage(messages[i], server, tab, state, true);
|
|
|
|
}
|
2017-05-26 06:20:00 +00:00
|
|
|
} else {
|
2018-04-25 03:36:27 +00:00
|
|
|
messages.forEach(message => {
|
|
|
|
init(state, server, message.tab || tab);
|
2018-12-14 13:24:23 +00:00
|
|
|
reducerAddMessage(message, server, message.tab || tab, state);
|
2018-04-25 03:36:27 +00:00
|
|
|
});
|
2017-05-26 06:20:00 +00:00
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[actions.DISCONNECT](state, { server }) {
|
|
|
|
delete state[server];
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.PART](state, { server, channels }) {
|
|
|
|
channels.forEach(channel => delete state[server][channel]);
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.UPDATE_MESSAGE_HEIGHT](
|
|
|
|
state,
|
|
|
|
{ wrapWidth, charWidth, windowWidth }
|
|
|
|
) {
|
|
|
|
Object.keys(state).forEach(server =>
|
|
|
|
Object.keys(state[server]).forEach(target =>
|
|
|
|
state[server][target].forEach(message => {
|
2018-12-14 13:24:23 +00:00
|
|
|
if (message.type === 'date') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
message.height = messageHeight(
|
|
|
|
message,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
6 * charWidth,
|
|
|
|
windowWidth
|
|
|
|
);
|
|
|
|
})
|
2017-05-26 06:20:00 +00:00
|
|
|
)
|
2018-04-25 03:36:27 +00:00
|
|
|
);
|
2018-05-25 21:54:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[actions.socket.SERVERS](state, { data }) {
|
|
|
|
if (data) {
|
|
|
|
data.forEach(({ host }) => {
|
|
|
|
state[host] = {};
|
|
|
|
});
|
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
2017-05-26 06:20:00 +00:00
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
);
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2017-05-26 06:20:00 +00:00
|
|
|
function initMessage(message, tab, state) {
|
2017-04-19 23:51:55 +00:00
|
|
|
if (message.time) {
|
2018-12-14 13:24:23 +00:00
|
|
|
message.date = new Date(message.time * 1000);
|
2017-04-19 23:51:55 +00:00
|
|
|
} else {
|
2018-12-14 13:24:23 +00:00
|
|
|
message.date = new Date();
|
2016-02-16 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 13:24:23 +00:00
|
|
|
message.time = timestamp(message.date);
|
|
|
|
|
2017-04-20 03:32:22 +00:00
|
|
|
if (!message.id) {
|
|
|
|
message.id = nextID;
|
|
|
|
nextID++;
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:51:55 +00:00
|
|
|
if (tab.charAt(0) === '#') {
|
2016-02-16 21:43:25 +00:00
|
|
|
message.channel = true;
|
|
|
|
}
|
|
|
|
|
2017-04-17 02:11:45 +00:00
|
|
|
// Collapse multiple adjacent spaces into a single one
|
2017-04-17 20:36:37 +00:00
|
|
|
message.content = message.content.replace(/\s\s+/g, ' ');
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-04-17 20:36:37 +00:00
|
|
|
if (message.content.indexOf('\x01ACTION') === 0) {
|
2018-03-25 00:34:41 +00:00
|
|
|
const { from } = message;
|
2017-04-17 02:11:45 +00:00
|
|
|
message.from = null;
|
|
|
|
message.type = 'action';
|
2017-04-17 20:36:37 +00:00
|
|
|
message.content = from + message.content.slice(7, -1);
|
2017-04-17 02:11:45 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 23:03:35 +00:00
|
|
|
const { wrapWidth, charWidth, windowWidth } = getApp(state);
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-04-17 20:36:37 +00:00
|
|
|
message.length = message.content.length;
|
|
|
|
message.breakpoints = findBreakpoints(message.content);
|
2018-04-05 23:46:22 +00:00
|
|
|
message.height = messageHeight(
|
|
|
|
message,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
6 * charWidth,
|
|
|
|
windowWidth
|
|
|
|
);
|
2017-04-17 20:36:37 +00:00
|
|
|
message.content = linkify(message.content);
|
2016-02-16 21:43:25 +00:00
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2018-04-05 19:13:32 +00:00
|
|
|
export function getMessageTab(server, to) {
|
2017-06-23 00:27:56 +00:00
|
|
|
if (!to || to === '*' || (!isChannel(to) && to.indexOf('.') !== -1)) {
|
2017-04-19 23:51:55 +00:00
|
|
|
return server;
|
|
|
|
}
|
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
export function fetchMessages() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2018-04-25 03:36:27 +00:00
|
|
|
const first = getSelectedMessages(state)[0];
|
2017-05-02 21:21:25 +00:00
|
|
|
|
|
|
|
if (!first) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tab = state.tab.selected;
|
2018-04-25 03:36:27 +00:00
|
|
|
if (isChannel(tab)) {
|
2017-05-02 21:21:25 +00:00
|
|
|
dispatch({
|
|
|
|
type: actions.FETCH_MESSAGES,
|
|
|
|
socket: {
|
|
|
|
type: 'fetch_messages',
|
|
|
|
data: {
|
|
|
|
server: tab.server,
|
|
|
|
channel: tab.name,
|
|
|
|
next: first.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:04:37 +00:00
|
|
|
export function addFetchedMessages(server, tab) {
|
|
|
|
return {
|
|
|
|
type: actions.ADD_FETCHED_MESSAGES,
|
|
|
|
server,
|
|
|
|
tab
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-26 06:20:00 +00:00
|
|
|
export function updateMessageHeight(wrapWidth, charWidth, windowWidth) {
|
2017-05-12 08:51:37 +00:00
|
|
|
return {
|
2016-02-16 21:43:25 +00:00
|
|
|
type: actions.UPDATE_MESSAGE_HEIGHT,
|
2017-05-12 08:51:37 +00:00
|
|
|
wrapWidth,
|
2017-05-26 06:20:00 +00:00
|
|
|
charWidth,
|
|
|
|
windowWidth
|
2017-05-12 08:51:37 +00:00
|
|
|
};
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
2015-01-21 02:06:34 +00:00
|
|
|
|
2017-04-17 20:36:37 +00:00
|
|
|
export function sendMessage(content, to, server) {
|
2016-02-16 21:43:25 +00:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
|
2017-04-17 02:11:45 +00:00
|
|
|
dispatch({
|
2017-05-15 03:57:12 +00:00
|
|
|
type: actions.ADD_MESSAGE,
|
2017-04-19 23:51:55 +00:00
|
|
|
server,
|
|
|
|
tab: to,
|
2018-04-05 23:46:22 +00:00
|
|
|
message: initMessage(
|
|
|
|
{
|
2018-04-25 03:36:27 +00:00
|
|
|
from: state.servers[server].nick,
|
2018-04-05 23:46:22 +00:00
|
|
|
content
|
|
|
|
},
|
|
|
|
to,
|
|
|
|
state
|
|
|
|
),
|
2016-02-16 21:43:25 +00:00
|
|
|
socket: {
|
2017-04-17 20:36:37 +00:00
|
|
|
type: 'message',
|
|
|
|
data: { content, to, server }
|
2016-02-16 21:43:25 +00:00
|
|
|
}
|
2017-04-17 02:11:45 +00:00
|
|
|
});
|
2016-02-16 21:43:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:51:55 +00:00
|
|
|
export function addMessage(message, server, to) {
|
|
|
|
const tab = getMessageTab(server, to);
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2018-04-05 23:46:22 +00:00
|
|
|
return (dispatch, getState) =>
|
|
|
|
dispatch({
|
|
|
|
type: actions.ADD_MESSAGE,
|
|
|
|
server,
|
|
|
|
tab,
|
|
|
|
message: initMessage(message, tab, getState())
|
|
|
|
});
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
export function addMessages(messages, server, to, prepend, next) {
|
2017-04-19 23:51:55 +00:00
|
|
|
const tab = getMessageTab(server, to);
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2016-02-16 21:43:25 +00:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
if (next) {
|
|
|
|
messages[0].id = next;
|
2017-05-07 20:19:15 +00:00
|
|
|
messages[0].next = true;
|
2017-05-02 21:21:25 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 23:46:22 +00:00
|
|
|
messages.forEach(message =>
|
|
|
|
initMessage(message, message.tab || tab, state)
|
|
|
|
);
|
2016-02-16 21:43:25 +00:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: actions.ADD_MESSAGES,
|
2017-04-19 23:51:55 +00:00
|
|
|
server,
|
|
|
|
tab,
|
2017-05-02 21:21:25 +00:00
|
|
|
messages,
|
|
|
|
prepend
|
2016-02-16 21:43:25 +00:00
|
|
|
});
|
2015-12-28 23:34:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function broadcast(message, server, channels) {
|
2018-04-05 23:46:22 +00:00
|
|
|
return addMessages(
|
|
|
|
channels.map(channel => ({
|
|
|
|
tab: channel,
|
|
|
|
content: message,
|
|
|
|
type: 'info'
|
|
|
|
})),
|
|
|
|
server
|
|
|
|
);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
export function print(message, server, channel, type) {
|
2015-12-28 23:34:32 +00:00
|
|
|
if (Array.isArray(message)) {
|
2018-04-05 23:46:22 +00:00
|
|
|
return addMessages(
|
|
|
|
message.map(line => ({
|
|
|
|
content: line,
|
|
|
|
type
|
|
|
|
})),
|
|
|
|
server,
|
|
|
|
channel
|
|
|
|
);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 23:46:22 +00:00
|
|
|
return addMessage(
|
|
|
|
{
|
|
|
|
content: message,
|
|
|
|
type
|
|
|
|
},
|
|
|
|
server,
|
|
|
|
channel
|
|
|
|
);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
export function inform(message, server, channel) {
|
|
|
|
return print(message, server, channel, 'info');
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
export function runCommand(command, channel, server) {
|
|
|
|
return {
|
|
|
|
type: actions.COMMAND,
|
|
|
|
command,
|
|
|
|
channel,
|
|
|
|
server
|
|
|
|
};
|
|
|
|
}
|
2016-01-27 19:48:47 +00:00
|
|
|
|
|
|
|
export function raw(message, server) {
|
|
|
|
return {
|
|
|
|
type: actions.RAW,
|
|
|
|
message,
|
|
|
|
server,
|
|
|
|
socket: {
|
|
|
|
type: 'raw',
|
|
|
|
data: { message, server }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|