Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #126 from 94117nl/master
Browse files Browse the repository at this point in the history
Add addCommand, regenCommandToken and deleteCommand actions and other…
  • Loading branch information
jwilander authored and hmhealey committed Aug 28, 2020
1 parent 8a9cd54 commit 9c550d8
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 6 deletions.
18 changes: 16 additions & 2 deletions packages/mattermost-redux/src/action_types/integrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,25 @@ export default keyMirror({
UPDATE_OUTGOING_HOOK_SUCCESS: null,
UPDATE_OUTGOING_HOOK_FAILURE: null,

ADD_COMMAND_REQUEST: null,
ADD_COMMAND_SUCCESS: null,
ADD_COMMAND_FAILURE: null,

DELETE_COMMAND_REQUEST: null,
DELETE_COMMAND_SUCCESS: null,
DELETE_COMMAND_FAILURE: null,

REGEN_COMMAND_TOKEN_REQUEST: null,
REGEN_COMMAND_TOKEN_SUCCESS: null,
REGEN_COMMAND_TOKEN_FAILURE: null,

RECEIVED_INCOMING_HOOK: null,
RECEIVED_INCOMING_HOOKS: null,
DELETED_INCOMING_HOOK: null,
RECEIVED_OUTGOING_HOOK: null,
RECEIVED_OUTGOING_HOOKS: null,
DELETED_OUTGOING_HOOK: null
DELETED_OUTGOING_HOOK: null,
RECEIVED_COMMAND: null,
RECEIVED_COMMAND_TOKEN: null,
DELETED_COMMAND: null
});

75 changes: 75 additions & 0 deletions packages/mattermost-redux/src/actions/integrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,78 @@ export function updateOutgoingHook(hook) {
hook
);
}

export function addCommand(teamId, command) {
return bindClientFunc(
Client4.addCommand,
IntegrationTypes.ADD_COMMAND_REQUEST,
[IntegrationTypes.RECEIVED_COMMAND, IntegrationTypes.ADD_COMMAND_SUCCESS],
IntegrationTypes.ADD_COMMAND_FAILURE,
teamId,
command
);
}

export function regenCommandToken(id) {
return async (dispatch, getState) => {
dispatch({type: IntegrationTypes.REGEN_COMMAND_TOKEN_REQUEST}, getState);

let res;
try {
res = await Client4.regenCommandToken(id);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);

dispatch(batchActions([
{type: IntegrationTypes.REGEN_COMMAND_TOKEN_FAILURE, error},
getLogErrorAction(error)
]), getState);
return null;
}

dispatch(batchActions([
{
type: IntegrationTypes.RECEIVED_COMMAND_TOKEN,
data: {
id,
token: res.token
}
},
{
type: IntegrationTypes.REGEN_COMMAND_TOKEN_SUCCESS
}
]), getState);

return true;
};
}

export function deleteCommand(id) {
return async (dispatch, getState) => {
dispatch({type: IntegrationTypes.DELETE_COMMAND_REQUEST}, getState);

try {
await Client4.deleteCommand(id);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);

dispatch(batchActions([
{type: IntegrationTypes.DELETE_COMMAND_FAILURE, error},
getLogErrorAction(error)
]), getState);
return null;
}

dispatch(batchActions([
{
type: IntegrationTypes.DELETED_COMMAND,
data: {id}
},
{
type: IntegrationTypes.DELETE_COMMAND_SUCCESS
}
]), getState);

return true;
};
}
22 changes: 22 additions & 0 deletions packages/mattermost-redux/src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,28 @@ export default class Client {
);
};

// Integration Routes
addCommand = async (teamId, command) => {
return this.doFetch(
`${this.getCommandsRoute(teamId)}/create`,
{method: 'post', body: JSON.stringify(command)}
);
};

regenCommandToken = async (teamId, id) => {
return this.doFetch(
`${this.getCommandsRoute(teamId)}/regen_token`,
{method: 'post', body: JSON.stringify({id})}
);
};

deleteCommand = async (teamId, id) => {
return this.doFetch(
`${this.getCommandsRoute(teamId)}/delete`,
{method: 'post', body: JSON.stringify({id})}
);
};

