diff --git a/messages/de.json b/messages/de.json index 797f448..6143e59 100644 --- a/messages/de.json +++ b/messages/de.json @@ -486,7 +486,8 @@ "typeVaccineDue": "💉 Die Impfung „{name}“ ist am {date} fällig – Zeit, einen Termin zu vereinbaren.", "typeStoryMention": "hat dich in einer Story markiert.", "typeStoryComment": "hat deine Story kommentiert.", - "typeStoryReaction": "hat deiner Story eine Pfote gegeben." + "typeStoryReaction": "hat deiner Story eine Pfote gegeben.", + "typeUnknown": "hat eine neue Mitteilung für dich." }, "PhotoPostForm": { "captionPlaceholder": "Bildunterschrift schreiben…", diff --git a/messages/en.json b/messages/en.json index cc4b03f..1eb841a 100644 --- a/messages/en.json +++ b/messages/en.json @@ -486,7 +486,8 @@ "typeVaccineDue": "💉 The \"{name}\" vaccination is due on {date} – time to book an appointment.", "typeStoryMention": "tagged you in a story.", "typeStoryComment": "commented on your story.", - "typeStoryReaction": "gave your story a paw." + "typeStoryReaction": "gave your story a paw.", + "typeUnknown": "has a new notification for you." }, "PhotoPostForm": { "captionPlaceholder": "Write a caption…", diff --git a/mobile/app/(app)/(tabs)/notifications.tsx b/mobile/app/(app)/(tabs)/notifications.tsx index da64e90..5b4fb8e 100644 --- a/mobile/app/(app)/(tabs)/notifications.tsx +++ b/mobile/app/(app)/(tabs)/notifications.tsx @@ -50,7 +50,8 @@ function getNotificationSuffix(item: NotificationItem, t: TFunction): string { if (selfKey) return t(selfKey); const actorKey = ACTOR_MESSAGE_KEYS[item.type]; if (actorKey && item.actorPet) return t(actorKey); - return item.type; + // Nie den Code-Namen zeigen (z. B. neuer Typ vom Server, den diese App-Version noch nicht kennt). + return t("notifications.unknown"); } const SELF_ICONS: Partial> = { @@ -79,6 +80,7 @@ function getNotificationRoute(item: NotificationItem): string | null { petId: item.story.petId, petName: item.story.pet.name, petAvatarKey: item.story.pet.avatarKey ?? "", + storyId: item.story.id, }); return `/story-viewer?${params.toString()}`; } @@ -196,7 +198,19 @@ export default function NotificationsScreen() { {item.actorPet && ( - router.push(`/pets/${item.actorPet!.id}`)}> + { + // Bei Story-Mitteilungen führt auch der Name zur Story (nicht zum Profil-Umweg). + const storyRoute = item.story ? getNotificationRoute(item) : null; + if (storyRoute) { + if (!item.read) markRead.mutate({ notificationId: item.id }); + router.push(storyRoute); + } else { + router.push(`/pets/${item.actorPet!.id}`); + } + }} + > {formatPetName(item.actorPet)}{" "} )} diff --git a/mobile/app/(app)/story-viewer.tsx b/mobile/app/(app)/story-viewer.tsx index 60ccc38..69afb31 100644 --- a/mobile/app/(app)/story-viewer.tsx +++ b/mobile/app/(app)/story-viewer.tsx @@ -34,10 +34,12 @@ type Sheet = "comments" | "viewers" | "confirm-delete" | "away" | null; * die Lösch-Rückfrage offen ist, hält der Timer an. */ export default function StoryViewerScreen() { - const { petId, petName, petAvatarKey } = useLocalSearchParams<{ + const { petId, petName, petAvatarKey, storyId: initialStoryId } = useLocalSearchParams<{ petId: string; petName: string; petAvatarKey?: string; + /** Aus einer Mitteilung: direkt diese Story zeigen statt der ersten. */ + storyId?: string; }>(); const { t } = useTranslation(); const router = useRouter(); @@ -50,6 +52,7 @@ export default function StoryViewerScreen() { const listQuery = trpc.stories.listActive.queryOptions({ petId, viewerPetId: activePetId ?? undefined }); const { data: stories = [], isSuccess: storiesLoaded } = useQuery(listQuery); const [index, setIndex] = useState(0); + const jumpedToInitialRef = useRef(false); const [progress, setProgress] = useState(0); const [openSheet, setOpenSheet] = useState(null); const advancedRef = useRef(false); @@ -190,6 +193,15 @@ export default function StoryViewerScreen() { return () => cancelAnimationFrame(rafId); }, [currentStory, isVideo, isPaused, advance]); + // Aus einer Mitteilung geöffnet → einmalig zur genannten Story springen. + useEffect(() => { + if (jumpedToInitialRef.current || !initialStoryId || stories.length === 0) return; + jumpedToInitialRef.current = true; + const target = stories.findIndex((s) => s.id === initialStoryId); + // eslint-disable-next-line react-hooks/set-state-in-effect -- einmaliger Sprung, sobald die Liste da ist + if (target > 0) setIndex(target); + }, [initialStoryId, stories]); + // Keine (laufende) Story mehr, z. B. über eine alte Mitteilung geöffnet → zurück statt schwarzem Bildschirm. useEffect(() => { if (storiesLoaded && stories.length === 0) router.back(); diff --git a/mobile/src/i18n/locales/de.json b/mobile/src/i18n/locales/de.json index 7ee46e2..7d23f32 100644 --- a/mobile/src/i18n/locales/de.json +++ b/mobile/src/i18n/locales/de.json @@ -213,7 +213,8 @@ "referralRankUp": "Du hast einen neuen Empfehlungsrang erreicht!", "storyMention": "hat dich in einer Story markiert.", "storyComment": "hat deine Story kommentiert.", - "storyReaction": "hat deiner Story eine Pfote gegeben." + "storyReaction": "hat deiner Story eine Pfote gegeben.", + "unknown": "Neue Mitteilung." }, "pets": { "empty": "Noch keine Tiere angelegt.", diff --git a/mobile/src/i18n/locales/en.json b/mobile/src/i18n/locales/en.json index b2ed61e..55155e1 100644 --- a/mobile/src/i18n/locales/en.json +++ b/mobile/src/i18n/locales/en.json @@ -213,7 +213,8 @@ "referralRankUp": "You reached a new referral rank!", "storyMention": "tagged you in a story.", "storyComment": "commented on your story.", - "storyReaction": "gave your story a paw." + "storyReaction": "gave your story a paw.", + "unknown": "New notification." }, "pets": { "empty": "No pets added yet.", diff --git a/src/__tests__/story-editor.test.ts b/src/__tests__/story-editor.test.ts index e056ecd..7332650 100644 --- a/src/__tests__/story-editor.test.ts +++ b/src/__tests__/story-editor.test.ts @@ -45,7 +45,7 @@ import { storyMediaSize, storyOverlaysSchema, } from "@/lib/story-overlays"; -import { filterMentionablePetIds } from "@/lib/story-mentions"; +import { filterMentionablePetIds, petsOfOtherOwners } from "@/lib/story-mentions"; import { isStoryEditorEnabled, setStoryEditorEnabled } from "@/lib/feature-flags"; function createCaller() { @@ -152,6 +152,19 @@ describe("filterMentionablePetIds — „Wer darf die Follower sehen?“ regelt }); }); +describe("petsOfOtherOwners", () => { + it("skips the creator's own pets when notifying", async () => { + const p = createMockPrisma(); + p.pet.findMany.mockResolvedValue([ + { id: "me", ownerId: "owner-1" }, + { id: "my-other-pet", ownerId: "owner-1" }, + { id: "friend", ownerId: "owner-2" }, + ]); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + expect(await petsOfOtherOwners(p as any, "me", ["my-other-pet", "friend"])).toEqual(["friend"]); + }); +}); + describe("stories.create with overlays", () => { it("stores overlays + media size, drops disallowed mentions and notifies only allowed pets", async () => { const { caller, mockPrisma } = createCaller(); diff --git a/src/app/(app)/notifications/page.tsx b/src/app/(app)/notifications/page.tsx index b8d9a08..ba01c1d 100644 --- a/src/app/(app)/notifications/page.tsx +++ b/src/app/(app)/notifications/page.tsx @@ -16,9 +16,12 @@ import type { PostCardPost } from "@/components/feed/PostCard"; import { useLocale, useTranslations } from "next-intl"; import { formatRelativeCompact } from "@/lib/format-relative-time"; import { RankRing } from "@/components/referrals/RankRing"; +import { StoryViewer } from "@/components/stories/StoryViewer"; type NotificationType = "FOLLOW" | "REACTION" | "COMMENT" | "MESSAGE" | "BIRTHDAY" | "ADOPTION_DAY" | "MENTION" | "PAW_BACK" | "PET_SUGGESTION" | "REFERRAL_JOINED" | "REFERRAL_RANK_UP" | "VACCINE_DUE" | "STORY_MENTION" | "STORY_COMMENT" | "STORY_REACTION"; +const KNOWN_TYPES = new Set(["FOLLOW", "REACTION", "COMMENT", "MESSAGE", "BIRTHDAY", "ADOPTION_DAY", "MENTION", "PAW_BACK", "PET_SUGGESTION", "REFERRAL_JOINED", "REFERRAL_RANK_UP", "VACCINE_DUE", "STORY_MENTION", "STORY_COMMENT", "STORY_REACTION"]); + function notificationIcon(type: NotificationType) { switch (type) { case "FOLLOW": return ; @@ -49,7 +52,7 @@ export default function NotificationsPage() { const locale = useLocale(); const formatRelativeTime = (date: Date | string) => formatRelativeCompact(date, trt); - const { data, isLoading } = useQuery({ + const { data, isLoading, dataUpdatedAt } = useQuery({ ...trpc.notifications.list.queryOptions({ petId: activePetId ?? "", limit: 30 }), enabled: !!activePetId, }); @@ -59,6 +62,9 @@ export default function NotificationsPage() { // swapped out the notifications page in the background, so closing the // dialog stranded the user on that profile instead of back on this list. const [openPostId, setOpenPostId] = useState(null); + // Story-Mitteilungen öffnen die Story direkt hier als Overlay (an der richtigen Story), + // statt über das Tierprofil — Tester-Feedback 04.10. + const [openStory, setOpenStory] = useState<{ petId: string; petName: string; petAvatarKey: string | null; storyId: string } | null>(null); const [selectedPost, setSelectedPost] = useState(null); const { data: fetchedPost } = useQuery({ @@ -103,11 +109,12 @@ export default function NotificationsPage() { return notif.actorPet ? `/pets/${notif.actorPet.id}` : null; case "REFERRAL_RANK_UP": return "/einladen"; - // Story-Mitteilungen → Tierprofil (dort läuft die Story über den Ring) + // Story-Mitteilungen: läuft die Story noch (oder ist es die eigene, 7 Tage sichtbar), + // öffnet getStoryTarget() sie direkt; sonst bleibt nur das Tierprofil. case "STORY_MENTION": case "STORY_COMMENT": case "STORY_REACTION": - return notif.story ? `/pets/${notif.story.petId}` : null; + return notif.story && !getStoryTarget(notif) ? `/pets/${notif.story.petId}` : null; case "BIRTHDAY": case "ADOPTION_DAY": case "VACCINE_DUE": @@ -117,6 +124,21 @@ export default function NotificationsPage() { } } + function getStoryTarget(notif: (typeof notifications)[number]) { + if (!notif.story) return null; + if (notif.type !== "STORY_MENTION" && notif.type !== "STORY_COMMENT" && notif.type !== "STORY_REACTION") return null; + const isOwnStory = notif.story.petId === notif.recipientPetId; + // Bezugszeit = Ladezeitpunkt der Liste (kein Date.now() im Rendern) + const isRunning = new Date(notif.story.expiresAt).getTime() > dataUpdatedAt; + if (!isRunning && !isOwnStory) return null; + return { + petId: notif.story.petId, + petName: notif.story.pet.name, + petAvatarKey: notif.story.pet.avatarKey, + storyId: notif.story.id, + }; + } + // Post-related notifications — opened in-place via PostDetailDialog instead. function getPostId(notif: (typeof notifications)[number]): string | null { switch (notif.type) { @@ -171,17 +193,20 @@ export default function NotificationsPage() { : null; const href = getHref(notif); const postId = getPostId(notif); - const isClickable = !!href || !!postId; + const storyTarget = getStoryTarget(notif); + const isClickable = !!href || !!postId || !!storyTarget; return (
  • router.push(href) - : postId - ? () => setOpenPostId(postId) - : undefined + storyTarget + ? () => setOpenStory(storyTarget) + : href + ? () => router.push(href) + : postId + ? () => setOpenPostId(postId) + : undefined } className={`flex items-start gap-3 p-3 rounded-lg transition-colors ${ !notif.read ? "bg-orange-500/5" : "hover:bg-muted/50" @@ -248,6 +273,7 @@ export default function NotificationsPage() { {notif.type === "PET_SUGGESTION" && t("typePetSuggestion")} {notif.type === "REFERRAL_JOINED" && t("typeReferralJoined")} {notif.type === "REFERRAL_RANK_UP" && t("typeReferralRankUp")} + {!KNOWN_TYPES.has(notif.type) && t("typeUnknown")} {notif.type === "STORY_MENTION" && t("typeStoryMention")} {notif.type === "STORY_COMMENT" && t("typeStoryComment")} {notif.type === "STORY_REACTION" && t("typeStoryReaction")} @@ -293,6 +319,18 @@ export default function NotificationsPage() { onClose={closePostDialog} onDeleted={closePostDialog} /> + {openStory && ( + { + if (!isOpen) setOpenStory(null); + }} + /> + )} ); } diff --git a/src/app/api/webhooks/mux/route.ts b/src/app/api/webhooks/mux/route.ts index 5896e04..d03f353 100644 --- a/src/app/api/webhooks/mux/route.ts +++ b/src/app/api/webhooks/mux/route.ts @@ -5,7 +5,7 @@ import { redis } from "@/lib/redis"; import { fanOutPost } from "@/lib/feed-helpers"; import { MAX_STORY_VIDEO_DURATION_SECS } from "@/lib/story-video-limits"; import { maybeQualifyReferral } from "@/lib/referrals"; -import { notifyStoryMentions } from "@/lib/story-mentions"; +import { notifyStoryMentions, petsOfOtherOwners } from "@/lib/story-mentions"; /** * POST /api/webhooks/mux @@ -130,7 +130,8 @@ export async function POST(req: NextRequest) { // jetzt ist die Story sichtbar → jetzt benachrichtigen. if (!overStoryLimit) { const mentions = await prisma.storyMention.findMany({ where: { storyId: story.id }, select: { petId: true } }); - notifyStoryMentions(prisma, story.id, story.petId, mentions.map((m) => m.petId)); + const recipients = await petsOfOtherOwners(prisma, story.petId, mentions.map((m) => m.petId)); + notifyStoryMentions(prisma, story.id, story.petId, recipients); } break; } diff --git a/src/lib/story-mentions.ts b/src/lib/story-mentions.ts index 7b8b94c..5b2cdb1 100644 --- a/src/lib/story-mentions.ts +++ b/src/lib/story-mentions.ts @@ -72,7 +72,25 @@ export async function saveStoryMentions( data: petIds.map((petId) => ({ storyId, petId })), skipDuplicates: true, }); - if (sendNotifications) notifyStoryMentions(prisma, storyId, actorPetId, petIds); + if (sendNotifications) notifyStoryMentions(prisma, storyId, actorPetId, await petsOfOtherOwners(prisma, actorPetId, petIds)); +} + +/** Eigene Tiere des Erstellers markieren ist erlaubt, aber eine Mitteilung an sich selbst ist sinnlos. */ +export async function petsOfOtherOwners( + prisma: Pick, + actorPetId: string, + petIds: readonly string[] +): Promise { + if (petIds.length === 0) return []; + const pets = await prisma.pet.findMany({ + where: { id: { in: [actorPetId, ...petIds] } }, + select: { id: true, ownerId: true }, + }); + const actorOwner = pets.find((p) => p.id === actorPetId)?.ownerId; + return petIds.filter((id) => { + const owner = pets.find((p) => p.id === id)?.ownerId; + return owner !== undefined && owner !== actorOwner; + }); } /** Fire-and-forget wie notify() selbst — darf das Erstellen der Story nie scheitern lassen. */