-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
stateManager.go
91 lines (78 loc) · 2.19 KB
/
stateManager.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
package acfun
import (
"errors"
localdb "github.com/Sora233/DDBOT/lsp/buntdb"
"github.com/Sora233/DDBOT/lsp/concern"
"github.com/tidwall/buntdb"
)
type StateManager struct {
*concern.StateManager
extraKey
}
func (s *StateManager) GetGroupConcernConfig(groupCode int64, id interface{}) (concernConfig concern.IConfig) {
return NewGroupConcernConfig(s.StateManager.GetGroupConcernConfig(groupCode, id))
}
func NewStateManager(notify chan<- concern.Notify) *StateManager {
return &StateManager{
StateManager: concern.NewStateManagerWithInt64ID("Acfun", notify),
}
}
func (s *StateManager) GetUserInfo(uid int64) (*UserInfo, error) {
var userInfo *UserInfo
err := s.GetJson(s.UserInfoKey(uid), &userInfo)
if err != nil {
return nil, err
}
return userInfo, nil
}
func (s *StateManager) AddUserInfo(info *UserInfo) error {
if info == nil {
return errors.New("<nil userinfo>")
}
return s.SetJson(s.UserInfoKey(info.Uid), info)
}
func (s *StateManager) AddLiveInfo(info *LiveInfo) error {
return s.RWCover(func() error {
err := s.SetJson(s.UserInfoKey(info.Uid), info.UserInfo)
if err != nil {
return err
}
err = s.SetJson(s.LiveInfoKey(info.Uid), info)
return err
})
}
func (s *StateManager) GetLiveInfo(uid int64) (*LiveInfo, error) {
var liveInfo *LiveInfo
err := s.GetJson(s.LiveInfoKey(uid), &liveInfo)
if err != nil {
return nil, err
}
return liveInfo, nil
}
func (s *StateManager) DeleteLiveInfo(uid int64) error {
return s.RWCoverTx(func(tx *buntdb.Tx) error {
_, err := tx.Delete(s.LiveInfoKey(uid))
return err
})
}
func (s *StateManager) IncNotLiveCount(uid int64) int64 {
result, err := s.SeqNext(s.NotLiveKey(uid))
if err != nil {
result = 0
}
return result
}
func (s *StateManager) ClearNotLiveCount(uid int64) error {
_, err := s.Delete(s.NotLiveKey(uid), localdb.IgnoreNotFoundOpt())
return err
}
func (s *StateManager) SetUidFirstTimestampIfNotExist(uid int64, timestamp int64) error {
return s.SetInt64(s.UidFirstTimestamp(uid), timestamp, localdb.SetNoOverWriteOpt())
}
func (s *StateManager) GetUidFirstTimestamp(uid int64) (timestamp int64, err error) {
timestamp, err = s.GetInt64(s.UidFirstTimestamp(uid))
if err != nil {
timestamp = 0
}
return
}