Skip to content

Commit

Permalink
make it possible to track relays on publish.
Browse files Browse the repository at this point in the history
  • Loading branch information
AsaiToshiya authored and fiatjaf committed Oct 10, 2024
1 parent 4f6976f commit 7064e0b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
12 changes: 11 additions & 1 deletion abstract-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})
}

Expand Down
30 changes: 30 additions & 0 deletions pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit 7064e0b

Please sign in to comment.