feat(stories): Overlay-Spiegel fuer die App + Paritaetstest
- mobile/src/lib/story-overlays.ts + story-stickers.ts (identisch zu src/lib, plus Gesten-Geometrie fuer den Editor); Paritaetstest liegt in mobile/ (Docker-Build kennt mobile/ nicht) - Rueckgaben von create/createText/getVideoStatus ohne overlays-JSON und flacher Eingabetyp: Prismas rekursiver JsonValue- bzw. der tiefe Zod-Typ sprengten sonst den App-Compiler (TS2589) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8c640e2837
commit
795a39355e
5 files changed
+305
-10
No files matched your search
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Story-Editor: Web/Server und App haben je eine Kopie der Overlay-Konstanten,
|
||||
* Sticker und Geometrie (die App kann nichts aus src/ importieren, der
|
||||
* Docker-Build nichts aus mobile/). Dieser Test hält beide identisch. Er liegt
|
||||
* bewusst in mobile/ (vom Docker-Build und der Web-tsconfig ausgeschlossen) und
|
||||
* läuft über das Root-Vitest mit.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import * as web from "@/lib/story-overlays";
|
||||
import * as webStickers from "@/lib/story-stickers";
|
||||
import * as app from "./story-overlays";
|
||||
import * as appStickers from "./story-stickers";
|
||||
|
||||
describe("Story-Overlay-Spiegel Web ↔ App", () => {
|
||||
it("has identical constants", () => {
|
||||
for (const key of [
|
||||
"MAX_OVERLAYS",
|
||||
"MAX_GIFS",
|
||||
"MAX_MENTIONS",
|
||||
"MAX_TEXT_LENGTH",
|
||||
"TEXT_COLORS",
|
||||
"TEXT_STYLES",
|
||||
"TEXT_FONTS",
|
||||
"STORY_BACKGROUNDS",
|
||||
"STORY_BACKGROUND_KEYS",
|
||||
"TEXT_STORY_ASPECT",
|
||||
"OVERLAY_SIZES",
|
||||
"MIN_SCALE",
|
||||
"MAX_SCALE",
|
||||
] as const) {
|
||||
expect(app[key], key).toEqual(web[key]);
|
||||
}
|
||||
});
|
||||
|
||||
it("has identical stickers", () => {
|
||||
expect(appStickers.STORY_STICKERS).toEqual(webStickers.STORY_STICKERS);
|
||||
expect(appStickers.STICKER_OUTLINE).toEqual(webStickers.STICKER_OUTLINE);
|
||||
});
|
||||
|
||||
it("computes identical geometry", () => {
|
||||
const cases: [number, number, number, number][] = [
|
||||
[400, 800, 1080, 1350],
|
||||
[390, 844, 1920, 1080],
|
||||
[1280, 720, 9, 16],
|
||||
[0, 100, 10, 10],
|
||||
];
|
||||
for (const c of cases) expect(app.containRect(...c)).toEqual(web.containRect(...c));
|
||||
const rect = { x: 10, y: 20, width: 300, height: 500 };
|
||||
expect(app.overlayCenter(rect, { x: 0.25, y: 0.75 })).toEqual(web.overlayCenter(rect, { x: 0.25, y: 0.75 }));
|
||||
for (const color of web.TEXT_COLORS) expect(app.contrastTextColor(color)).toBe(web.contrastTextColor(color));
|
||||
for (const story of [{ mediaType: "TEXT" }, { mediaType: "VIDEO", aspectRatio: "16:9" }, { mediaType: "IMAGE", mediaWidth: 3, mediaHeight: 4 }]) {
|
||||
expect(app.storyMediaSize(story)).toEqual(web.storyMediaSize(story));
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* Story-Editor — App-Spiegel von src/lib/story-overlays.ts (Web/Server).
|
||||
* Konstanten und Geometrie müssen identisch bleiben, damit Overlays in App und
|
||||
* Web an derselben Stelle sitzen (src/__tests__/story-overlays-parity.test.ts
|
||||
* prüft das). Validiert wird nur auf dem Server; hier gibt es deshalb kein Zod.
|
||||
*
|
||||
* Positionen sind relativ zur Medienfläche (0–1), Größen relativ zu deren Breite.
|
||||
*/
|
||||
|
||||
export const MAX_OVERLAYS = 15;
|
||||
export const MAX_GIFS = 3;
|
||||
export const MAX_MENTIONS = 10;
|
||||
export const MAX_TEXT_LENGTH = 200;
|
||||
|
||||
export const TEXT_COLORS = ["#ffffff", "#171717", "#f97316", "#facc15", "#22c55e", "#3b82f6", "#ec4899", "#a855f7"] as const;
|
||||
export const TEXT_STYLES = ["plain", "box", "soft"] as const;
|
||||
export const TEXT_FONTS = ["regular", "bold"] as const;
|
||||
|
||||
export const STORY_BACKGROUNDS: Record<string, readonly [string, string]> = {
|
||||
sunset: ["#f97316", "#ec4899"],
|
||||
ocean: ["#0ea5e9", "#6366f1"],
|
||||
forest: ["#22c55e", "#0f766e"],
|
||||
night: ["#1e1b4b", "#7c3aed"],
|
||||
peach: ["#fdba74", "#fde68a"],
|
||||
ink: ["#262626", "#525252"],
|
||||
};
|
||||
export const STORY_BACKGROUND_KEYS = Object.keys(STORY_BACKGROUNDS);
|
||||
|
||||
export const TEXT_STORY_ASPECT = { width: 9, height: 16 } as const;
|
||||
|
||||
export const OVERLAY_SIZES = {
|
||||
textFont: 0.07,
|
||||
textMaxWidth: 0.82,
|
||||
mentionFont: 0.055,
|
||||
sticker: 0.26,
|
||||
gifWidth: 0.42,
|
||||
} as const;
|
||||
export const MIN_SCALE = 0.3;
|
||||
export const MAX_SCALE = 4;
|
||||
|
||||
export type TextColor = (typeof TEXT_COLORS)[number];
|
||||
export type TextStyle = (typeof TEXT_STYLES)[number];
|
||||
export type TextFont = (typeof TEXT_FONTS)[number];
|
||||
|
||||
type Base = { x: number; y: number; scale: number; rotation: number };
|
||||
export type TextOverlay = Base & { kind: "text"; text: string; color: TextColor; style: TextStyle; font: TextFont };
|
||||
export type MentionOverlay = Base & { kind: "mention"; petId: string };
|
||||
export type GifOverlay = Base & { kind: "gif"; gifId: string; url: string; width: number; height: number };
|
||||
export type StickerOverlay = Base & { kind: "sticker"; stickerId: string };
|
||||
export type StoryOverlay = TextOverlay | MentionOverlay | GifOverlay | StickerOverlay;
|
||||
|
||||
export type Rect = { x: number; y: number; width: number; height: number };
|
||||
|
||||
export function containRect(containerWidth: number, containerHeight: number, mediaWidth: number, mediaHeight: number): Rect {
|
||||
if (containerWidth <= 0 || containerHeight <= 0 || mediaWidth <= 0 || mediaHeight <= 0) {
|
||||
return { x: 0, y: 0, width: Math.max(containerWidth, 0), height: Math.max(containerHeight, 0) };
|
||||
}
|
||||
const scale = Math.min(containerWidth / mediaWidth, containerHeight / mediaHeight);
|
||||
const width = mediaWidth * scale;
|
||||
const height = mediaHeight * scale;
|
||||
return { x: (containerWidth - width) / 2, y: (containerHeight - height) / 2, width, height };
|
||||
}
|
||||
|
||||
export function overlayCenter(rect: Rect, overlay: { x: number; y: number }): { left: number; top: number } {
|
||||
return { left: rect.x + overlay.x * rect.width, top: rect.y + overlay.y * rect.height };
|
||||
}
|
||||
|
||||
export function contrastTextColor(background: string): "#171717" | "#ffffff" {
|
||||
const hex = background.replace("#", "");
|
||||
const [r, g, b] = [0, 2, 4].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255);
|
||||
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
||||
return luminance > 0.6 ? "#171717" : "#ffffff";
|
||||
}
|
||||
|
||||
export function storyMediaSize(story: {
|
||||
mediaType: string;
|
||||
mediaWidth?: number | null;
|
||||
mediaHeight?: number | null;
|
||||
aspectRatio?: string | null;
|
||||
}): { width: number; height: number } | null {
|
||||
if (story.mediaType === "TEXT") return { ...TEXT_STORY_ASPECT };
|
||||
if (story.mediaWidth && story.mediaHeight) return { width: story.mediaWidth, height: story.mediaHeight };
|
||||
const match = /^(\d+(?:\.\d+)?):(\d+(?:\.\d+)?)$/.exec(story.aspectRatio ?? "");
|
||||
if (match) return { width: Number(match[1]), height: Number(match[2]) };
|
||||
return null;
|
||||
}
|
||||
|
||||
// ── Nur App (Editor) ─────────────────────────────────────────────────────────
|
||||
|
||||
export const clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value));
|
||||
|
||||
/**
|
||||
* Neue Lage eines Overlays aus einer Geste: Verschiebung in Pixeln (dx/dy),
|
||||
* Zwei-Finger-Zoom (pinch = aktueller / Start-Fingerabstand) und Drehung
|
||||
* (Grad). Bleibt innerhalb der Medienfläche und der erlaubten Größe.
|
||||
*/
|
||||
export function applyGesture<T extends Base>(
|
||||
start: T,
|
||||
rect: Rect,
|
||||
gesture: { dx: number; dy: number; pinch: number; rotationDelta: number }
|
||||
): T {
|
||||
if (rect.width <= 0 || rect.height <= 0) return start;
|
||||
return {
|
||||
...start,
|
||||
x: clamp(start.x + gesture.dx / rect.width, 0, 1),
|
||||
y: clamp(start.y + gesture.dy / rect.height, 0, 1),
|
||||
scale: clamp(start.scale * gesture.pinch, MIN_SCALE, MAX_SCALE),
|
||||
rotation: normalizeRotation(start.rotation + gesture.rotationDelta),
|
||||
};
|
||||
}
|
||||
|
||||
/** Auf -180…180 Grad bringen (Server erlaubt -360…360). */
|
||||
export function normalizeRotation(deg: number): number {
|
||||
let r = deg % 360;
|
||||
if (r > 180) r -= 360;
|
||||
if (r < -180) r += 360;
|
||||
return Math.round(r * 10) / 10;
|
||||
}
|
||||
|
||||
/** Fingerabstand und -winkel zweier Berührungen (für Zoom/Drehen). */
|
||||
export function touchGeometry(a: { pageX: number; pageY: number }, b: { pageX: number; pageY: number }) {
|
||||
const dx = b.pageX - a.pageX;
|
||||
const dy = b.pageY - a.pageY;
|
||||
return { distance: Math.hypot(dx, dy), angle: (Math.atan2(dy, dx) * 180) / Math.PI };
|
||||
}
|
||||
|
||||
/** Winkeldifferenz ohne Sprung bei ±180°. */
|
||||
export function angleDelta(from: number, to: number): number {
|
||||
let d = to - from;
|
||||
if (d > 180) d -= 360;
|
||||
if (d < -180) d += 360;
|
||||
return d;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Story-Sticker (Story-Editor). Reine Formdaten im 100×100-Raster, damit Web
|
||||
* (DOM-SVG) und App (react-native-svg) exakt dasselbe zeichnen. Jeder Sticker
|
||||
* wird zweimal gezeichnet: erst alle Teile als weißer Umriss (STICKER_OUTLINE),
|
||||
* dann in Farbe — das gibt den typischen Aufkleber-Look auf jedem Hintergrund.
|
||||
*
|
||||
* ⚠️ Spiegel von src/lib/story-stickers.ts (Web/Server) — identisch halten
|
||||
* (src/__tests__/story-overlays-parity.test.ts prüft das).
|
||||
*/
|
||||
|
||||
export type StickerPart =
|
||||
| { type: "circle"; cx: number; cy: number; r: number; fill: string }
|
||||
| { type: "rect"; x: number; y: number; width: number; height: number; rx: number; fill: string }
|
||||
| { type: "path"; d: string; fill: string };
|
||||
|
||||
export type StickerDef = { id: string; label: string; parts: StickerPart[] };
|
||||
|
||||
export const STICKER_OUTLINE = { color: "#ffffff", width: 8 } as const;
|
||||
|
||||
export const STORY_STICKERS: StickerDef[] = [
|
||||
{
|
||||
id: "paw",
|
||||
label: "Pfote",
|
||||
parts: [
|
||||
{ type: "circle", cx: 50, cy: 64, r: 20, fill: "#f97316" },
|
||||
{ type: "circle", cx: 22, cy: 42, r: 9, fill: "#f97316" },
|
||||
{ type: "circle", cx: 38, cy: 26, r: 10, fill: "#f97316" },
|
||||
{ type: "circle", cx: 62, cy: 26, r: 10, fill: "#f97316" },
|
||||
{ type: "circle", cx: 78, cy: 42, r: 9, fill: "#f97316" },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "heart",
|
||||
label: "Herz",
|
||||
parts: [{ type: "path", d: "M50 86 C22 66 8 50 13 32 C18 15 40 12 50 29 C60 12 82 15 87 32 C92 50 78 66 50 86 Z", fill: "#ec4899" }],
|
||||
},
|
||||
{
|
||||
id: "bone",
|
||||
label: "Knochen",
|
||||
parts: [
|
||||
{ type: "rect", x: 24, y: 41, width: 52, height: 18, rx: 4, fill: "#fde68a" },
|
||||
{ type: "circle", cx: 22, cy: 40, r: 11, fill: "#fde68a" },
|
||||
{ type: "circle", cx: 22, cy: 60, r: 11, fill: "#fde68a" },
|
||||
{ type: "circle", cx: 78, cy: 40, r: 11, fill: "#fde68a" },
|
||||
{ type: "circle", cx: 78, cy: 60, r: 11, fill: "#fde68a" },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "star",
|
||||
label: "Stern",
|
||||
parts: [{ type: "path", d: "M50 8 L61 37 L93 38 L68 58 L77 90 L50 72 L23 90 L32 58 L7 38 L39 37 Z", fill: "#facc15" }],
|
||||
},
|
||||
{
|
||||
id: "sparkle",
|
||||
label: "Funkeln",
|
||||
parts: [
|
||||
{ type: "path", d: "M44 10 C47 34 54 41 78 44 C54 47 47 54 44 78 C41 54 34 47 10 44 C34 41 41 34 44 10 Z", fill: "#a855f7" },
|
||||
{ type: "path", d: "M78 62 C79 72 82 75 92 76 C82 77 79 80 78 90 C77 80 74 77 64 76 C74 75 77 72 78 62 Z", fill: "#a855f7" },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "fish",
|
||||
label: "Fisch",
|
||||
parts: [
|
||||
{ type: "path", d: "M74 50 L92 34 L92 66 Z", fill: "#3b82f6" },
|
||||
{ type: "path", d: "M12 50 C24 28 58 26 76 50 C58 74 24 72 12 50 Z", fill: "#3b82f6" },
|
||||
{ type: "circle", cx: 30, cy: 46, r: 5, fill: "#171717" },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "crown",
|
||||
label: "Krone",
|
||||
parts: [
|
||||
{ type: "path", d: "M14 76 L18 30 L36 50 L50 22 L64 50 L82 30 L86 76 Z", fill: "#facc15" },
|
||||
{ type: "circle", cx: 50, cy: 62, r: 6, fill: "#ec4899" },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "sun",
|
||||
label: "Sonne",
|
||||
parts: [
|
||||
{ type: "path", d: "M50 4 L57 22 L43 22 Z M50 96 L43 78 L57 78 Z M4 50 L22 43 L22 57 Z M96 50 L78 57 L78 43 Z M17 17 L33 26 L26 33 Z M83 83 L67 74 L74 67 Z M83 17 L74 33 L67 26 Z M17 83 L26 67 L33 74 Z", fill: "#fb923c" },
|
||||
{ type: "circle", cx: 50, cy: 50, r: 22, fill: "#facc15" },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export const STICKER_IDS = STORY_STICKERS.map((s) => s.id);
|
||||
@@ -86,6 +86,14 @@ export const storyOverlaysSchema = z
|
||||
|
||||
export type StoryOverlay = z.infer<typeof storyOverlaySchema>;
|
||||
|
||||
/**
|
||||
* Dieselbe Prüfung, aber mit flachem Typ für tRPC-Eingaben: Der tief
|
||||
* verschachtelte Zod-Typ (discriminatedUnion + refine) wird sonst bis in die
|
||||
* App durchgereicht und sprengt dort den Compiler („Type instantiation is
|
||||
* excessively deep“). Zur Laufzeit identisch zu storyOverlaysSchema.
|
||||
*/
|
||||
export const storyOverlaysInput = storyOverlaysSchema as unknown as z.ZodType<StoryOverlay[]>;
|
||||
|
||||
/** Markierte Tiere einer Story, ohne Doppelte. */
|
||||
export function mentionedPetIds(overlays: readonly StoryOverlay[] | null | undefined): string[] {
|
||||
return [...new Set((overlays ?? []).flatMap((o) => (o.kind === "mention" ? [o.petId] : [])))];
|
||||
|
||||
+21
-10
@@ -32,7 +32,7 @@ import {
|
||||
STORY_BACKGROUND_KEYS,
|
||||
mentionedPetIds,
|
||||
parseStoredOverlays,
|
||||
storyOverlaysSchema,
|
||||
storyOverlaysInput,
|
||||
type StoryOverlay,
|
||||
} from "@/lib/story-overlays";
|
||||
|
||||
@@ -47,9 +47,20 @@ import {
|
||||
* - All queries filter expiresAt > now — expired stories never returned (T-02-44)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Story-Datensatz ohne das JSON-Feld `overlays` für Rückgaben: Prismas
|
||||
* rekursiver JsonValue-Typ würde über tRPC bis in die App durchgereicht und
|
||||
* dort den Compiler sprengen. Overlays liefert nur listActive (geprüft/typisiert).
|
||||
*/
|
||||
function withoutOverlays<T extends { overlays?: unknown }>(story: T): Omit<T, "overlays"> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- bewusst verworfen
|
||||
const { overlays: _overlays, ...rest } = story;
|
||||
return rest;
|
||||
}
|
||||
|
||||
/** Story-Editor-Felder, gemeinsam für create / createVideoUpload. */
|
||||
const editorFields = {
|
||||
overlays: storyOverlaysSchema.optional(),
|
||||
overlays: storyOverlaysInput.optional(),
|
||||
mediaWidth: z.number().int().positive().max(20000).optional(),
|
||||
mediaHeight: z.number().int().positive().max(20000).optional(),
|
||||
};
|
||||
@@ -169,7 +180,7 @@ export const storiesRouter = router({
|
||||
},
|
||||
});
|
||||
await saveStoryMentions(ctx.prisma, story.id, input.petId, mentionIds, { sendNotifications: true });
|
||||
return story;
|
||||
return withoutOverlays(story);
|
||||
}),
|
||||
|
||||
/**
|
||||
@@ -182,7 +193,7 @@ export const storiesRouter = router({
|
||||
z.object({
|
||||
petId: z.string().min(1),
|
||||
background: z.string().refine((key) => STORY_BACKGROUND_KEYS.includes(key), "Unbekannter Hintergrund"),
|
||||
overlays: storyOverlaysSchema.refine((items) => items.some((o) => o.kind === "text"), "Eine Text-Story braucht Text"),
|
||||
overlays: storyOverlaysInput.refine((items) => items.some((o) => o.kind === "text"), "Eine Text-Story braucht Text"),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
@@ -200,7 +211,7 @@ export const storiesRouter = router({
|
||||
},
|
||||
});
|
||||
await saveStoryMentions(ctx.prisma, story.id, input.petId, mentionIds, { sendNotifications: true });
|
||||
return story;
|
||||
return withoutOverlays(story);
|
||||
}),
|
||||
|
||||
/**
|
||||
@@ -292,7 +303,7 @@ export const storiesRouter = router({
|
||||
const asset = await mux.video.assets.retrieve(assetId);
|
||||
if (asset.status === "ready" && asset.playback_ids?.[0]) {
|
||||
const overLimit = (asset.duration ?? 0) > MAX_STORY_VIDEO_DURATION_SECS;
|
||||
return await ctx.prisma.story.update({
|
||||
return withoutOverlays(await ctx.prisma.story.update({
|
||||
where: { id: input.storyId },
|
||||
data: overLimit
|
||||
? { videoStatus: "ERROR", muxAssetId: assetId }
|
||||
@@ -303,12 +314,12 @@ export const storiesRouter = router({
|
||||
durationSecs: asset.duration ?? null,
|
||||
aspectRatio: asset.aspect_ratio ?? null,
|
||||
},
|
||||
});
|
||||
}));
|
||||
} else if (asset.status === "errored") {
|
||||
return await ctx.prisma.story.update({
|
||||
return withoutOverlays(await ctx.prisma.story.update({
|
||||
where: { id: input.storyId },
|
||||
data: { videoStatus: "ERROR", muxAssetId: assetId },
|
||||
});
|
||||
}));
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
@@ -316,7 +327,7 @@ export const storiesRouter = router({
|
||||
}
|
||||
}
|
||||
|
||||
return story;
|
||||
return withoutOverlays(story);
|
||||
}),
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user