feat: eigene Story löschen (Web + App)
- stories.delete: nur Owner; Aufrufe, Story (Pfoten/Kommentare per Cascade), danach Bild in Supabase Storage bzw. Mux-Asset best-effort - Web: Papierkorb neben der Zuschauerzahl + Rückfrage, Timer pausiert, gelöscht wird die beim Antippen gemerkte Story - App: gleicher Papierkorb mit nativer Rückfrage, Timer pausiert - found.md: Nutzer-Feedback "Story löschbar" erledigt, 7-Tage-Wunsch mit Befund als offener Punkt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4ec5d8c5f4
commit
81d9c6da30
9 files changed
+259
-27
No files matched your search
@@ -16,8 +16,16 @@ vi.mock("@/lib/supabase-storage", () => ({
|
||||
}),
|
||||
ALLOWED_CONTENT_TYPES: ["image/jpeg", "image/png", "image/webp"],
|
||||
MAX_FILE_SIZE: 10 * 1024 * 1024,
|
||||
deleteFiles: (...args: unknown[]) => mockDeleteFiles(...args),
|
||||
}));
|
||||
|
||||
const mockDeleteFiles = vi.fn().mockResolvedValue(undefined);
|
||||
const mockMuxDelete = vi.fn().mockResolvedValue(undefined);
|
||||
vi.mock("@/lib/mux", () => ({
|
||||
mux: { video: { assets: { delete: (...args: unknown[]) => mockMuxDelete(...args), retrieve: vi.fn() } } },
|
||||
}));
|
||||
vi.mock("@/lib/rate-limit", () => ({ checkRateLimit: vi.fn().mockResolvedValue({ success: true }) }));
|
||||
|
||||
vi.mock("@clerk/nextjs/server", () => ({
|
||||
auth: vi.fn().mockResolvedValue({ userId: "owner-1" }),
|
||||
}));
|
||||
@@ -141,6 +149,65 @@ describe("stories.create", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("stories.delete", () => {
|
||||
beforeEach(() => {
|
||||
mockDeleteFiles.mockClear();
|
||||
mockMuxDelete.mockClear();
|
||||
});
|
||||
|
||||
it("deletes the owner's photo story with its views and storage file", async () => {
|
||||
const { caller, mockPrisma } = createCaller();
|
||||
mockPrisma.story.findUnique.mockResolvedValue({ id: "s1", petId: "pet-1", storageKey: "stories/a.jpg", muxAssetId: null });
|
||||
mockPrisma.pet.findUnique.mockResolvedValue({ id: "pet-1", ownerId: "owner-1" });
|
||||
mockPrisma.$transaction.mockResolvedValue([]);
|
||||
|
||||
await expect(caller.delete({ storyId: "s1" })).resolves.toEqual({ deleted: true });
|
||||
|
||||
expect(mockPrisma.storyView.deleteMany).toHaveBeenCalledWith({ where: { storyId: "s1" } });
|
||||
expect(mockPrisma.story.delete).toHaveBeenCalledWith({ where: { id: "s1" } });
|
||||
expect(mockDeleteFiles).toHaveBeenCalledWith(["stories/a.jpg"]);
|
||||
expect(mockMuxDelete).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("also removes the Mux asset of a video story", async () => {
|
||||
const { caller, mockPrisma } = createCaller();
|
||||
mockPrisma.story.findUnique.mockResolvedValue({ id: "s2", petId: "pet-1", storageKey: null, muxAssetId: "asset-9" });
|
||||
mockPrisma.pet.findUnique.mockResolvedValue({ id: "pet-1", ownerId: "owner-1" });
|
||||
mockPrisma.$transaction.mockResolvedValue([]);
|
||||
|
||||
await caller.delete({ storyId: "s2" });
|
||||
|
||||
expect(mockMuxDelete).toHaveBeenCalledWith("asset-9");
|
||||
expect(mockDeleteFiles).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("refuses to delete another owner's story", async () => {
|
||||
const { caller, mockPrisma } = createCaller();
|
||||
mockPrisma.story.findUnique.mockResolvedValue({ id: "s3", petId: "pet-2", storageKey: "stories/b.jpg", muxAssetId: null });
|
||||
mockPrisma.pet.findUnique.mockResolvedValue({ id: "pet-2", ownerId: "owner-2" });
|
||||
|
||||
await expect(caller.delete({ storyId: "s3" })).rejects.toMatchObject({ code: "FORBIDDEN" });
|
||||
expect(mockPrisma.$transaction).not.toHaveBeenCalled();
|
||||
expect(mockDeleteFiles).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("still succeeds when the storage cleanup fails", async () => {
|
||||
const { caller, mockPrisma } = createCaller();
|
||||
mockPrisma.story.findUnique.mockResolvedValue({ id: "s4", petId: "pet-1", storageKey: "stories/c.jpg", muxAssetId: null });
|
||||
mockPrisma.pet.findUnique.mockResolvedValue({ id: "pet-1", ownerId: "owner-1" });
|
||||
mockPrisma.$transaction.mockResolvedValue([]);
|
||||
mockDeleteFiles.mockRejectedValueOnce(new Error("storage down"));
|
||||
|
||||
await expect(caller.delete({ storyId: "s4" })).resolves.toEqual({ deleted: true });
|
||||
});
|
||||
|
||||
it("returns NOT_FOUND for unknown stories", async () => {
|
||||
const { caller, mockPrisma } = createCaller();
|
||||
mockPrisma.story.findUnique.mockResolvedValue(null);
|
||||
await expect(caller.delete({ storyId: "nope" })).rejects.toMatchObject({ code: "NOT_FOUND" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("stories.listActive", () => {
|
||||
it("marks stories the viewer already pawed and strips the raw reactions", async () => {
|
||||
const { caller, mockPrisma } = createCaller();
|
||||
|
||||
@@ -3,7 +3,18 @@
|
||||
import { useState, useEffect, useCallback, useRef } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import dynamic from "next/dynamic";
|
||||
import { X, Eye, Volume2, VolumeX, Loader2, MessageCircle } from "lucide-react";
|
||||
import { X, Eye, Volume2, VolumeX, Loader2, MessageCircle, Trash2 } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from "@/components/ui/alert-dialog";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useTRPC } from "@/trpc/client";
|
||||
@@ -70,6 +81,9 @@ export function StoryViewer({
|
||||
const [progress, setProgress] = useState(0);
|
||||
const [viewerSheetOpen, setViewerSheetOpen] = useState(false);
|
||||
const [commentsOpen, setCommentsOpen] = useState(false);
|
||||
// Story-ID beim Antippen des Papierkorbs festhalten — der Timer kann
|
||||
// (bei Videos) weiterlaufen, gelöscht wird trotzdem genau diese Story.
|
||||
const [deleteTargetId, setDeleteTargetId] = useState<string | null>(null);
|
||||
const [isMuted, setIsMuted] = useState(true);
|
||||
const advancedRef = useRef(false);
|
||||
const musicAudioRef = useRef<HTMLAudioElement>(null);
|
||||
@@ -139,7 +153,7 @@ export function StoryViewer({
|
||||
// Image progress bar — fixed duration, unused for video stories (those
|
||||
// advance on the player's "ended" event instead, see onVideoEnded below).
|
||||
useEffect(() => {
|
||||
if (!open || !currentStory || isVideo) return;
|
||||
if (!open || !currentStory || isVideo || deleteTargetId) return;
|
||||
|
||||
setProgress(0);
|
||||
advancedRef.current = false;
|
||||
@@ -159,7 +173,7 @@ export function StoryViewer({
|
||||
|
||||
rafId = requestAnimationFrame(tick);
|
||||
return () => cancelAnimationFrame(rafId);
|
||||
}, [open, currentStory, isVideo, advance]);
|
||||
}, [open, currentStory, isVideo, advance, deleteTargetId]);
|
||||
|
||||
// Reset progress/advance-guard when a video story becomes current.
|
||||
useEffect(() => {
|
||||
@@ -228,6 +242,24 @@ export function StoryViewer({
|
||||
queryClient.invalidateQueries(trpc.stories.hasActiveStory.queryOptions({ petId }));
|
||||
}, [onOpenChange, queryClient, trpc, petId]);
|
||||
|
||||
const deleteStoryMutation = useMutation(
|
||||
trpc.stories.delete.mutationOptions({
|
||||
onSuccess: () => {
|
||||
const remaining = stories.length - 1;
|
||||
setDeleteTargetId(null);
|
||||
toast.success(t("storyDeleted"));
|
||||
void queryClient.invalidateQueries(trpc.stories.listActive.queryOptions({ petId }));
|
||||
void queryClient.invalidateQueries(trpc.follows.pathFilter());
|
||||
if (remaining <= 0) handleClose();
|
||||
else setCurrentIndex((i) => Math.min(i, remaining - 1));
|
||||
},
|
||||
onError: () => {
|
||||
setDeleteTargetId(null);
|
||||
toast.error(t("storyDeleteError"));
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
if (!currentStory) return null;
|
||||
|
||||
const mediaUrl = currentStory.storageKey ? getMediaUrl(currentStory.storageKey) : null;
|
||||
@@ -399,10 +431,11 @@ export function StoryViewer({
|
||||
<X className="h-5 w-5" aria-hidden="true" />
|
||||
</button>
|
||||
|
||||
{/* Viewer count — top left (owner only) */}
|
||||
{/* Viewer count + delete — top left (owner only) */}
|
||||
{isOwnPet && (
|
||||
<div className="absolute top-8 left-4 flex items-center gap-2">
|
||||
<button
|
||||
className="absolute top-8 left-4 flex items-center gap-1.5 px-3 py-2 rounded-full bg-black/40 text-white text-xs hover:bg-black/60"
|
||||
className="flex items-center gap-1.5 px-3 py-2 rounded-full bg-black/40 text-white text-xs hover:bg-black/60"
|
||||
onClick={() => setViewerSheetOpen(true)}
|
||||
aria-label={
|
||||
viewers.length === 1
|
||||
@@ -413,6 +446,15 @@ export function StoryViewer({
|
||||
<Eye className="h-3.5 w-3.5" aria-hidden="true" />
|
||||
<span>{viewers.length}</span>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="flex h-[34px] w-[34px] items-center justify-center rounded-full bg-black/40 text-white hover:bg-red-600/80 focus-visible:outline-2 focus-visible:outline-white"
|
||||
onClick={() => setDeleteTargetId(currentStory.id)}
|
||||
aria-label={t("deleteStory")}
|
||||
>
|
||||
<Trash2 className="h-3.5 w-3.5" aria-hidden="true" />
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Gitea #29 — paw + comment buttons, bottom right */}
|
||||
@@ -493,6 +535,27 @@ export function StoryViewer({
|
||||
)}
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
<AlertDialog open={!!deleteTargetId} onOpenChange={(next) => !next && !deleteStoryMutation.isPending && setDeleteTargetId(null)}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>{t("deleteStoryTitle")}</AlertDialogTitle>
|
||||
<AlertDialogDescription>{t("deleteStoryDescription")}</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel disabled={deleteStoryMutation.isPending}>{t("deleteStoryCancel")}</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
disabled={deleteStoryMutation.isPending}
|
||||
onClick={(event) => {
|
||||
event.preventDefault();
|
||||
if (deleteTargetId) deleteStoryMutation.mutate({ storyId: deleteTargetId });
|
||||
}}
|
||||
className="bg-red-600 text-white hover:bg-red-700"
|
||||
>
|
||||
{deleteStoryMutation.isPending ? <Loader2 className="h-4 w-4 animate-spin" /> : t("deleteStoryConfirm")}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
createSignedUploadUrl,
|
||||
ALLOWED_CONTENT_TYPES,
|
||||
MAX_FILE_SIZE,
|
||||
deleteFiles,
|
||||
} from "@/lib/supabase-storage";
|
||||
import { randomUUID } from "crypto";
|
||||
import { petIdentitySelect } from "@/repositories/pet-repository";
|
||||
@@ -442,6 +443,38 @@ export const storiesRouter = router({
|
||||
return comment;
|
||||
}),
|
||||
|
||||
/**
|
||||
* Owner löscht eine eigene Story (auch eine abgelaufene). Entfernt Aufrufe
|
||||
* (StoryView hat bewusst kein onDelete: Cascade, D-08), dann die Story —
|
||||
* Pfoten und Kommentare hängen per Cascade dran. Bild in Supabase Storage
|
||||
* und Mux-Asset werden danach best-effort gelöscht: ein Fehler dort darf
|
||||
* das Löschen für den Nutzer nicht scheitern lassen (nur geloggt).
|
||||
*/
|
||||
delete: protectedProcedure
|
||||
.use(rateLimited("stories.delete", 20, 60))
|
||||
.input(z.object({ storyId: z.string().min(1) }))
|
||||
.mutation(async ({ ctx, input }): Promise<{ deleted: true }> => {
|
||||
const story = await ctx.prisma.story.findUnique({
|
||||
where: { id: input.storyId },
|
||||
select: { id: true, petId: true, storageKey: true, muxAssetId: true },
|
||||
});
|
||||
if (!story) throw new TRPCError({ code: "NOT_FOUND" });
|
||||
await assertPetOwnership(ctx.prisma, story.petId, ctx.userId);
|
||||
|
||||
await ctx.prisma.$transaction([
|
||||
ctx.prisma.storyView.deleteMany({ where: { storyId: story.id } }),
|
||||
ctx.prisma.story.delete({ where: { id: story.id } }),
|
||||
]);
|
||||
|
||||
if (story.storageKey) {
|
||||
await deleteFiles([story.storageKey]).catch((err) => console.error("[stories] storage delete failed", err));
|
||||
}
|
||||
if (story.muxAssetId) {
|
||||
await mux.video.assets.delete(story.muxAssetId).catch((err) => console.error("[stories] mux delete failed", err));
|
||||
}
|
||||
return { deleted: true };
|
||||
}),
|
||||
|
||||
deleteComment: protectedProcedure
|
||||
.input(z.object({ commentId: z.string().min(1), petId: z.string().min(1) }))
|
||||
.mutation(async ({ ctx, input }): Promise<void> => {
|
||||
|
||||
Reference in new issue
Block a user