bigbuffet-rw/packages/pl-api/lib/entities/translation.ts
marcin mikołajczak a6bc160caa pl-api: Do some blind search and replace
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2024-10-14 00:25:30 +02:00

42 lines
1.2 KiB
TypeScript

import * as v from 'valibot';
import { filteredArray } from './utils';
const translationPollSchema = v.object({
id: v.string(),
options: z.array(v.object({
title: v.string(),
})),
});
const translationMediaAttachment = v.object({
id: v.string(),
description: v.fallback(v.string(), ''),
});
/** @see {@link https://docs.joinmastodon.org/entities/Translation/} */
const translationSchema = z.preprocess((translation: any) => {
/**
* handle Akkoma
* @see {@link https://akkoma.dev/AkkomaGang/akkoma/src/branch/develop/lib/pleroma/web/mastodon_api/controllers/status_controller.ex#L504}
*/
if (translation?.text) return {
content: translation.text,
detected_source_language: translation.detected_language,
provider: '',
};
return translation;
}, v.object({
id: v.fallback(v.nullable(v.string()), null),
content: v.fallback(v.string(), ''),
spoiler_text: v.fallback(v.string(), ''),
poll: translationPollSchema.optional().catch(undefined),
media_attachments: filteredArray(translationMediaAttachment),
detected_source_language: v.string(),
provider: v.string(),
}));
type Translation = v.InferOutput<typeof translationSchema>;
export { translationSchema, type Translation };