From a7bb7c031ac28b96163979302edef1480629a0e2 Mon Sep 17 00:00:00 2001 From: Rudy Fraser Date: Sat, 31 Jan 2026 15:14:57 -0500 Subject: [PATCH] Fetch trending topics from Bluesky public API without auth Replace authenticated agent calls with direct fetch to public.api.bsky.app for getTrendingTopics and getTrends endpoints. Force-enable trending in service config since we source data from Bluesky's public API regardless of our appview config. This ensures banned Bluesky users can still see trending topics and avoids routing through the Blacksky appview which doesn't support these endpoints. --- .../queries/trending/useGetTrendsQuery.ts | 42 ++++++------- .../queries/trending/useTrendingTopics.ts | 60 ++++++++----------- src/state/service-config.tsx | 4 +- 3 files changed, 48 insertions(+), 58 deletions(-) diff --git a/src/state/queries/trending/useGetTrendsQuery.ts b/src/state/queries/trending/useGetTrendsQuery.ts index e434d9de0..3c53bd336 100644 --- a/src/state/queries/trending/useGetTrendsQuery.ts +++ b/src/state/queries/trending/useGetTrendsQuery.ts @@ -1,4 +1,4 @@ -import React from 'react' +import {useMemo} from 'react' import {type AppBskyUnspeccedGetTrends, hasMutedWord} from '@atproto/api' import {useQuery} from '@tanstack/react-query' @@ -9,16 +9,16 @@ import { import {getContentLanguages} from '#/state/preferences/languages' import {STALE} from '#/state/queries' import {usePreferencesQuery} from '#/state/queries/preferences' -import {useAgent} from '#/state/session' export const DEFAULT_LIMIT = 5 export const createGetTrendsQueryKey = () => ['trends'] +const PUBLIC_API = 'https://public.api.bsky.app' + export function useGetTrendsQuery() { - const agent = useAgent() const {data: preferences} = usePreferencesQuery() - const mutedWords = React.useMemo(() => { + const mutedWords = useMemo(() => { return preferences?.moderationPrefs?.mutedWords || [] }, [preferences?.moderationPrefs]) @@ -28,10 +28,9 @@ export function useGetTrendsQuery() { queryKey: createGetTrendsQueryKey(), queryFn: async () => { const contentLangs = getContentLanguages().join(',') - const {data} = await agent.app.bsky.unspecced.getTrends( - { - limit: DEFAULT_LIMIT, - }, + const params = new URLSearchParams({limit: String(DEFAULT_LIMIT)}) + const res = await fetch( + `${PUBLIC_API}/xrpc/app.bsky.unspecced.getTrends?${params}`, { headers: { ...createBskyTopicsHeader(aggregateUserInterests(preferences)), @@ -39,20 +38,21 @@ export function useGetTrendsQuery() { }, }, ) + if (!res.ok) { + throw new Error(`getTrends failed: ${res.status}`) + } + const data = (await res.json()) as AppBskyUnspeccedGetTrends.OutputSchema return data }, - select: React.useCallback( - (data: AppBskyUnspeccedGetTrends.OutputSchema) => { - return { - trends: (data.trends ?? []).filter(t => { - return !hasMutedWord({ - mutedWords, - text: t.topic + ' ' + t.displayName + ' ' + t.category, - }) - }), - } - }, - [mutedWords], - ), + select(data: AppBskyUnspeccedGetTrends.OutputSchema) { + return { + trends: (data.trends ?? []).filter(t => { + return !hasMutedWord({ + mutedWords, + text: t.topic + ' ' + t.displayName + ' ' + t.category, + }) + }), + } + }, }) } diff --git a/src/state/queries/trending/useTrendingTopics.ts b/src/state/queries/trending/useTrendingTopics.ts index 517083f1a..bb8aff259 100644 --- a/src/state/queries/trending/useTrendingTopics.ts +++ b/src/state/queries/trending/useTrendingTopics.ts @@ -1,11 +1,9 @@ -import React from 'react' +import {useMemo} from 'react' import {type AppBskyUnspeccedDefs, hasMutedWord} from '@atproto/api' import {useQuery} from '@tanstack/react-query' import {STALE} from '#/state/queries' import {usePreferencesQuery} from '#/state/queries/preferences' -import {useAgent} from '#/state/session' -import {ALT_PROXY_DID} from '#/env' export type TrendingTopic = AppBskyUnspeccedDefs.TrendingTopic @@ -18,13 +16,11 @@ export const DEFAULT_LIMIT = 14 export const trendingTopicsQueryKey = ['trending-topics'] -// Proxy configuration -const PROXY_TO_BLUESKY = `${ALT_PROXY_DID}#bsky_appview` +const PUBLIC_API = 'https://public.api.bsky.app' export function useTrendingTopics() { - const agent = useAgent() const {data: preferences} = usePreferencesQuery() - const mutedWords = React.useMemo(() => { + const mutedWords = useMemo(() => { return preferences?.moderationPrefs?.mutedWords || [] }, [preferences?.moderationPrefs]) @@ -33,39 +29,33 @@ export function useTrendingTopics() { staleTime: STALE.MINUTES.THREE, queryKey: trendingTopicsQueryKey, async queryFn() { - const {data} = await agent.api.app.bsky.unspecced.getTrendingTopics( - { - limit: DEFAULT_LIMIT, - }, - { - headers: { - 'atproto-proxy': PROXY_TO_BLUESKY, - }, - }, + const res = await fetch( + `${PUBLIC_API}/xrpc/app.bsky.unspecced.getTrendingTopics?limit=${DEFAULT_LIMIT}`, ) + if (!res.ok) { + throw new Error(`getTrendingTopics failed: ${res.status}`) + } + const data = (await res.json()) as Response return { topics: data.topics ?? [], suggested: data.suggested ?? [], } }, - select: React.useCallback( - (data: Response) => { - return { - topics: data.topics.filter(t => { - return !hasMutedWord({ - mutedWords, - text: t.topic + ' ' + t.displayName + ' ' + t.description, - }) - }), - suggested: data.suggested.filter(t => { - return !hasMutedWord({ - mutedWords, - text: t.topic + ' ' + t.displayName + ' ' + t.description, - }) - }), - } - }, - [mutedWords], - ), + select(data: Response) { + return { + topics: data.topics.filter(t => { + return !hasMutedWord({ + mutedWords, + text: t.topic + ' ' + t.displayName + ' ' + t.description, + }) + }), + suggested: data.suggested.filter(t => { + return !hasMutedWord({ + mutedWords, + text: t.topic + ' ' + t.displayName + ' ' + t.description, + }) + }), + } + }, }) } diff --git a/src/state/service-config.tsx b/src/state/service-config.tsx index 29fa6bec9..e0fa61b05 100644 --- a/src/state/service-config.tsx +++ b/src/state/service-config.tsx @@ -52,13 +52,13 @@ export function Provider({children}: {children: React.ReactNode}) { return {enabled: Boolean(cachedEnabled)} } - const enabled = Boolean(config?.topicsEnabled) + const enabled = true // update cache device.set(['trendingBetaEnabled'], enabled) return {enabled} - }, [isInitialLoad, config, langPrefs.contentLanguages]) + }, [isInitialLoad, langPrefs.contentLanguages]) const liveNow = useMemo(() => config?.liveNow ?? [], [config]) -- knotmirror 0.0.1