Skip to content
Merged

Dev #43

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
286 changes: 220 additions & 66 deletions apps/api/src/lib/geekdaily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,92 @@ import { buildPaginatedMeta, resolvePagination, type PaginationInput } from './p
import { combineFilters, toContainsPattern } from './query-filters.js';
import { toIsoString } from './utils.js';

const loadItemsByEpisodeId = async () => {
const mapEpisodeItem = (row: typeof geekdailyEpisodeItems.$inferSelect) => ({
title: row.title,
authorName: row.authorName,
sourceUrl: row.sourceUrl,
summary: row.summary,
});

const mapSearchEpisodeItem = (row: Pick<typeof geekdailyEpisodeItems.$inferSelect, 'title' | 'authorName' | 'summary'>) => ({
title: row.title,
authorName: row.authorName,
summary: row.summary,
});

const PUBLIC_GEEKDAILY_CACHE_TTL_MS = 5 * 60 * 1000;

const publicGeekDailyCache = new Map<string, { expiresAt: number; value: Promise<unknown> }>();

const withPublicGeekDailyCache = async <T>(key: string, loader: () => Promise<T>) => {
const now = Date.now();
const cached = publicGeekDailyCache.get(key);
if (cached && cached.expiresAt > now) {
return cached.value as Promise<T>;
}

const promise = loader().catch((error) => {
const current = publicGeekDailyCache.get(key);
if (current?.value === promise) {
publicGeekDailyCache.delete(key);
}
throw error;
});

publicGeekDailyCache.set(key, {
expiresAt: now + PUBLIC_GEEKDAILY_CACHE_TTL_MS,
value: promise,
});

return promise;
};

export const invalidatePublicGeekDailyCache = () => {
publicGeekDailyCache.clear();
};

const loadItemsByEpisodeIds = async (episodeIds?: string[]) => {
const db = getDb();
const rows = await db.select().from(geekdailyEpisodeItems).orderBy(asc(geekdailyEpisodeItems.sortOrder));
if (episodeIds && episodeIds.length === 0) {
return new Map<string, ReturnType<typeof mapEpisodeItem>[]>();
}

const query = db.select().from(geekdailyEpisodeItems);
const rows = episodeIds
? await query.where(inArray(geekdailyEpisodeItems.episodeId, episodeIds)).orderBy(asc(geekdailyEpisodeItems.sortOrder))
: await query.orderBy(asc(geekdailyEpisodeItems.sortOrder));
const result = new Map<string, any[]>();

for (const row of rows) {
const current = result.get(row.episodeId) ?? [];
current.push({
title: row.title,
authorName: row.authorName,
sourceUrl: row.sourceUrl,
summary: row.summary,
});
current.push(mapEpisodeItem(row));
result.set(row.episodeId, current);
}

return result;
};

const loadSearchItemsByEpisodeIds = async (episodeIds: string[]) => {
const db = getDb();
if (episodeIds.length === 0) {
return new Map<string, ReturnType<typeof mapSearchEpisodeItem>[]>();
}

const rows = await db
.select({
episodeId: geekdailyEpisodeItems.episodeId,
title: geekdailyEpisodeItems.title,
authorName: geekdailyEpisodeItems.authorName,
summary: geekdailyEpisodeItems.summary,
})
.from(geekdailyEpisodeItems)
.where(inArray(geekdailyEpisodeItems.episodeId, episodeIds))
.orderBy(asc(geekdailyEpisodeItems.sortOrder));
const result = new Map<string, ReturnType<typeof mapSearchEpisodeItem>[]>();

for (const row of rows) {
const current = result.get(row.episodeId) ?? [];
current.push(mapSearchEpisodeItem(row));
result.set(row.episodeId, current);
}

Expand Down Expand Up @@ -67,6 +140,9 @@ const getEpisodeRecordBySlug = async (slug: string) => {
const getEditors = (value: unknown) =>
Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string' && item.trim().length > 0) : [];

const getTags = (value: unknown) =>
Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string' && item.trim().length > 0) : [];

const resolveEpisodeEditors = (actor: AuditActor, fallback: unknown = []) => {
const actorName = actor.actorDisplayName?.trim();
if (actorName) {
Expand Down Expand Up @@ -97,6 +173,22 @@ const mapEpisodeDetail = (row: any, items: any[]) => ({
updatedAt: toIsoString(row.updatedAt) ?? new Date().toISOString(),
});

const mapPublicEpisodePreview = (row: any, items: any[]) => ({
slug: getGeekDailyEpisodeSlug(row.episodeNumber),
episodeNumber: row.episodeNumber,
title: row.title,
summary: row.summary,
publishedAt: toIsoString(row.publishedAt) ?? new Date().toISOString(),
editors: getEditors(row.editorsJson),
tags: getTags(row.tagsJson),
items,
});

const mapPublicEpisode = (row: any, items: any[]) => ({
...mapPublicEpisodePreview(row, items),
body: row.bodyMarkdown,
});

const ensureEpisodeNumberAvailable = async (episodeNumber: number, currentId?: string) => {
const db = getDb();
const rows = await db
Expand Down Expand Up @@ -203,7 +295,7 @@ export const getAdminGeekDailyEpisode = async (id: string) => {
const db = getDb();
const [rows, itemsByEpisode] = await Promise.all([
db.select().from(geekdailyEpisodes).where(eq(geekdailyEpisodes.id, id)).limit(1),
loadItemsByEpisodeId(),
loadItemsByEpisodeIds([id]),
]);
const row = rows[0] ?? null;
return row ? mapEpisodeDetail(row, itemsByEpisode.get(row.id) ?? []) : null;
Expand Down Expand Up @@ -231,6 +323,7 @@ export const createAdminGeekDailyEpisode = async (input: GeekDailyEpisodeInput,
.returning();

await replaceItems(created.id, input.items);
invalidatePublicGeekDailyCache();

await createAuditEntry({
...actor,
Expand Down Expand Up @@ -271,6 +364,7 @@ export const updateAdminGeekDailyEpisode = async (id: string, input: GeekDailyEp
.where(eq(geekdailyEpisodes.id, id));

await replaceItems(id, input.items);
invalidatePublicGeekDailyCache();

await createAuditEntry({
...actor,
Expand Down Expand Up @@ -307,6 +401,7 @@ export const publishAdminGeekDailyEpisode = async (id: string, actor: AuditActor
updatedAt: new Date(),
})
.where(eq(geekdailyEpisodes.id, id));
invalidatePublicGeekDailyCache();

await createAuditEntry({
...actor,
Expand Down Expand Up @@ -334,6 +429,7 @@ export const archiveAdminGeekDailyEpisode = async (id: string, actor: AuditActor
updatedAt: new Date(),
})
.where(eq(geekdailyEpisodes.id, id));
invalidatePublicGeekDailyCache();

await createAuditEntry({
...actor,
Expand All @@ -347,68 +443,126 @@ export const archiveAdminGeekDailyEpisode = async (id: string, actor: AuditActor
};

export const listPublicGeekDailyEpisodes = async (limit = -1) => {
const db = getDb();
const query = db
.select()
.from(geekdailyEpisodes)
.where(eq(geekdailyEpisodes.status, 'published'))
.orderBy(desc(geekdailyEpisodes.episodeNumber));
const rows = limit > 0 ? await query.limit(limit) : await query;
const itemsByEpisode = await loadItemsByEpisodeId();

return rows.map((row) => ({
slug: getGeekDailyEpisodeSlug(row.episodeNumber),
episodeNumber: row.episodeNumber,
title: row.title,
summary: row.summary,
publishedAt: toIsoString(row.publishedAt) ?? new Date().toISOString(),
editors: getEditors(row.editorsJson),
tags: Array.isArray(row.tagsJson) ? row.tagsJson : [],
body: row.bodyMarkdown,
items: itemsByEpisode.get(row.id) ?? [],
}));
return withPublicGeekDailyCache(`episodes:${limit}`, async () => {
const db = getDb();
const query = db
.select({
id: geekdailyEpisodes.id,
episodeNumber: geekdailyEpisodes.episodeNumber,
title: geekdailyEpisodes.title,
summary: geekdailyEpisodes.summary,
bodyMarkdown: geekdailyEpisodes.bodyMarkdown,
editorsJson: geekdailyEpisodes.editorsJson,
tagsJson: geekdailyEpisodes.tagsJson,
publishedAt: geekdailyEpisodes.publishedAt,
})
.from(geekdailyEpisodes)
.where(eq(geekdailyEpisodes.status, 'published'))
.orderBy(desc(geekdailyEpisodes.episodeNumber));
const rows = limit > 0 ? await query.limit(limit) : await query;
const itemsByEpisode = await loadItemsByEpisodeIds(rows.map((row) => row.id));

return rows.map((row) => mapPublicEpisode(row, itemsByEpisode.get(row.id) ?? []));
});
};

export const listPublicGeekDailyEpisodePreviews = async (limit = -1) => {
return withPublicGeekDailyCache(`previews:${limit}`, async () => {
const db = getDb();
const query = db
.select({
id: geekdailyEpisodes.id,
episodeNumber: geekdailyEpisodes.episodeNumber,
title: geekdailyEpisodes.title,
summary: geekdailyEpisodes.summary,
editorsJson: geekdailyEpisodes.editorsJson,
tagsJson: geekdailyEpisodes.tagsJson,
publishedAt: geekdailyEpisodes.publishedAt,
})
.from(geekdailyEpisodes)
.where(eq(geekdailyEpisodes.status, 'published'))
.orderBy(desc(geekdailyEpisodes.episodeNumber));
const rows = limit > 0 ? await query.limit(limit) : await query;
const itemsByEpisode = await loadItemsByEpisodeIds(rows.map((row) => row.id));

return rows.map((row) => mapPublicEpisodePreview(row, itemsByEpisode.get(row.id) ?? []));
});
};

export const getPublicGeekDailyEpisodeBySlug = async (slug: string) => {
const [row, itemsByEpisode] = await Promise.all([getEpisodeRecordBySlug(slug), loadItemsByEpisodeId()]);
if (!row || row.status !== 'published') {
return null;
}
return withPublicGeekDailyCache(`episode:${slug}`, async () => {
const row = await getEpisodeRecordBySlug(slug);
if (!row || row.status !== 'published') {
return null;
}
const itemsByEpisode = await loadItemsByEpisodeIds([row.id]);

return {
slug: getGeekDailyEpisodeSlug(row.episodeNumber),
episodeNumber: row.episodeNumber,
title: row.title,
summary: row.summary,
publishedAt: toIsoString(row.publishedAt) ?? new Date().toISOString(),
editors: getEditors(row.editorsJson),
tags: Array.isArray(row.tagsJson) ? row.tagsJson : [],
body: row.bodyMarkdown,
items: itemsByEpisode.get(row.id) ?? [],
};
return mapPublicEpisode(row, itemsByEpisode.get(row.id) ?? []);
});
};

export const getPublicGeekDailyOverview = async () => {
return withPublicGeekDailyCache('overview', async () => {
const db = getDb();
const [countRow, yearRows, tagRows] = await Promise.all([
db.select({ value: count() }).from(geekdailyEpisodes).where(eq(geekdailyEpisodes.status, 'published')),
db
.select({
year: sql<number>`extract(year from ${geekdailyEpisodes.publishedAt})::int`,
})
.from(geekdailyEpisodes)
.where(eq(geekdailyEpisodes.status, 'published'))
.groupBy(sql`1`)
.orderBy(desc(sql`1`)),
db
.select({
tagsJson: geekdailyEpisodes.tagsJson,
})
.from(geekdailyEpisodes)
.where(eq(geekdailyEpisodes.status, 'published')),
]);

const years = yearRows.map((row) => row.year).filter(Number.isFinite);
const featuredTags = [...new Set(tagRows.flatMap((row) => getTags(row.tagsJson)))].slice(0, 8);

return {
totalEpisodes: countRow[0]?.value ?? 0,
years,
featuredTags,
};
});
};

export const getGeekDailySearchDocuments = async () => {
const episodes = await listPublicGeekDailyEpisodes();
return episodes.map((episode) => ({
slug: episode.slug,
episodeNumber: episode.episodeNumber,
title: episode.title,
summary: episode.summary,
body: episode.body,
tags: episode.tags,
publishedAt: episode.publishedAt,
year: String(new Date(episode.publishedAt).getUTCFullYear()),
itemTitles: episode.items.map((item: any) => item.title),
searchableText: [
episode.title,
episode.summary,
episode.body,
episode.editors.join(' '),
episode.tags.join(' '),
...episode.items.flatMap((item: any) => [item.title, item.authorName, item.summary]),
]
.join(' ')
.toLowerCase(),
}));
return withPublicGeekDailyCache('search-documents', async () => {
const db = getDb();
const rows = await db
.select({
id: geekdailyEpisodes.id,
episodeNumber: geekdailyEpisodes.episodeNumber,
title: geekdailyEpisodes.title,
summary: geekdailyEpisodes.summary,
tagsJson: geekdailyEpisodes.tagsJson,
publishedAt: geekdailyEpisodes.publishedAt,
})
.from(geekdailyEpisodes)
.where(eq(geekdailyEpisodes.status, 'published'))
.orderBy(desc(geekdailyEpisodes.episodeNumber));
const itemsByEpisode = await loadSearchItemsByEpisodeIds(rows.map((row) => row.id));

return rows.map((row) => {
const publishedAt = toIsoString(row.publishedAt) ?? new Date().toISOString();

return {
slug: getGeekDailyEpisodeSlug(row.episodeNumber),
episodeNumber: row.episodeNumber,
title: row.title,
summary: row.summary,
tags: getTags(row.tagsJson),
publishedAt,
year: String(new Date(publishedAt).getUTCFullYear()),
items: itemsByEpisode.get(row.id) ?? [],
};
});
});
};
Loading
Loading