Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374import {useMemo} from 'react'import { AppBskyFeedDefs, type AppBskyFeedPost, AtUri, type BskyAgent, jsonToLex, moderatePost, type ModerationDecision,} from '@atproto/api'import { type InfiniteData, type QueryClient, type QueryKey, useInfiniteQuery, useQuery,} from '@tanstack/react-query'
import {communityXrpc} from '#/lib/api/community'import {FeedTuner} from '#/lib/api/feed-manip'import {useModerationOpts} from '#/state/preferences/moderation-opts'import {STALE} from '#/state/queries'import {usePreferencesQuery} from '#/state/queries/preferences'import { didOrHandleUriMatches, embedViewRecordToPostView, getEmbeddedPost,} from '#/state/queries/util'import {useAgent} from '#/state/session'
const PAGE_SIZE = 30type RQPageParam = string | undefined
export const RQKEY_ROOT = 'community-feed'export const RQKEY = (actor: string) => [RQKEY_ROOT, actor]
const TIMELINE_RQKEY_ROOT = 'community-timeline'export const TIMELINE_RQKEY = () => [TIMELINE_RQKEY_ROOT]
// Server returns feedViewPost format with hydrated posts// Support both old 'posts' format (raw) and new 'feed' format (hydrated)interface CommunityFeedPage { cursor?: string feed?: AppBskyFeedDefs.FeedViewPost[] // Legacy format for backward compatibility during deployment posts?: Array<{ uri: string cid?: string creator: string text: string createdAt: string indexedAt: string }>}
export function useCommunityFeedQuery(actor: string | undefined) { const agent = useAgent() return useInfiniteQuery< CommunityFeedPage, Error, InfiniteData<CommunityFeedPage>, QueryKey, RQPageParam >({ queryKey: RQKEY(actor || ''), async queryFn({pageParam}: {pageParam: RQPageParam}) { const params: Record<string, string> = { actor: actor || '', limit: String(PAGE_SIZE), } if (pageParam) { params.cursor = pageParam } const res = await communityXrpc( agent, 'community.blacksky.feed.getCommunityFeed', {params}, ) if (!res.ok) { throw new Error(`getCommunityFeed failed: ${res.status}`) } return jsonToLex(await res.json()) as CommunityFeedPage }, initialPageParam: undefined, getNextPageParam: lastPage => lastPage.cursor, enabled: !!actor, })}
/** * Query for the global community timeline (all community posts). * Used on the Home screen Community tab. */export function useCommunityTimelineQuery(enabled: boolean) { const agent = useAgent() return useInfiniteQuery< CommunityFeedPage, Error, InfiniteData<CommunityFeedPage>, QueryKey, RQPageParam >({ queryKey: TIMELINE_RQKEY(), async queryFn({pageParam}: {pageParam: RQPageParam}) { const params: Record<string, string> = { limit: String(PAGE_SIZE), } if (pageParam) { params.cursor = pageParam } const res = await communityXrpc( agent, 'community.blacksky.feed.getCommunityTimeline', {params}, ) if (!res.ok) { throw new Error(`getCommunityTimeline failed: ${res.status}`) } return jsonToLex(await res.json()) as CommunityFeedPage }, initialPageParam: undefined, getNextPageParam: lastPage => lastPage.cursor, // Never auto-refetch when `enabled` flips back on: replaying every // loaded page shifts content under the viewport (same reason the // page polls a head probe instead of refetching on a timer). staleTime: STALE.INFINITY, enabled, })}
/** * Fetch the newest surfacing community timeline post, for cheap freshness * probes. Probes a small window rather than a single item because the raw * head is often a non-surfacing reply that would hide newer posts behind it. */export async function fetchCommunityTimelineHead( agent: BskyAgent,): Promise<AppBskyFeedDefs.FeedViewPost | undefined> { const res = await communityXrpc( agent, 'community.blacksky.feed.getCommunityTimeline', {params: {limit: '10'}}, ) if (!res.ok) { throw new Error(`getCommunityTimeline failed: ${res.status}`) } const page = jsonToLex(await res.json()) as CommunityFeedPage return page.feed?.find(surfacesInCommunityFeed)}
// A reply only resurfaces its thread when the root author is// continuing their own thread; other people's replies stay in the// thread view. Stands in for followedRepliesOnly, which would pass// everything here since the whole community is "followed".export function surfacesInCommunityFeed( item: AppBskyFeedDefs.FeedViewPost,): boolean { // Blocked/muted authors never surface, same as the Following feed. const authorViewer = item.post.author.viewer if ( authorViewer?.blocking || authorViewer?.blockedBy || authorViewer?.muted ) { return false } const reply = (item.post.record as AppBskyFeedPost.Record)?.reply if (!reply?.root?.uri) return true // Replies whose parent the appview didn't hydrate are orphans; the // tuner drops them (removeOrphans), so they must not count as new. if (!item.reason && !AppBskyFeedDefs.isPostView(item.reply?.parent)) { return false } return new AtUri(reply.root.uri).host === item.post.author.did}
const COMMUNITY_POST_RQKEY_ROOT = 'community-post'export const COMMUNITY_POST_RQKEY = (uri: string) => [ COMMUNITY_POST_RQKEY_ROOT, uri,]
export function useCommunityPostQuery(uri: string | undefined) { const agent = useAgent() return useQuery<AppBskyFeedDefs.PostView>({ queryKey: COMMUNITY_POST_RQKEY(uri || ''), async queryFn() { const res = await communityXrpc( agent, 'community.blacksky.feed.getCommunityPost', {params: {uri: uri || ''}}, ) if (!res.ok) { throw new Error(`getCommunityPost failed: ${res.status}`) } const data = jsonToLex(await res.json()) as { post: AppBskyFeedDefs.PostView } return data.post }, enabled: !!uri, })}
export interface HydratedCommunityPost { post: AppBskyFeedDefs.PostView record: AppBskyFeedPost.Record moderation: ModerationDecision}
export interface CommunityFeedSliceItem extends HydratedCommunityPost { _reactKey: string uri: string parentAuthor?: AppBskyFeedDefs.PostView['author']}
export interface CommunityFeedSlice { _reactKey: string items: CommunityFeedSliceItem[] isIncompleteThread: boolean}
/** * Takes pre-hydrated feed items from the server and adds moderation decisions. * The server now returns properly hydrated PostViews with author info and counts. */export function useCommunityFeedHydrated( feedItems: AppBskyFeedDefs.FeedViewPost[],): HydratedCommunityPost[] { const moderationOpts = useModerationOpts()
return useMemo(() => { if (!moderationOpts) return []
return feedItems .filter(item => item?.post) // Filter out any undefined items .map(item => { const postView = item.post const record = postView.record as AppBskyFeedPost.Record const moderation = moderatePost(postView, moderationOpts) return {post: postView, record, moderation} }) }, [feedItems, moderationOpts])}
// Group feed items into slices so a self-reply (parent + reply by same author)// renders as one visually-connected card instead of two flat ones.export function useCommunityFeedSlices( feedItems: AppBskyFeedDefs.FeedViewPost[],): CommunityFeedSlice[] { const moderationOpts = useModerationOpts() const {data: preferences} = usePreferencesQuery()
return useMemo(() => { if (!moderationOpts) return [] const surfaced = feedItems.filter(surfacesInCommunityFeed) // Same tuner stack as the Following feed (see useFeedTuners). const tunerFns = [FeedTuner.removeOrphans] if (preferences?.feedViewPrefs.hideReposts) { tunerFns.push(FeedTuner.removeReposts) } if (preferences?.feedViewPrefs.hideReplies) { tunerFns.push(FeedTuner.removeReplies) } if (preferences?.feedViewPrefs.hideQuotePosts) { tunerFns.push(FeedTuner.removeQuotePosts) } tunerFns.push(FeedTuner.dedupThreads) tunerFns.push(FeedTuner.removeMutedThreads) const tuner = new FeedTuner(tunerFns) const raw = tuner.tune(surfaced)
return raw .map((slice, sliceIdx) => { const items: CommunityFeedSliceItem[] = slice.items.map((item, i) => { const record = item.post.record as AppBskyFeedPost.Record return { _reactKey: `${slice._reactKey}-${i}-${item.post.uri}`, uri: item.post.uri, post: item.post, record, moderation: moderatePost(item.post, moderationOpts), parentAuthor: item.parentAuthor, } }) return { _reactKey: slice._reactKey || `slice-${sliceIdx}`, items, isIncompleteThread: slice.isIncompleteThread, } }) .filter(slice => { // Nuclear block: drop the whole thread if any author in it is // blocked or blocking the viewer, matching the standard app. const hasBlock = slice.items.some(item => { const v = item.post.author.viewer return !!(v?.blocking || v?.blockedBy) }) if (hasBlock) return false // Drop items the moderation engine filters from list contexts // (hidden-for-me posts, muted authors, etc.). slice.items = slice.items.filter( item => !item.moderation.ui('contentList').filter, ) return slice.items.length > 0 }) }, [ feedItems, moderationOpts, preferences?.feedViewPrefs.hideQuotePosts, preferences?.feedViewPrefs.hideReplies, preferences?.feedViewPrefs.hideReposts, ])}
/** * Generator function to find all posts in the community feed cache. * Used by the post shadow system to update cached posts after mutations. */export function* findAllPostsInQueryData( queryClient: QueryClient, uri: string,): Generator<AppBskyFeedDefs.PostView, void> { const atUri = new AtUri(uri)
// Search both actor feeds and timeline const queryDatas = [ ...queryClient.getQueriesData<InfiniteData<CommunityFeedPage>>({ queryKey: [RQKEY_ROOT], }), ...queryClient.getQueriesData<InfiniteData<CommunityFeedPage>>({ queryKey: [TIMELINE_RQKEY_ROOT], }), ]
for (const [_queryKey, queryData] of queryDatas) { if (!queryData?.pages) { continue } for (const page of queryData.pages) { if (!page.feed) { continue } for (const item of page.feed) { if (!item?.post) { continue } if (didOrHandleUriMatches(atUri, item.post)) { yield item.post }
// Check for quoted posts in embeds const quotedPost = getEmbeddedPost(item.post.embed) if (quotedPost && didOrHandleUriMatches(atUri, quotedPost)) { yield embedViewRecordToPostView(quotedPost) }
// Reply-context rows render in slices too; without these, likes on // a parent/root row never reach the shadow cache. if (AppBskyFeedDefs.isPostView(item.reply?.parent)) { if (didOrHandleUriMatches(atUri, item.reply.parent)) { yield item.reply.parent } } if (AppBskyFeedDefs.isPostView(item.reply?.root)) { if (didOrHandleUriMatches(atUri, item.reply.root)) { yield item.reply.root } } } } }}