Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
5.7 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import {EmbedMediaFlags, EmbedMediaFlagsDescriptions} from '@fluxer/constants/src/ChannelConstants';
import {createBitflagInt32Type, Int32Type} from '@fluxer/schema/src/primitives/SchemaPrimitives';
import {z} from 'zod';

export const EmbedAuthorResponse = z.object({
	name: z.string().describe('The name of the author'),
	url: z.url().nullish().describe('The URL of the author'),
	icon_url: z.url().nullish().describe('The URL of the author icon'),
	proxy_icon_url: z.url().nullish().describe('The proxied URL of the author icon'),
});

export type EmbedAuthorResponse = z.infer<typeof EmbedAuthorResponse>;

export const EmbedProviderResponse = z.object({
	name: z.string().describe('The name of the provider'),
	url: z.url().nullish().describe('The URL of the provider'),
});

export const EmbedFooterResponse = z.object({
	text: z.string().describe('The footer text'),
	icon_url: z.url().nullish().describe('The URL of the footer icon'),
	proxy_icon_url: z.url().nullish().describe('The proxied URL of the footer icon'),
});

export type EmbedFooterResponse = z.infer<typeof EmbedFooterResponse>;

export const EmbedMediaResponse = z.object({
	url: z.string().describe('The URL of the media'),
	proxy_url: z.url().nullish().describe('The proxied URL of the media'),
	content_type: z.string().nullish().describe('The MIME type of the media'),
	content_hash: z.string().nullish().describe('The hash of the media content'),
	width: Int32Type.nullish().describe('The width of the media in pixels'),
	height: Int32Type.nullish().describe('The height of the media in pixels'),
	description: z.string().nullish().describe('The description of the media'),
	placeholder: z.string().nullish().describe('The base64 encoded placeholder image for lazy loading'),
	duration: Int32Type.nullish().describe('The duration of the media in seconds'),
	flags: createBitflagInt32Type(
		EmbedMediaFlags,
		EmbedMediaFlagsDescriptions,
		'The bitwise flags for this media',
		'EmbedMediaFlags',
	),
});

export type EmbedMediaResponse = z.infer<typeof EmbedMediaResponse>;

export const EmbedFieldResponse = z.object({
	name: z.string().describe('The name of the field'),
	value: z.string().describe('The value of the field'),
	inline: z.boolean().describe('Whether the field should be displayed inline'),
});

export type EmbedFieldResponse = z.infer<typeof EmbedFieldResponse>;

export const MessageEmbedChildResponse = z.object({
	type: z.string().describe('The type of embed (e.g., rich, image, video, gifv, article, link)'),
	url: z.url().nullish().describe('The URL of the embed'),
	title: z.string().nullish().describe('The title of the embed'),
	color: Int32Type.nullish().describe('The color code of the embed sidebar'),
	timestamp: z.iso.datetime().nullish().describe('The ISO 8601 timestamp of the embed content'),
	description: z.string().nullish().describe('The description of the embed'),
	author: EmbedAuthorResponse.nullish().describe('The author information of the embed'),
	image: EmbedMediaResponse.nullish().describe('The image of the embed'),
	thumbnail: EmbedMediaResponse.nullish().describe('The thumbnail of the embed'),
	footer: EmbedFooterResponse.nullish().describe('The footer of the embed'),
	fields: z.array(EmbedFieldResponse).nullish().describe('The fields of the embed'),
	provider: EmbedProviderResponse.nullish().describe('The provider of the embed (e.g., YouTube, Twitter)'),
	video: EmbedMediaResponse.nullish().describe('The video of the embed'),
	audio: EmbedMediaResponse.nullish().describe('The audio of the embed'),
	html: z.string().nullish().describe('Sanitized oEmbed HTML for trusted specialized embed renderers'),
	html_width: Int32Type.nullish().describe('The preferred width of sanitized oEmbed HTML'),
	html_height: Int32Type.nullish().describe('The preferred height of sanitized oEmbed HTML'),
	nsfw: z.boolean().nullish().describe('Whether the embed is flagged as NSFW'),
});

export type MessageEmbedChildResponse = z.infer<typeof MessageEmbedChildResponse>;

export const MessageEmbedResponse = MessageEmbedChildResponse.extend({
	children: z
		.array(MessageEmbedChildResponse)
		.max(1)
		.nullish()
		.describe('Internal nested embeds generated by unfurlers'),
});

export type MessageEmbedResponse = z.infer<typeof MessageEmbedResponse>;

export interface EmbedAuthor {
	readonly name: string;
	readonly url?: string;
	readonly icon_url?: string;
	readonly proxy_icon_url?: string;
}

interface EmbedProvider {
	readonly name: string;
	readonly url?: string;
}

export interface EmbedFooter {
	readonly text: string;
	readonly icon_url?: string;
	readonly proxy_icon_url?: string;
}

export interface EmbedMedia {
	readonly url: string;
	readonly proxy_url?: string;
	readonly content_type?: string;
	readonly content_hash?: string | null;
	readonly width?: number;
	readonly height?: number;
	readonly placeholder?: string;
	readonly flags: number;
	readonly description?: string;
	readonly duration?: number;
	readonly nsfw?: boolean;
}

export type EmbedField = Readonly<EmbedFieldResponse>;

export interface MessageEmbed extends MessageEmbedChild {
	readonly children?: ReadonlyArray<MessageEmbedChild>;
}

interface MessageEmbedChild {
	readonly id?: string;
	readonly type: string;
	readonly url?: string;
	readonly title?: string;
	readonly color?: number;
	readonly timestamp?: string;
	readonly description?: string;
	readonly author?: EmbedAuthor;
	readonly image?: EmbedMedia;
	readonly thumbnail?: EmbedMedia;
	readonly footer?: EmbedFooter;
	readonly fields?: ReadonlyArray<EmbedField>;
	readonly provider?: EmbedProvider;
	readonly video?: EmbedMedia;
	readonly audio?: EmbedMedia;
	readonly html?: string;
	readonly html_width?: number;
	readonly html_height?: number;
	readonly flags?: number;
	readonly nsfw?: boolean;
}