-
Notifications
You must be signed in to change notification settings - Fork 938
/
coreserverconf.go
65 lines (49 loc) · 1.42 KB
/
coreserverconf.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
package common
import (
"context"
"database/sql"
"time"
"github.com/jonas747/yagpdb/common/models"
"github.com/karlseguin/rcache"
"github.com/volatiletech/sqlboiler/boil"
)
const CoreServerConfDBSchema = `
CREATE TABLE IF NOT EXISTS core_configs (
guild_id BIGINT PRIMARY KEY,
allowed_read_only_roles BIGINT[],
allowed_write_roles BIGINT[],
allow_all_members_read_only BOOLEAN NOT NULL,
allow_non_members_read_only BOOLEAN NOT NULL
)
`
var CoreServerConfigCache = rcache.NewInt(coreServerConfigCacheFetcher, time.Minute)
func GetCoreServerConfCached(guildID int64) *models.CoreConfig {
return CoreServerConfigCache.Get(int(guildID)).(*models.CoreConfig)
}
func coreServerConfigCacheFetcher(key int) interface{} {
conf, err := models.FindCoreConfigG(context.Background(), int64(key))
if err != nil && err != sql.ErrNoRows {
logger.WithError(err).WithField("guild", key).Error("failed fetching core server config")
}
if conf == nil {
conf = &models.CoreConfig{
GuildID: int64(key),
}
}
return conf
}
func ContextCoreConf(ctx context.Context) *models.CoreConfig {
v := ctx.Value(ContextKeyCoreConfig)
if v == nil {
return nil
}
return v.(*models.CoreConfig)
}
func CoreConfigSave(ctx context.Context, m *models.CoreConfig) error {
err := m.UpsertG(ctx, true, []string{"guild_id"}, boil.Infer(), boil.Infer())
if err != nil {
return err
}
CoreServerConfigCache.Delete(int(m.GuildID))
return nil
}