-
Notifications
You must be signed in to change notification settings - Fork 927
/
models.go
201 lines (157 loc) · 3.78 KB
/
models.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package cplogs
import (
"fmt"
"time"
"github.com/botlabs-gg/yagpdb/common"
)
const DBSchema = `
CREATE TABLE IF NOT EXISTS panel_logs (
guild_id BIGINT NOT NULL,
local_id BIGINT NOT NULL,
author_id BIGINT NOT NULL,
author_username TEXT NOT NULL,
action TEXT NOT NULL,
param1_type SMALLINT NOT NULL,
param1_int BIGINT NOT NULL,
param1_string TEXT NOT NULL,
param2_type SMALLINT NOT NULL,
param2_int BIGINT NOT NULL,
param2_string TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
PRIMARY KEY(guild_id, local_id)
)
`
func init() {
common.RegisterDBSchemas("cplogs", DBSchema)
}
type rawLogEntry struct {
GuildID int64 `db:"guild_id"`
LocalID int64 `db:"local_id"`
AuthorID int64 `db:"author_id"`
AuthorUsername string `db:"author_username"`
Action string `db:"action"`
Param1Type uint8 `db:"param1_type"`
Param1Int int64 `db:"param1_int"`
Param1String string `db:"param1_string"`
Param2Type uint8 `db:"param2_type"`
Param2Int int64 `db:"param2_int"`
Param2String string `db:"param2_string"`
CreatedAt time.Time `db:"created_at"`
}
func (r *rawLogEntry) toLogEntry() *LogEntry {
format, ok := actionFormats[r.Action]
if !ok {
format = &ActionFormat{
FormatString: r.Action,
Key: r.Action,
Found: false,
}
// panic("unknown action format: " + r.Action)
}
entry := &LogEntry{
GuildID: r.GuildID,
LocalID: r.LocalID,
AuthorID: r.AuthorID,
AuthorUsername: r.AuthorUsername,
Action: &LogAction{
FormatStr: format.FormatString,
Key: r.Action,
},
CreatedAt: r.CreatedAt,
}
if format.Found {
if r.Param1Type > 0 {
entry.Action.Params = append(entry.Action.Params, readParam(r.Param1Type, r.Param1Int, r.Param1String))
if r.Param2Type > 0 {
entry.Action.Params = append(entry.Action.Params, readParam(r.Param2Type, r.Param2Int, r.Param2String))
}
}
}
return entry
}
func readParam(paramType uint8, paramInt int64, paramString string) *Param {
var value interface{}
switch ParamType(paramType) {
case ParamTypeInt:
value = paramInt
case ParamTypeString:
value = paramString
}
return &Param{
Type: ParamType(paramType),
Value: value,
}
}
type LogEntry struct {
GuildID int64
LocalID int64
AuthorID int64
AuthorUsername string
Action *LogAction
CreatedAt time.Time
}
func (l *LogEntry) toRawLogEntry() *rawLogEntry {
rawEntry := &rawLogEntry{
GuildID: l.GuildID,
LocalID: l.LocalID,
AuthorID: l.AuthorID,
AuthorUsername: l.AuthorUsername,
Action: l.Action.Key,
CreatedAt: l.CreatedAt,
}
if len(l.Action.Params) > 0 {
rawEntry.Param1Type = uint8(l.Action.Params[0].Type)
switch t := l.Action.Params[0].Value.(type) {
case int64:
rawEntry.Param1Int = t
case string:
rawEntry.Param1String = t
}
if len(l.Action.Params) > 1 {
rawEntry.Param2Type = uint8(l.Action.Params[1].Type)
switch t := l.Action.Params[1].Value.(type) {
case int64:
rawEntry.Param2Int = t
case string:
rawEntry.Param2String = t
}
}
}
return rawEntry
}
func NewEntry(guildID int64, authorID int64, authorUsername string, action string, params ...*Param) *LogEntry {
entry := &LogEntry{
CreatedAt: time.Now(),
GuildID: guildID,
AuthorID: authorID,
AuthorUsername: authorUsername,
Action: &LogAction{
Key: action,
Params: params,
},
}
return entry
}
type LogAction struct {
Key string
FormatStr string
Params []*Param
}
func (l *LogAction) String() string {
f := l.FormatStr
args := []interface{}{}
for _, v := range l.Params {
args = append(args, v.Value)
}
return fmt.Sprintf(f, args...)
}
type ParamType uint8
const (
ParamTypeNone ParamType = 0
ParamTypeInt ParamType = 1
ParamTypeString ParamType = 2
)
type Param struct {
Type ParamType
Value interface{}
}