bigbuffet-rw/app/soapbox/schemas/group.ts

58 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-03-10 10:42:49 -08:00
import escapeTextContentForBrowser from 'escape-html';
import z from 'zod';
import emojify from 'soapbox/features/emoji';
import { unescapeHTML } from 'soapbox/utils/html';
import { customEmojiSchema } from './custom-emoji';
import { groupRelationshipSchema } from './group-relationship';
2023-04-04 12:54:32 -07:00
import { groupTagSchema } from './group-tag';
2023-03-10 10:42:49 -08:00
import { filteredArray, makeCustomEmojiMap } from './utils';
const avatarMissing = require('assets/images/avatar-missing.png');
const headerMissing = require('assets/images/header-missing.png');
const groupSchema = z.object({
avatar: z.string().catch(avatarMissing),
avatar_static: z.string().catch(''),
created_at: z.string().datetime().catch(new Date().toUTCString()),
deleted_at: z.string().datetime().or(z.null()).catch(null),
2023-03-10 10:42:49 -08:00
display_name: z.string().catch(''),
domain: z.string().catch(''),
2023-05-02 16:30:21 -07:00
emojis: filteredArray(customEmojiSchema),
2023-03-10 10:42:49 -08:00
group_visibility: z.string().catch(''), // TruthSocial
header: z.string().catch(headerMissing),
header_static: z.string().catch(''),
2023-04-19 13:45:26 -07:00
id: z.coerce.string(),
2023-03-10 10:42:49 -08:00
locked: z.boolean().catch(false),
membership_required: z.boolean().catch(false),
members_count: z.number().catch(0),
2023-05-30 09:08:28 -07:00
owner: z.object({ id: z.string() }),
2023-03-13 10:05:56 -07:00
note: z.string().transform(note => note === '<p></p>' ? '' : note).catch(''),
relationship: groupRelationshipSchema.nullable().catch(null), // Dummy field to be overwritten later
2023-04-12 13:51:30 -07:00
slug: z.string().catch(''), // TruthSocial
source: z.object({
note: z.string(),
}).optional(), // TruthSocial
2023-03-10 10:42:49 -08:00
statuses_visibility: z.string().catch('public'),
2023-04-04 12:54:32 -07:00
tags: z.array(groupTagSchema).catch([]),
2023-03-10 10:42:49 -08:00
uri: z.string().catch(''),
url: z.string().catch(''),
}).transform(group => {
2023-03-10 10:42:49 -08:00
group.avatar_static = group.avatar_static || group.avatar;
group.header_static = group.header_static || group.header;
group.locked = group.locked || group.group_visibility === 'members_only'; // TruthSocial
const customEmojiMap = makeCustomEmojiMap(group.emojis);
return {
...group,
display_name_html: emojify(escapeTextContentForBrowser(group.display_name), customEmojiMap),
note_emojified: emojify(group.note, customEmojiMap),
note_plain: group.source?.note || unescapeHTML(group.note),
2023-03-10 10:42:49 -08:00
};
});
type Group = z.infer<typeof groupSchema>;
export { groupSchema, type Group };