diff --git a/src/utils/domains.ts b/src/utils/domains.ts index 21f2977c..b8697091 100644 --- a/src/utils/domains.ts +++ b/src/utils/domains.ts @@ -1,6 +1,8 @@ import { fetchWorker } from '@/workers/fetch.ts'; import { SimpleLRU } from '@/utils/SimpleLRU.ts'; import { Time } from '@/utils/time.ts'; +import { DittoTables } from '@/db/DittoTables.ts'; +import { unfurlCardCached } from '@/utils/unfurl.ts'; type NodeInfoV1 = { usage: { @@ -33,9 +35,35 @@ export const nodeInfoCache = new SimpleLRU( return { usage, }; - } catch (e) { - throw e; + } catch { + return { + usage: { + users: { + total: 0, + activeMonth: 0, + activeHalfyear: 0, + }, + }, + }; } }, { max: 500, ttl: Time.hours(1) }, ); + +/** Fetches a Ditto Domain by making one network request to + * '/.well-known/nodeinfo' and another to root URL (to get OpenGraph data) + * 'domain' is just a domain, without the HTTPS protocol as prefix */ +export async function fetchDittoDomain(domain: string): Promise { + const total_users = (await nodeInfoCache.fetch(domain)).usage.users.total; + + const { title, description, image } = (await unfurlCardCached(`https://${domain}`)) ?? { title: '', description: '' }; + + return { + domain, + title, + description, + categories: new Set(''), + image: image ?? '', + total_users, + }; +}