mattermost-redux
Version:
Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client
223 lines (222 loc) • 7.97 kB
JavaScript
;
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.systemEmojis = void 0;
exports.setSystemEmojis = setSystemEmojis;
exports.createCustomEmoji = createCustomEmoji;
exports.getCustomEmoji = getCustomEmoji;
exports.getCustomEmojiByName = getCustomEmojiByName;
exports.getCustomEmojisByName = getCustomEmojisByName;
exports.getCustomEmojisInText = getCustomEmojisInText;
exports.getCustomEmojis = getCustomEmojis;
exports.loadProfilesForCustomEmojis = loadProfilesForCustomEmojis;
exports.deleteCustomEmoji = deleteCustomEmoji;
exports.searchCustomEmojis = searchCustomEmojis;
exports.autocompleteCustomEmojis = autocompleteCustomEmojis;
const redux_batched_actions_1 = require("redux-batched-actions");
const action_types_1 = require("mattermost-redux/action_types");
const client_1 = require("mattermost-redux/client");
const emojis_1 = require("mattermost-redux/selectors/entities/emojis");
const emoji_utils_1 = require("mattermost-redux/utils/emoji_utils");
const errors_1 = require("./errors");
const helpers_1 = require("./helpers");
const users_1 = require("./users");
const constants_1 = require("../constants");
exports.systemEmojis = new Set();
function setSystemEmojis(emojis) {
exports.systemEmojis = emojis;
}
function createCustomEmoji(emoji, image) {
return (0, helpers_1.bindClientFunc)({
clientFunc: client_1.Client4.createCustomEmoji,
onSuccess: action_types_1.EmojiTypes.RECEIVED_CUSTOM_EMOJI,
params: [
emoji,
image,
],
});
}
function getCustomEmoji(emojiId) {
return (0, helpers_1.bindClientFunc)({
clientFunc: client_1.Client4.getCustomEmoji,
onSuccess: action_types_1.EmojiTypes.RECEIVED_CUSTOM_EMOJI,
params: [
emojiId,
],
});
}
function getCustomEmojiByName(name) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.getCustomEmojiByName(name);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
if (error.status_code === 404) {
dispatch({ type: action_types_1.EmojiTypes.CUSTOM_EMOJI_DOES_NOT_EXIST, data: name });
}
else {
dispatch((0, errors_1.logError)(error));
}
return { error };
}
dispatch({
type: action_types_1.EmojiTypes.RECEIVED_CUSTOM_EMOJI,
data,
});
return { data };
};
}
function getCustomEmojisByName(names) {
return async (dispatch, getState) => {
const neededNames = filterNeededCustomEmojis(getState(), names);
if (neededNames.length === 0) {
return { data: true };
}
// If necessary, split up the list of names into batches based on api4.GetEmojisByNamesMax on the server
const batchSize = 200;
const batches = [];
for (let i = 0; i < names.length; i += batchSize) {
batches.push(neededNames.slice(i, i + batchSize));
}
let results;
try {
results = await Promise.all(batches.map((batch) => {
return client_1.Client4.getCustomEmojisByNames(batch);
}));
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
const data = results.flat();
const actions = [{
type: action_types_1.EmojiTypes.RECEIVED_CUSTOM_EMOJIS,
data,
}];
if (data.length !== neededNames.length) {
const foundNames = new Set(data.map((emoji) => emoji.name));
for (const name of neededNames) {
if (foundNames.has(name)) {
continue;
}
actions.push({
type: action_types_1.EmojiTypes.CUSTOM_EMOJI_DOES_NOT_EXIST,
data: name,
});
}
}
dispatch(actions.length > 1 ? (0, redux_batched_actions_1.batchActions)(actions) : actions[0]);
return { data: true };
};
}
function filterNeededCustomEmojis(state, names) {
const nonExistentEmoji = state.entities.emojis.nonExistentEmoji;
const customEmojisByName = (0, emojis_1.getCustomEmojisByName)(state);
return names.filter((name) => {
return !exports.systemEmojis.has(name) && !nonExistentEmoji.has(name) && !customEmojisByName.has(name);
});
}
function getCustomEmojisInText(text) {
return async (dispatch) => {
if (!text) {
return { data: true };
}
return dispatch(getCustomEmojisByName((0, emoji_utils_1.parseEmojiNamesFromText)(text)));
};
}
function getCustomEmojis(page = 0, perPage = constants_1.General.PAGE_SIZE_DEFAULT, sort = constants_1.Emoji.SORT_BY_NAME, loadUsers = false) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.getCustomEmojis(page, perPage, sort);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
if (loadUsers) {
dispatch(loadProfilesForCustomEmojis(data));
}
dispatch({
type: action_types_1.EmojiTypes.RECEIVED_CUSTOM_EMOJIS,
data,
});
return { data };
};
}
function loadProfilesForCustomEmojis(emojis) {
return async (dispatch, getState) => {
const usersToLoad = {};
emojis.forEach((emoji) => {
if (!getState().entities.users.profiles[emoji.creator_id]) {
usersToLoad[emoji.creator_id] = true;
}
});
const userIds = Object.keys(usersToLoad);
if (userIds.length > 0) {
await dispatch((0, users_1.getProfilesByIds)(userIds));
}
return { data: true };
};
}
function deleteCustomEmoji(emojiId) {
return async (dispatch, getState) => {
try {
await client_1.Client4.deleteCustomEmoji(emojiId);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
dispatch({
type: action_types_1.EmojiTypes.DELETED_CUSTOM_EMOJI,
data: { id: emojiId },
});
return { data: true };
};
}
function searchCustomEmojis(term, options = {}, loadUsers = false) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.searchCustomEmoji(term, options);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
if (loadUsers) {
dispatch(loadProfilesForCustomEmojis(data));
}
dispatch({
type: action_types_1.EmojiTypes.RECEIVED_CUSTOM_EMOJIS,
data,
});
return { data };
};
}
function autocompleteCustomEmojis(name) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.autocompleteCustomEmoji(name);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
dispatch({
type: action_types_1.EmojiTypes.RECEIVED_CUSTOM_EMOJIS,
data,
});
return { data };
};
}