/
database.go
184 lines (149 loc) · 3.53 KB
/
database.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/Xe/ln"
"github.com/Xe/uuid"
"github.com/asdine/storm"
"github.com/golang/groupcache"
"github.com/withinsoft/ventriloquist/internal/proxytag"
)
type DB struct {
s *storm.DB
systemmateCache *groupcache.Group
}
type Systemmate struct {
ID string `storm:"id"`
Name string
CoreDiscordID string `storm:"index"`
AvatarURL string
Match proxytag.Match
}
type Webhook struct {
ID string `storm:"id"`
ChannelID string `storm:"unique"`
WebhookURL string
}
func (d DB) AddSystemmate(s Systemmate) (Systemmate, error) {
if len(s.Name) > 15 {
return Systemmate{}, fmt.Errorf("%s is too long, 15 characters max", s.Name)
}
sms, err := d.FindSystemmates(s.CoreDiscordID)
if err != nil {
goto skip
}
for _, sm := range sms {
if strings.EqualFold(s.Name, sm.Name) {
return Systemmate{}, errors.New("can't add duplicate systemmate")
}
}
skip:
s.ID = uuid.New()
return s, d.s.Save(&s)
}
func (d DB) UpdateSystemmate(s Systemmate) error {
return d.s.Save(&s)
}
func (d DB) FindSystemmates(id string) ([]Systemmate, error) {
var bs []byte
sink := groupcache.AllocatingByteSliceSink(&bs)
key := filepath.Join(time.Now().Round(time.Second).Format(time.RFC3339), id)
err := d.systemmateCache.Get(nil, key, sink)
if err != nil {
return nil, err
}
var result []Systemmate
err = json.Unmarshal(bs, &result)
if err != nil {
return nil, err
}
return result, nil
}
func (d DB) cacheSystemmates(ctx groupcache.Context, key string, dest groupcache.Sink) error {
did := filepath.Base(key)
sms, err := d.findSystemmates(did)
if err != nil {
return err
}
data, err := json.Marshal(&sms)
if err != nil {
return err
}
dest.SetBytes(data)
return nil
}
func (d DB) findSystemmates(id string) ([]Systemmate, error) {
var result []Systemmate
err := d.s.Find("CoreDiscordID", id, &result)
if err != nil {
return nil, err
}
return result, nil
}
func (d DB) DeleteSystemmate(coreDiscordID, name string) error {
mates, err := d.FindSystemmates(coreDiscordID)
if err != nil {
return err
}
for _, m := range mates {
if strings.EqualFold(name, m.Name) {
return d.s.DeleteStruct(&m)
}
}
return errors.New("database: systemmate not found")
}
func (d DB) FindSystemmateByMatch(coreDiscordID string, m proxytag.Match) (Systemmate, error) {
sms, err := d.FindSystemmates(coreDiscordID)
if err != nil {
return Systemmate{}, err
}
for _, sm := range sms {
if sm.Match.String() == m.String() {
return sm, nil
}
}
return Systemmate{}, errors.New("database: systemmate not found")
}
func (d DB) NukeSystem(coreDiscordID string) error {
mates, err := d.FindSystemmates(coreDiscordID)
if err != nil {
return err
}
var errs []error
for _, m := range mates {
if err := d.s.DeleteStruct(&m); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
ctx := ln.WithF(context.Background(), ln.F{
"core_discord_id": coreDiscordID,
})
for _, err := range errs {
ln.Error(ctx, err, ln.F{"to_discord": true})
}
return errors.New("error in deletion, contact the bot admin")
}
return nil
}
func (d DB) AddWebhook(channelID, whurl string) error {
wh := Webhook{
ID: uuid.New(),
ChannelID: channelID,
WebhookURL: whurl,
}
return d.s.Save(&wh)
}
func (d DB) FindWebhook(channelID string) (string, error) {
var result Webhook
err := d.s.One("ChannelID", channelID, &result)
if err != nil {
return "", err
}
return result.WebhookURL, nil
}