2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
2016-03-17 10:30:49 -04:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
import EventEmitter from 'events';
|
|
|
|
|
|
|
|
|
|
const CHANGE_EVENT = 'changed';
|
|
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
import store from 'stores/redux_store.jsx';
|
|
|
|
|
|
2016-03-17 10:30:49 -04:00
|
|
|
class IntegrationStore extends EventEmitter {
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
this.entities = {};
|
2016-04-05 09:29:01 -04:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
store.subscribe(() => {
|
|
|
|
|
const newEntities = store.getState().entities.integrations;
|
|
|
|
|
if (newEntities !== this.entities) {
|
|
|
|
|
this.emitChange();
|
|
|
|
|
}
|
2016-08-03 12:19:27 -05:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
this.entities = newEntities;
|
|
|
|
|
});
|
2016-03-17 10:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addChangeListener(callback) {
|
|
|
|
|
this.on(CHANGE_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeChangeListener(callback) {
|
|
|
|
|
this.removeListener(CHANGE_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emitChange() {
|
|
|
|
|
this.emit(CHANGE_EVENT);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-12 11:30:53 -04:00
|
|
|
hasReceivedIncomingWebhooks(teamId) {
|
2017-06-19 13:55:47 -04:00
|
|
|
const hooks = store.getState().entities.integrations.incomingHooks || {};
|
2016-03-28 09:41:03 -04:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
let hasTeam = false;
|
|
|
|
|
Object.values(hooks).forEach((hook) => {
|
|
|
|
|
if (hook.team_id === teamId) {
|
|
|
|
|
hasTeam = true;
|
2017-02-27 00:18:20 +05:30
|
|
|
}
|
2017-06-19 13:55:47 -04:00
|
|
|
});
|
2017-02-27 00:18:20 +05:30
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
return hasTeam;
|
2017-02-27 00:18:20 +05:30
|
|
|
}
|
|
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
getIncomingWebhooks(teamId) {
|
|
|
|
|
const hooks = store.getState().entities.integrations.incomingHooks;
|
2016-05-12 11:30:53 -04:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
const teamHooks = [];
|
|
|
|
|
Object.values(hooks).forEach((hook) => {
|
|
|
|
|
if (hook.team_id === teamId) {
|
|
|
|
|
teamHooks.push(hook);
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-05-12 11:30:53 -04:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
return teamHooks;
|
2016-03-28 16:17:17 -04:00
|
|
|
}
|
|
|
|
|
|
2016-05-12 11:30:53 -04:00
|
|
|
hasReceivedOutgoingWebhooks(teamId) {
|
2017-06-19 13:55:47 -04:00
|
|
|
const hooks = store.getState().entities.integrations.outgoingHooks;
|
2016-03-17 10:30:49 -04:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
let hasTeam = false;
|
|
|
|
|
Object.values(hooks).forEach((hook) => {
|
|
|
|
|
if (hook.team_id === teamId) {
|
|
|
|
|
hasTeam = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-05-12 11:30:53 -04:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
return hasTeam;
|
2016-03-28 09:41:03 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
getOutgoingWebhooks(teamId) {
|
|
|
|
|
const hooks = store.getState().entities.integrations.outgoingHooks;
|
2016-05-12 11:30:53 -04:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
const teamHooks = [];
|
|
|
|
|
Object.values(hooks).forEach((hook) => {
|
|
|
|
|
if (hook.team_id === teamId) {
|
|
|
|
|
teamHooks.push(hook);
|
2016-03-29 10:04:53 -04:00
|
|
|
}
|
2017-06-19 13:55:47 -04:00
|
|
|
});
|
2016-05-12 11:30:53 -04:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
return teamHooks;
|
2016-03-29 10:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
getOutgoingWebhook(teamId, id) {
|
|
|
|
|
return store.getState().entities.integrations.outgoingHooks[id];
|
2016-03-28 16:17:17 -04:00
|
|
|
}
|
|
|
|
|
|
2016-05-12 11:30:53 -04:00
|
|
|
hasReceivedCommands(teamId) {
|
2017-06-19 13:55:47 -04:00
|
|
|
const commands = store.getState().entities.integrations.commands;
|
2016-05-12 11:30:53 -04:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
let hasTeam = false;
|
|
|
|
|
Object.values(commands).forEach((command) => {
|
|
|
|
|
if (command.team_id === teamId) {
|
|
|
|
|
hasTeam = true;
|
2016-04-05 09:29:01 -04:00
|
|
|
}
|
2017-06-19 13:55:47 -04:00
|
|
|
});
|
2016-04-05 09:29:01 -04:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
return hasTeam;
|
2016-04-05 09:29:01 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
getCommands(teamId) {
|
|
|
|
|
const commands = store.getState().entities.integrations.commands;
|
2016-08-03 12:19:27 -05:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
const teamCommands = [];
|
|
|
|
|
Object.values(commands).forEach((command) => {
|
|
|
|
|
if (command.team_id === teamId) {
|
|
|
|
|
teamCommands.push(command);
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-08-03 12:19:27 -05:00
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
return teamCommands;
|
2016-08-03 12:19:27 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
getCommand(teamId, id) {
|
|
|
|
|
return store.getState().entities.integrations.commands[id];
|
2016-08-03 12:19:27 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
hasReceivedOAuthApps() {
|
|
|
|
|
return Object.keys(store.getState().entities.integrations.oauthApps).length > 0;
|
2016-08-03 12:19:27 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-19 13:55:47 -04:00
|
|
|
getOAuthApps() {
|
|
|
|
|
return Object.values(store.getState().entities.integrations.oauthApps);
|
2016-03-17 10:30:49 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-05 09:43:26 -04:00
|
|
|
export default new IntegrationStore();
|