From fb65bd1110f768a902d517a9cec6bd092242ecd6 Mon Sep 17 00:00:00 2001 From: Rudy Fraser Date: Mon, 2 Mar 2026 10:12:09 -0500 Subject: [PATCH] Switch appview from api.bsky.app to api.blacksky.community - Default proxy DID: did:web:api.blacksky.community - Public appview: https://api.blacksky.community - Video service: https://video.blacksky.community (60 min / 5GB limits) - Route trending feed requests through Bluesky appview via ALT_PROXY_DID - Update embeds, functions, and test snapshots --- bskyembed/index.html | 2 +- bskyembed/post.html | 2 +- bskyembed/src/screens/landing.tsx | 2 +- bskyembed/src/screens/post.tsx | 2 +- bskyweb/cmd/embedr/main.go | 2 +- bskyweb/example.env | 2 +- docs/build.md | 2 +- functions/profile/[handleOrDID].ts | 2 +- .../profile/[handleOrDID]/post/[rkey].ts | 2 +- src/env/common.ts | 3 +- src/lib/api/feed/utils.ts | 10 +- src/lib/api/gatekeeper.ts | 97 +++++++++++++++++++ src/lib/blacksky-pds.ts | 9 ++ src/lib/constants.ts | 14 +-- src/lib/hooks/useIsBlackskyPds.ts | 33 +++++++ src/state/session/__tests__/session-test.ts | 18 ++-- 16 files changed, 174 insertions(+), 28 deletions(-) create mode 100644 src/lib/api/gatekeeper.ts create mode 100644 src/lib/blacksky-pds.ts create mode 100644 src/lib/hooks/useIsBlackskyPds.ts diff --git a/bskyembed/index.html b/bskyembed/index.html index 888e440ea..2bf1cdb4a 100644 --- a/bskyembed/index.html +++ b/bskyembed/index.html @@ -4,7 +4,7 @@ Bluesky Embed - + diff --git a/bskyembed/post.html b/bskyembed/post.html index 00bf0f35d..72568eaf5 100644 --- a/bskyembed/post.html +++ b/bskyembed/post.html @@ -4,7 +4,7 @@ Blacksky Embed - + diff --git a/bskyembed/src/screens/landing.tsx b/bskyembed/src/screens/landing.tsx index f0f35b139..e32c8a439 100644 --- a/bskyembed/src/screens/landing.tsx +++ b/bskyembed/src/screens/landing.tsx @@ -31,7 +31,7 @@ if (!root) throw new Error('No root element') initSystemColorMode() const agent = new AtpAgent({ - service: 'https://api.bsky.app', + service: 'https://api.blacksky.community', }) render(, root) diff --git a/bskyembed/src/screens/post.tsx b/bskyembed/src/screens/post.tsx index 96ac0a4f3..7c7e99122 100644 --- a/bskyembed/src/screens/post.tsx +++ b/bskyembed/src/screens/post.tsx @@ -14,7 +14,7 @@ const root = document.getElementById('app') if (!root) throw new Error('No root element') const agent = new AtpAgent({ - service: 'https://api.bsky.app', + service: 'https://api.blacksky.community', }) const uri = `at://${window.location.pathname.slice('/embed/'.length)}` diff --git a/bskyweb/cmd/embedr/main.go b/bskyweb/cmd/embedr/main.go index 89e178d81..f18cd25b9 100644 --- a/bskyweb/cmd/embedr/main.go +++ b/bskyweb/cmd/embedr/main.go @@ -36,7 +36,7 @@ func run(args []string) { &cli.StringFlag{ Name: "appview-host", Usage: "method, hostname, and port of PDS instance", - Value: "https://api.bsky.app", + Value: "https://api.blacksky.community", EnvVars: []string{"ATP_APPVIEW_HOST"}, }, &cli.StringFlag{ diff --git a/bskyweb/example.env b/bskyweb/example.env index f8a45d7fc..f8b189f4b 100644 --- a/bskyweb/example.env +++ b/bskyweb/example.env @@ -1,2 +1,2 @@ GOLOG_LOG_LEVEL=info -ATP_APPVIEW_HOST=https://api.bsky.app +ATP_APPVIEW_HOST=https://api.blacksky.community diff --git a/docs/build.md b/docs/build.md index 2bad603a8..6722fc7bc 100644 --- a/docs/build.md +++ b/docs/build.md @@ -135,7 +135,7 @@ yarn && yarn build-web cd bskyweb/ go mod tidy go build -v -tags timetzdata -o bskyweb ./cmd/bskyweb -./bskyweb serve --appview-host=https://api.bsky.app +./bskyweb serve --appview-host=https://api.blacksky.community ``` On build success, access the application at [http://localhost:8100/](http://localhost:8100/). Subsequent changes require re-running the above steps in order to be reflected. diff --git a/functions/profile/[handleOrDID].ts b/functions/profile/[handleOrDID].ts index fd934e2e1..5fc40d09c 100644 --- a/functions/profile/[handleOrDID].ts +++ b/functions/profile/[handleOrDID].ts @@ -136,7 +136,7 @@ class NoscriptHandler { } export async function onRequest(context) { - const agent = new AtpAgent({service: 'https://api.bsky.app/'}) + const agent = new AtpAgent({service: 'https://api.blacksky.community/'}) const {request, env} = context const origin = new URL(request.url).origin diff --git a/functions/profile/[handleOrDID]/post/[rkey].ts b/functions/profile/[handleOrDID]/post/[rkey].ts index f09f5c558..b5aedcc19 100644 --- a/functions/profile/[handleOrDID]/post/[rkey].ts +++ b/functions/profile/[handleOrDID]/post/[rkey].ts @@ -198,7 +198,7 @@ class NoscriptHandler { } export async function onRequest(context) { - const agent = new AtpAgent({service: 'https://api.bsky.app/'}) + const agent = new AtpAgent({service: 'https://api.blacksky.community/'}) const {request, env} = context const origin = new URL(request.url).origin const {handleOrDID, rkey}: {handleOrDID: string; rkey: string} = diff --git a/src/env/common.ts b/src/env/common.ts index 45d6a2b41..b751bb04a 100644 --- a/src/env/common.ts +++ b/src/env/common.ts @@ -76,7 +76,8 @@ export const LOG_DEBUG: string = process.env.EXPO_PUBLIC_LOG_DEBUG || '' * The DID of the Bluesky appview to proxy to */ export const BLUESKY_PROXY_DID: Did = - process.env.EXPO_PUBLIC_BLUESKY_PROXY_DID || 'did:web:api.bsky.app' + process.env.EXPO_PUBLIC_BLUESKY_PROXY_DID || 'did:web:api.blacksky.community' +export const ALT_PROXY_DID: Did = 'did:web:api.bsky.app' /** * The DID of the chat service to proxy to diff --git a/src/lib/api/feed/utils.ts b/src/lib/api/feed/utils.ts index f02b8f90b..0b710c311 100644 --- a/src/lib/api/feed/utils.ts +++ b/src/lib/api/feed/utils.ts @@ -2,7 +2,10 @@ import {AtUri} from '@atproto/api' import {BSKY_FEED_OWNER_DIDS} from '#/lib/constants' import {type UsePreferencesQueryResponse} from '#/state/queries/preferences' -import {IS_WEB} from '#/env' +import {ALT_PROXY_DID, IS_WEB} from '#/env' + +const TRENDING_FEED_DID = 'did:plc:qrz3lhbyuxbeilrc6nekdqme' +const PROXY_TO_BLUESKY = `${ALT_PROXY_DID}#bsky_appview` let debugTopics = '' if (IS_WEB && typeof window !== 'undefined') { @@ -27,6 +30,9 @@ export function isBlueskyOwnedFeed(feedUri: string) { return BSKY_FEED_OWNER_DIDS.includes(uri.host) } -export function getProxyHeadersForFeed(_feedUri: string) { +export function getProxyHeadersForFeed(feedUri: string) { + if (feedUri.includes(TRENDING_FEED_DID)) { + return {'atproto-proxy': PROXY_TO_BLUESKY} + } return {} } diff --git a/src/lib/api/gatekeeper.ts b/src/lib/api/gatekeeper.ts new file mode 100644 index 000000000..7eee2ed54 --- /dev/null +++ b/src/lib/api/gatekeeper.ts @@ -0,0 +1,97 @@ +import {getGatekeeperUrl} from '#/lib/blacksky-pds' + +class GatekeeperError extends Error { + constructor( + public status: number, + public errorType: string, + message: string, + ) { + super(message) + this.name = 'GatekeeperError' + } +} + +async function gatekeeperPost( + serviceUrl: string, + path: string, + body: Record, +): Promise { + const base = getGatekeeperUrl(serviceUrl) + const res = await fetch(`${base}${path}`, { + method: 'POST', + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify(body), + }) + + if (!res.ok) { + let errorType = 'UnknownError' + let message = `Request failed with status ${res.status}` + try { + const json = (await res.json()) as {error?: string; message?: string} + errorType = json.error || errorType + message = json.message || message + } catch {} + throw new GatekeeperError(res.status, errorType, message) + } + + const text = await res.text() + if (!text) return {} + try { + return JSON.parse(text) + } catch { + return {} + } +} + +export async function gateUpdateEmail(params: { + serviceUrl: string + did: string + password: string + email: string + token?: string + emailAuthFactor?: boolean +}): Promise<{status: 'success' | 'tokenRequired'}> { + const body: Record = { + did: params.did, + password: params.password, + email: params.email, + } + if (params.token) { + body.token = params.token + } + if (params.emailAuthFactor !== undefined) { + body.emailAuthFactor = params.emailAuthFactor + } + + try { + await gatekeeperPost(params.serviceUrl, '/gate/update-email', body) + return {status: 'success'} + } catch (e) { + if (e instanceof GatekeeperError && e.errorType === 'TokenRequired') { + return {status: 'tokenRequired'} + } + throw e + } +} + +export async function gateRequestAccountDelete(params: { + serviceUrl: string + did: string + password: string +}): Promise { + await gatekeeperPost(params.serviceUrl, '/gate/request-account-delete', { + did: params.did, + password: params.password, + }) +} + +export async function gateDeactivateAccount(params: { + serviceUrl: string + did: string + password: string +}): Promise { + await gatekeeperPost(params.serviceUrl, '/gate/deactivate-account', { + did: params.did, + password: params.password, + }) +} diff --git a/src/lib/blacksky-pds.ts b/src/lib/blacksky-pds.ts new file mode 100644 index 000000000..5df383b37 --- /dev/null +++ b/src/lib/blacksky-pds.ts @@ -0,0 +1,9 @@ +/** + * Returns the gatekeeper base URL for a given PDS service URL. + * The gatekeeper runs behind the same Caddy reverse proxy, so + * the base URL is the same as the PDS service URL. + */ +export function getGatekeeperUrl(serviceUrl: string): string { + // Strip trailing slash + return serviceUrl.replace(/\/+$/, '') +} diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 19c39e3d3..83f13e1d5 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -9,7 +9,7 @@ export const LOCAL_DEV_SERVICE = export const STAGING_SERVICE = 'https://staging.bsky.dev' export const BSKY_SERVICE = 'https://blacksky.app' export const BSKY_SERVICE_DID = 'did:web:bsky.social' -export const PUBLIC_BSKY_SERVICE = 'https://public.api.bsky.app' +export const PUBLIC_BSKY_SERVICE = 'https://api.blacksky.community' export const DEFAULT_SERVICE = BSKY_SERVICE export const HELP_DESK_URL = `https://github.com/blacksky-algorithms/blacksky.community/issues/new/choose` export const EMBED_SERVICE = 'https://embed.bsky.app' @@ -167,15 +167,15 @@ export const GIF_FEATURED = (params: string) => export const MAX_LABELERS = 20 -export const VIDEO_SERVICE = 'https://video.bsky.app' -export const VIDEO_SERVICE_DID = 'did:web:video.bsky.app' +export const VIDEO_SERVICE = 'https://video.blacksky.community' +export const VIDEO_SERVICE_DID = 'did:web:video.blacksky.community' -export const VIDEO_MAX_DURATION_MS = 3 * 60 * 1000 // 3 minutes in milliseconds +export const VIDEO_MAX_DURATION_MS = 60 * 60 * 1000 // 60 minutes in milliseconds /** * Maximum size of a video in megabytes, _not_ mebibytes. Backend uses * ISO megabytes. */ -export const VIDEO_MAX_SIZE = 1000 * 1000 * 100 // 100mb +export const VIDEO_MAX_SIZE = 1000 * 1000 * 1000 * 5 // 5gb export const SUPPORTED_MIME_TYPES = [ 'video/mp4', @@ -205,8 +205,8 @@ export const urls = { }, } -export const PUBLIC_APPVIEW = 'https://api.bsky.app' -export const PUBLIC_APPVIEW_DID = 'did:web:api.bsky.app' +export const PUBLIC_APPVIEW = 'https://api.blacksky.community' +export const PUBLIC_APPVIEW_DID = 'did:web:api.blacksky.community' export const PUBLIC_STAGING_APPVIEW_DID = 'did:web:api.staging.bsky.dev' export const DEV_ENV_APPVIEW = `http://localhost:2584` // always the same diff --git a/src/lib/hooks/useIsBlackskyPds.ts b/src/lib/hooks/useIsBlackskyPds.ts new file mode 100644 index 000000000..75568183e --- /dev/null +++ b/src/lib/hooks/useIsBlackskyPds.ts @@ -0,0 +1,33 @@ +import {useQuery} from '@tanstack/react-query' + +import {useSession} from '#/state/session' + +const RQKEY_ROOT = 'gatekeeper-check' +const RQKEY = (serviceUrl: string) => [RQKEY_ROOT, serviceUrl] + +/** + * Returns true if the current user's PDS runs the pds-gatekeeper sidecar. + * + * Detection works by checking `/xrpc/_health` on the PDS -- the gatekeeper + * intercepts this endpoint and adds a `gatekeeper_version` field to the + * response. Any PDS running the gatekeeper is automatically detected. + */ +export function useIsBlackskyPds(): boolean { + const {currentAccount} = useSession() + const serviceUrl = currentAccount?.service ?? '' + + const {data} = useQuery({ + queryKey: RQKEY(serviceUrl), + queryFn: async () => { + const base = serviceUrl.replace(/\/+$/, '') + const res = await fetch(`${base}/xrpc/_health`) + if (!res.ok) return false + const json = (await res.json()) as {gatekeeper_version?: string} + return typeof json.gatekeeper_version === 'string' + }, + staleTime: Infinity, + enabled: !!serviceUrl, + }) + + return data === true +} diff --git a/src/state/session/__tests__/session-test.ts b/src/state/session/__tests__/session-test.ts index 3afe13af9..60d897e15 100644 --- a/src/state/session/__tests__/session-test.ts +++ b/src/state/session/__tests__/session-test.ts @@ -26,7 +26,7 @@ describe('session', () => { "accounts": [], "currentAgentState": { "agent": { - "service": "https://api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -115,7 +115,7 @@ describe('session', () => { ], "currentAgentState": { "agent": { - "service": "https://api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -453,7 +453,7 @@ describe('session', () => { ], "currentAgentState": { "agent": { - "service": "https://api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -515,7 +515,7 @@ describe('session', () => { ], "currentAgentState": { "agent": { - "service": "https://api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -608,7 +608,7 @@ describe('session', () => { "accounts": [], "currentAgentState": { "agent": { - "service": "https://api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -788,7 +788,7 @@ describe('session', () => { ], "currentAgentState": { "agent": { - "service": "https://api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -1437,7 +1437,7 @@ describe('session', () => { ], "currentAgentState": { "agent": { - "service": "https://api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -1503,7 +1503,7 @@ describe('session', () => { ], "currentAgentState": { "agent": { - "service": "https://api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -1666,7 +1666,7 @@ describe('session', () => { ], "currentAgentState": { "agent": { - "service": "https://api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, -- knotmirror 0.0.1