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';
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-02-16 21:43:25 +00:00
|
|
|
function initMessage(message, state) {
|
|
|
|
message.dest = message.to || message.from || message.server;
|
|
|
|
if (message.from && message.from.indexOf('.') !== -1) {
|
|
|
|
message.dest = message.server;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.dest.charAt(0) === '#') {
|
|
|
|
message.channel = true;
|
|
|
|
}
|
|
|
|
|
2017-04-17 02:11:45 +00:00
|
|
|
// Collapse multiple adjacent spaces into a single one
|
2016-02-16 21:43:25 +00:00
|
|
|
message.message = message.message.replace(/\s\s+/g, ' ');
|
|
|
|
|
2017-04-17 02:11:45 +00:00
|
|
|
if (message.message.indexOf('\x01ACTION') === 0) {
|
|
|
|
const from = message.from;
|
|
|
|
message.from = null;
|
|
|
|
message.type = 'action';
|
|
|
|
message.message = from + message.message.slice(7, -1);
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:43:25 +00:00
|
|
|
const charWidth = state.environment.get('charWidth');
|
|
|
|
const wrapWidth = state.environment.get('wrapWidth');
|
|
|
|
|
2017-04-17 02:11:45 +00:00
|
|
|
message.length = message.message.length;
|
|
|
|
message.breakpoints = findBreakpoints(message.message);
|
2016-02-16 21:43:25 +00:00
|
|
|
message.height = messageHeight(message, wrapWidth, charWidth, 6 * charWidth);
|
2017-04-17 02:11:45 +00:00
|
|
|
message.message = linkify(message.message);
|
2016-02-16 21:43:25 +00:00
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateMessageHeight() {
|
2016-02-04 02:35:50 +00:00
|
|
|
return (dispatch, getState) => dispatch({
|
2016-02-16 21:43:25 +00:00
|
|
|
type: actions.UPDATE_MESSAGE_HEIGHT,
|
|
|
|
wrapWidth: getState().environment.get('wrapWidth'),
|
|
|
|
charWidth: getState().environment.get('charWidth')
|
2016-02-04 02:35:50 +00:00
|
|
|
});
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
2015-01-21 02:06:34 +00:00
|
|
|
|
2016-02-16 21:43:25 +00:00
|
|
|
export function sendMessage(message, to, server) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
|
2017-04-17 02:11:45 +00:00
|
|
|
dispatch({
|
2016-02-16 21:43:25 +00:00
|
|
|
type: actions.SEND_MESSAGE,
|
2017-04-17 02:11:45 +00:00
|
|
|
message: initMessage({
|
|
|
|
from: state.servers.getIn([server, 'nick']),
|
|
|
|
message,
|
|
|
|
to,
|
|
|
|
server,
|
|
|
|
time: timestamp()
|
|
|
|
}, state),
|
2016-02-16 21:43:25 +00:00
|
|
|
socket: {
|
|
|
|
type: 'chat',
|
|
|
|
data: { message, to, server }
|
|
|
|
}
|
2017-04-17 02:11:45 +00:00
|
|
|
});
|
2016-02-16 21:43:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
export function addMessage(message) {
|
2017-04-17 02:11:45 +00:00
|
|
|
message.time = timestamp();
|
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,
|
2016-02-16 21:43:25 +00:00
|
|
|
message: initMessage(message, getState())
|
|
|
|
});
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
export function addMessages(messages) {
|
2017-04-17 02:11:45 +00:00
|
|
|
const now = timestamp();
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2016-02-16 21:43:25 +00:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
|
2016-03-14 22:22:24 +00:00
|
|
|
messages.forEach(message => {
|
|
|
|
initMessage(message, state).time = now;
|
|
|
|
});
|
2016-02-16 21:43:25 +00:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: actions.ADD_MESSAGES,
|
|
|
|
messages
|
|
|
|
});
|
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 => ({
|
|
|
|
server,
|
|
|
|
to: channel,
|
|
|
|
message,
|
|
|
|
type: 'info'
|
|
|
|
})));
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function inform(message, server, channel) {
|
|
|
|
if (Array.isArray(message)) {
|
2016-02-04 02:35:50 +00:00
|
|
|
return addMessages(message.map(msg => ({
|
|
|
|
server,
|
|
|
|
to: channel,
|
|
|
|
message: msg,
|
|
|
|
type: 'info'
|
|
|
|
})));
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return addMessage({
|
|
|
|
server,
|
|
|
|
to: channel,
|
|
|
|
message,
|
|
|
|
type: 'info'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|