mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-04 14:14:24 +01:00
Deliver API events to outbox relays
This commit is contained in:
@@ -14,7 +14,7 @@ import {
|
||||
import * as pipeline from '@/pipeline.ts';
|
||||
import { AdminSigner } from '@/signers/AdminSigner.ts';
|
||||
import { APISigner } from '@/signers/APISigner.ts';
|
||||
import { eventsDB } from '@/storages.ts';
|
||||
import { client, eventsDB } from '@/storages.ts';
|
||||
import { nostrNow } from '@/utils.ts';
|
||||
|
||||
const debug = Debug('ditto:api');
|
||||
@@ -89,7 +89,10 @@ async function createAdminEvent(t: EventStub, c: AppContext): Promise<NostrEvent
|
||||
async function publishEvent(event: NostrEvent, c: AppContext): Promise<NostrEvent> {
|
||||
debug('EVENT', event);
|
||||
try {
|
||||
await pipeline.handleEvent(event, c.req.raw.signal);
|
||||
await Promise.all([
|
||||
pipeline.handleEvent(event, c.req.raw.signal),
|
||||
client.event(event),
|
||||
]);
|
||||
} catch (e) {
|
||||
if (e instanceof pipeline.RelayError) {
|
||||
throw new HTTPException(422, {
|
||||
|
||||
27
src/utils/outbox.ts
Normal file
27
src/utils/outbox.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Conf } from '@/config.ts';
|
||||
import { eventsDB } from '@/storages.ts';
|
||||
|
||||
export async function getRelays(pubkey: string): Promise<Set<string>> {
|
||||
const relays = new Set<`wss://${string}`>();
|
||||
|
||||
const events = await eventsDB.query([
|
||||
{ kinds: [10002], authors: [pubkey, Conf.pubkey], limit: 2 },
|
||||
]);
|
||||
|
||||
for (const event of events) {
|
||||
for (const [name, relay, marker] of event.tags) {
|
||||
if (name === 'r' && (marker === 'write' || !marker)) {
|
||||
try {
|
||||
const url = new URL(relay);
|
||||
if (url.protocol === 'wss:') {
|
||||
relays.add(url.toString() as `wss://${string}`);
|
||||
}
|
||||
} catch (_e) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return relays;
|
||||
}
|
||||
Reference in New Issue
Block a user