MM-54640 Add API to get multiple emojis by name at once (#24651)
* MM-54640 Add API to get multiple emojis by name at once * Fix status code when too many names are requested * Address feedback * Update unit tests * Fix styling * Fix more styling * Fix mismatched i18n id
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
77cc356d46
Коммит
3d0fd16666
@@ -264,36 +264,75 @@ describe('Actions.Emojis', () => {
|
||||
expect(state.entities.emojis.nonExistentEmoji.has(missingName)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('getCustomEmojisByName', async () => {
|
||||
const testImageData = fs.createReadStream('src/packages/mattermost-redux/test/assets/images/test.png');
|
||||
describe('getCustomEmojisByName', () => {
|
||||
test('should be able to request a single emoji', async () => {
|
||||
const emoji1 = TestHelper.getCustomEmojiMock({name: 'emoji1', id: 'emojiId1'});
|
||||
|
||||
nock(Client4.getBaseRoute()).
|
||||
post('/emoji').
|
||||
reply(201, {id: TestHelper.generateId(), create_at: 1507918415696, update_at: 1507918415696, delete_at: 0, creator_id: TestHelper.basicUser!.id, name: TestHelper.generateId()});
|
||||
nock(Client4.getBaseRoute()).
|
||||
post('/emoji/names', ['emoji1']).
|
||||
reply(200, [emoji1]);
|
||||
|
||||
const {data: created} = await Actions.createCustomEmoji(
|
||||
{
|
||||
name: TestHelper.generateId(),
|
||||
creator_id: TestHelper.basicUser!.id,
|
||||
},
|
||||
testImageData,
|
||||
)(store.dispatch, store.getState) as ActionResult;
|
||||
await store.dispatch(Actions.getCustomEmojisByName(['emoji1']));
|
||||
|
||||
nock(Client4.getBaseRoute()).
|
||||
get(`/emoji/name/${created.name}`).
|
||||
reply(200, created);
|
||||
const state = store.getState();
|
||||
expect(state.entities.emojis.customEmoji[emoji1.id]).toEqual(emoji1);
|
||||
});
|
||||
|
||||
const missingName = TestHelper.generateId();
|
||||
test('should be able to request multiple emojis', async () => {
|
||||
const emoji1 = TestHelper.getCustomEmojiMock({name: 'emoji1', id: 'emojiId1'});
|
||||
const emoji2 = TestHelper.getCustomEmojiMock({name: 'emoji2', id: 'emojiId2'});
|
||||
|
||||
nock(Client4.getBaseRoute()).
|
||||
get(`/emoji/name/${missingName}`).
|
||||
reply(404, {message: 'Not found', status_code: 404});
|
||||
nock(Client4.getBaseRoute()).
|
||||
post('/emoji/names', ['emoji1', 'emoji2']).
|
||||
reply(200, [emoji1, emoji2]);
|
||||
|
||||
await Actions.getCustomEmojisByName([created.name, missingName])(store.dispatch, store.getState);
|
||||
await store.dispatch(Actions.getCustomEmojisByName(['emoji1', 'emoji2']));
|
||||
|
||||
const state = store.getState();
|
||||
expect(state.entities.emojis.customEmoji[created.id]).toBeTruthy();
|
||||
expect(state.entities.emojis.nonExistentEmoji.has(missingName)).toBeTruthy();
|
||||
const state = store.getState();
|
||||
expect(state.entities.emojis.customEmoji[emoji1.id]).toEqual(emoji1);
|
||||
expect(state.entities.emojis.customEmoji[emoji2.id]).toEqual(emoji2);
|
||||
});
|
||||
|
||||
test('should correctly track non-existent emojis', async () => {
|
||||
const emoji1 = TestHelper.getCustomEmojiMock({name: 'emoji1', id: 'emojiId1'});
|
||||
|
||||
nock(Client4.getBaseRoute()).
|
||||
post('/emoji/names', ['emoji1', 'emoji2']).
|
||||
reply(200, [emoji1]);
|
||||
|
||||
await store.dispatch(Actions.getCustomEmojisByName(['emoji1', 'emoji2']));
|
||||
|
||||
const state = store.getState();
|
||||
expect(state.entities.emojis.customEmoji[emoji1.id]).toEqual(emoji1);
|
||||
expect(state.entities.emojis.nonExistentEmoji).toEqual(new Set(['emoji2']));
|
||||
});
|
||||
|
||||
test('should be able to request over 200 emojis', async () => {
|
||||
const emojis = [];
|
||||
for (let i = 0; i < 500; i++) {
|
||||
emojis.push(TestHelper.getCustomEmojiMock({name: 'emoji' + i, id: 'emojiId' + i}));
|
||||
}
|
||||
|
||||
const names = emojis.map((emoji) => emoji.name);
|
||||
|
||||
nock(Client4.getBaseRoute()).
|
||||
post('/emoji/names', names.slice(0, 200)).
|
||||
reply(200, emojis.slice(0, 200));
|
||||
nock(Client4.getBaseRoute()).
|
||||
post('/emoji/names', names.slice(200, 400)).
|
||||
reply(200, emojis.slice(200, 400));
|
||||
nock(Client4.getBaseRoute()).
|
||||
post('/emoji/names', names.slice(400, 500)).
|
||||
reply(200, emojis.slice(400, 500));
|
||||
|
||||
await store.dispatch(Actions.getCustomEmojisByName(names));
|
||||
|
||||
const state = store.getState();
|
||||
expect(Object.keys(state.entities.emojis.customEmoji)).toHaveLength(emojis.length);
|
||||
for (const emoji of emojis) {
|
||||
expect(state.entities.emojis.customEmoji[emoji.id]).toEqual(emoji);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('getCustomEmojisInText', async () => {
|
||||
@@ -311,15 +350,11 @@ describe('Actions.Emojis', () => {
|
||||
testImageData,
|
||||
)(store.dispatch, store.getState) as ActionResult;
|
||||
|
||||
nock(Client4.getBaseRoute()).
|
||||
get(`/emoji/name/${created.name}`).
|
||||
reply(200, created);
|
||||
|
||||
const missingName = TestHelper.generateId();
|
||||
|
||||
nock(Client4.getBaseRoute()).
|
||||
get(`/emoji/name/${missingName}`).
|
||||
reply(404, {message: 'Not found', status_code: 404});
|
||||
post('/emoji/names', [created.name, missingName]).
|
||||
reply(200, [created]);
|
||||
|
||||
await Actions.getCustomEmojisInText(`some text :${created.name}: :${missingName}:`)(store.dispatch, store.getState);
|
||||
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
import type {AnyAction} from 'redux';
|
||||
import {batchActions} from 'redux-batched-actions';
|
||||
|
||||
import type {CustomEmoji} from '@mattermost/types/emojis';
|
||||
|
||||
import {EmojiTypes} from 'mattermost-redux/action_types';
|
||||
import {Client4} from 'mattermost-redux/client';
|
||||
import {getCustomEmojisByName as selectCustomEmojisByName} from 'mattermost-redux/selectors/entities/emojis';
|
||||
import type {GetStateFunc, DispatchFunc, ActionFunc, ActionResult} from 'mattermost-redux/types/actions';
|
||||
import type {GetStateFunc, DispatchFunc, ActionFunc} from 'mattermost-redux/types/actions';
|
||||
import {parseNeededCustomEmojisFromText} from 'mattermost-redux/utils/emoji_utils';
|
||||
|
||||
import {logError} from './errors';
|
||||
@@ -69,16 +72,52 @@ export function getCustomEmojiByName(name: string): ActionFunc {
|
||||
}
|
||||
|
||||
export function getCustomEmojisByName(names: string[]): ActionFunc {
|
||||
return async (dispatch: DispatchFunc) => {
|
||||
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
|
||||
if (!names || names.length === 0) {
|
||||
return {data: true};
|
||||
}
|
||||
|
||||
const promises: Array<Promise<ActionResult|ActionResult[]>> = [];
|
||||
names.forEach((name) => promises.push(dispatch(getCustomEmojiByName(name))));
|
||||
// If necessary, split up the list of names into batches based on api4.GetEmojisByNamesMax on the server
|
||||
const batchSize = 200;
|
||||
|
||||
await Promise.all(promises);
|
||||
return {data: true};
|
||||
const batches = [];
|
||||
for (let i = 0; i < names.length; i += batchSize) {
|
||||
batches.push(names.slice(i, i + batchSize));
|
||||
}
|
||||
|
||||
let results;
|
||||
try {
|
||||
results = await Promise.all(batches.map((batch) => {
|
||||
return Client4.getCustomEmojisByNames(batch);
|
||||
}));
|
||||
} catch (error) {
|
||||
forceLogoutIfNecessary(error, dispatch, getState);
|
||||
dispatch(logError(error));
|
||||
return {error};
|
||||
}
|
||||
|
||||
const data = results.flat();
|
||||
const actions: AnyAction[] = [{
|
||||
type: EmojiTypes.RECEIVED_CUSTOM_EMOJIS,
|
||||
data,
|
||||
}];
|
||||
|
||||
if (data.length !== names.length) {
|
||||
const foundNames = new Set(data.map((emoji) => emoji.name));
|
||||
|
||||
for (const name of names) {
|
||||
if (foundNames.has(name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
actions.push({
|
||||
type: EmojiTypes.CUSTOM_EMOJI_DOES_NOT_EXIST,
|
||||
data: name,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return dispatch(actions.length > 1 ? batchActions(actions) : actions[0]);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2749,6 +2749,13 @@ export default class Client4 {
|
||||
);
|
||||
};
|
||||
|
||||
getCustomEmojisByNames = (names: string[]) => {
|
||||
return this.doFetch<CustomEmoji[]>(
|
||||
`${this.getEmojisRoute()}/names`,
|
||||
{method: 'post', body: JSON.stringify(names)},
|
||||
);
|
||||
};
|
||||
|
||||
getCustomEmojis = (page = 0, perPage = PER_PAGE_DEFAULT, sort = '') => {
|
||||
return this.doFetch<CustomEmoji[]>(
|
||||
`${this.getEmojisRoute()}${buildQueryString({page, per_page: perPage, sort})}`,
|
||||
|
||||
Ссылка в новой задаче
Block a user