bigbuffet-rw/packages/pl-api/lib/entities/emoji-reaction.ts
2024-09-13 13:05:06 +02:00

34 lines
1.1 KiB
TypeScript

import { z } from 'zod';
import { accountSchema } from './account';
import { emojiSchema, filteredArray } from './utils';
const baseEmojiReactionSchema = z.object({
count: z.number().nullable().catch(null),
me: z.boolean().catch(false),
name: emojiSchema,
url: z.literal(undefined).catch(undefined),
static_url: z.literal(undefined).catch(undefined),
accounts: filteredArray(accountSchema),
account_ids: filteredArray(z.string()).catch([]),
});
const customEmojiReactionSchema = baseEmojiReactionSchema.extend({
name: z.string(),
url: z.string().url(),
static_url: z.string().url(),
});
/**
* Pleroma emoji reaction.
* @see {@link https://docs.pleroma.social/backend/development/API/differences_in_mastoapi_responses/#statuses}
*/
const emojiReactionSchema = z.preprocess((reaction: any) => reaction ? {
static_url: reaction.url,
account_ids: reaction.accounts?.map((account: any) => account?.id),
...reaction,
} : null, baseEmojiReactionSchema.or(customEmojiReactionSchema));
type EmojiReaction = z.infer<typeof emojiReactionSchema>;
export { emojiReactionSchema, type EmojiReaction };