Forward irc errors to the client, improve command validation and feedback, handle topic changes

This commit is contained in:
Ken-Håvard Lieng 2017-05-28 07:20:43 +02:00
parent 993d29242e
commit aa59e71745
17 changed files with 328 additions and 96 deletions

View file

@ -1,9 +1,26 @@
import { inform } from '../state/messages';
import { addMessages, inform, print } from '../state/messages';
import { isChannel } from '../util';
const notFound = 'commandNotFound';
export const beforeHandler = '_before';
export const notFoundHandler = 'commandNotFound';
function createContext({ dispatch, getState }, { server, channel }) {
return { dispatch, getState, server, channel };
return { dispatch, getState, server, channel, isChannel: isChannel(channel) };
}
// TODO: Pull this out as convenience action
function process({ dispatch, server, channel }, result) {
if (typeof result === 'string') {
dispatch(inform(result, server, channel));
} else if (Array.isArray(result)) {
if (typeof result[0] === 'string') {
dispatch(inform(result, server, channel));
} else if (typeof result[0] === 'object') {
dispatch(addMessages(result, server, channel));
}
} else if (typeof result === 'object' && result) {
dispatch(print(result.content, server, channel, result.type));
}
}
export default function createCommandMiddleware(type, handlers) {
@ -13,16 +30,15 @@ export default function createCommandMiddleware(type, handlers) {
const command = words[0];
const params = words.slice(1);
let result;
if (command in handlers) {
result = handlers[command](createContext(store, action), ...params);
} else if (notFound in handlers) {
result = handlers[notFound](createContext(store, action), command);
}
if (typeof result === 'string' || Array.isArray(result)) {
store.dispatch(inform(result, action.server, action.channel));
const ctx = createContext(store, action);
if (beforeHandler in handlers) {
process(ctx, handlers[beforeHandler](ctx, command, ...params));
}
process(ctx, handlers[command](ctx, ...params));
} else if (notFoundHandler in handlers) {
const ctx = createContext(store, action);
process(ctx, handlers[notFoundHandler](ctx, command));
}
}