summaryrefslogtreecommitdiffstats
path: root/webapp/stores/preference_store.jsx
blob: cd8ae68be65fa1f76f0d643c17125521a85d0327 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import Constants from 'utils/constants.jsx';
const ActionTypes = Constants.ActionTypes;
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
import EventEmitter from 'events';

const CHANGE_EVENT = 'change';

import store from 'stores/redux_store.jsx';
import * as Selectors from 'mattermost-redux/selectors/entities/preferences';
import {PreferenceTypes} from 'mattermost-redux/action_types';

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

        this.handleEventPayload = this.handleEventPayload.bind(this);
        this.dispatchToken = AppDispatcher.register(this.handleEventPayload);

        this.preferences = new Map();
        this.entities = Selectors.getMyPreferences(store.getState());
        Object.keys(this.entities).forEach((key) => {
            this.preferences.set(key, this.entities[key].value);
        });

        store.subscribe(() => {
            const newEntities = Selectors.getMyPreferences(store.getState());
            if (this.entities !== newEntities) {
                this.preferences = new Map();
                Object.keys(newEntities).forEach((key) => {
                    this.preferences.set(key, newEntities[key].value);
                });
                this.emitChange();
            }

            this.entities = newEntities;
        });

        this.setMaxListeners(600);
    }

    getKey(category, name) {
        return `${category}--${name}`;
    }

    get(category, name, defaultValue = '') {
        const key = this.getKey(category, name);

        if (!this.preferences.has(key)) {
            return defaultValue;
        }

        return this.preferences.get(key);
    }

    getBool(category, name, defaultValue = false) {
        const key = this.getKey(category, name);

        if (!this.preferences.has(key)) {
            return defaultValue;
        }

        return this.preferences.get(key) !== 'false';
    }

    getInt(category, name, defaultValue = 0) {
        const key = this.getKey(category, name);

        if (!this.preferences.has(key)) {
            return defaultValue;
        }

        return parseInt(this.preferences.get(key), 10);
    }

    getObject(category, name, defaultValue = null) {
        const key = this.getKey(category, name);

        if (!this.preferences.has(key)) {
            return defaultValue;
        }

        return JSON.parse(this.preferences.get(key));
    }

    getCategory(category) {
        const prefix = category + '--';

        const preferences = new Map();

        for (const [key, value] of this.preferences) {
            if (key.startsWith(prefix)) {
                preferences.set(key.substring(prefix.length), value);
            }
        }

        return preferences;
    }

    setPreference(category, name, value) {
        store.dispatch({
            type: PreferenceTypes.RECEIVED_PREFERENCES,
            data: [{category, name, value}]
        });
    }

    setPreferencesFromServer(newPreferences) {
        store.dispatch({
            type: PreferenceTypes.RECEIVED_PREFERENCES,
            data: newPreferences
        });
    }

    deletePreference(preference) {
        store.dispatch({
            type: PreferenceTypes.DELETED_PREFERENCES,
            data: [preference]
        });
    }

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

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

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

    getTheme(teamId) {
        if (this.preferences.has(this.getKey(Constants.Preferences.CATEGORY_THEME, teamId))) {
            return this.getObject(Constants.Preferences.CATEGORY_THEME, teamId);
        }

        if (this.preferences.has(this.getKey(Constants.Preferences.CATEGORY_THEME, ''))) {
            return this.getObject(Constants.Preferences.CATEGORY_THEME, '');
        }

        for (const k in Constants.THEMES) {
            if (Constants.THEMES.hasOwnProperty(k) && k === global.mm_config.DefaultTheme) {
                return Constants.THEMES[k];
            }
        }

        return Constants.THEMES.default;
    }

    handleEventPayload(payload) {
        const action = payload.action;

        switch (action.type) {
        case ActionTypes.RECEIVED_PREFERENCE: {
            const preference = action.preference;
            this.setPreference(preference.category, preference.name, preference.value);
            this.emitChange(preference.category);
            break;
        }
        case ActionTypes.RECEIVED_PREFERENCES:
            this.setPreferencesFromServer(action.preferences);
            this.emitChange();
            break;
        case ActionTypes.DELETED_PREFERENCES:
            for (const preference of action.preferences) {
                this.deletePreference(preference);
            }
            this.emitChange();
            break;
        case ActionTypes.CLICK_CHANNEL:
            this.setPreference(action.team_id, 'channel', action.id);
            break;
        }
    }
}

global.PreferenceStore = new PreferenceStore();
export default global.PreferenceStore;