Prefer reference manifest over fallback
This commit is contained in:
@@ -328,20 +328,17 @@ async function getLatestManifestFromAo() {
|
||||
}
|
||||
}
|
||||
|
||||
function timestampMs(value) {
|
||||
if (typeof value !== "string" || !value) return 0;
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isNaN(parsed) ? 0 : parsed;
|
||||
}
|
||||
async function fetchPostSlugs(gateway, blogManifestTxId) {
|
||||
if (!blogManifestTxId) return null;
|
||||
|
||||
function manifestFreshness(payload) {
|
||||
const posts = Array.isArray(payload?.posts) ? payload.posts : [];
|
||||
return Math.max(
|
||||
timestampMs(payload?.updated),
|
||||
timestampMs(payload?.date),
|
||||
timestampMs(payload?.generatedAt),
|
||||
...posts.map((post) => Math.max(timestampMs(post?.updated), timestampMs(post?.publishedAt)))
|
||||
);
|
||||
const response = await fetch(`${gateway.replace(/\/+$/, "")}/${blogManifestTxId}`);
|
||||
if (!response.ok) return null;
|
||||
const payload = await response.json();
|
||||
if (!Array.isArray(payload?.posts)) return null;
|
||||
|
||||
return payload.posts
|
||||
.map((post) => (typeof post?.slug === "string" ? post.slug.trim() : ""))
|
||||
.filter((slug) => slug.length > 0);
|
||||
}
|
||||
|
||||
async function resolvePostSlugs(gateway) {
|
||||
@@ -351,28 +348,16 @@ async function resolvePostSlugs(gateway) {
|
||||
await resolveBlogManifestTxId()
|
||||
].filter(Boolean);
|
||||
|
||||
const manifests = [];
|
||||
for (const blogManifestTxId of candidates) {
|
||||
try {
|
||||
const response = await fetch(`${gateway.replace(/\/+$/, "")}/${blogManifestTxId}`);
|
||||
if (!response.ok) continue;
|
||||
const payload = await response.json();
|
||||
if (!Array.isArray(payload?.posts)) continue;
|
||||
manifests.push({
|
||||
txId: blogManifestTxId,
|
||||
freshness: manifestFreshness(payload),
|
||||
posts: payload.posts
|
||||
});
|
||||
const slugs = await fetchPostSlugs(gateway, blogManifestTxId);
|
||||
if (slugs) return slugs;
|
||||
} catch {
|
||||
// Try the next manifest source.
|
||||
}
|
||||
}
|
||||
|
||||
if (!manifests.length) return [];
|
||||
manifests.sort((a, b) => b.freshness - a.freshness);
|
||||
return manifests[0].posts
|
||||
.map((post) => (typeof post?.slug === "string" ? post.slug.trim() : ""))
|
||||
.filter((slug) => slug.length > 0);
|
||||
return [];
|
||||
}
|
||||
|
||||
async function main() {
|
||||
|
||||
Reference in New Issue
Block a user