Merge branch 'zapsplit-conf' into 'main'

Remove `@/config.ts` import from utils/zap-split.ts

See merge request soapbox-pub/ditto!697
This commit is contained in:
Alex Gleason 2025-02-27 20:03:05 +00:00
commit bebc7dc897
4 changed files with 28 additions and 16 deletions

View file

@ -202,7 +202,7 @@ const pgstore = new DittoPgStore({
const pool = new DittoPool({ conf, relay: pgstore }); const pool = new DittoPool({ conf, relay: pgstore });
const relay = new DittoRelayStore({ db, conf, relay: pgstore }); const relay = new DittoRelayStore({ db, conf, relay: pgstore });
await seedZapSplits(relay); await seedZapSplits({ conf, relay });
if (conf.firehoseEnabled) { if (conf.firehoseEnabled) {
startFirehose({ startFirehose({

View file

@ -186,7 +186,8 @@ const zapSplitSchema = z.record(
); );
export const updateZapSplitsController: AppController = async (c) => { export const updateZapSplitsController: AppController = async (c) => {
const { conf, relay } = c.var; const { conf } = c.var;
const body = await parseBody(c.req.raw); const body = await parseBody(c.req.raw);
const result = zapSplitSchema.safeParse(body); const result = zapSplitSchema.safeParse(body);
@ -196,7 +197,7 @@ export const updateZapSplitsController: AppController = async (c) => {
const adminPubkey = await conf.signer.getPublicKey(); const adminPubkey = await conf.signer.getPublicKey();
const dittoZapSplit = await getZapSplits(relay, adminPubkey); const dittoZapSplit = await getZapSplits(adminPubkey, c.var);
if (!dittoZapSplit) { if (!dittoZapSplit) {
return c.json({ error: 'Zap split not activated, restart the server.' }, 404); return c.json({ error: 'Zap split not activated, restart the server.' }, 404);
} }
@ -223,7 +224,8 @@ export const updateZapSplitsController: AppController = async (c) => {
const deleteZapSplitSchema = z.array(n.id()).min(1); const deleteZapSplitSchema = z.array(n.id()).min(1);
export const deleteZapSplitsController: AppController = async (c) => { export const deleteZapSplitsController: AppController = async (c) => {
const { conf, relay } = c.var; const { conf } = c.var;
const body = await parseBody(c.req.raw); const body = await parseBody(c.req.raw);
const result = deleteZapSplitSchema.safeParse(body); const result = deleteZapSplitSchema.safeParse(body);
@ -233,7 +235,7 @@ export const deleteZapSplitsController: AppController = async (c) => {
const adminPubkey = await conf.signer.getPublicKey(); const adminPubkey = await conf.signer.getPublicKey();
const dittoZapSplit = await getZapSplits(relay, adminPubkey); const dittoZapSplit = await getZapSplits(adminPubkey, c.var);
if (!dittoZapSplit) { if (!dittoZapSplit) {
return c.json({ error: 'Zap split not activated, restart the server.' }, 404); return c.json({ error: 'Zap split not activated, restart the server.' }, 404);
} }
@ -253,9 +255,9 @@ export const deleteZapSplitsController: AppController = async (c) => {
}; };
export const getZapSplitsController: AppController = async (c) => { export const getZapSplitsController: AppController = async (c) => {
const { conf, relay } = c.var; const { conf } = c.var;
const dittoZapSplit: DittoZapSplits | undefined = await getZapSplits(relay, await conf.signer.getPublicKey()) ?? {}; const dittoZapSplit: DittoZapSplits | undefined = await getZapSplits(await conf.signer.getPublicKey(), c.var) ?? {};
if (!dittoZapSplit) { if (!dittoZapSplit) {
return c.json({ error: 'Zap split not activated, restart the server.' }, 404); return c.json({ error: 'Zap split not activated, restart the server.' }, 404);
} }

View file

@ -196,7 +196,7 @@ const createStatusController: AppController = async (c) => {
if (conf.zapSplitsEnabled) { if (conf.zapSplitsEnabled) {
const meta = n.json().pipe(n.metadata()).catch({}).parse(author?.content); const meta = n.json().pipe(n.metadata()).catch({}).parse(author?.content);
const lnurl = getLnurl(meta); const lnurl = getLnurl(meta);
const dittoZapSplit = await getZapSplits(relay, await conf.signer.getPublicKey()); const dittoZapSplit = await getZapSplits(await conf.signer.getPublicKey(), c.var);
if (lnurl && dittoZapSplit) { if (lnurl && dittoZapSplit) {
const totalSplit = Object.values(dittoZapSplit).reduce((total, { weight }) => total + weight, 0); const totalSplit = Object.values(dittoZapSplit).reduce((total, { weight }) => total + weight, 0);
for (const zapPubkey in dittoZapSplit) { for (const zapPubkey in dittoZapSplit) {

View file

@ -1,8 +1,9 @@
import { Conf } from '@/config.ts';
import { NSchema as n, NStore } from '@nostrify/nostrify'; import { NSchema as n, NStore } from '@nostrify/nostrify';
import { nostrNow } from '@/utils.ts'; import { nostrNow } from '@/utils.ts';
import { percentageSchema } from '@/schema.ts'; import { percentageSchema } from '@/schema.ts';
import type { DittoConf } from '@ditto/conf';
type Pubkey = string; type Pubkey = string;
type ExtraMessage = string; type ExtraMessage = string;
/** Number from 1 to 100, stringified. */ /** Number from 1 to 100, stringified. */
@ -12,11 +13,18 @@ export type DittoZapSplits = {
[key: Pubkey]: { weight: splitPercentages; message: ExtraMessage }; [key: Pubkey]: { weight: splitPercentages; message: ExtraMessage };
}; };
interface GetZapSplitsOpts {
conf: DittoConf;
relay: NStore;
}
/** Gets zap splits from NIP-78 in DittoZapSplits format. */ /** Gets zap splits from NIP-78 in DittoZapSplits format. */
export async function getZapSplits(store: NStore, pubkey: string): Promise<DittoZapSplits | undefined> { export async function getZapSplits(pubkey: string, opts: GetZapSplitsOpts): Promise<DittoZapSplits | undefined> {
const { relay } = opts;
const zapSplits: DittoZapSplits = {}; const zapSplits: DittoZapSplits = {};
const [event] = await store.query([{ const [event] = await relay.query([{
authors: [pubkey], authors: [pubkey],
kinds: [30078], kinds: [30078],
'#d': ['pub.ditto.zapSplits'], '#d': ['pub.ditto.zapSplits'],
@ -36,15 +44,17 @@ export async function getZapSplits(store: NStore, pubkey: string): Promise<Ditto
return zapSplits; return zapSplits;
} }
export async function seedZapSplits(store: NStore) { export async function seedZapSplits(opts: GetZapSplitsOpts): Promise<void> {
const zapSplit: DittoZapSplits | undefined = await getZapSplits(store, await Conf.signer.getPublicKey()); const { conf, relay } = opts;
const pubkey = await conf.signer.getPublicKey();
const zapSplit: DittoZapSplits | undefined = await getZapSplits(pubkey, opts);
if (!zapSplit) { if (!zapSplit) {
const dittoPubkey = '781a1527055f74c1f70230f10384609b34548f8ab6a0a6caa74025827f9fdae5'; const dittoPubkey = '781a1527055f74c1f70230f10384609b34548f8ab6a0a6caa74025827f9fdae5';
const dittoMsg = 'Official Ditto Account'; const dittoMsg = 'Official Ditto Account';
const signer = Conf.signer; const event = await conf.signer.signEvent({
const event = await signer.signEvent({
content: '', content: '',
created_at: nostrNow(), created_at: nostrNow(),
kind: 30078, kind: 30078,
@ -54,6 +64,6 @@ export async function seedZapSplits(store: NStore) {
], ],
}); });
await store.event(event); await relay.event(event);
} }
} }