syncova-links/server/model.js
jf 27924e55e9
All checks were successful
Docker-Image veröffentlichen / publish (push) Successful in 1m44s
Initial import with Gitea Docker publishing
2026-11-16 15:14:43 +01:00

57 lines
2.2 KiB
JavaScript

import { z } from 'zod'
export function httpUrl(value) {
const url = new URL(value)
if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password) {
throw new Error('Bitte eine HTTP- oder HTTPS-Adresse ohne Zugangsdaten eingeben.')
}
return url
}
const title = z.string().trim().min(1).max(120)
const id = z.uuid()
const link = z.object({
id,
title,
url: z.string().trim().max(2048).refine((value) => {
try { return Boolean(httpUrl(value)) } catch { return false }
}, 'Bitte eine gültige HTTP- oder HTTPS-Adresse eingeben.'),
description: z.string().trim().max(500),
icon: id.nullable(),
})
const links = z.array(link).max(200)
const zone = z.string().min(1).max(100).refine((value) => {
try { new Intl.DateTimeFormat('de', { timeZone: value }); return true } catch { return false }
}, 'Bitte eine gültige Zeitzone eingeben.')
export const settings = z.object({
name: title,
slug: z.string().trim().min(1).max(60)
.regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/, 'Der URL-Pfad darf Kleinbuchstaben, Zahlen und Bindestriche enthalten.')
.refine((value) => !['api', 'assets', 'healthz'].includes(value), 'Dieser URL-Pfad ist reserviert.'),
view: z.enum(['list', 'grid']),
})
export const layout = z.object({
loose: links,
blocks: z.array(z.discriminatedUnion('type', [
z.object({ id, type: z.literal('group'), title, links }),
z.object({ id, type: z.literal('text'), title: z.string().trim().max(120), text: z.string().max(5000) }),
z.object({ id, type: z.literal('clock'), timeZone: zone, hour12: z.boolean() }),
])).max(80),
}).refine(({ blocks, loose }) => {
const ids = [...loose.map((link) => link.id)]
for (const block of blocks) {
ids.push(block.id)
if (block.type === 'group') ids.push(...block.links.map((link) => link.id))
}
return ids.length <= 400 && new Set(ids).size === ids.length
}, 'Die Seite darf höchstens 400 eindeutige Einträge enthalten.')
export const update = settings.extend({ version: z.number().int().positive(), layout })
export function iconIds({ loose, blocks }) {
const links = [...loose, ...blocks.flatMap((block) => block.type === 'group' ? block.links : [])]
return new Set(links.map((link) => link.icon).filter(Boolean))
}