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,
|
2020-06-03 01:04:38 +00:00
|
|
|
formatDate,
|
|
|
|
unix
|
2018-04-05 23:46:22 +00:00
|
|
|
} from 'utils';
|
2020-05-16 06:25:58 +00:00
|
|
|
import colorify from 'utils/colorify';
|
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) => {
|
2020-06-15 08:58:51 +00:00
|
|
|
const target = tab.name || tab.network;
|
|
|
|
if (has(messages, [tab.network, target])) {
|
|
|
|
return messages[tab.network][target];
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
function init(state, network, tab) {
|
|
|
|
if (!state[network]) {
|
|
|
|
state[network] = {};
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
2020-06-15 08:58:51 +00:00
|
|
|
if (!state[network][tab]) {
|
|
|
|
state[network][tab] = [];
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 11:24:23 +00:00
|
|
|
function initNetworks(state, networks = []) {
|
|
|
|
networks.forEach(({ host }) => {
|
|
|
|
state[host] = {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:32:29 +00:00
|
|
|
const collapsedEvents = ['join', 'part', 'quit', 'nick'];
|
2020-06-03 01:04:38 +00:00
|
|
|
|
|
|
|
function shouldCollapse(msg1, msg2) {
|
|
|
|
return (
|
|
|
|
msg1.events &&
|
|
|
|
msg2.events &&
|
|
|
|
collapsedEvents.indexOf(msg1.events[0].type) !== -1 &&
|
|
|
|
collapsedEvents.indexOf(msg2.events[0].type) !== -1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-30 11:24:23 +00:00
|
|
|
const blocks = {
|
|
|
|
nick: nick => ({ type: 'nick', text: nick }),
|
|
|
|
text: text => ({ type: 'text', text }),
|
|
|
|
events: count => ({ type: 'events', text: `${count} more` })
|
|
|
|
};
|
|
|
|
|
2020-06-03 01:04:38 +00:00
|
|
|
const eventVerbs = {
|
2020-06-30 11:24:23 +00:00
|
|
|
join: 'joined',
|
|
|
|
part: 'left',
|
2020-06-03 01:04:38 +00:00
|
|
|
quit: 'quit'
|
|
|
|
};
|
|
|
|
|
2020-07-05 06:32:29 +00:00
|
|
|
function renderEvent(result, type, events) {
|
2020-06-03 01:04:38 +00:00
|
|
|
const ending = eventVerbs[type];
|
|
|
|
|
2020-07-05 06:32:29 +00:00
|
|
|
if (result.length > 1) {
|
|
|
|
result[result.length - 1].text += ', ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events.length === 1) {
|
|
|
|
result.push(blocks.nick(events[0][0]));
|
|
|
|
result.push(blocks.text(` ${ending}`));
|
|
|
|
} else if (events.length === 2) {
|
|
|
|
result.push(blocks.nick(events[0][0]));
|
|
|
|
result.push(blocks.text(' and '));
|
|
|
|
result.push(blocks.nick(events[1][0]));
|
|
|
|
result.push(blocks.text(` ${ending}`));
|
|
|
|
} else if (events.length > 2) {
|
|
|
|
result.push(blocks.nick(events[0][0]));
|
|
|
|
result.push(blocks.text(', '));
|
|
|
|
result.push(blocks.nick(events[1][0]));
|
|
|
|
result.push(blocks.text(' and '));
|
|
|
|
result.push(blocks.events(events.length - 2));
|
|
|
|
result.push(blocks.text(` ${ending}`));
|
2020-06-03 01:04:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderEvents(events) {
|
|
|
|
const first = events[0];
|
2020-06-15 08:58:51 +00:00
|
|
|
|
|
|
|
if (first.type === 'kick') {
|
|
|
|
const [kicked, by] = first.params;
|
|
|
|
|
2020-06-30 11:24:23 +00:00
|
|
|
return [blocks.nick(by), blocks.text(' kicked '), blocks.nick(kicked)];
|
2020-06-15 08:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 01:04:38 +00:00
|
|
|
if (first.type === 'topic') {
|
2020-06-30 11:24:23 +00:00
|
|
|
const [nick, topic] = first.params;
|
2020-06-03 01:04:38 +00:00
|
|
|
|
|
|
|
if (!topic) {
|
2020-06-30 11:24:23 +00:00
|
|
|
return [blocks.nick(nick), blocks.text(' cleared the topic')];
|
2020-06-03 01:04:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 11:24:23 +00:00
|
|
|
return [
|
|
|
|
blocks.nick(nick),
|
|
|
|
blocks.text(' changed the topic to: '),
|
|
|
|
...colorify(linkify(topic))
|
|
|
|
];
|
2020-06-03 01:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const byType = {};
|
|
|
|
for (let i = events.length - 1; i >= 0; i--) {
|
|
|
|
const event = events[i];
|
|
|
|
const [nick] = event.params;
|
|
|
|
|
|
|
|
if (!byType[event.type]) {
|
2020-07-05 06:32:29 +00:00
|
|
|
byType[event.type] = [event.params];
|
2020-06-03 01:04:38 +00:00
|
|
|
} else if (byType[event.type].indexOf(nick) === -1) {
|
2020-07-05 06:32:29 +00:00
|
|
|
byType[event.type].push(event.params);
|
2020-06-03 01:04:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = [];
|
|
|
|
|
|
|
|
if (byType.join) {
|
|
|
|
renderEvent(result, 'join', byType.join);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (byType.part) {
|
|
|
|
renderEvent(result, 'part', byType.part);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (byType.quit) {
|
2020-07-05 06:32:29 +00:00
|
|
|
renderEvent(result, 'quit', byType.quit);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (byType.nick) {
|
2020-06-03 01:04:38 +00:00
|
|
|
if (result.length > 1) {
|
2020-06-30 11:24:23 +00:00
|
|
|
result[result.length - 1].text += ', ';
|
2020-06-03 01:04:38 +00:00
|
|
|
}
|
2020-07-05 06:32:29 +00:00
|
|
|
|
|
|
|
const [oldNick, newNick] = byType.nick[0];
|
|
|
|
|
|
|
|
result.push(blocks.nick(oldNick));
|
|
|
|
result.push(blocks.text(' changed nick to '));
|
|
|
|
result.push(blocks.nick(newNick));
|
|
|
|
|
|
|
|
if (byType.nick.length > 1) {
|
|
|
|
result.push(blocks.text(' and '));
|
|
|
|
result.push(blocks.events(byType.nick.length - 1));
|
|
|
|
result.push(blocks.text(' changed nick'));
|
|
|
|
}
|
2020-06-03 01:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-14 13:24:23 +00:00
|
|
|
let nextID = 0;
|
|
|
|
|
2020-06-03 01:04:38 +00:00
|
|
|
function initMessage(
|
|
|
|
state,
|
|
|
|
message,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2020-06-03 01:04:38 +00:00
|
|
|
tab,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
windowWidth,
|
|
|
|
prepend
|
|
|
|
) {
|
2020-06-15 08:58:51 +00:00
|
|
|
const messages = state[network][tab];
|
2020-06-03 01:04:38 +00:00
|
|
|
|
|
|
|
if (messages.length > 0 && !prepend) {
|
|
|
|
const lastMessage = messages[messages.length - 1];
|
|
|
|
if (shouldCollapse(lastMessage, message)) {
|
|
|
|
lastMessage.events.push(message.events[0]);
|
|
|
|
lastMessage.content = renderEvents(lastMessage.events);
|
|
|
|
|
|
|
|
[lastMessage.breakpoints, lastMessage.length] = findBreakpoints(
|
|
|
|
lastMessage.content
|
|
|
|
);
|
|
|
|
lastMessage.height = messageHeight(
|
|
|
|
lastMessage,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
6 * charWidth,
|
|
|
|
windowWidth
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.time) {
|
|
|
|
message.date = new Date(message.time * 1000);
|
|
|
|
} else {
|
|
|
|
message.date = new Date();
|
|
|
|
}
|
|
|
|
|
|
|
|
message.time = timestamp(message.date);
|
|
|
|
|
|
|
|
if (!message.id) {
|
|
|
|
message.id = nextID;
|
|
|
|
nextID++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tab.charAt(0) === '#') {
|
|
|
|
message.channel = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.events) {
|
|
|
|
message.type = 'info';
|
|
|
|
message.content = renderEvents(message.events);
|
|
|
|
} else {
|
|
|
|
message.content = message.content || '';
|
|
|
|
// Collapse multiple adjacent spaces into a single one
|
|
|
|
message.content = message.content.replace(/\s\s+/g, ' ');
|
|
|
|
|
|
|
|
if (message.content.indexOf('\x01ACTION') === 0) {
|
|
|
|
const { from } = message;
|
|
|
|
message.from = null;
|
|
|
|
message.type = 'action';
|
|
|
|
message.content = from + message.content.slice(7, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!message.events) {
|
|
|
|
message.content = colorify(linkify(message.content));
|
|
|
|
}
|
|
|
|
|
|
|
|
[message.breakpoints, message.length] = findBreakpoints(message.content);
|
|
|
|
message.height = messageHeight(
|
|
|
|
message,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
6 * charWidth,
|
|
|
|
windowWidth
|
|
|
|
);
|
|
|
|
message.indent = 6 * charWidth;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-14 13:24:23 +00:00
|
|
|
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()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-03 01:04:38 +00:00
|
|
|
function reducerPrependMessages(
|
|
|
|
state,
|
|
|
|
messages,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2020-06-03 01:04:38 +00:00
|
|
|
tab,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
windowWidth
|
|
|
|
) {
|
2018-12-15 10:02:35 +00:00
|
|
|
const msgs = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < messages.length; i++) {
|
2020-06-03 01:04:38 +00:00
|
|
|
const message = messages[i];
|
|
|
|
initMessage(
|
|
|
|
state,
|
|
|
|
message,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2020-06-03 01:04:38 +00:00
|
|
|
tab,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
windowWidth,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
if (i > 0 && !isSameDay(messages[i - 1].date, message.date)) {
|
|
|
|
msgs.push(createDateMessage(message.date));
|
2018-12-15 10:02:35 +00:00
|
|
|
}
|
2020-06-03 01:04:38 +00:00
|
|
|
msgs.push(message);
|
2018-12-15 10:02:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
const m = state[network][tab];
|
2018-12-15 10:02:35 +00:00
|
|
|
|
|
|
|
if (m.length > 0) {
|
|
|
|
const lastNewMessage = msgs[msgs.length - 1];
|
|
|
|
const firstMessage = m[0];
|
|
|
|
if (
|
|
|
|
firstMessage.date &&
|
|
|
|
!isSameDay(firstMessage.date, lastNewMessage.date)
|
|
|
|
) {
|
|
|
|
msgs.push(createDateMessage(firstMessage.date));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.unshift(...msgs);
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
function reducerAddMessage(message, network, tab, state) {
|
|
|
|
const messages = state[network][tab];
|
2018-12-14 13:24:23 +00:00
|
|
|
|
|
|
|
if (messages.length > 0) {
|
2018-12-15 10:02:35 +00:00
|
|
|
const lastMessage = messages[messages.length - 1];
|
|
|
|
if (lastMessage.date && !isSameDay(lastMessage.date, message.date)) {
|
|
|
|
messages.push(createDateMessage(message.date));
|
2018-12-14 13:24:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 10:02:35 +00:00
|
|
|
messages.push(message);
|
2018-12-14 13:24:23 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
export default createReducer(
|
|
|
|
{},
|
|
|
|
{
|
2020-06-03 01:04:38 +00:00
|
|
|
[actions.ADD_MESSAGE](
|
|
|
|
state,
|
2020-06-15 08:58:51 +00:00
|
|
|
{ network, tab, message, wrapWidth, charWidth, windowWidth }
|
2020-06-03 01:04:38 +00:00
|
|
|
) {
|
2020-06-15 08:58:51 +00:00
|
|
|
init(state, network, tab);
|
2020-06-03 01:04:38 +00:00
|
|
|
|
|
|
|
const shouldAdd = initMessage(
|
|
|
|
state,
|
|
|
|
message,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2020-06-03 01:04:38 +00:00
|
|
|
tab,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
windowWidth
|
|
|
|
);
|
|
|
|
if (shouldAdd) {
|
2020-06-15 08:58:51 +00:00
|
|
|
reducerAddMessage(message, network, tab, state);
|
2020-06-03 01:04:38 +00:00
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
},
|
2017-05-26 06:20:00 +00:00
|
|
|
|
2020-06-03 01:04:38 +00:00
|
|
|
[actions.ADD_MESSAGES](
|
|
|
|
state,
|
2020-06-15 08:58:51 +00:00
|
|
|
{ network, tab, messages, prepend, wrapWidth, charWidth, windowWidth }
|
2020-06-03 01:04:38 +00:00
|
|
|
) {
|
2017-05-26 06:20:00 +00:00
|
|
|
if (prepend) {
|
2020-06-15 08:58:51 +00:00
|
|
|
init(state, network, tab);
|
2020-06-03 01:04:38 +00:00
|
|
|
reducerPrependMessages(
|
|
|
|
state,
|
|
|
|
messages,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2020-06-03 01:04:38 +00:00
|
|
|
tab,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
windowWidth
|
|
|
|
);
|
2017-05-26 06:20:00 +00:00
|
|
|
} else {
|
2018-12-15 10:02:35 +00:00
|
|
|
if (!messages[0].tab) {
|
2020-06-15 08:58:51 +00:00
|
|
|
init(state, network, tab);
|
2018-12-15 10:02:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
messages.forEach(message => {
|
2018-12-15 10:02:35 +00:00
|
|
|
if (message.tab) {
|
2020-06-15 08:58:51 +00:00
|
|
|
init(state, network, message.tab);
|
2018-12-15 10:02:35 +00:00
|
|
|
}
|
2020-06-03 01:04:38 +00:00
|
|
|
|
|
|
|
const shouldAdd = initMessage(
|
|
|
|
state,
|
|
|
|
message,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2020-06-03 01:04:38 +00:00
|
|
|
message.tab || tab,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
windowWidth
|
|
|
|
);
|
|
|
|
if (shouldAdd) {
|
2020-06-15 08:58:51 +00:00
|
|
|
reducerAddMessage(message, network, message.tab || tab, state);
|
2020-06-03 01:04:38 +00:00
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
});
|
2017-05-26 06:20:00 +00:00
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
[actions.DISCONNECT](state, { network }) {
|
|
|
|
delete state[network];
|
2018-04-25 03:36:27 +00:00
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
[actions.PART](state, { network, channels }) {
|
|
|
|
channels.forEach(channel => delete state[network][channel]);
|
2018-04-25 03:36:27 +00:00
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
[actions.CLOSE_PRIVATE_CHAT](state, { network, nick }) {
|
|
|
|
delete state[network][nick];
|
2020-05-08 00:56:54 +00:00
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
[actions.socket.CHANNEL_FORWARD](state, { network, old }) {
|
|
|
|
if (state[network]) {
|
|
|
|
delete state[network][old];
|
2019-01-25 10:02:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
[actions.UPDATE_MESSAGE_HEIGHT](
|
|
|
|
state,
|
|
|
|
{ wrapWidth, charWidth, windowWidth }
|
|
|
|
) {
|
2020-06-15 08:58:51 +00:00
|
|
|
Object.keys(state).forEach(network =>
|
|
|
|
Object.keys(state[network]).forEach(target =>
|
|
|
|
state[network][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
|
|
|
},
|
|
|
|
|
2020-06-30 11:24:23 +00:00
|
|
|
[actions.INIT](state, { networks }) {
|
|
|
|
initNetworks(state, networks);
|
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
[actions.socket.NETWORKS](state, { data }) {
|
2020-06-30 11:24:23 +00:00
|
|
|
initNetworks(state, data);
|
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
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function getMessageTab(network, to) {
|
2017-06-23 00:27:56 +00:00
|
|
|
if (!to || to === '*' || (!isChannel(to) && to.indexOf('.') !== -1)) {
|
2020-06-15 08:58:51 +00:00
|
|
|
return network;
|
2017-04-19 23:51:55 +00:00
|
|
|
}
|
|
|
|
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;
|
2020-05-19 22:30:44 +00:00
|
|
|
if (tab.name) {
|
2017-05-02 21:21:25 +00:00
|
|
|
dispatch({
|
|
|
|
type: actions.FETCH_MESSAGES,
|
|
|
|
socket: {
|
|
|
|
type: 'fetch_messages',
|
|
|
|
data: {
|
2020-06-15 08:58:51 +00:00
|
|
|
network: tab.network,
|
2017-05-02 21:21:25 +00:00
|
|
|
channel: tab.name,
|
|
|
|
next: first.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function addFetchedMessages(network, tab) {
|
2017-06-06 22:04:37 +00:00
|
|
|
return {
|
|
|
|
type: actions.ADD_FETCHED_MESSAGES,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2017-06-06 22:04:37 +00:00
|
|
|
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
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function sendMessage(content, to, network) {
|
2016-02-16 21:43:25 +00:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2020-06-03 01:04:38 +00:00
|
|
|
const { wrapWidth, charWidth, windowWidth } = getApp(state);
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-04-17 02:11:45 +00:00
|
|
|
dispatch({
|
2017-05-15 03:57:12 +00:00
|
|
|
type: actions.ADD_MESSAGE,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2017-04-19 23:51:55 +00:00
|
|
|
tab: to,
|
2020-06-03 01:04:38 +00:00
|
|
|
message: {
|
2020-06-15 08:58:51 +00:00
|
|
|
from: state.networks[network].nick,
|
2020-06-03 01:04:38 +00:00
|
|
|
content
|
|
|
|
},
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
windowWidth,
|
2016-02-16 21:43:25 +00:00
|
|
|
socket: {
|
2017-04-17 20:36:37 +00:00
|
|
|
type: 'message',
|
2020-06-15 08:58:51 +00:00
|
|
|
data: { content, to, network }
|
2016-02-16 21:43:25 +00:00
|
|
|
}
|
2017-04-17 02:11:45 +00:00
|
|
|
});
|
2016-02-16 21:43:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function addMessage(message, network, to) {
|
|
|
|
const tab = getMessageTab(network, to);
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2020-06-03 01:04:38 +00:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const { wrapWidth, charWidth, windowWidth } = getApp(getState());
|
|
|
|
|
2018-04-05 23:46:22 +00:00
|
|
|
dispatch({
|
|
|
|
type: actions.ADD_MESSAGE,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2018-04-05 23:46:22 +00:00
|
|
|
tab,
|
2020-06-03 01:04:38 +00:00
|
|
|
message,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
windowWidth
|
2018-04-05 23:46:22 +00:00
|
|
|
});
|
2020-06-03 01:04:38 +00:00
|
|
|
};
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function addMessages(messages, network, to, prepend, next) {
|
|
|
|
const tab = getMessageTab(network, 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
|
|
|
}
|
|
|
|
|
2020-06-03 01:04:38 +00:00
|
|
|
const { wrapWidth, charWidth, windowWidth } = getApp(state);
|
2016-02-16 21:43:25 +00:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: actions.ADD_MESSAGES,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2017-04-19 23:51:55 +00:00
|
|
|
tab,
|
2017-05-02 21:21:25 +00:00
|
|
|
messages,
|
2020-06-03 01:04:38 +00:00
|
|
|
prepend,
|
|
|
|
wrapWidth,
|
|
|
|
charWidth,
|
|
|
|
windowWidth
|
2016-02-16 21:43:25 +00:00
|
|
|
});
|
2015-12-28 23:34:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function addEvent(network, tab, type, ...params) {
|
2020-06-03 01:04:38 +00:00
|
|
|
return addMessage(
|
|
|
|
{
|
|
|
|
type: 'info',
|
|
|
|
events: [
|
|
|
|
{
|
|
|
|
type,
|
|
|
|
params,
|
|
|
|
time: unix()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2020-06-03 01:04:38 +00:00
|
|
|
tab
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function broadcastEvent(network, channels, type, ...params) {
|
2020-06-03 01:04:38 +00:00
|
|
|
const now = unix();
|
|
|
|
|
|
|
|
return addMessages(
|
|
|
|
channels.map(channel => ({
|
|
|
|
type: 'info',
|
|
|
|
tab: channel,
|
|
|
|
events: [
|
|
|
|
{
|
|
|
|
type,
|
|
|
|
params,
|
|
|
|
time: now
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})),
|
2020-06-15 08:58:51 +00:00
|
|
|
network
|
2020-06-03 01:04:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function broadcast(message, network, channels) {
|
2018-04-05 23:46:22 +00:00
|
|
|
return addMessages(
|
|
|
|
channels.map(channel => ({
|
|
|
|
tab: channel,
|
|
|
|
content: message,
|
|
|
|
type: 'info'
|
|
|
|
})),
|
2020-06-15 08:58:51 +00:00
|
|
|
network
|
2018-04-05 23:46:22 +00:00
|
|
|
);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function print(message, network, 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
|
|
|
|
})),
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2018-04-05 23:46:22 +00:00
|
|
|
channel
|
|
|
|
);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 23:46:22 +00:00
|
|
|
return addMessage(
|
|
|
|
{
|
|
|
|
content: message,
|
|
|
|
type
|
|
|
|
},
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2018-04-05 23:46:22 +00:00
|
|
|
channel
|
|
|
|
);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function inform(message, network, channel) {
|
|
|
|
return print(message, network, channel, 'info');
|
2017-05-28 05:20:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function runCommand(command, channel, network) {
|
2015-12-28 23:34:32 +00:00
|
|
|
return {
|
|
|
|
type: actions.COMMAND,
|
|
|
|
command,
|
|
|
|
channel,
|
2020-06-15 08:58:51 +00:00
|
|
|
network
|
2015-12-28 23:34:32 +00:00
|
|
|
};
|
|
|
|
}
|
2016-01-27 19:48:47 +00:00
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function raw(message, network) {
|
2016-01-27 19:48:47 +00:00
|
|
|
return {
|
|
|
|
type: actions.RAW,
|
|
|
|
message,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2016-01-27 19:48:47 +00:00
|
|
|
socket: {
|
|
|
|
type: 'raw',
|
2020-06-15 08:58:51 +00:00
|
|
|
data: { message, network }
|
2016-01-27 19:48:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|