summaryrefslogtreecommitdiffstats
path: root/webapp/actions/emoji_actions.jsx
blob: a6323449e05dd6fef8d5334f7f30835c392058b1 (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
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import UserStore from 'stores/user_store.jsx';

import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import {ActionTypes} from 'utils/constants.jsx';

import store from 'stores/redux_store.jsx';
const dispatch = store.dispatch;
const getState = store.getState;
import {getProfilesByIds} from 'mattermost-redux/actions/users';
import * as EmojiActions from 'mattermost-redux/actions/emojis';

export async function loadEmoji(getProfiles = true) {
    const data = await EmojiActions.getAllCustomEmojis(10000)(dispatch, getState);

    if (data && getProfiles) {
        loadProfilesForEmoji(data);
    }
}

function loadProfilesForEmoji(emojiList) {
    const profilesToLoad = {};
    for (let i = 0; i < emojiList.length; i++) {
        const emoji = emojiList[i];
        if (!UserStore.hasProfile(emoji.creator_id)) {
            profilesToLoad[emoji.creator_id] = true;
        }
    }

    const list = Object.keys(profilesToLoad);
    if (list.length === 0) {
        return;
    }

    getProfilesByIds(list)(dispatch, getState);
}

export function addEmoji(emoji, image, success, error) {
    EmojiActions.createCustomEmoji(emoji, image)(dispatch, getState).then(
        (data) => {
            if (data && success) {
                success(data);
            } else if (data == null && error) {
                const serverError = getState().requests.emojis.createCustomEmoji.error;
                error({id: serverError.server_error_id, ...serverError});
            }
        }
    );
}

export function deleteEmoji(emojiId, success, error) {
    EmojiActions.deleteCustomEmoji(emojiId)(dispatch, getState).then(
        (data) => {
            if (data) {
                // Needed to remove recently used emoji
                AppDispatcher.handleServerAction({
                    type: ActionTypes.REMOVED_CUSTOM_EMOJI,
                    id: emojiId
                });

                if (success) {
                    success(data);
                }
            } else if (data == null && error) {
                const serverError = getState().requests.emojis.deleteCustomEmoji.error;
                error({id: serverError.server_error_id, ...serverError});
            }
        }
    );
}