From b65db37d4747486faca6ab0168d7982ac574a6f6 Mon Sep 17 00:00:00 2001 From: idoshamun <1993245+idoshamun@users.noreply.github.com> Date: Mon, 30 Nov 2026 14:15:50 +0000 Subject: [PATCH] fix(shell): count the read when the iOS app sends it to the browser With "Open links in: In your browser" on, the iOS wrapper hands every outside link to the system browser, including a feed card's Read link (api.daily.dev/r/). Safari does not share the app's session, so the redirect could not tell who was reading and the read never reached the member's history or streak. The in-app path counts it on the post page. When the link going out is a post redirect, send viewPost from the app first. The API dedupes views per post, so a redirect that does know the reader counts once. --- .../src/components/shell/readingStore.ts | 10 +++++ .../shared/src/hooks/useInAppLinks.spec.tsx | 43 +++++++++++++++++++ packages/shared/src/hooks/useInAppLinks.ts | 15 +++++++ 3 files changed, 68 insertions(+) diff --git a/packages/shared/src/components/shell/readingStore.ts b/packages/shared/src/components/shell/readingStore.ts index 3645996f10..cdb71fc370 100644 --- a/packages/shared/src/components/shell/readingStore.ts +++ b/packages/shared/src/components/shell/readingStore.ts @@ -3,6 +3,7 @@ import type { Post } from '../../graphql/posts'; import { WebKitMessageHandlers } from '../../lib/ios'; import { hasNativeHandler, postNativeMessage } from '../../lib/nativeBridge'; import { getAbsoluteWebappUrl } from '../../lib/links'; +import { publicApiUrl } from '../../lib/config'; // The article the wrapper shows behind the post while the post page is the // reading drawer. The wrapper reports the page's state as it changes. @@ -98,6 +99,15 @@ const appHosts = (): string[] => export const isOutsideLink = (anchor: HTMLAnchorElement): boolean => /^https?:$/.test(anchor.protocol) && !appHosts().includes(anchor.host); +// The post behind a redirect link (`api.daily.dev/r/`): the redirect only +// counts the read when it can tell who is reading. +export const getRedirectPostId = (anchor: HTMLAnchorElement): string | null => { + if (!publicApiUrl || anchor.host !== new URL(publicApiUrl).host) { + return null; + } + return anchor.pathname.match(/^\/r\/([^/]+)\/?$/)?.[1] ?? null; +}; + const shareIntents = /^(?:[a-z0-9-]+\.)?(?:twitter\.com|x\.com|facebook\.com|linkedin\.com|reddit\.com|wa\.me|whatsapp\.com|t\.me|telegram\.me)$/; diff --git a/packages/shared/src/hooks/useInAppLinks.spec.tsx b/packages/shared/src/hooks/useInAppLinks.spec.tsx index e974721009..b5a15f414a 100644 --- a/packages/shared/src/hooks/useInAppLinks.spec.tsx +++ b/packages/shared/src/hooks/useInAppLinks.spec.tsx @@ -33,6 +33,18 @@ jest.mock('../contexts/SettingsContext', () => ({ useSettingsContext: () => ({ loadedSettings: mockLoadedSettings }), })); +const mockViewPost = jest.fn(() => Promise.resolve()); + +jest.mock('./post/useViewPost', () => ({ + useViewPost: () => mockViewPost, +})); + +let mockUser: { id: string } | undefined = { id: 'u1' }; + +jest.mock('../contexts/AuthContext', () => ({ + useAuthContext: () => ({ user: mockUser }), +})); + type BridgeGlobal = typeof globalThis & { webkit?: unknown }; const postMessage = jest.fn(); @@ -43,6 +55,7 @@ const Page = () => { <> Outside Inside + Read ); }; @@ -51,6 +64,8 @@ beforeEach(() => { postMessage.mockClear(); mockPush.mockClear(); mockSetOptedOut.mockClear(); + mockViewPost.mockClear(); + mockUser = { id: 'u1' }; mockLoadedSettings = true; (globalThis as BridgeGlobal).webkit = { messageHandlers: { reading: { postMessage } }, @@ -140,4 +155,32 @@ describe('useInAppLinks', () => { openReadingFromFeed({ id: 'p1' } as Post, 'https://api.daily.dev/r/p1'), ).toBe(false); }); + + it('counts the read of a post it hands to the system browser', () => { + mockOptedOut = true; + render(); + + fireEvent.click(screen.getByText('Outside')); + expect(mockViewPost).not.toHaveBeenCalled(); + + fireEvent.click(screen.getByText('Read')); + expect(mockViewPost).toHaveBeenCalledWith('p1'); + expect(postMessage).toHaveBeenCalledWith({ + action: 'external', + url: `${process.env.NEXT_PUBLIC_API_URL}/r/p1`, + }); + }); + + it('counts no read for a logged-out reader', () => { + mockOptedOut = true; + mockUser = undefined; + render(); + + fireEvent.click(screen.getByText('Read')); + expect(mockViewPost).not.toHaveBeenCalled(); + expect(postMessage).toHaveBeenCalledWith({ + action: 'external', + url: `${process.env.NEXT_PUBLIC_API_URL}/r/p1`, + }); + }); }); diff --git a/packages/shared/src/hooks/useInAppLinks.ts b/packages/shared/src/hooks/useInAppLinks.ts index ab763b7fc8..f5872f1ae2 100644 --- a/packages/shared/src/hooks/useInAppLinks.ts +++ b/packages/shared/src/hooks/useInAppLinks.ts @@ -3,7 +3,10 @@ import { useRouter } from 'next/router'; import { useIsPhone } from './useViewSize'; import { useSettingsBooleanFlag } from './useSettingsBooleanFlag'; import { useSettingsContext } from '../contexts/SettingsContext'; +import { useAuthContext } from '../contexts/AuthContext'; +import { useViewPost } from './post/useViewPost'; import { + getRedirectPostId, isOutsideLink, openInSystemBrowser, postReading, @@ -26,8 +29,16 @@ export const useInAppLinks = (): void => { 'openLinksInAppOptOut', ); const { loadedSettings } = useSettingsContext(); + const { user } = useAuthContext(); + const onSendViewPost = useViewPost(); const setOptedOutRef = useRef(setOptedOut); setOptedOutRef.current = setOptedOut; + const viewPostRef = useRef<((postId: string) => void) | null>(null); + viewPostRef.current = user?.id + ? (postId) => { + onSendViewPost(postId).catch(() => undefined); + } + : null; // The wrapper mirrors the choice into the system Settings and reports a // change made there when the app comes back; both sides say what they @@ -60,6 +71,10 @@ export const useInAppLinks = (): void => { return; } event.preventDefault(); + const postId = getRedirectPostId(anchor); + if (postId) { + viewPostRef.current?.(postId); + } openInSystemBrowser(anchor.href); }; document.addEventListener('click', onClick, true);