Fix parsing custom emoji reactions

Fixes https://gitlab.com/soapbox-pub/soapbox/-/issues/1602
This commit is contained in:
Alex Gleason 2023-11-13 16:11:40 -06:00
parent ded13132ff
commit f77aa7b411
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 18 additions and 8 deletions

View file

@ -14,6 +14,7 @@ import { normalizeAttachment } from 'soapbox/normalizers/attachment';
import { normalizeEmoji } from 'soapbox/normalizers/emoji';
import { normalizeMention } from 'soapbox/normalizers/mention';
import { accountSchema, cardSchema, emojiReactionSchema, groupSchema, pollSchema, tombstoneSchema } from 'soapbox/schemas';
import { filteredArray } from 'soapbox/schemas/utils';
import { maybeFromJS } from 'soapbox/utils/normalizers';
import type { Account, Attachment, Card, Emoji, Group, Mention, Poll, EmbeddedEntity, EmojiReaction } from 'soapbox/types/entities';
@ -219,11 +220,13 @@ const normalizeEvent = (status: ImmutableMap<string, any>) => {
}
};
// Normalize emojis
/** Normalize emojis. */
const normalizeEmojis = (status: ImmutableMap<string, any>) => {
const reactions = status.getIn(['pleroma', 'emoji_reactions'], status.get('reactions')) as ImmutableList<ImmutableMap<string, any>>;
const data = ImmutableList<ImmutableMap<string, any>>(status.getIn(['pleroma', 'emoji_reactions']) || status.get('reactions'));
const reactions = filteredArray(emojiReactionSchema).parse(data.toJS());
if (reactions) {
status.set('reactions', ImmutableList(reactions.map(((reaction: ImmutableMap<string, any>) => emojiReactionSchema.parse(reaction.toJS())))));
status.set('reactions', ImmutableList(reactions));
}
};

View file

@ -2,15 +2,22 @@ import { z } from 'zod';
import { emojiSchema } from './utils';
/** Pleroma emoji reaction. */
const emojiReactionSchema = z.object({
name: emojiSchema,
const baseEmojiReactionSchema = z.object({
count: z.number().nullable().catch(null),
me: z.boolean().catch(false),
/** Akkoma custom emoji reaction. */
url: z.string().url().optional().catch(undefined),
name: emojiSchema,
url: z.literal(undefined).catch(undefined),
});
const customEmojiReactionSchema = baseEmojiReactionSchema.extend({
name: z.string(),
/** Akkoma custom emoji reaction. */
url: z.string().url(),
});
/** Pleroma emoji reaction. */
const emojiReactionSchema = baseEmojiReactionSchema.or(customEmojiReactionSchema);
type EmojiReaction = z.infer<typeof emojiReactionSchema>;
export { emojiReactionSchema, type EmojiReaction };