dispatch/client/src/js/actions/message.js

141 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-12-28 23:34:32 +00:00
import * as actions from '../actions';
import { findBreakpoints, messageHeight, linkify, timestamp } from '../util';
2015-01-17 01:37:21 +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
}
if (tab.charAt(0) === '#') {
2016-02-16 21:43:25 +00:00
message.channel = true;
}
// Collapse multiple adjacent spaces into a single one
message.content = message.content.replace(/\s\s+/g, ' ');
2016-02-16 21:43:25 +00:00
if (message.content.indexOf('\x01ACTION') === 0) {
const from = message.from;
message.from = null;
message.type = 'action';
message.content = from + message.content.slice(7, -1);
}
2016-02-16 21:43:25 +00:00
const charWidth = state.environment.get('charWidth');
const wrapWidth = state.environment.get('wrapWidth');
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);
message.content = linkify(message.content);
2016-02-16 21:43:25 +00:00
return message;
}
function getMessageTab(server, to) {
if (!to || to.indexOf('.') !== -1) {
return server;
}
return to;
}
2016-02-16 21:43:25 +00:00
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
}
export function sendMessage(content, to, server) {
2016-02-16 21:43:25 +00:00
return (dispatch, getState) => {
const state = getState();
dispatch({
2016-02-16 21:43:25 +00:00
type: actions.SEND_MESSAGE,
server,
tab: to,
message: initMessage({
from: state.servers.getIn([server, 'nick']),
content
}, server, to, state),
2016-02-16 21:43:25 +00:00
socket: {
type: 'message',
data: { content, to, server }
2016-02-16 21:43:25 +00:00
}
});
2016-02-16 21:43:25 +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,
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
export function addMessages(messages, server, to) {
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();
messages.forEach(message => initMessage(message, server, message.tab || tab, state));
2016-02-16 21:43:25 +00:00
dispatch({
type: actions.ADD_MESSAGES,
server,
tab,
2016-02-16 21:43:25 +00:00
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 => ({
tab: channel,
content: message,
2016-02-04 02:35:50 +00:00
type: 'info'
})), server);
2015-12-28 23:34:32 +00:00
}
export function inform(message, server, channel) {
if (Array.isArray(message)) {
return addMessages(message.map(line => ({
content: line,
2016-02-04 02:35:50 +00:00
type: 'info'
})), server, channel);
2015-12-28 23:34:32 +00:00
}
return addMessage({
content: message,
2015-12-28 23:34:32 +00:00
type: 'info'
}, 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 }
}
};
}