From ea69d02af6e691a1b32e5198a1143aa142ba82ac Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Fri, 6 Jan 2023 14:31:20 -0500 Subject: subscription lookup on outbox item create --- cgi/servers.go | 13 ++++++++++++- models/persister.go | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/cgi/servers.go b/cgi/servers.go index 9d08b6c..a419417 100644 --- a/cgi/servers.go +++ b/cgi/servers.go @@ -116,7 +116,18 @@ func processTick(h config.Handler, output []byte, persister *models.Persister, l log.Debugf("extracted content '%s'", extractedContent) outboxItem := models.CreateOutboxItem(h, []byte(extractedContent)) err = persister.StoreWithCallback(outboxItem, func() { - log.With("handler", h.Name).Debug("callback for save") + logger := log.With("handler", h.Name).With("type", "subscription") + // go through handler subscriptions and deliver signed outbox items + logger.Debug("callback for save") + sub := models.NewSubscription(h) + keys, err := persister.CollectKeys(sub) + if err != nil { + logger.Error(err) + return + } + for _, v := range keys { + logger.Debugf("found subscription %s", string(v)) + } }) if err != nil { return err diff --git a/models/persister.go b/models/persister.go index f877488..b4b9c49 100644 --- a/models/persister.go +++ b/models/persister.go @@ -98,6 +98,23 @@ func (p *Persister) Collect(model model) ([][]byte, error) { return result, err } +// Much faster, "keys only" version of Collect() +func (p *Persister) CollectKeys(model model) ([][]byte, error) { + var keys [][]byte + err := p.db.View(func(txn *badger.Txn) error { + opts := badger.DefaultIteratorOptions + opts.PrefetchValues = false + it := txn.NewIterator(opts) + defer it.Close() + prefix := []byte(model.Keybase()) + for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() { + keys = append(keys, it.Item().Key()) + } + return nil + }) + return keys, err +} + func (p *Persister) Find(model model) ([]byte, error) { var result []byte var item *badger.Item -- cgit v1.2.3