generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
encryption_status_store.go
73 lines (64 loc) · 1.77 KB
/
encryption_status_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
package store
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/pkg/errors"
)
const (
// EncryptionStatusStoreKeyPrefix prefix for encryption status data key is kvsotre
EncryptionStatusStoreKeyPrefix = "es_"
)
// EncryptionStatusStore API for encryption statuses in KVStore
type EncryptionStatusStore interface {
IsEncryptionEnabled(channelID, userID string) bool
SetEncryptionStatus(channelID, userID string, status bool) error
}
// IsEncryptionEnabled checks if encryption is enabled for channel and user
func (s *pluginStore) IsEncryptionEnabled(channelID, userID string) bool {
data, err := s.encryptionStatusStore.Load(fmt.Sprintf("%s%s", EncryptionStatusStoreKeyPrefix, channelID))
if err != nil {
return false
}
var users []string
_ = gob.NewDecoder(bytes.NewBuffer(data)).Decode(&users)
for _, user := range users {
if user == userID {
return true
}
}
return false
}
// SetEncryptionStatus changes encryption state
func (s *pluginStore) SetEncryptionStatus(channelID, userID string, status bool) error {
data, err := s.encryptionStatusStore.Load(fmt.Sprintf("%s%s", EncryptionStatusStoreKeyPrefix, channelID))
var users []string
if err == nil {
_ = gob.NewDecoder(bytes.NewBuffer(data)).Decode(&users)
}
ind := -1
for idx, user := range users {
if user == userID {
ind = idx
break
}
}
if status {
if ind != -1 {
return nil
}
users = append(users, userID)
} else {
if ind == -1 {
return nil
}
users = append(users[:ind], users[ind+1:]...)
}
var newData bytes.Buffer
_ = gob.NewEncoder(&newData).Encode(users)
err = s.encryptionStatusStore.Store(fmt.Sprintf("%s%s", EncryptionStatusStoreKeyPrefix, channelID), newData.Bytes())
if err != nil {
return errors.Wrap(err, "error while storing encryption data")
}
return nil
}