summaryrefslogtreecommitdiffstats
path: root/webapp/stores/integration_store.jsx
blob: ff01774e14996ebed4825725ceffb43a2caa7470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import EventEmitter from 'events';

const CHANGE_EVENT = 'changed';

import store from 'stores/redux_store.jsx';

class IntegrationStore extends EventEmitter {
    constructor() {
        super();

        this.entities = {};

        store.subscribe(() => {
            const newEntities = store.getState().entities.integrations;
            if (newEntities !== this.entities) {
                this.emitChange();
            }

            this.entities = newEntities;
        });
    }

    addChangeListener(callback) {
        this.on(CHANGE_EVENT, callback);
    }

    removeChangeListener(callback) {
        this.removeListener(CHANGE_EVENT, callback);
    }

    emitChange() {
        this.emit(CHANGE_EVENT);
    }

    hasReceivedIncomingWebhooks(teamId) {
        const hooks = store.getState().entities.integrations.incomingHooks || {};

        let hasTeam = false;
        Object.values(hooks).forEach((hook) => {
            if (hook.team_id === teamId) {
                hasTeam = true;
            }
        });

        return hasTeam;
    }

    getIncomingWebhooks(teamId) {
        const hooks = store.getState().entities.integrations.incomingHooks;

        const teamHooks = [];
        Object.values(hooks).forEach((hook) => {
            if (hook.team_id === teamId) {
                teamHooks.push(hook);
            }
        });

        return teamHooks;
    }

    hasReceivedOutgoingWebhooks(teamId) {
        const hooks = store.getState().entities.integrations.outgoingHooks;

        let hasTeam = false;
        Object.values(hooks).forEach((hook) => {
            if (hook.team_id === teamId) {
                hasTeam = true;
            }
        });

        return hasTeam;
    }

    getOutgoingWebhooks(teamId) {
        const hooks = store.getState().entities.integrations.outgoingHooks;

        const teamHooks = [];
        Object.values(hooks).forEach((hook) => {
            if (hook.team_id === teamId) {
                teamHooks.push(hook);
            }
        });

        return teamHooks;
    }

    getOutgoingWebhook(teamId, id) {
        return store.getState().entities.integrations.outgoingHooks[id];
    }

    hasReceivedCommands(teamId) {
        const commands = store.getState().entities.integrations.commands;

        let hasTeam = false;
        Object.values(commands).forEach((command) => {
            if (command.team_id === teamId) {
                hasTeam = true;
            }
        });

        return hasTeam;
    }

    getCommands(teamId) {
        const commands = store.getState().entities.integrations.commands;

        const teamCommands = [];
        Object.values(commands).forEach((command) => {
            if (command.team_id === teamId) {
                teamCommands.push(command);
            }
        });

        return teamCommands;
    }

    getCommand(teamId, id) {
        return store.getState().entities.integrations.commands[id];
    }

    hasReceivedOAuthApps() {
        return Object.keys(store.getState().entities.integrations.oauthApps).length > 0;
    }

    getOAuthApps() {
        return Object.values(store.getState().entities.integrations.oauthApps);
    }
}

export default new IntegrationStore();