Add notificationSchema

This commit is contained in:
Alex Gleason 2023-05-04 10:24:34 -05:00
parent f8d31aa505
commit 55ebc8c6ee
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 119 additions and 3 deletions

View File

@ -0,0 +1,10 @@
import { z } from 'zod';
import { normalizeChatMessage } from 'soapbox/normalizers';
import { toSchema } from 'soapbox/utils/normalizers';
const chatMessageSchema = toSchema(normalizeChatMessage);
type ChatMessage = z.infer<typeof chatMessageSchema>;
export { chatMessageSchema, type ChatMessage };

View File

@ -1,7 +1,6 @@
import { z } from 'zod';
/** Validates the string as an emoji. */
const emojiSchema = z.string().refine((v) => /\p{Extended_Pictographic}/u.test(v));
import { emojiSchema } from './utils';
/** Pleroma emoji reaction. */
const emojiReactionSchema = z.object({

View File

@ -0,0 +1,104 @@
import { z } from 'zod';
import { accountSchema } from './account';
import { chatMessageSchema } from './chat-message';
import { statusSchema } from './status';
import { emojiSchema } from './utils';
const baseNotificationSchema = z.object({
account: accountSchema,
created_at: z.string().datetime().catch(new Date().toUTCString()),
id: z.string(),
type: z.string(),
total_count: z.number().optional().catch(undefined), // TruthSocial
});
const mentionNotificationSchema = baseNotificationSchema.extend({
type: z.literal('mention'),
status: statusSchema,
});
const statusNotificationSchema = baseNotificationSchema.extend({
type: z.literal('status'),
status: statusSchema,
});
const reblogNotificationSchema = baseNotificationSchema.extend({
type: z.literal('reblog'),
status: statusSchema,
});
const followNotificationSchema = baseNotificationSchema.extend({
type: z.literal('follow'),
});
const followRequestNotificationSchema = baseNotificationSchema.extend({
type: z.literal('follow_request'),
});
const favouriteNotificationSchema = baseNotificationSchema.extend({
type: z.literal('favourite'),
status: statusSchema,
});
const pollNotificationSchema = baseNotificationSchema.extend({
type: z.literal('poll'),
status: statusSchema,
});
const updateNotificationSchema = baseNotificationSchema.extend({
type: z.literal('update'),
status: statusSchema,
});
const moveNotificationSchema = baseNotificationSchema.extend({
type: z.literal('move'),
target: accountSchema,
});
const chatMessageNotificationSchema = baseNotificationSchema.extend({
type: z.literal('chat_message'),
chat_message: chatMessageSchema,
});
const emojiReactionNotificationSchema = baseNotificationSchema.extend({
type: z.literal('pleroma:emoji_reaction'),
emoji: emojiSchema,
emoji_url: z.string().url().optional().catch(undefined),
});
const eventReminderNotificationSchema = baseNotificationSchema.extend({
type: z.literal('pleroma:event_reminder'),
status: statusSchema,
});
const participationRequestNotificationSchema = baseNotificationSchema.extend({
type: z.literal('pleroma:participation_request'),
status: statusSchema,
});
const participationAcceptedNotificationSchema = baseNotificationSchema.extend({
type: z.literal('pleroma:participation_accepted'),
status: statusSchema,
});
const notificationSchema = z.discriminatedUnion('type', [
mentionNotificationSchema,
statusNotificationSchema,
reblogNotificationSchema,
followNotificationSchema,
followRequestNotificationSchema,
favouriteNotificationSchema,
pollNotificationSchema,
updateNotificationSchema,
moveNotificationSchema,
chatMessageNotificationSchema,
emojiReactionNotificationSchema,
eventReminderNotificationSchema,
participationRequestNotificationSchema,
participationAcceptedNotificationSchema,
]);
type Notification = z.infer<typeof notificationSchema>;
export { notificationSchema, type Notification };

View File

@ -13,6 +13,9 @@ function filteredArray<T extends z.ZodTypeAny>(schema: T) {
));
}
/** Validates the string as an emoji. */
const emojiSchema = z.string().refine((v) => /\p{Extended_Pictographic}/u.test(v));
/** Map a list of CustomEmoji to their shortcodes. */
function makeCustomEmojiMap(customEmojis: CustomEmoji[]) {
return customEmojis.reduce<Record<string, CustomEmoji>>((result, emoji) => {
@ -21,4 +24,4 @@ function makeCustomEmojiMap(customEmojis: CustomEmoji[]) {
}, {});
}
export { filteredArray, makeCustomEmojiMap };
export { filteredArray, makeCustomEmojiMap, emojiSchema };