/
store.go
151 lines (126 loc) 路 3.22 KB
/
store.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
package main
import (
"encoding/json"
"github.com/pkg/errors"
)
// Store handles any data we might need to persist
type Store interface {
StoreUserDOToken(token string, key string) error
LoadUserDOToken(key string) (string, error)
DeleteUserDOToken(key string) error
StoreSubscription(sub Subscription) error
LoadSubscription() (*Subscription, error)
AddChannelToSubcription(id string) error
RemoveChannelFromSubscription(id string) error
}
// PluginStore is
type PluginStore struct {
plugin *Plugin
}
// CreateStore is
func CreateStore(p *Plugin) *PluginStore {
return &PluginStore{
plugin: p,
}
}
// StoreUserDOToken is
func (s *PluginStore) StoreUserDOToken(token, key string) error {
apiErr := s.plugin.API.KVSet(key, []byte(token))
if apiErr != nil {
s.plugin.API.LogError("Failed to set token", "key", key, "Err", apiErr.Error())
return apiErr
}
return nil
}
// LoadUserDOToken is
func (s *PluginStore) LoadUserDOToken(key string) (string, error) {
data, apiErr := s.plugin.API.KVGet(key)
if apiErr != nil {
s.plugin.API.LogError("Failed to get token", "key", key, "Err", apiErr.Error())
return "", apiErr
}
return string(data), nil
}
// DeleteUserDOToken is
func (s *PluginStore) DeleteUserDOToken(key string) error {
apiErr := s.plugin.API.KVDelete(key)
if apiErr != nil {
s.plugin.API.LogError("Failed to delete token", "key", key, "Err", apiErr.Error())
return apiErr
}
return nil
}
// StoreSubscription adds an empty subcription on plugin activation
func (s *PluginStore) StoreSubscription(sub Subscription) error {
data, err := json.Marshal(sub)
if err != nil {
return err
}
apiErr := s.plugin.API.KVSet(subscriptionKVKey, data)
if apiErr != nil {
return apiErr
}
return nil
}
// LoadSubscription adds an empty subcription on plugin activation
func (s *PluginStore) LoadSubscription() (*Subscription, error) {
data, apiErr := s.plugin.API.KVGet(subscriptionKVKey)
if apiErr != nil {
s.plugin.API.LogError("Failed to get subscription", "key", subscriptionKVKey, "Err", apiErr.Error())
return nil, apiErr
}
subscription := &Subscription{}
err := json.Unmarshal(data, subscription)
if err != nil {
return nil, err
}
return subscription, nil
}
// AddChannelToSubcription adds a channel
func (s *PluginStore) AddChannelToSubcription(id string) error {
sub, err := s.LoadSubscription()
if err != nil {
return err
}
channels := sub.Channels
i := indexOf(id, channels)
// Channel is already subscribed
if i != -1 {
return errors.New("already_subcribed")
}
channels = append(channels, id)
sub.Channels = channels
e := s.StoreSubscription(*sub)
if e != nil {
return e
}
return nil
}
// RemoveChannelFromSubscription removes a channel from subcriptions
func (s *PluginStore) RemoveChannelFromSubscription(id string) error {
sub, err := s.LoadSubscription()
if err != nil {
return err
}
channels := sub.Channels
i := indexOf(id, channels)
channels = remove(channels, i)
sub.Channels = channels
e := s.StoreSubscription(*sub)
if e != nil {
return e
}
return nil
}
func indexOf(element string, data []string) int {
for k, v := range data {
if element == v {
return k
}
}
return -1
}
func remove(s []string, i int) []string {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}