From 5a135f5b863b85e4da90d0758e77d18c0baa391b Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 3 Jun 2026 18:28:17 -0300 Subject: [PATCH] khatru/blossom: ListAllBlobs() and OwnersForBlob() --- khatru/blossom/blob.go | 3 +++ khatru/blossom/eventstorewrapper.go | 24 ++++++++++++++++++++++++ khatru/blossom/memoryindex.go | 19 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/khatru/blossom/blob.go b/khatru/blossom/blob.go index c7232c1..7631dc0 100644 --- a/khatru/blossom/blob.go +++ b/khatru/blossom/blob.go @@ -13,6 +13,9 @@ type BlobIndex interface { List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[blossom.BlobDescriptor] Get(ctx context.Context, sha256 string) (*blossom.BlobDescriptor, error) Delete(ctx context.Context, sha256 string, pubkey nostr.PubKey) error + + ListAllBlobs(ctx context.Context) iter.Seq2[nostr.PubKey, blossom.BlobDescriptor] + OwnersForBlob(ctx context.Context, sha256 string) []nostr.PubKey } var ( diff --git a/khatru/blossom/eventstorewrapper.go b/khatru/blossom/eventstorewrapper.go index 26967f8..e08e0c5 100644 --- a/khatru/blossom/eventstorewrapper.go +++ b/khatru/blossom/eventstorewrapper.go @@ -59,6 +59,30 @@ func (es EventStoreBlobIndexWrapper) List(ctx context.Context, pubkey nostr.PubK } } +func (es EventStoreBlobIndexWrapper) ListAllBlobs(ctx context.Context) iter.Seq2[nostr.PubKey, blossom.BlobDescriptor] { + return func(yield func(nostr.PubKey, blossom.BlobDescriptor) bool) { + for evt := range es.Store.QueryEvents(nostr.Filter{ + Kinds: []nostr.Kind{24242}, + }, 1000) { + bd := es.parseEvent(evt) + if !yield(evt.PubKey, bd) { + return + } + } + } +} + +func (es EventStoreBlobIndexWrapper) OwnersForBlob(ctx context.Context, sha256 string) []nostr.PubKey { + var owners []nostr.PubKey + for evt := range es.Store.QueryEvents(nostr.Filter{ + Tags: nostr.TagMap{"x": []string{sha256}}, + Kinds: []nostr.Kind{24242}, + }, 1000) { + owners = append(owners, evt.PubKey) + } + return owners +} + func (es EventStoreBlobIndexWrapper) Get(ctx context.Context, sha256 string) (*blossom.BlobDescriptor, error) { next, stop := iter.Pull( es.Store.QueryEvents(nostr.Filter{Tags: nostr.TagMap{"x": []string{sha256}}, Kinds: []nostr.Kind{24242}, Limit: 1}, 1), diff --git a/khatru/blossom/memoryindex.go b/khatru/blossom/memoryindex.go index ab0d7aa..06c8f73 100644 --- a/khatru/blossom/memoryindex.go +++ b/khatru/blossom/memoryindex.go @@ -59,6 +59,25 @@ func (x MemoryBlobIndex) List(ctx context.Context, pubkey nostr.PubKey) iter.Seq } } +func (x MemoryBlobIndex) ListAllBlobs(ctx context.Context) iter.Seq2[nostr.PubKey, blossom.BlobDescriptor] { + return func(yield func(nostr.PubKey, blossom.BlobDescriptor) bool) { + for _, v := range x.m.Range { + for _, owner := range v.owners { + if !yield(owner, v.blob) { + return + } + } + } + } +} + +func (x MemoryBlobIndex) OwnersForBlob(ctx context.Context, sha256 string) []nostr.PubKey { + if val, ok := x.m.Load(sha256); ok { + return val.owners + } + return nil +} + func (x MemoryBlobIndex) Get(ctx context.Context, sha256 string) (*blossom.BlobDescriptor, error) { if val, ok := x.m.Load(sha256); ok { return &val.blob, nil