aboutsummaryrefslogtreecommitdiff
path: root/models/subscription.go
blob: 191ffc68ab56219b641c58b0c8d152951713040d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)
}