dispatch/client/src/js/state/messages.js

260 lines
5.9 KiB
JavaScript
Raw Normal View History

import { List, Map, Record } from 'immutable';
import { createSelector } from 'reselect';
import createReducer from '../util/createReducer';
import { getWrapWidth, getCharWidth, getWindowWidth } from './environment';
import { getSelectedTab } from './tab';
import { findBreakpoints, messageHeight, linkify, timestamp } from '../util';
import * as actions from './actions';
const Message = Record({
id: null,
from: null,
content: '',
time: null,
type: null,
channel: false,
next: false,
height: 0,
length: 0,
breakpoints: null
});
export const getMessages = state => state.messages;
export const getSelectedMessages = createSelector(
getSelectedTab,
getMessages,
(tab, messages) => messages.getIn([tab.server, tab.name || tab.server], List())
);
export const getHasMoreMessages = createSelector(
getSelectedMessages,
messages => {
const first = messages.get(0);
return first && first.next;
}
);
export default createReducer(Map(), {
[actions.ADD_MESSAGE](state, { server, tab, message }) {
return state.updateIn([server, tab], List(), list => list.push(new Message(message)));
},
[actions.ADD_MESSAGES](state, { server, tab, messages, prepend }) {
return state.withMutations(s => {
if (prepend) {
for (let i = messages.length - 1; i >= 0; i--) {
s.updateIn([server, tab], List(), list => list.unshift(new Message(messages[i])));
}
} else {
messages.forEach(message =>
s.updateIn([server, message.tab || tab], List(), list => list.push(new Message(message)))
);
}
});
},
[actions.DISCONNECT](state, { server }) {
return state.delete(server);
},
[actions.PART](state, { server, channels }) {
return state.withMutations(s =>
channels.forEach(channel =>
s.deleteIn([server, channel])
)
);
},
[actions.UPDATE_MESSAGE_HEIGHT](state, { wrapWidth, charWidth, windowWidth }) {
return state.withMutations(s =>
s.forEach((server, serverKey) =>
server.forEach((target, targetKey) =>
target.forEach((message, index) => s.setIn([serverKey, targetKey, index, 'height'],
messageHeight(message, wrapWidth, charWidth, 6 * charWidth, windowWidth))
)
)
)
);
}
});
2015-01-17 01:37:21 +00:00
2017-04-20 03:32:22 +00:00
let nextID = 0;
function initMessage(message, 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++;
}
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);
}
const wrapWidth = getWrapWidth(state);
const charWidth = getCharWidth(state);
const windowWidth = getWindowWidth(state);
2016-02-16 21:43:25 +00:00
message.length = message.content.length;
message.breakpoints = findBreakpoints(message.content);
message.height = messageHeight(message, wrapWidth, charWidth, 6 * charWidth, windowWidth);
message.content = linkify(message.content);
2016-02-16 21:43:25 +00:00
return message;
}
function getMessageTab(server, to) {
if (!to || to.indexOf('.') !== -1 || to === '*') {
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
}
}
});
}
};
}
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,
charWidth,
windowWidth
2017-05-12 08:51:37 +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({
2017-05-15 03:57:12 +00:00
type: actions.ADD_MESSAGE,
server,
tab: to,
message: initMessage({
from: state.servers.getIn([server, 'nick']),
content
}, 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, 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) {
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;
messages[0].next = true;
2017-05-02 21:21:25 +00:00
}
messages.forEach(message => initMessage(message, message.tab || tab, state));
2016-02-16 21:43:25 +00:00
dispatch({
type: actions.ADD_MESSAGES,
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 => ({
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 }
}
};
}