2015-12-28 23:34:32 +00:00
|
|
|
import * as actions from '../actions';
|
2017-04-17 02:11:45 +00:00
|
|
|
import { findBreakpoints, messageHeight, linkify, timestamp } from '../util';
|
2017-05-02 21:21:25 +00:00
|
|
|
import { getSelectedMessages } from '../reducers/messages';
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2017-04-20 03:32:22 +00:00
|
|
|
let nextID = 0;
|
|
|
|
|
2017-04-19 23:51:55 +00:00
|
|
|
function initMessage(message, server, tab, state) {
|
|
|
|
if (message.time) {
|
|
|
|
message.time = timestamp(new Date(message.time * 1000));
|
|
|
|
} else {
|
|
|
|
message.time = timestamp();
|
2016-02-16 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2017-04-17 02:11:45 +00:00
|
|
|
const from = message.from;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-16 21:43:25 +00:00
|
|
|
const charWidth = state.environment.get('charWidth');
|
|
|
|
const wrapWidth = state.environment.get('wrapWidth');
|
|
|
|
|
2017-04-17 20:36:37 +00:00
|
|
|
message.length = message.content.length;
|
|
|
|
message.breakpoints = findBreakpoints(message.content);
|
2016-02-16 21:43:25 +00:00
|
|
|
message.height = messageHeight(message, wrapWidth, charWidth, 6 * charWidth);
|
2017-04-17 20:36:37 +00:00
|
|
|
message.content = linkify(message.content);
|
2016-02-16 21:43:25 +00:00
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:51:55 +00:00
|
|
|
function getMessageTab(server, to) {
|
2017-05-16 21:22:03 +00:00
|
|
|
if (!to || to.indexOf('.') !== -1 || to === '*') {
|
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();
|
|
|
|
const first = getSelectedMessages(state).get(0);
|
|
|
|
|
|
|
|
if (!first) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tab = state.tab.selected;
|
|
|
|
if (tab.isChannel()) {
|
|
|
|
dispatch({
|
|
|
|
type: actions.FETCH_MESSAGES,
|
|
|
|
socket: {
|
|
|
|
type: 'fetch_messages',
|
|
|
|
data: {
|
|
|
|
server: tab.server,
|
|
|
|
channel: tab.name,
|
|
|
|
next: first.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-12 08:51:37 +00:00
|
|
|
export function updateMessageHeight(wrapWidth, charWidth) {
|
|
|
|
return {
|
2016-02-16 21:43:25 +00:00
|
|
|
type: actions.UPDATE_MESSAGE_HEIGHT,
|
2017-05-12 08:51:37 +00:00
|
|
|
wrapWidth,
|
|
|
|
charWidth
|
|
|
|
};
|
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,
|
2017-04-17 02:11:45 +00:00
|
|
|
message: initMessage({
|
|
|
|
from: state.servers.getIn([server, 'nick']),
|
2017-04-19 23:51:55 +00:00
|
|
|
content
|
|
|
|
}, server, 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
|
|
|
|
2016-02-16 21:43:25 +00:00
|
|
|
return (dispatch, getState) => dispatch({
|
2015-12-28 23:34:32 +00:00
|
|
|
type: actions.ADD_MESSAGE,
|
2017-04-19 23:51:55 +00:00
|
|
|
server,
|
|
|
|
tab,
|
|
|
|
message: initMessage(message, server, tab, getState())
|
2016-02-16 21:43:25 +00:00
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 23:51:55 +00:00
|
|
|
messages.forEach(message => initMessage(message, server, 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) {
|
2016-02-04 02:35:50 +00:00
|
|
|
return addMessages(channels.map(channel => ({
|
2017-04-19 23:51:55 +00:00
|
|
|
tab: channel,
|
2017-04-17 20:36:37 +00:00
|
|
|
content: message,
|
2016-02-04 02:35:50 +00:00
|
|
|
type: 'info'
|
2017-04-19 23:51:55 +00:00
|
|
|
})), server);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function inform(message, server, channel) {
|
|
|
|
if (Array.isArray(message)) {
|
2017-04-17 20:36:37 +00:00
|
|
|
return addMessages(message.map(line => ({
|
|
|
|
content: line,
|
2016-02-04 02:35:50 +00:00
|
|
|
type: 'info'
|
2017-04-19 23:51:55 +00:00
|
|
|
})), server, channel);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return addMessage({
|
2017-04-17 20:36:37 +00:00
|
|
|
content: message,
|
2015-12-28 23:34:32 +00:00
|
|
|
type: 'info'
|
2017-04-19 23:51:55 +00:00
|
|
|
}, server, channel);
|
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 }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|