// File routes
getFileUrl(fileId, timestamp) {
let url = `${this.getFilesRoute()}/${fileId}/get`;
Expand Down
26 changes: 26 additions & 0 deletions packages/mattermost-redux/src/client/client4.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ export default class Client4 {
return `${this.getBaseRoute()}/reactions`;
}

getCommandsRoute() {
return `${this.getBaseRoute()}/commands`;
}

getFilesRoute() {
return `${this.getBaseRoute()}/files`;
}
Expand Down Expand Up @@ -1024,6 +1028,28 @@ export default class Client4 {
);
};

addCommand = async (teamId, command) => {
command.team_id = teamId;
return this.doFetch(
`${this.getCommandsRoute()}`,
{method: 'post', body: JSON.stringify(command)}
);
};

regenCommandToken = async (id) => {
return this.doFetch(
`${this.getCommandsRoute()}/${id}/regen_token`,
{method: 'put'}
);
};

deleteCommand = async (id) => {
return this.doFetch(
`${this.getCommandsRoute()}/${id}`,
{method: 'delete'}
);
};

// Admin Routes

getLogs = async (page = 0, perPage = PER_PAGE_DEFAULT) => {
Expand Down
35 changes: 34 additions & 1 deletion packages/mattermost-redux/src/reducers/entities/integrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,45 @@ function outgoingHooks(state = {}, action) {
}
}

function commands(state = {}, action) {
const nextState = {...state};
switch (action.type) {
case IntegrationTypes.RECEIVED_COMMAND:
return {
...state,
[action.data.id]: action.data
};
case IntegrationTypes.RECEIVED_COMMAND_TOKEN: {
const {id, token} = action.data;
return {
...state,
[id]: {
...state[id],
token
}
};
}
case IntegrationTypes.DELETED_COMMAND: {
Reflect.deleteProperty(nextState, action.data.id);
return nextState;
}
case UserTypes.LOGOUT_SUCCESS:
return {};

default:
return state;
}
}

export default combineReducers({

// object where every key is the hook id and has an object with the incoming hook details
incomingHooks,

// object where every key is the hook id and has an object with the outgoing hook details
outgoingHooks
outgoingHooks,

// object to represent installed slash commands for a current team
commands

});
35 changes: 34 additions & 1 deletion packages/mattermost-redux/src/reducers/requests/integrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,36 @@ function updateOutgoingHook(state = initialRequestState(), action) {
);
}

function addCommand(state = initialRequestState(), action) {
return handleRequest(
IntegrationTypes.ADD_COMMAND_REQUEST,
IntegrationTypes.ADD_COMMAND_SUCCESS,
IntegrationTypes.ADD_COMMAND_FAILURE,
state,
action
);
}

function regenCommandToken(state = initialRequestState(), action) {
return handleRequest(
IntegrationTypes.REGEN_COMMAND_TOKEN_REQUEST,
IntegrationTypes.REGEN_COMMAND_TOKEN_SUCCESS,
IntegrationTypes.REGEN_COMMAND_TOKEN_FAILURE,
state,
action
);
}

function deleteCommand(state = initialRequestState(), action) {
return handleRequest(
IntegrationTypes.DELETE_COMMAND_REQUEST,
IntegrationTypes.DELETE_COMMAND_SUCCESS,
IntegrationTypes.DELETE_COMMAND_FAILURE,
state,
action
);
}

export default combineReducers({
createIncomingHook,
getIncomingHooks,
Expand All @@ -94,5 +124,8 @@ export default combineReducers({
createOutgoingHook,
getOutgoingHooks,
deleteOutgoingHook,
updateOutgoingHook
updateOutgoingHook,
addCommand,
regenCommandToken,
deleteCommand
});
16 changes: 14 additions & 2 deletions packages/mattermost-redux/src/store/initial_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const state = {
},
integrations: {
incomingHooks: {},
outgoingHooks: {}
outgoingHooks: {},
commands: {}
},
files: {
files: {},
Expand Down Expand Up @@ -405,10 +406,21 @@ const state = {
updateOutgoingHook: {
status: 'not_started',
error: null
},
addCommand: {
status: 'not_started',
error: null
},
regenCommandToken: {
status: 'not_started',
error: null
},
deleteCommand: {
status: 'not_started',
error: null
}
}
}
};

export default state;

Loading

0 comments on commit 9c550d8

Please sign in to comment.