feat: support sending only to current relays

This commit is contained in:
codytseng
2025-02-05 15:18:58 +08:00
parent 29f5ccc4bb
commit ccf8c21954
11 changed files with 199 additions and 75 deletions

View File

@@ -105,9 +105,26 @@ class ClientService extends EventTarget {
return this.defaultRelayUrls
}
async publishEvent(relayUrls: string[], event: NEvent) {
async publishEvent(
relayUrls: string[],
event: NEvent,
{
signer
}: {
signer?: (evt: TDraftEvent) => Promise<VerifiedEvent>
} = {}
) {
const result = await Promise.any(
this.pool.publish(relayUrls.concat(this.defaultRelayUrls), event)
relayUrls.map(async (url) => {
const relay = await this.pool.ensureRelay(url)
return relay.publish(event).catch((error) => {
if (error instanceof Error && error.message.startsWith('auth-required:') && signer) {
relay.auth((authEvt: EventTemplate) => signer(authEvt)).then(() => relay.publish(event))
} else {
throw error
}
})
})
)
this.dispatchEvent(new CustomEvent('eventPublished', { detail: event }))
return result