ditto/src/db/relays.ts

32 lines
803 B
TypeScript
Raw Normal View History

2023-08-15 23:59:49 +00:00
import { tldts } from '@/deps.ts';
import { db } from '@/db.ts';
/** Inserts relays into the database, skipping duplicates. */
function addRelays(relays: `wss://${string}`[]) {
2023-08-15 00:00:54 +00:00
if (!relays.length) return Promise.resolve();
2023-08-15 23:59:49 +00:00
const values = relays.map((url) => ({
url: new URL(url).toString(),
2023-08-16 00:07:26 +00:00
domain: tldts.getDomain(url)!,
active: true,
2023-08-15 23:59:49 +00:00
}));
return db.insertInto('relays')
.values(values)
.onConflict((oc) => oc.column('url').doNothing())
.execute();
}
2023-08-16 13:28:52 +00:00
/** Get a list of all known active relay URLs. */
async function getActiveRelays(): Promise<string[]> {
2023-08-15 01:41:20 +00:00
const rows = await db
.selectFrom('relays')
.select('relays.url')
2023-08-16 13:28:52 +00:00
.where('relays.active', '=', true)
2023-08-15 01:41:20 +00:00
.execute();
return rows.map((row) => row.url);
}
2023-08-16 13:28:52 +00:00
export { addRelays, getActiveRelays };