feat: create fetchDittoDomain function

refactor: return NodeInfo object with default values in case of errors
This commit is contained in:
P. Reis 2024-08-13 17:09:17 -03:00
parent effb316409
commit b09bcd33df

View file

@ -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<Domain, NodeInfoV1>(
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<DittoTables['ditto_domains'] | undefined> {
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,
};
}