-
Notifications
You must be signed in to change notification settings - Fork 921
/
notifications.go
167 lines (133 loc) · 4.35 KB
/
notifications.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
package notifications
import (
"strconv"
"strings"
"emperror.dev/errors"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/common/configstore"
"golang.org/x/net/context"
)
const (
RecordSeparator = "\x1e"
MaxUserMessages = 10
)
var logger = common.GetPluginLogger(&Plugin{})
type Plugin struct{}
func RegisterPlugin() {
plugin := &Plugin{}
common.RegisterPlugin(plugin)
common.GORM.AutoMigrate(&Config{})
configstore.RegisterConfig(configstore.SQL, &Config{})
}
func (p *Plugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "General Notifications",
SysName: "notifications",
Category: common.PluginCategoryFeeds,
}
}
type Config struct {
configstore.GuildConfigModel
JoinServerEnabled bool `json:"join_server_enabled" schema:"join_server_enabled"`
JoinServerChannel string `json:"join_server_channel" schema:"join_server_channel" valid:"channel,true"`
// Implementation note: gorilla/schema currently requires manual index
// setting in forms to parse sub-objects. GORM has_many is also complicated
// by manual handling of associations and loss of IDs through the web form
// (without which Replace() is currently n^2).
// For strings, we greatly simplify things by flattening for storage.
// TODO: Remove the legacy single-message variant when ready to migrate the
// database.
JoinServerMsg string `json:"join_server_msg" valid:"template,5000"`
JoinServerMsgs []string `json:"join_server_msgs" schema:"join_server_msgs" gorm:"-" valid:"template,5000"`
// Do Not Use! For persistence only.
JoinServerMsgs_ string `json:"-"`
JoinDMEnabled bool `json:"join_dm_enabled" schema:"join_dm_enabled"`
JoinDMMsg string `json:"join_dm_msg" schema:"join_dm_msg" valid:"template,5000"`
LeaveEnabled bool `json:"leave_enabled" schema:"leave_enabled"`
LeaveChannel string `json:"leave_channel" schema:"leave_channel" valid:"channel,true"`
LeaveMsg string `json:"leave_msg" schema:"leave_msg" valid:"template,5000"`
LeaveMsgs []string `json:"leave_msgs" schema:"leave_msgs" gorm:"-" valid:"template,5000"`
// Do Not Use! For persistence only.
LeaveMsgs_ string `json:"-"`
TopicEnabled bool `json:"topic_enabled" schema:"topic_enabled"`
TopicChannel string `json:"topic_channel" schema:"topic_channel" valid:"channel,true"`
CensorInvites bool `schema:"censor_invites"`
}
func (c *Config) JoinServerChannelInt() (i int64) {
i, _ = strconv.ParseInt(c.JoinServerChannel, 10, 64)
return
}
func (c *Config) LeaveChannelInt() (i int64) {
i, _ = strconv.ParseInt(c.LeaveChannel, 10, 64)
return
}
func (c *Config) TopicChannelInt() (i int64) {
i, _ = strconv.ParseInt(c.TopicChannel, 10, 64)
return
}
func (c *Config) GetName() string {
return "general_notifications"
}
func (c *Config) TableName() string {
return "general_notification_configs"
}
// GORM BeforeSave hook
func (c *Config) BeforeSave() (err error) {
filterAndJoin := func(a []string) string {
joined := ""
msgsJoined := 0
for _, s := range a {
if s == "" {
continue
}
if msgsJoined >= MaxUserMessages {
break
}
msgsJoined++
if len(joined) > 0 {
joined += RecordSeparator
}
joined += s
}
return joined
}
c.JoinServerMsgs_ = filterAndJoin(c.JoinServerMsgs)
c.LeaveMsgs_ = filterAndJoin(c.LeaveMsgs)
return nil
}
// GORM AfterFind hook
func (c *Config) AfterFind() (err error) {
if c.JoinServerMsg != "" {
c.JoinServerMsgs = append(c.JoinServerMsgs, c.JoinServerMsg)
c.JoinServerMsg = ""
}
if c.JoinServerMsgs_ != "" {
c.JoinServerMsgs = append(c.JoinServerMsgs, strings.Split(c.JoinServerMsgs_, RecordSeparator)...)
}
if c.LeaveMsg != "" {
c.LeaveMsgs = append(c.LeaveMsgs, c.LeaveMsg)
c.LeaveMsg = ""
}
if c.LeaveMsgs_ != "" {
c.LeaveMsgs = append(c.LeaveMsgs, strings.Split(c.LeaveMsgs_, RecordSeparator)...)
}
return nil
}
var DefaultConfig = &Config{}
func GetConfig(guildID int64) (*Config, error) {
var conf Config
err := configstore.Cached.GetGuildConfig(context.Background(), guildID, &conf)
if err == nil {
return &conf, nil
}
if err == configstore.ErrNotFound {
// if err != configstore.ErrNotFound {
// log.WithError(err).Error("Failed retrieving config")
// }
return &Config{
JoinServerMsgs: []string{"<@{{.User.ID}}> Joined!"},
LeaveMsgs: []string{"**{{.User.Username}}** Left... :'("},
}, nil
}
return nil, errors.WithStackIf(err)
}