Something went wrong. Try again.
leaflet fork for copy edit / bug fixes forked from leaflet.pub/leaflet
Something went wrong. Try again.
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465"use server";
import { supabaseServerClient } from "supabase/serverClient";import { deduplicateByUriOrdered } from "src/utils/deduplicateRecords";import { enrichDocumentToPost } from "src/utils/enrichPost";import type { Cursor, Post } from "./getReaderFeed";
export async function getNewFeed( cursor?: Cursor | null,): Promise<{ posts: Post[]; nextCursor: Cursor | null }> { // plan-checked: this is the global discover feed, so the newest-first walk // of documents_sort_date_idx is the intended access path — the filters // (document is in a publication, not opted out of Discover) exclude only a // small minority of recent documents, so the limit short-circuits after a // few index entries. Revisit if either filter ever becomes selective. let query = supabaseServerClient .from("documents") .select( `*, comments_on_documents(count), document_mentions_in_bsky(count), recommends_on_documents(count), documents_in_publications!inner(publications!inner(*))`, ) .or( "record->preferences->showInDiscover.is.null,record->preferences->>showInDiscover.eq.true", { referencedTable: "documents_in_publications.publications" }, ) // Exclude posts published "quietly" (the document record opts itself out of // Discover). Absent/true is kept; only an explicit false is hidden. .or( "data->preferences->showInDiscover.is.null,data->preferences->>showInDiscover.eq.true", ) .order("sort_date", { ascending: false }) .order("uri", { ascending: false }) .limit(25);
if (cursor) { query = query.or( `sort_date.lt.${cursor.timestamp},and(sort_date.eq.${cursor.timestamp},uri.lt.${cursor.uri})`, ); }
let { data: rawFeed, error } = await query;
const feed = deduplicateByUriOrdered(rawFeed || []);
let posts = ( await Promise.all(feed.map((post) => enrichDocumentToPost(post as any))) ).filter((post): post is Post => post !== null);
const nextCursor = posts.length > 0 ? { timestamp: posts[posts.length - 1].documents.sort_date, uri: posts[posts.length - 1].documents.uri, } : null;
return { posts, nextCursor, };}