merge: fix custom emoji pagination when searching - fixes #490 (!489)

View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/489

Closes #490
This commit is contained in:
dakkar 2024-04-12 00:09:16 +00:00
commit 4440e693fc
3 changed files with 12 additions and 6 deletions

View file

@ -66,6 +66,7 @@ export const paramDef = {
properties: { properties: {
query: { type: 'string', nullable: true, default: null }, query: { type: 'string', nullable: true, default: null },
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 }, limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
offset: { type: 'integer', minimum: 1, nullable: true, default: null },
sinceId: { type: 'string', format: 'misskey:id' }, sinceId: { type: 'string', format: 'misskey:id' },
untilId: { type: 'string', format: 'misskey:id' }, untilId: { type: 'string', format: 'misskey:id' },
}, },
@ -91,7 +92,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
//q.andWhere('emoji.name ILIKE :q', { q: `%${ sqlLikeEscape(ps.query) }%` }); //q.andWhere('emoji.name ILIKE :q', { q: `%${ sqlLikeEscape(ps.query) }%` });
//const emojis = await q.limit(ps.limit).getMany(); //const emojis = await q.limit(ps.limit).getMany();
emojis = await q.orderBy('length(emoji.name)', 'ASC').getMany(); emojis = await q.orderBy('length(emoji.name)', 'ASC').addOrderBy('id', 'DESC').getMany();
const queryarry = ps.query.match(/\:([a-z0-9_]*)\:/g); const queryarry = ps.query.match(/\:([a-z0-9_]*)\:/g);
if (queryarry) { if (queryarry) {
@ -104,9 +105,9 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
emoji.aliases.some(a => a.includes(ps.query!)) || emoji.aliases.some(a => a.includes(ps.query!)) ||
emoji.category?.includes(ps.query!)); emoji.category?.includes(ps.query!));
} }
emojis.splice(ps.limit + 1); emojis = emojis.slice((ps.offset ?? 0), ((ps.offset ?? 0) + ps.limit));
} else { } else {
emojis = await q.limit(ps.limit).getMany(); emojis = await q.take(ps.limit).skip(ps.offset ?? 0).getMany();
} }
return this.emojiEntityService.packDetailedMany(emojis); return this.emojiEntityService.packDetailedMany(emojis);

View file

@ -73,7 +73,7 @@ export type Paging<E extends keyof Misskey.Endpoints = keyof Misskey.Endpoints>
*/ */
reversed?: boolean; reversed?: boolean;
offsetMode?: boolean; offsetMode?: boolean | ComputedRef<boolean>;
pageEl?: HTMLElement; pageEl?: HTMLElement;
}; };
@ -240,10 +240,11 @@ const fetchMore = async (): Promise<void> => {
if (!more.value || fetching.value || moreFetching.value || items.value.size === 0) return; if (!more.value || fetching.value || moreFetching.value || items.value.size === 0) return;
moreFetching.value = true; moreFetching.value = true;
const params = props.pagination.params ? isRef(props.pagination.params) ? props.pagination.params.value : props.pagination.params : {}; const params = props.pagination.params ? isRef(props.pagination.params) ? props.pagination.params.value : props.pagination.params : {};
const offsetMode = props.offsetMode ? isRef(props.offsetMode) ? props.offsetMode.value : props.offsetMode : false;
await misskeyApi<MisskeyEntity[]>(props.pagination.endpoint, { await misskeyApi<MisskeyEntity[]>(props.pagination.endpoint, {
...params, ...params,
limit: SECOND_FETCH_LIMIT, limit: SECOND_FETCH_LIMIT,
...(props.pagination.offsetMode ? { ...(offsetMode ? {
offset: offset.value, offset: offset.value,
} : { } : {
untilId: Array.from(items.value.keys()).at(-1), untilId: Array.from(items.value.keys()).at(-1),
@ -304,10 +305,11 @@ const fetchMoreAhead = async (): Promise<void> => {
if (!more.value || fetching.value || moreFetching.value || items.value.size === 0) return; if (!more.value || fetching.value || moreFetching.value || items.value.size === 0) return;
moreFetching.value = true; moreFetching.value = true;
const params = props.pagination.params ? isRef(props.pagination.params) ? props.pagination.params.value : props.pagination.params : {}; const params = props.pagination.params ? isRef(props.pagination.params) ? props.pagination.params.value : props.pagination.params : {};
const offsetMode = props.offsetMode ? isRef(props.offsetMode) ? props.offsetMode.value : props.offsetMode : false;
await misskeyApi<MisskeyEntity[]>(props.pagination.endpoint, { await misskeyApi<MisskeyEntity[]>(props.pagination.endpoint, {
...params, ...params,
limit: SECOND_FETCH_LIMIT, limit: SECOND_FETCH_LIMIT,
...(props.pagination.offsetMode ? { ...(offsetMode ? {
offset: offset.value, offset: offset.value,
} : { } : {
sinceId: Array.from(items.value.keys()).at(-1), sinceId: Array.from(items.value.keys()).at(-1),

View file

@ -98,6 +98,9 @@ const selectedEmojis = ref<string[]>([]);
const pagination = { const pagination = {
endpoint: 'admin/emoji/list' as const, endpoint: 'admin/emoji/list' as const,
limit: 30, limit: 30,
offsetMode: computed(() => (
(query.value && query.value !== '') ? true : false
)),
params: computed(() => ({ params: computed(() => ({
query: (query.value && query.value !== '') ? query.value : null, query: (query.value && query.value !== '') ? query.value : null,
})), })),