From 63dddda3ef7c80158c1d16a1fc43a7fd04c58e04 Mon Sep 17 00:00:00 2001 From: Hailey Date: Thu, 27 Feb 2025 12:39:04 -0800 Subject: [PATCH] add --- src/components/Link.tsx | 10 +++++++++- src/components/RichText.tsx | 4 ++++ src/lib/hooks/useOpenLink.ts | 7 ++++++- src/lib/strings/url-helpers.ts | 15 +++++++++++++++ src/view/com/post/Post.tsx | 1 + .../com/util/post-embeds/ExternalLinkEmbed.tsx | 1 + 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/components/Link.tsx b/src/components/Link.tsx index 26cea59686..616084817c 100644 --- a/src/components/Link.tsx +++ b/src/components/Link.tsx @@ -69,6 +69,11 @@ type BaseLinkProps = Pick< * Native-only attribute. If true, will open the share sheet on long press. */ shareOnLongPress?: boolean + + /** + * Whether the link should be opened through the redirect proxy. + */ + shouldProxy?: boolean } export function useLink({ @@ -80,9 +85,11 @@ export function useLink({ onLongPress: outerOnLongPress, shareOnLongPress, overridePresentation, + shouldProxy, }: BaseLinkProps & { displayText: string overridePresentation?: boolean + shouldProxy?: boolean }) { const navigation = useNavigationDeduped() const {href} = useLinkProps({ @@ -118,7 +125,7 @@ export function useLink({ }) } else { if (isExternal) { - openLink(href, overridePresentation) + openLink(href, overridePresentation, shouldProxy) } else { const shouldOpenInNewTab = shouldClickOpenNewTab(e) @@ -161,6 +168,7 @@ export function useLink({ action, navigation, overridePresentation, + shouldProxy, ], ) diff --git a/src/components/RichText.tsx b/src/components/RichText.tsx index 7005d07428..d501f4287b 100644 --- a/src/components/RichText.tsx +++ b/src/components/RichText.tsx @@ -23,6 +23,7 @@ export type RichTextProps = TextStyleProp & onLinkPress?: LinkProps['onPress'] interactiveStyle?: TextStyle emojiMultiplier?: number + shouldProxyLinks?: boolean } export function RichText({ @@ -39,6 +40,7 @@ export function RichText({ emojiMultiplier = 1.85, onLayout, onTextLayout, + shouldProxyLinks, }: RichTextProps) { const richText = React.useMemo( () => @@ -110,6 +112,7 @@ export function RichText({ style={interactiveStyles} // @ts-ignore TODO dataSet={WORD_WRAP} + shouldProxy={shouldProxyLinks} onPress={onLinkPress}> {segment.text} @@ -128,6 +131,7 @@ export function RichText({ // @ts-ignore TODO dataSet={WORD_WRAP} shareOnLongPress + shouldProxy={shouldProxyLinks} onPress={onLinkPress} emoji> {toShortUrl(segment.text)} diff --git a/src/lib/hooks/useOpenLink.ts b/src/lib/hooks/useOpenLink.ts index 0629656ac6..a949dacc68 100644 --- a/src/lib/hooks/useOpenLink.ts +++ b/src/lib/hooks/useOpenLink.ts @@ -5,6 +5,7 @@ import * as WebBrowser from 'expo-web-browser' import {logEvent} from '#/lib/statsig/statsig' import { createBskyAppAbsoluteUrl, + createProxiedUrl, isBskyAppUrl, isBskyRSSUrl, isRelativeUrl, @@ -23,7 +24,7 @@ export function useOpenLink() { const sheetWrapper = useSheetWrapper() const openLink = useCallback( - async (url: string, override?: boolean) => { + async (url: string, override?: boolean, shouldProxy?: boolean) => { if (isBskyRSSUrl(url) && isRelativeUrl(url)) { url = createBskyAppAbsoluteUrl(url) } @@ -33,6 +34,10 @@ export function useOpenLink() { domain: toNiceDomain(url), url, }) + + if (shouldProxy) { + url = createProxiedUrl(url) + } } if (isNative && !url.startsWith('mailto:')) { diff --git a/src/lib/strings/url-helpers.ts b/src/lib/strings/url-helpers.ts index c44fdf7c2e..bb533b12e6 100644 --- a/src/lib/strings/url-helpers.ts +++ b/src/lib/strings/url-helpers.ts @@ -320,6 +320,21 @@ export function createBskyAppAbsoluteUrl(path: string): string { return `${BSKY_APP_HOST.replace(/\/$/, '')}/${sanitizedPath}` } +export function createProxiedUrl(url: string): string { + let u + try { + u = URL.parse(url) + } catch { + return url + } + + if (u?.protocol !== 'http:' && u?.protocol !== 'https:') { + return url + } + + return `https://go.bsky.app/redirect?u=${url}` +} + export function isShortLink(url: string): boolean { return url.startsWith('https://go.bsky.app/') } diff --git a/src/view/com/post/Post.tsx b/src/view/com/post/Post.tsx index e3fca3e8b6..f28bb66186 100644 --- a/src/view/com/post/Post.tsx +++ b/src/view/com/post/Post.tsx @@ -234,6 +234,7 @@ function PostInner({ numberOfLines={limitLines ? MAX_POST_LINES : undefined} style={[a.flex_1, a.text_md]} authorHandle={post.author.handle} + shouldProxyLinks /> ) : undefined} diff --git a/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx b/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx index 64e869c94a..5b05cd963e 100644 --- a/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx +++ b/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx @@ -70,6 +70,7 @@ export const ExternalLinkEmbed = ({ {({hovered}) => (