-
Notifications
You must be signed in to change notification settings - Fork 1
/
bookmark.go
160 lines (140 loc) · 4.04 KB
/
bookmark.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package eventmgr
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/dgraph-io/badger/v4"
bh "github.com/digisan/db-helper/badger"
. "github.com/digisan/go-generics"
"github.com/digisan/gotk/strs"
lk "github.com/digisan/logkit"
)
// key: User (uname)
// value: EventIDs ([uuid])
type Bookmark struct {
User string // uname
EventIDTMs []string // [uuid@event-time], picked from all events
fnDbStore func(*Bookmark) error // in db.go
}
func newBookmark(user string) *Bookmark {
return &Bookmark{
User: user,
EventIDTMs: []string{},
fnDbStore: bh.UpsertOneObject[Bookmark],
}
}
func NewBookmark(user string, useExisting bool) (*Bookmark, error) {
if bm, err := FetchBookmark(user); err == nil && bm != nil {
if useExisting {
return bm, err
}
return nil, fmt.Errorf("user <%s> is already existing, cannot be NewBookmark,", user)
}
return newBookmark(user), nil
}
func (bm Bookmark) String() string {
sb := strings.Builder{}
sb.WriteString(bm.User + "\n")
for i, idtm := range bm.EventIDTMs {
id := strs.SplitPartTo[string](idtm, "@", 0)
sb.WriteString(fmt.Sprintf("\t%02d\t%s\n", i, id))
}
return sb.String()
}
func (bm *Bookmark) BadgerDB() *badger.DB {
return DbGrp.BookmarkID
}
func (bm *Bookmark) Key() []byte {
return []byte(bm.User)
}
func (bm *Bookmark) Marshal(at any) (forKey, forValue []byte) {
lk.FailOnErrWhen(len(bm.User) == 0, "%v", errors.New("empty bookmark user"))
forKey = bm.Key()
forValue = []byte(fmt.Sprint(bm.EventIDTMs))
return
}
func (bm *Bookmark) Unmarshal(dbKey, dbVal []byte) (any, error) {
bm.User = string(dbKey)
dbValStr := string(dbVal)
dbValStr = strings.TrimPrefix(dbValStr, "[")
dbValStr = strings.TrimSuffix(dbValStr, "]")
dbValStr = strings.TrimSpace(dbValStr)
bm.EventIDTMs = IF(len(dbValStr) > 0, strings.Split(dbValStr, " "), []string{})
bm.fnDbStore = bh.UpsertOneObject[Bookmark]
return bm, nil
}
/////////////////////////////////////////////////////////////////////////
func (bm *Bookmark) AddEvent(evtID string) error {
mtx.Lock()
defer mtx.Unlock()
evt, err := FetchEvent(true, evtID)
if err != nil {
return err
}
if evt == nil {
return fmt.Errorf("event [%s] is not alive, cannot be bookmarked", evtID)
}
idtm := evtID + "@" + evt.Tm.Format("20060102150405")
bm.EventIDTMs = append(bm.EventIDTMs, idtm)
bm.EventIDTMs = Settify(bm.EventIDTMs...)
return bm.fnDbStore(bm)
}
func (bm *Bookmark) RemoveEvent(evtID string) (int, error) {
mtx.Lock()
defer mtx.Unlock()
prevN := len(bm.EventIDTMs)
FilterFast(&bm.EventIDTMs, func(i int, e string) bool { return !strings.HasPrefix(e, evtID) })
if err := bm.fnDbStore(bm); err != nil {
return -1, err
}
return prevN - len(bm.EventIDTMs), nil
}
// get all bookmarked events
func (bm *Bookmark) Bookmarks(order string) (bms []string) {
switch order {
case "desc":
sort.SliceStable(bm.EventIDTMs, func(i, j int) bool {
left, right := bm.EventIDTMs[i], bm.EventIDTMs[j]
leftTm, rightTm := strs.SplitPartTo[string](left, "@", 1), strs.SplitPartTo[string](right, "@", 1)
return leftTm > rightTm
})
case "asc":
sort.SliceStable(bm.EventIDTMs, func(i, j int) bool {
left, right := bm.EventIDTMs[i], bm.EventIDTMs[j]
leftTm, rightTm := strs.SplitPartTo[string](left, "@", 1), strs.SplitPartTo[string](right, "@", 1)
return leftTm < rightTm
})
}
for _, idtm := range bm.EventIDTMs {
bms = append(bms, strs.SplitPartTo[string](idtm, "@", 0))
}
return bms
}
func (bm *Bookmark) HasEvent(evtID string) bool {
return In(evtID, bm.Bookmarks("")...)
}
func (bm *Bookmark) ToggleEvent(evtID string) (bool, error) {
if bm.HasEvent(evtID) {
n, err := bm.RemoveEvent(evtID)
if err != nil {
return false, err
}
if n != 1 {
return false, fmt.Errorf("remove event [%s] error", evtID)
}
return false, nil
} else {
if err := bm.AddEvent(evtID); err != nil {
return false, err
}
return true, nil
}
}
func FetchBookmark(user string) (*Bookmark, error) {
bookmark, err := bh.GetOneObject[Bookmark]([]byte(user))
if err != nil {
return nil, err
}
return bookmark, nil
}