Refactor config to use a better interface

This commit is contained in:
Alex Gleason 2023-06-10 17:15:08 -05:00
parent 8a62cb604d
commit b5643c4aba
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
8 changed files with 46 additions and 28 deletions

View File

@ -1,7 +1,7 @@
import { Author, findReplyTag, matchFilter, RelayPool, TTLCache } from '@/deps.ts';
import { type Event, type SignedEvent } from '@/event.ts';
import { poolRelays, publishRelays } from './config.ts';
import { Conf } from './config.ts';
import { eventDateComparator, nostrNow } from './utils.ts';
@ -24,7 +24,7 @@ function getPool(): Pool {
if (cached !== undefined) return cached;
console.log('Creating new pool.');
const pool = new RelayPool(poolRelays);
const pool = new RelayPool(Conf.poolRelays);
poolCache.set(0, pool);
return pool;
}
@ -52,7 +52,7 @@ function getFilter<K extends number>(filter: Filter<K>, opts: GetFilterOpts = {}
const unsub = getPool().subscribe(
[filter],
poolRelays,
Conf.poolRelays,
(event: SignedEvent | null) => {
if (event && matchFilter(filter, event)) {
results.push({
@ -90,7 +90,7 @@ function getFilter<K extends number>(filter: Filter<K>, opts: GetFilterOpts = {}
/** Get a Nostr event by its ID. */
const getEvent = async <K extends number = number>(id: string, kind?: K): Promise<SignedEvent<K> | undefined> => {
const event = await (getPool().getEventById(id, poolRelays, 0) as Promise<SignedEvent>);
const event = await (getPool().getEventById(id, Conf.poolRelays, 0) as Promise<SignedEvent>);
if (event) {
if (event.id !== id) return undefined;
if (kind && event.kind !== kind) return undefined;
@ -100,7 +100,7 @@ const getEvent = async <K extends number = number>(id: string, kind?: K): Promis
/** Get a Nostr `set_medatadata` event for a user's pubkey. */
const getAuthor = async (pubkey: string): Promise<SignedEvent<0> | undefined> => {
const author = new Author(getPool(), poolRelays, pubkey);
const author = new Author(getPool(), Conf.poolRelays, pubkey);
const event: SignedEvent<0> | null = await new Promise((resolve) => author.metaData(resolve, 0));
return event?.pubkey === pubkey ? event : undefined;
};
@ -170,7 +170,7 @@ function getDescendants(eventId: string): Promise<SignedEvent<1>[]> {
}
/** Publish an event to the Nostr relay. */
function publish(event: SignedEvent, relays = publishRelays): void {
function publish(event: SignedEvent, relays = Conf.publishRelays): void {
console.log('Publishing event', event);
try {
getPool().publish(event, relays);

View File

@ -1,6 +1,23 @@
export const LOCAL_DOMAIN = Deno.env.get('LOCAL_DOMAIN') || 'http://localhost:8000';
export const POST_CHAR_LIMIT = Number(Deno.env.get('POST_CHAR_LIMIT') || 5000);
export const ADMIN_EMAIL = Deno.env.get('ADMIN_EMAIL') || 'webmaster@localhost';
/** Application-wide configuration. */
const Conf = {
get nsec() {
return Deno.env.get('DITTO_NSEC');
},
get localDomain() {
return Deno.env.get('LOCAL_DOMAIN') || 'http://localhost:8000';
},
get postCharLimit() {
return Number(Deno.env.get('POST_CHAR_LIMIT') || 5000);
},
get adminEmail() {
return Deno.env.get('ADMIN_EMAIL') || 'webmaster@localhost';
},
get poolRelays() {
return (Deno.env.get('RELAY_POOL') || '').split(',').filter(Boolean);
},
get publishRelays() {
return ['wss://relay.mostr.pub'];
},
};
export const poolRelays = (Deno.env.get('RELAY_POOL') || '').split(',').filter(Boolean);
export const publishRelays = ['wss://relay.mostr.pub'];
export { Conf };

View File

@ -1,9 +1,9 @@
import { ADMIN_EMAIL, LOCAL_DOMAIN, POST_CHAR_LIMIT } from '@/config.ts';
import { Conf } from '@/config.ts';
import type { Context } from '@/deps.ts';
function instanceController(c: Context) {
const { host, protocol } = new URL(LOCAL_DOMAIN);
const { host, protocol } = new URL(Conf.localDomain);
return c.json({
uri: host,
@ -11,7 +11,7 @@ function instanceController(c: Context) {
description: 'An efficient and flexible social media server.',
short_description: 'An efficient and flexible social media server.',
registrations: false,
max_toot_chars: POST_CHAR_LIMIT,
max_toot_chars: Conf.postCharLimit,
configuration: {
media_attachments: {
image_size_limit: 100000000,
@ -24,7 +24,7 @@ function instanceController(c: Context) {
min_expiration: 0,
},
statuses: {
max_characters: POST_CHAR_LIMIT,
max_characters: Conf.postCharLimit,
max_media_attachments: 20,
},
},
@ -38,7 +38,7 @@ function instanceController(c: Context) {
streaming_api: `${protocol === 'http:' ? 'ws:' : 'wss:'}//${host}`,
},
version: '0.0.0 (compatible; Ditto 0.0.1)',
email: ADMIN_EMAIL,
email: Conf.adminEmail,
rules: [],
});
}

View File

@ -1,4 +1,4 @@
import { LOCAL_DOMAIN } from '@/config.ts';
import { Conf } from '@/config.ts';
import type { AppController } from '@/app.ts';
@ -6,7 +6,7 @@ import type { AppController } from '@/app.ts';
const indexController: AppController = (c) => {
return c.text(`Please connect with a Mastodon client:
${LOCAL_DOMAIN}
${Conf.localDomain}
Ditto <https://gitlab.com/soapbox-pub/ditto>
`);

View File

@ -12,6 +12,7 @@ export { z } from 'https://deno.land/x/zod@v3.20.5/mod.ts';
export { Author, RelayPool } from 'https://dev.jspm.io/nostr-relaypool@0.5.3';
export {
type Filter,
finishEvent,
getEventHash,
getPublicKey,
getSignature,

View File

@ -1,10 +1,10 @@
import { LOCAL_DOMAIN } from '@/config.ts';
import { Conf } from '@/config.ts';
import { linkify, linkifyStr, mime, nip19, nip21 } from '@/deps.ts';
linkify.registerCustomProtocol('nostr', true);
linkify.registerCustomProtocol('wss');
const url = (path: string) => new URL(path, LOCAL_DOMAIN).toString();
const url = (path: string) => new URL(path, Conf.localDomain).toString();
const linkifyOpts: linkify.Opts = {
render: {

View File

@ -2,7 +2,7 @@ import { findReplyTag, lodash, nip19, sanitizeHtml, TTLCache, unfurl, z } from '
import { type Event } from '@/event.ts';
import { emojiTagSchema, filteredArray, type MetaContent, parseMetaContent } from '@/schema.ts';
import { LOCAL_DOMAIN } from './config.ts';
import { Conf } from './config.ts';
import { getAuthor } from './client.ts';
import { verifyNip05Cached } from './nip05.ts';
import { getMediaLinks, type MediaLink, parseNoteContent } from './note.ts';
@ -20,7 +20,7 @@ async function toAccount(event: Event<0>, opts: ToAccountOpts = {}) {
const { pubkey } = event;
const { name, nip05, picture, banner, about }: MetaContent = parseMetaContent(event);
const { origin } = new URL(LOCAL_DOMAIN);
const { origin } = new URL(Conf.localDomain);
const npub = nip19.npubEncode(pubkey);
let parsed05: Nip05 | undefined;
@ -81,7 +81,7 @@ async function toMention(pubkey: string) {
url: account.url,
};
} else {
const { origin } = new URL(LOCAL_DOMAIN);
const { origin } = new URL(Conf.localDomain);
const npub = nip19.npubEncode(pubkey);
return {
id: pubkey,
@ -143,8 +143,8 @@ async function toStatus(event: Event<1>) {
tags: [],
emojis: toEmojis(event),
poll: null,
uri: `${LOCAL_DOMAIN}/posts/${event.id}`,
url: `${LOCAL_DOMAIN}/posts/${event.id}`,
uri: `${Conf.localDomain}/posts/${event.id}`,
url: `${Conf.localDomain}/posts/${event.id}`,
};
}

View File

@ -1,5 +1,5 @@
import { getAuthor } from '@/client.ts';
import { LOCAL_DOMAIN } from '@/config.ts';
import { Conf } from '@/config.ts';
import { nip19, parseFormData, z } from '@/deps.ts';
import { type Event } from '@/event.ts';
import { lookupNip05Cached } from '@/nip05.ts';
@ -87,8 +87,8 @@ function buildLinkHeader(url: string, events: Event[]): string | undefined {
const lastEvent = events[events.length - 1];
const { pathname, search } = new URL(url);
const next = new URL(pathname + search, LOCAL_DOMAIN);
const prev = new URL(pathname + search, LOCAL_DOMAIN);
const next = new URL(pathname + search, Conf.localDomain);
const prev = new URL(pathname + search, Conf.localDomain);
next.searchParams.set('until', String(lastEvent.created_at));
prev.searchParams.set('since', String(firstEvent.created_at));