forked from digisan/event-mgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow.go
134 lines (113 loc) · 3.23 KB
/
follow.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package eventmgr
import (
"errors"
"fmt"
"strings"
"github.com/dgraph-io/badger/v4"
bh "github.com/digisan/db-helper/badger"
. "github.com/digisan/go-generics"
lk "github.com/digisan/logkit"
)
// Follow a Post
type EventFollow struct {
evtFlwee string // this event id
evtFlwers []string // follower event ids
fnDbStore func(*EventFollow) error
}
func newEventFollow(flwee string) *EventFollow {
return &EventFollow{
evtFlwee: flwee,
evtFlwers: []string{},
fnDbStore: bh.UpsertOneObject[EventFollow],
}
}
func NewEventFollow(flwee string, useExisting bool) (*EventFollow, error) {
if flw, err := FetchFollow(flwee); err == nil && flw != nil {
if useExisting {
return flw, err
}
return nil, fmt.Errorf("<%s> is already existing, cannot be New", flwee)
}
return newEventFollow(flwee), nil
}
func (ef EventFollow) String() string {
sb := strings.Builder{}
sb.WriteString("followee: " + ef.evtFlwee + "\n")
sb.WriteString("follower:")
for _, f := range ef.evtFlwers {
sb.WriteString("\n " + f)
}
return sb.String()
}
func (ef *EventFollow) Key() []byte {
return []byte(ef.evtFlwee)
}
func (ef *EventFollow) Marshal(at any) (forKey, forValue []byte) {
forKey = ef.Key()
lk.FailOnErrWhen(len(forKey) == 0, "%v", errors.New("empty followee"))
forValue = []byte(fmt.Sprint(ef.evtFlwers))
return
}
func (ef *EventFollow) Unmarshal(dbKey, dbVal []byte) (any, error) {
ef.evtFlwee = string(dbKey)
dbValStr := string(dbVal)
dbValStr = strings.TrimPrefix(dbValStr, "[")
dbValStr = strings.TrimSuffix(dbValStr, "]")
dbValStr = strings.TrimSpace(dbValStr)
ef.evtFlwers = IF(len(dbValStr) > 0, strings.Split(dbValStr, " "), []string{})
ef.fnDbStore = bh.UpsertOneObject[EventFollow]
return ef, nil
}
func (ef *EventFollow) BadgerDB() *badger.DB {
return DbGrp.IDFlwID
}
/////////////////////////////////////////////////////////////////////////////
func (ef *EventFollow) AddFollower(followers ...string) error {
mtx.Lock()
defer mtx.Unlock()
if !EventHappened(ef.evtFlwee) {
return fmt.Errorf("<%s> is not existing, cannot add followers", ef.evtFlwee)
}
ef.evtFlwers = append(ef.evtFlwers, followers...)
ef.evtFlwers = Settify(ef.evtFlwers...)
if err := ef.fnDbStore(ef); err != nil {
return err
}
return nil
}
func (ef *EventFollow) RmFollower(followers ...string) (int, error) {
mtx.Lock()
defer mtx.Unlock()
if !EventHappened(ef.evtFlwee) {
return -1, fmt.Errorf("<%s> is not existing, cannot remove followers", ef.evtFlwee)
}
prevN := len(ef.evtFlwers)
FilterFast(&ef.evtFlwers, func(i int, e string) bool { return NotIn(e, followers...) })
if err := ef.fnDbStore(ef); err != nil {
return -1, err
}
return prevN - len(ef.evtFlwers), nil
}
func FetchFollow(flwee string) (*EventFollow, error) {
mtx.Lock()
defer mtx.Unlock()
if !EventHappened(flwee) {
return nil, fmt.Errorf("<%s> is not existing, cannot be fetched", flwee)
}
return bh.GetOneObject[EventFollow]([]byte(flwee))
}
func Followers(flwee string) ([]string, error) {
ef, err := FetchFollow(flwee)
if err != nil {
return nil, err
}
if ef == nil {
return []string{}, nil
}
return ef.evtFlwers, nil
}
func deleteEventFollow(flwee string) (int, error) {
mtx.Lock()
defer mtx.Unlock()
return bh.DeleteOneObject[EventFollow]([]byte(flwee))
}