Improve schemas for statuses

This commit is contained in:
Alex Gleason 2023-05-19 11:13:44 -05:00
parent fa0bf8f5df
commit 6062a06746
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 33 additions and 7 deletions

View file

@ -49,7 +49,7 @@ const accountSchema = z.object({
verified: z.boolean().default(false), verified: z.boolean().default(false),
website: z.string().catch(''), website: z.string().catch(''),
/** /*
* Internal fields * Internal fields
*/ */
display_name_html: z.string().catch(''), display_name_html: z.string().catch(''),
@ -57,7 +57,7 @@ const accountSchema = z.object({
note_emojified: z.string().catch(''), note_emojified: z.string().catch(''),
relationship: relationshipSchema.nullable().catch(null), relationship: relationshipSchema.nullable().catch(null),
/** /*
* Misc * Misc
*/ */
other_settings: z.any(), other_settings: z.any(),
@ -99,7 +99,7 @@ const accountSchema = z.object({
// Notes // Notes
account.note_emojified = emojify(account.note, customEmojiMap); account.note_emojified = emojify(account.note, customEmojiMap);
/** /*
* Todo * Todo
* - internal fields * - internal fields
* - donor * - donor

View file

@ -62,6 +62,12 @@ const audioAttachmentSchema = baseAttachmentSchema.extend({
type: z.literal('audio'), type: z.literal('audio'),
meta: z.object({ meta: z.object({
duration: z.number().optional().catch(undefined), duration: z.number().optional().catch(undefined),
colors: z.object({
background: z.string().optional().catch(undefined),
foreground: z.string().optional().catch(undefined),
accent: z.string().optional().catch(undefined),
duration: z.number().optional().catch(undefined),
}).optional().catch(undefined),
}).catch({}), }).catch({}),
}); });

View file

@ -40,6 +40,9 @@ const baseStatusSchema = z.object({
mentions: filteredArray(mentionSchema), mentions: filteredArray(mentionSchema),
muted: z.coerce.boolean(), muted: z.coerce.boolean(),
pinned: z.coerce.boolean(), pinned: z.coerce.boolean(),
pleroma: z.object({
quote_visible: z.boolean().catch(true),
}).optional().catch(undefined),
poll: pollSchema.nullable().catch(null), poll: pollSchema.nullable().catch(null),
quote: z.literal(null).catch(null), quote: z.literal(null).catch(null),
quotes_count: z.number().catch(0), quotes_count: z.number().catch(0),
@ -58,7 +61,7 @@ const baseStatusSchema = z.object({
}); });
type BaseStatus = z.infer<typeof baseStatusSchema>; type BaseStatus = z.infer<typeof baseStatusSchema>;
type TransformableStatus = Omit<BaseStatus, 'reblog' | 'quote'>; type TransformableStatus = Omit<BaseStatus, 'reblog' | 'quote' | 'pleroma'>;
/** Creates search index from the status. */ /** Creates search index from the status. */
const buildSearchIndex = (status: TransformableStatus): string => { const buildSearchIndex = (status: TransformableStatus): string => {
@ -76,6 +79,11 @@ const buildSearchIndex = (status: TransformableStatus): string => {
return new DOMParser().parseFromString(searchContent, 'text/html').documentElement.textContent || ''; return new DOMParser().parseFromString(searchContent, 'text/html').documentElement.textContent || '';
}; };
type Translation = {
content: string
provider: string
}
/** Add internal fields to the status. */ /** Add internal fields to the status. */
const transformStatus = <T extends TransformableStatus>(status: T) => { const transformStatus = <T extends TransformableStatus>(status: T) => {
const emojiMap = makeCustomEmojiMap(status.emojis); const emojiMap = makeCustomEmojiMap(status.emojis);
@ -89,6 +97,11 @@ const transformStatus = <T extends TransformableStatus>(status: T) => {
spoilerHtml, spoilerHtml,
search_index: buildSearchIndex(status), search_index: buildSearchIndex(status),
hidden: false, hidden: false,
filtered: [],
showFiltered: false, // TODO: this should be removed from the schema and done somewhere else
approval_status: 'approval' as const,
translation: undefined as Translation | undefined,
expectsCard: false,
}; };
}; };
@ -103,12 +116,19 @@ const statusSchema = baseStatusSchema.extend({
pleroma: z.object({ pleroma: z.object({
event: eventSchema, event: eventSchema,
quote: embeddedStatusSchema, quote: embeddedStatusSchema,
}), quote_visible: z.boolean().catch(true),
}).optional().catch(undefined),
}).transform(({ pleroma, ...status }) => { }).transform(({ pleroma, ...status }) => {
return { return {
...status, ...status,
event: pleroma.event, event: pleroma?.event,
quote: pleroma.quote || status.quote, quote: pleroma?.quote || status.quote || null,
// There's apparently no better way to do this...
// Just trying to remove the `event` and `quote` keys from the object.
pleroma: pleroma ? (() => {
const { event, quote, ...rest } = pleroma;
return rest;
})() : undefined,
}; };
}).transform(transformStatus); }).transform(transformStatus);