2018-04-25 03:36:27 +00:00
|
|
|
import sortBy from 'lodash/sortBy';
|
|
|
|
import { findIndex } from 'utils';
|
2018-04-05 19:13:32 +00:00
|
|
|
import createReducer from 'utils/createReducer';
|
2017-05-26 06:20:00 +00:00
|
|
|
import { updateSelection } from './tab';
|
|
|
|
import * as actions from './actions';
|
|
|
|
|
|
|
|
export const getPrivateChats = state => state.privateChats;
|
2015-12-28 23:34:32 +00:00
|
|
|
|
|
|
|
function open(state, server, nick) {
|
2018-04-25 03:36:27 +00:00
|
|
|
if (!state[server]) {
|
|
|
|
state[server] = [];
|
|
|
|
}
|
|
|
|
if (findIndex(state[server], n => n === nick) === -1) {
|
|
|
|
state[server].push(nick);
|
|
|
|
state[server] = sortBy(state[server], v => v.toLowerCase());
|
|
|
|
}
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
export default createReducer(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
[actions.OPEN_PRIVATE_CHAT](state, action) {
|
|
|
|
open(state, action.server, action.nick);
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.CLOSE_PRIVATE_CHAT](state, { server, nick }) {
|
|
|
|
const i = findIndex(state[server], n => n === nick);
|
|
|
|
if (i !== -1) {
|
|
|
|
state[server].splice(i, 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-05-06 02:19:55 +00:00
|
|
|
[actions.PRIVATE_CHATS](state, { privateChats }) {
|
|
|
|
privateChats.forEach(({ server, name }) => {
|
|
|
|
if (!state[server]) {
|
|
|
|
state[server] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
state[server].push(name);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
[actions.socket.PM](state, action) {
|
|
|
|
if (action.from.indexOf('.') === -1) {
|
|
|
|
open(state, action.server, action.from);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.DISCONNECT](state, { server }) {
|
|
|
|
delete state[server];
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
);
|
2017-05-26 06:20:00 +00:00
|
|
|
|
|
|
|
export function openPrivateChat(server, nick) {
|
|
|
|
return {
|
|
|
|
type: actions.OPEN_PRIVATE_CHAT,
|
|
|
|
server,
|
2020-05-06 02:19:55 +00:00
|
|
|
nick,
|
|
|
|
socket: {
|
|
|
|
type: 'open_dm',
|
|
|
|
data: { server, name: nick }
|
|
|
|
}
|
2017-05-26 06:20:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function closePrivateChat(server, nick) {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: actions.CLOSE_PRIVATE_CHAT,
|
|
|
|
server,
|
2020-05-06 02:19:55 +00:00
|
|
|
nick,
|
|
|
|
socket: {
|
|
|
|
type: 'close_dm',
|
|
|
|
data: { server, name: nick }
|
|
|
|
}
|
2017-05-26 06:20:00 +00:00
|
|
|
});
|
|
|
|
dispatch(updateSelection());
|
|
|
|
};
|
|
|
|
}
|