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');
}
get policyMode(): "script" | "event" {
return this.env.get("DITTO_CUSTOM_POLICY") ? "script" : "event";
get policyMode(): 'script' | 'event' {
return this.env.get('DITTO_CUSTOM_POLICY') ? 'script' : 'event';
}
/** 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 pleromaStatusesRoute from '@/routes/pleromaStatusesRoute.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';
export interface AppEnv extends DittoEnv {

View file

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

View file

@ -1,26 +1,28 @@
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) => {
return c.json(Object.entries(policyRegistry.available)
.map(([internalName, item]) => {
return {
internalName,
...item,
instantiate: undefined,
}
}))
return c.json(
Object.entries(policyRegistry.available)
.map(([internalName, item]) => {
return {
internalName,
...item,
instantiate: undefined,
};
}),
);
};
export const adminCurrentPolicyController: AppController = async (c) => {
const { relay, conf } = c.var;
const pubkey = await conf.signer.getPublicKey();
const { relay, conf } = c.var;
const pubkey = await conf.signer.getPublicKey();
const current = await relay.query([{
authors: [pubkey],
kinds: [11984]
}]).then(events => events[0]);
const current = await relay.query([{
authors: [pubkey],
kinds: [11984],
}]).then((events) => events[0]);
if (current) return c.json({ mode: conf.policyMode, spec: current });
return c.json({ mode: conf.policyMode, spec: DEFAULT_POLICY_SPEC });
}
if (current) return c.json({ mode: conf.policyMode, spec: current });
return c.json({ mode: conf.policyMode, spec: DEFAULT_POLICY_SPEC });
};

View file

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

View file

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

View file

@ -2,7 +2,7 @@ import { DittoConf } from '@ditto/conf';
import { DittoPolyPg } from '@ditto/db';
import '@soapbox/safe-fetch/load';
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 { ReadOnlySigner } from '@/signers/ReadOnlySigner.ts';
@ -17,17 +17,17 @@ interface PolicyInitCommon {
databaseUrl: string;
/** Admin pubkey to use for DittoPgStore checks. */
pubkey: string;
mode: "script" | "event";
mode: 'script' | 'event';
}
interface ScriptPolicyInit {
/** Path to the policy module (https, jsr, file, etc) */
path: string;
mode: "script";
mode: 'script';
}
interface EventPolicyInit {
mode: "event";
mode: 'event';
}
type PolicyInit = PolicyInitCommon & (ScriptPolicyInit | EventPolicyInit);
@ -60,16 +60,15 @@ export class CustomPolicy implements NPolicy {
timeout: 5_000,
});
if (opts.mode === "script") {
if (opts.mode === 'script') {
const Policy = (await import(opts.path)).default;
this.policy = new Policy({ db, store, pubkey });
}
else {
} else {
const registry = new PolicyRegistry({ store, antiDuplicationPolicyStore: await Deno.openKv() });
const policies: NPolicy[] = [];
const event = await store
.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;