mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
feat: create fetchDittoDomain function
refactor: return NodeInfo object with default values in case of errors
This commit is contained in:
parent
effb316409
commit
b09bcd33df
1 changed files with 30 additions and 2 deletions
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue