make it possible to track relays on publish.

This commit is contained in:
Asai Toshiya 2024-10-11 01:04:32 +09:00 committed by fiatjaf_
parent 4f6976f6f8
commit 7064e0b828
2 changed files with 41 additions and 1 deletions

View File

@ -205,7 +205,17 @@ export class AbstractSimplePool {
}
let r = await this.ensureRelay(url)
return r.publish(event)
return r.publish(event).then(reason => {
if (this.trackRelays) {
let set = this.seenOn.get(event.id)
if (!set) {
set = new Set()
this.seenOn.set(event.id, set)
}
set.add(r)
}
return reason
})
})
}

View File

@ -205,3 +205,33 @@ test('get()', async () => {
expect(event).not.toBeNull()
expect(event).toHaveProperty('id', ids[0])
})
test('track relays when publishing', async () => {
let event1 = finalizeEvent(
{
kind: 1,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: 'hello',
},
generateSecretKey(),
)
let event2 = finalizeEvent(
{
kind: 1,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: 'hello',
},
generateSecretKey(),
)
pool.trackRelays = true
await Promise.all(pool.publish(relayURLs, event1))
expect(pool.seenOn.get(event1.id)).toBeDefined()
expect(Array.from(pool.seenOn.get(event1.id)!).map(r => r.url)).toEqual(expect.arrayContaining(relayURLs))
pool.trackRelays = false
await Promise.all(pool.publish(relayURLs, event2))
expect(pool.seenOn.get(event2.id)).toBeUndefined()
})