format all the things

This commit is contained in:
Siddharth Singh 2025-03-30 14:34:16 +05:30
parent 282748ec80
commit 0589728b16
No known key found for this signature in database
7 changed files with 62 additions and 62 deletions

View file

@ -368,8 +368,8 @@ export class DittoConf {
return this.env.get('DITTO_POLICY') || path.join(this.dataDir, 'policy.ts'); return this.env.get('DITTO_POLICY') || path.join(this.dataDir, 'policy.ts');
} }
get policyMode(): "script" | "event" { get policyMode(): 'script' | 'event' {
return this.env.get("DITTO_CUSTOM_POLICY") ? "script" : "event"; return this.env.get('DITTO_CUSTOM_POLICY') ? 'script' : 'event';
} }
/** Absolute path to the data directory used by Ditto. */ /** Absolute path to the data directory used by Ditto. */

View file

@ -151,7 +151,7 @@ import dittoNamesRoute from '@/routes/dittoNamesRoute.ts';
import pleromaAdminPermissionGroupsRoute from '@/routes/pleromaAdminPermissionGroupsRoute.ts'; import pleromaAdminPermissionGroupsRoute from '@/routes/pleromaAdminPermissionGroupsRoute.ts';
import pleromaStatusesRoute from '@/routes/pleromaStatusesRoute.ts'; import pleromaStatusesRoute from '@/routes/pleromaStatusesRoute.ts';
import { DittoRelayStore } from '@/storages/DittoRelayStore.ts'; import { DittoRelayStore } from '@/storages/DittoRelayStore.ts';
import { adminListPoliciesController, adminCurrentPolicyController } from '@/controllers/api/policies.ts'; import { adminCurrentPolicyController, adminListPoliciesController } from '@/controllers/api/policies.ts';
import { createPolicyEvent, DEFAULT_POLICY_SPEC } from '@/utils/policies.ts'; import { createPolicyEvent, DEFAULT_POLICY_SPEC } from '@/utils/policies.ts';
export interface AppEnv extends DittoEnv { export interface AppEnv extends DittoEnv {

View file

@ -27,8 +27,8 @@ const cache = (f: () => (number | undefined) | Promise<number | undefined>, inte
} }
console.log('error', value); console.log('error', value);
return Number(value || 0); return Number(value || 0);
} };
} };
const instanceV1Controller: AppController = async (c) => { const instanceV1Controller: AppController = async (c) => {
const { conf, db } = c.var; const { conf, db } = c.var;
@ -44,14 +44,14 @@ const instanceV1Controller: AppController = async (c) => {
.selectFrom('author_stats') .selectFrom('author_stats')
.where('nip05_domain', '=', host) .where('nip05_domain', '=', host)
.select(({ fn }) => fn.count<number>('pubkey').distinct().as('users')) .select(({ fn }) => fn.count<number>('pubkey').distinct().as('users'))
.executeTakeFirst().then(obj => obj?.users || 0); .executeTakeFirst().then((obj) => obj?.users || 0);
}, MINS_10); }, MINS_10);
const domainCount = cache(async () => { const domainCount = cache(async () => {
return await db.kysely return await db.kysely
.selectFrom('author_stats') .selectFrom('author_stats')
.select(({ fn }) => fn.count<number>('nip05_domain').distinct().as('domains')) .select(({ fn }) => fn.count<number>('nip05_domain').distinct().as('domains'))
.executeTakeFirst().then(obj => obj?.domains || 0); .executeTakeFirst().then((obj) => obj?.domains || 0);
}, MINS_10); }, MINS_10);
const statusCount = cache(async () => { const statusCount = cache(async () => {
@ -59,7 +59,7 @@ const instanceV1Controller: AppController = async (c) => {
.selectFrom('nostr_events') .selectFrom('nostr_events')
.where('kind', '=', 1) .where('kind', '=', 1)
.select(({ fn }) => fn.countAll<number>().as('events')) .select(({ fn }) => fn.countAll<number>().as('events'))
.executeTakeFirst().then(obj => obj?.events || 0); .executeTakeFirst().then((obj) => obj?.events || 0);
}, MINS_10); }, MINS_10);
return c.json({ return c.json({

View file

@ -1,15 +1,17 @@
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { DEFAULT_POLICY_SPEC, policyRegistry } from "@/utils/policies.ts"; import { DEFAULT_POLICY_SPEC, policyRegistry } from '@/utils/policies.ts';
export const adminListPoliciesController: AppController = (c) => { export const adminListPoliciesController: AppController = (c) => {
return c.json(Object.entries(policyRegistry.available) return c.json(
Object.entries(policyRegistry.available)
.map(([internalName, item]) => { .map(([internalName, item]) => {
return { return {
internalName, internalName,
...item, ...item,
instantiate: undefined, instantiate: undefined,
} };
})) }),
);
}; };
export const adminCurrentPolicyController: AppController = async (c) => { export const adminCurrentPolicyController: AppController = async (c) => {
@ -18,9 +20,9 @@ export const adminCurrentPolicyController: AppController = async (c) => {
const current = await relay.query([{ const current = await relay.query([{
authors: [pubkey], authors: [pubkey],
kinds: [11984] kinds: [11984],
}]).then(events => events[0]); }]).then((events) => events[0]);
if (current) return c.json({ mode: conf.policyMode, spec: current }); if (current) return c.json({ mode: conf.policyMode, spec: current });
return c.json({ mode: conf.policyMode, spec: DEFAULT_POLICY_SPEC }); return c.json({ mode: conf.policyMode, spec: DEFAULT_POLICY_SPEC });
} };

View file

@ -1,5 +1,5 @@
import { nostrNow } from '@/utils.ts'; import { nostrNow } from '@/utils.ts';
import type { DittoConf } from "@ditto/conf"; import type { DittoConf } from '@ditto/conf';
import { PolicyRegistry } from '@nostrify/policies'; import { PolicyRegistry } from '@nostrify/policies';
import { MockRelay } from '@nostrify/nostrify/test'; import { MockRelay } from '@nostrify/nostrify/test';
@ -8,31 +8,30 @@ type ParamValue = string | number | boolean;
export const policyRegistry = new PolicyRegistry({ export const policyRegistry = new PolicyRegistry({
antiDuplicationPolicyStore: { antiDuplicationPolicyStore: {
get: (key: Deno.KvKey) => Promise.resolve({ key, value: null, versionstamp: null }), get: (key: Deno.KvKey) => Promise.resolve({ key, value: null, versionstamp: null }),
set: () => Promise.resolve({ ok: true, versionstamp: "00000000000000000000" }) set: () => Promise.resolve({ ok: true, versionstamp: '00000000000000000000' }),
}, },
store: new MockRelay() store: new MockRelay(),
}); });
interface PolicySpecItem { interface PolicySpecItem {
name: keyof typeof policyRegistry.available; name: keyof typeof policyRegistry.available;
params?: Record<string, ParamValue | ParamValue[]>; params?: Record<string, ParamValue | ParamValue[]>;
} }
export interface PolicySpec { export interface PolicySpec {
policies: PolicySpecItem[] policies: PolicySpecItem[];
} }
export const DEFAULT_POLICY_SPEC: PolicySpec = { export const DEFAULT_POLICY_SPEC: PolicySpec = {
policies: [ policies: [
{ "name": "AntiDuplicationPolicy" }, { 'name': 'AntiDuplicationPolicy' },
{ "name": "AuthorPolicy" }, { 'name': 'AuthorPolicy' },
{ "name": "DomainPolicy" }, { 'name': 'DomainPolicy' },
{ "name": "HellthreadPolicy" }, { 'name': 'HellthreadPolicy' },
{ "name": "ReplyBotPolicy" }, { 'name': 'ReplyBotPolicy' },
{ "name": "SizePolicy" }, { 'name': 'SizePolicy' },
{ "name": "HashtagPolicy", "params": { "hashtags": ["NSFW", "explicit", "violence", "cp", "porn"] } }, { 'name': 'HashtagPolicy', 'params': { 'hashtags': ['NSFW', 'explicit', 'violence', 'cp', 'porn'] } },
] ],
}; };
export const createPolicyEvent = async (conf: DittoConf, policies: PolicySpec) => { export const createPolicyEvent = async (conf: DittoConf, policies: PolicySpec) => {
@ -40,6 +39,6 @@ export const createPolicyEvent = async (conf: DittoConf, policies: PolicySpec) =
kind: 11984, kind: 11984,
content: JSON.stringify(policies), content: JSON.stringify(policies),
created_at: nostrNow(), created_at: nostrNow(),
tags: [] tags: [],
}) });
} };

View file

@ -53,9 +53,9 @@ export class PolicyWorker implements NPolicy {
path: conf.policy, path: conf.policy,
databaseUrl: conf.databaseUrl, databaseUrl: conf.databaseUrl,
pubkey: await conf.signer.getPublicKey(), pubkey: await conf.signer.getPublicKey(),
mode: conf.policyMode mode: conf.policyMode,
}); });
if (conf.policyMode === "script") { if (conf.policyMode === 'script') {
logi({ logi({
level: 'info', level: 'info',
ns: 'ditto.system.policy', ns: 'ditto.system.policy',

View file

@ -2,7 +2,7 @@ import { DittoConf } from '@ditto/conf';
import { DittoPolyPg } from '@ditto/db'; import { DittoPolyPg } from '@ditto/db';
import '@soapbox/safe-fetch/load'; import '@soapbox/safe-fetch/load';
import { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/nostrify'; import { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/nostrify';
import { ReadOnlyPolicy, PolicyRegistry, PipePolicy } from '@nostrify/policies'; import { PipePolicy, PolicyRegistry, ReadOnlyPolicy } from '@nostrify/policies';
import * as Comlink from 'comlink'; import * as Comlink from 'comlink';
import { ReadOnlySigner } from '@/signers/ReadOnlySigner.ts'; import { ReadOnlySigner } from '@/signers/ReadOnlySigner.ts';
@ -17,17 +17,17 @@ interface PolicyInitCommon {
databaseUrl: string; databaseUrl: string;
/** Admin pubkey to use for DittoPgStore checks. */ /** Admin pubkey to use for DittoPgStore checks. */
pubkey: string; pubkey: string;
mode: "script" | "event"; mode: 'script' | 'event';
} }
interface ScriptPolicyInit { interface ScriptPolicyInit {
/** Path to the policy module (https, jsr, file, etc) */ /** Path to the policy module (https, jsr, file, etc) */
path: string; path: string;
mode: "script"; mode: 'script';
} }
interface EventPolicyInit { interface EventPolicyInit {
mode: "event"; mode: 'event';
} }
type PolicyInit = PolicyInitCommon & (ScriptPolicyInit | EventPolicyInit); type PolicyInit = PolicyInitCommon & (ScriptPolicyInit | EventPolicyInit);
@ -60,16 +60,15 @@ export class CustomPolicy implements NPolicy {
timeout: 5_000, timeout: 5_000,
}); });
if (opts.mode === "script") { if (opts.mode === 'script') {
const Policy = (await import(opts.path)).default; const Policy = (await import(opts.path)).default;
this.policy = new Policy({ db, store, pubkey }); this.policy = new Policy({ db, store, pubkey });
} } else {
else {
const registry = new PolicyRegistry({ store, antiDuplicationPolicyStore: await Deno.openKv() }); const registry = new PolicyRegistry({ store, antiDuplicationPolicyStore: await Deno.openKv() });
const policies: NPolicy[] = []; const policies: NPolicy[] = [];
const event = await store const event = await store
.query([{ kinds: [11984], authors: [await conf.signer.getPublicKey()] }]) .query([{ kinds: [11984], authors: [await conf.signer.getPublicKey()] }])
.then(results => results[0]); .then((results) => results[0]);
const spec: PolicySpec = event ? JSON.parse(event.content) : DEFAULT_POLICY_SPEC; const spec: PolicySpec = event ? JSON.parse(event.content) : DEFAULT_POLICY_SPEC;