From 8186c36b9aa87e6b515c31a51f08a75e52c4d37e Mon Sep 17 00:00:00 2001 From: neodk2004 Date: Sun, 16 Aug 2026 14:36:44 +0200 Subject: [PATCH] feat(stories): support 30s video uploads, mirroring normal video posts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Story gains mediaType (IMAGE/VIDEO) plus the same Mux fields as VideoPost (muxUploadId/AssetId/PlaybackId, videoStatus, durationSecs, aspectRatio); storageKey becomes optional since video stories don't use Supabase Storage. - 30s duration cap (deliberately shorter than the 180s normal-post cap) enforced client-side before upload (reads the file's own duration) and again server-side in the mux webhook and stories.getVideoStatus's self-heal path, so a bypassed client check or a direct API call can't produce an oversized story video. - MAX_STORY_VIDEO_DURATION_SECS lives in a new client-safe src/lib/story-video-limits.ts rather than stories.ts, which is server-only — importing from a server-only module into StoryForm (a client component) pulled the pg driver into the client bundle and broke the build. - Mux webhook now resolves an asset to either a VideoPost or a Story; video stories never fan out to the feed (mirrors the normal-post webhook branch, minus fanOutPost). - StoryViewer: video stories autoplay muted (tap to unmute — browsers block autoplay-with-sound past the story that was directly clicked open) with a real playback-time progress bar (image stories keep the fixed 5s bar), rendered via object-contain to correctly handle the ~9:16 portrait ratio typical of phone-shot story videos instead of a fixed 16:9 box. --- messages/de.json | 17 +- messages/en.json | 17 +- prisma/schema.prisma | 28 ++- src/app/api/webhooks/mux/route.ts | 68 +++++-- src/components/stories/StoryForm.tsx | 261 +++++++++++++++++++------ src/components/stories/StoryViewer.tsx | 158 ++++++++++++--- src/lib/delete-account.ts | 2 +- src/lib/story-video-limits.ts | 13 ++ src/trpc/routers/stories.ts | 100 +++++++++- 9 files changed, 538 insertions(+), 126 deletions(-) create mode 100644 src/lib/story-video-limits.ts diff --git a/messages/de.json b/messages/de.json index 86eb9bb..1848b8f 100644 --- a/messages/de.json +++ b/messages/de.json @@ -452,17 +452,21 @@ "noPhoto": "Bitte füge mindestens ein Foto hinzu." }, "StoryForm": { - "addPhoto": "Foto hinzufügen", + "addMedia": "Foto oder Video hinzufügen", "disappearsNote": "Storys verschwinden nach 24 Stunden", - "changePhoto": "Foto ändern", + "changeMedia": "Ändern", "postCta": "Story posten", "posting": "Wird gepostet…", "successToast": "Story gepostet", "errorToast": "Deine Story konnte nicht gepostet werden. Bitte versuch es erneut.", "noPetSelected": "Kein aktives Tier ausgewählt.", "fileTooLarge": "Foto muss unter 10 MB sein.", - "invalidType": "Lade ein JPG-, PNG- oder WebP-Bild hoch.", - "uploadFailed": "Upload fehlgeschlagen — bitte versuch es erneut." + "invalidType": "Lade ein JPG-, PNG-, WebP-Bild oder ein MP4-, MOV-, WebM-Video hoch.", + "uploadFailed": "Upload fehlgeschlagen — bitte versuch es erneut.", + "videoLengthHint": "Video max. {max} Sekunden", + "videoTooLarge": "Video muss unter 500 MB sein.", + "videoTooLong": "Story-Videos dürfen höchstens {max} Sekunden lang sein.", + "videoUnreadable": "Videolänge konnte nicht gelesen werden — bitte versuch eine andere Datei." }, "MilestoneForm": { "questionLabel": "Was ist der Meilenstein?", @@ -508,7 +512,10 @@ "sheetTitleZero": "Noch keine Aufrufe", "sheetTitleSingular": "Von 1 Tier gesehen", "sheetTitlePlural": "Von {count} Tieren gesehen", - "noViewersYet": "Noch niemand hat diese Story angesehen." + "noViewersYet": "Noch niemand hat diese Story angesehen.", + "mute": "Stummschalten", + "unmute": "Ton einschalten", + "videoError": "Video konnte nicht verarbeitet werden." }, "StoryTray": { "storiesAriaLabel": "Storys", diff --git a/messages/en.json b/messages/en.json index 486dc6f..c6ab4fb 100644 --- a/messages/en.json +++ b/messages/en.json @@ -452,17 +452,21 @@ "noPhoto": "Please add at least one photo." }, "StoryForm": { - "addPhoto": "Add a photo", + "addMedia": "Add a photo or video", "disappearsNote": "Stories disappear after 24 hours", - "changePhoto": "Change photo", + "changeMedia": "Change", "postCta": "Post Story", "posting": "Posting…", "successToast": "Story posted", "errorToast": "Couldn't post your story. Please try again.", "noPetSelected": "No active pet selected.", "fileTooLarge": "Photo must be under 10 MB.", - "invalidType": "Upload a JPG, PNG, or WebP image.", - "uploadFailed": "Upload failed — please try again." + "invalidType": "Upload a JPG, PNG, WebP image, or an MP4, MOV, WebM video.", + "uploadFailed": "Upload failed — please try again.", + "videoLengthHint": "Video up to {max} seconds", + "videoTooLarge": "Video must be under 500 MB.", + "videoTooLong": "Story videos can be at most {max} seconds long.", + "videoUnreadable": "Couldn't read the video length — please try a different file." }, "MilestoneForm": { "questionLabel": "What's the milestone?", @@ -508,7 +512,10 @@ "sheetTitleZero": "No views yet", "sheetTitleSingular": "Seen by 1 pet", "sheetTitlePlural": "Seen by {count} pets", - "noViewersYet": "No one has viewed this story yet." + "noViewersYet": "No one has viewed this story yet.", + "mute": "Mute", + "unmute": "Unmute", + "videoError": "Video could not be processed." }, "StoryTray": { "storiesAriaLabel": "Stories", diff --git a/prisma/schema.prisma b/prisma/schema.prisma index c26e58b..3157474 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -339,19 +339,39 @@ model Milestone { } model Story { - id String @id @default(cuid()) - pet Pet @relation(fields: [petId], references: [id], onDelete: Cascade) + id String @id @default(cuid()) + pet Pet @relation(fields: [petId], references: [id], onDelete: Cascade) petId String - storageKey String // Supabase Storage object key + mediaType StoryMediaType @default(IMAGE) + storageKey String? // Supabase Storage object key — IMAGE stories only blurhash String? // computed client-side at upload (src/lib/blurhash.ts), decoded as a loading placeholder expiresAt DateTime // set to NOW() + 24 hours on create (D-07) views StoryView[] - createdAt DateTime @default(now()) + createdAt DateTime @default(now()) + + // VIDEO stories — mirrors VideoPost. Typically 9:16 (phone-shot, portrait), + // unlike normal feed videos which skew 16:9 — aspectRatio drives rendering, + // never assume landscape. Duration capped at 30s (see + // MAX_STORY_VIDEO_DURATION_SECS in stories.ts), enforced client-side + // before upload and again server-side when the asset becomes ready. + muxUploadId String? + muxAssetId String? + muxPlaybackId String? + videoStatus VideoStatus? + durationSecs Float? + aspectRatio String? // e.g. "9:16" from Mux // Gitea #18 — legal AI-content disclosure, set once at creation. aiDisclosure AiDisclosure @default(NONE) @@index([petId, expiresAt]) // supports lazy-delete filter query + @@index([muxUploadId]) + @@index([muxAssetId]) +} + +enum StoryMediaType { + IMAGE + VIDEO } model StoryView { diff --git a/src/app/api/webhooks/mux/route.ts b/src/app/api/webhooks/mux/route.ts index 3a48110..8f38b63 100644 --- a/src/app/api/webhooks/mux/route.ts +++ b/src/app/api/webhooks/mux/route.ts @@ -3,6 +3,7 @@ import { prisma } from "@/lib/prisma"; import { mux, MUX_WEBHOOK_SECRET } from "@/lib/mux"; import { redis } from "@/lib/redis"; import { fanOutPost } from "@/lib/feed-helpers"; +import { MAX_STORY_VIDEO_DURATION_SECS } from "@/lib/story-video-limits"; /** * POST /api/webhooks/mux @@ -53,6 +54,10 @@ export async function POST(req: NextRequest) { where: { muxUploadId: uploadId }, data: { muxAssetId: assetId }, }); + await prisma.story.updateMany({ + where: { muxUploadId: uploadId }, + data: { muxAssetId: assetId }, + }); break; } @@ -70,26 +75,49 @@ export async function POST(req: NextRequest) { where: { muxAssetId: assetId }, include: { post: { select: { id: true, petId: true, createdAt: true } } }, }); - if (!videoPost) break; - await prisma.videoPost.update({ - where: { id: videoPost.id }, - data: { - muxPlaybackId: playbackId, - status: "READY", - durationSecs: durationSecs ?? null, - aspectRatio: aspectRatio ?? null, - }, + if (videoPost) { + await prisma.videoPost.update({ + where: { id: videoPost.id }, + data: { + muxPlaybackId: playbackId, + status: "READY", + durationSecs: durationSecs ?? null, + aspectRatio: aspectRatio ?? null, + }, + }); + + // Fan-out to followers now that the video is playable + await fanOutPost( + redis, + videoPost.post.id, + videoPost.post.petId, + videoPost.post.createdAt, + prisma + ).catch((err) => console.error("[mux-webhook] fanOutPost failed", err)); + break; + } + + const story = await prisma.story.findFirst({ where: { muxAssetId: assetId } }); + if (!story) break; + + // Server-side safety net for the 30s story-video cap — the client + // already rejects oversized files before upload, but a video that + // slips through (or an upload made outside the app) must not become + // playable in a story just because it made it to Mux. + const overStoryLimit = (durationSecs ?? 0) > MAX_STORY_VIDEO_DURATION_SECS; + + await prisma.story.update({ + where: { id: story.id }, + data: overStoryLimit + ? { videoStatus: "ERROR" } + : { + muxPlaybackId: playbackId, + videoStatus: "READY", + durationSecs: durationSecs ?? null, + aspectRatio: aspectRatio ?? null, + }, }); - - // Fan-out to followers now that the video is playable - await fanOutPost( - redis, - videoPost.post.id, - videoPost.post.petId, - videoPost.post.createdAt, - prisma - ).catch((err) => console.error("[mux-webhook] fanOutPost failed", err)); break; } @@ -101,6 +129,10 @@ export async function POST(req: NextRequest) { where: { muxAssetId: assetId }, data: { status: "ERROR" }, }); + await prisma.story.updateMany({ + where: { muxAssetId: assetId }, + data: { videoStatus: "ERROR" }, + }); break; } diff --git a/src/components/stories/StoryForm.tsx b/src/components/stories/StoryForm.tsx index e1a2766..b58ee26 100644 --- a/src/components/stories/StoryForm.tsx +++ b/src/components/stories/StoryForm.tsx @@ -1,7 +1,7 @@ "use client"; import { useState, useRef, useCallback } from "react"; -import { Camera, Loader2, ImagePlus } from "lucide-react"; +import { Camera, Loader2, ImagePlus, Video } from "lucide-react"; import { useTranslations } from "next-intl"; import { toast } from "sonner"; import { useMutation, useQueryClient } from "@tanstack/react-query"; @@ -10,26 +10,60 @@ import { useActivePet } from "@/context/ActivePetContext"; import { Button } from "@/components/ui/button"; import { Progress } from "@/components/ui/progress"; import { computeBlurhash } from "@/lib/blurhash"; +import { uploadFileWithProgress } from "@/lib/upload-xhr"; import { AiDisclosureSelect, type AiDisclosureValue } from "@/components/content/AiDisclosureSelect"; +import { MAX_STORY_VIDEO_DURATION_SECS } from "@/lib/story-video-limits"; -const ALLOWED_TYPES = ["image/jpeg", "image/png", "image/webp"] as const; -type AllowedContentType = (typeof ALLOWED_TYPES)[number]; -const MAX_FILE_SIZE = 10 * 1024 * 1024; +const ALLOWED_IMAGE_TYPES = ["image/jpeg", "image/png", "image/webp"] as const; +type AllowedImageType = (typeof ALLOWED_IMAGE_TYPES)[number]; +const MAX_IMAGE_SIZE = 10 * 1024 * 1024; + +const ALLOWED_VIDEO_TYPES = ["video/mp4", "video/quicktime", "video/webm"] as const; +const MAX_VIDEO_SIZE = 500 * 1024 * 1024; // same as normal video posts (VideoUploadForm.tsx) + +const ACCEPT_ATTR = [...ALLOWED_IMAGE_TYPES, ...ALLOWED_VIDEO_TYPES].join(","); interface StoryFormProps { onClose: () => void; } type UploadState = "idle" | "uploading" | "ready" | "submitting"; +type MediaKind = "image" | "video"; + +/** Reads a video File's duration via a throwaway