aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
authorJulio Capote <jcapote@gmail.com>2023-01-06 18:48:12 +0000
committerJulio Capote <jcapote@gmail.com>2023-01-06 18:48:12 +0000
commitca12c0d26c987e67deade02bdf645fda8af30016 (patch)
tree8d9b5d2bbd20150cf89fd9dad072bfc36a037f24 /models
parent422f0cc31521745bbf13dffb1ffa2d68aef9eedf (diff)
downloadcommunique-ca12c0d26c987e67deade02bdf645fda8af30016.tar.gz
start of subscriptions
Diffstat (limited to 'models')
-rw-r--r--models/persister.go10
-rw-r--r--models/subscription.go57
2 files changed, 67 insertions, 0 deletions
diff --git a/models/persister.go b/models/persister.go
index c6e0c72..5f79e4e 100644
--- a/models/persister.go
+++ b/models/persister.go
@@ -43,6 +43,16 @@ func (p *Persister) Store(model model) error {
return err
}
+func (p *Persister) Delete(model model) error {
+ log := p.log.With("model", model.Name()).With("DedupKey", model.DedupKey()).With("Key", model.Key())
+ log.Debug("Delete()")
+ err := p.db.Update(func(txn *badger.Txn) error {
+ txn.Delete([]byte(model.Key()))
+ return nil
+ })
+ return err
+}
+
func (p *Persister) Count(model model) (int, error) {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
diff --git a/models/subscription.go b/models/subscription.go
new file mode 100644
index 0000000..191ffc6
--- /dev/null
+++ b/models/subscription.go
@@ -0,0 +1,57 @@
+package models
+
+import (
+ "fmt"
+ "time"
+
+ "git.capotej.com/capotej/communique/config"
+ "github.com/dgraph-io/badger/v3"
+)
+
+type Subscription struct {
+ Handler config.Handler
+ InboxUrl string
+ CreatedAt time.Time
+}
+
+// used for lookup purposes (count, collect, find)
+func NewSubscription(h config.Handler) *Subscription {
+ aso := &Subscription{Handler: h}
+ return aso
+}
+
+func CreateSubscription(h config.Handler, inboxUrl string) (*Subscription, error) {
+ aso := &Subscription{
+ Handler: h,
+ InboxUrl: inboxUrl,
+ CreatedAt: time.Now(),
+ }
+ return aso, nil
+}
+
+func (a *Subscription) Name() string {
+ return "Subscription"
+}
+
+func (a *Subscription) Keybase() string {
+ keyBase := fmt.Sprintf("sub:%s", a.Handler.Name)
+ return keyBase
+}
+
+func (a *Subscription) DedupKey() string {
+ return a.Key()
+}
+
+func (a *Subscription) Key() string {
+ return fmt.Sprintf("sub:%s:%s", a.Handler.Name, a.InboxUrl)
+}
+
+func (a *Subscription) SaveDedup(txn *badger.Txn) error {
+ txn.Discard() // nothing to do here
+ return nil
+}
+
+func (a *Subscription) Save(txn *badger.Txn) error {
+ e := badger.NewEntry([]byte(a.Key()), []byte{})
+ return txn.SetEntry(e)
+}