-
Notifications
You must be signed in to change notification settings - Fork 4
/
route.go
230 lines (203 loc) · 5.68 KB
/
route.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package toggle_module
import (
"net/http"
"strconv"
"time"
"github.com/anti-raid/splashtail/splashcore/animusmagic"
"github.com/anti-raid/splashtail/splashcore/silverpelt"
"github.com/anti-raid/splashtail/splashcore/types"
"github.com/anti-raid/splashtail/splashcore/utils"
"github.com/anti-raid/splashtail/splashcore/utils/mewext"
"github.com/anti-raid/splashtail/webserver/state"
"github.com/anti-raid/splashtail/webserver/webutils"
"github.com/go-chi/chi/v5"
docs "github.com/infinitybotlist/eureka/doclib"
"github.com/infinitybotlist/eureka/ratelimit"
"github.com/infinitybotlist/eureka/uapi"
"go.uber.org/zap"
)
func Docs() *docs.Doc {
return &docs.Doc{
Summary: "Toggle Module",
Description: "Enable or disable a module for a specific guild",
Resp: types.ApiError{},
Params: []docs.Parameter{
{
Name: "user_id",
Description: "The ID of the user to get information about",
In: "path",
Required: true,
Schema: docs.IdSchema,
},
{
Name: "guild_id",
Description: "Whether to refresh the user's guilds from discord",
In: "path",
Required: true,
Schema: docs.IdSchema,
},
{
Name: "module",
Description: "The module to enable/disable",
In: "query",
Required: true,
Schema: docs.IdSchema,
},
{
Name: "disabled",
Description: "Whether the module is disabled or not",
In: "query",
Required: true,
Schema: docs.IdSchema,
},
},
}
}
func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
limit, err := ratelimit.Ratelimit{
Expiry: 2 * time.Minute,
MaxRequests: 10,
Bucket: "module_configuration",
}.Limit(d.Context, r)
if err != nil {
state.Logger.Error("Error while ratelimiting", zap.Error(err), zap.String("bucket", "get_user_guild_base_info"))
return uapi.DefaultResponse(http.StatusInternalServerError)
}
if limit.Exceeded {
return uapi.HttpResponse{
Json: types.ApiError{
Message: "You are being ratelimited. Please try again in " + limit.TimeToReset.String(),
},
Headers: limit.Headers(),
Status: http.StatusTooManyRequests,
}
}
guildId := chi.URLParam(r, "guild_id")
userId := chi.URLParam(r, "user_id")
if guildId == "" || userId == "" {
return uapi.DefaultResponse(http.StatusBadRequest)
}
clusterId, err := mewext.GetClusterIDFromGuildID(guildId, state.MewldInstanceList.Map, int(state.MewldInstanceList.ShardCount))
if err != nil {
state.Logger.Error("Error getting cluster ID", zap.Error(err))
return uapi.HttpResponse{
Status: http.StatusInternalServerError,
Json: types.ApiError{
Message: "Error getting cluster ID: " + err.Error(),
},
Headers: map[string]string{
"Retry-After": "10",
},
}
}
hresp, ok := webutils.ClusterCheck(clusterId)
if !ok {
return hresp
}
module := r.URL.Query().Get("module")
if module == "" {
return uapi.HttpResponse{
Status: http.StatusBadRequest,
Json: types.ApiError{
Message: "Query parameter `module` is required",
},
}
}
// Find module from cluster
modules, err := state.CachedAnimusMagicClient.GetClusterModules(d.Context, state.Rueidis, uint16(clusterId))
if err != nil {
return uapi.HttpResponse{
Status: http.StatusInternalServerError,
Json: types.ApiError{
Message: "Failed to fetch module list: " + err.Error(),
},
}
}
var moduleToToggle *silverpelt.CanonicalModule
for _, m := range modules {
if m.ID == module {
moduleToToggle = &m
break
}
}
if moduleToToggle == nil {
return uapi.HttpResponse{
Status: http.StatusBadRequest,
Json: types.ApiError{
Message: "Module not found",
},
}
}
if !moduleToToggle.Toggleable {
return uapi.HttpResponse{
Status: http.StatusBadRequest,
Json: types.ApiError{
Message: "Module cannot be enabled/disablable (is not toggleable)",
},
}
}
var disabled bool = r.URL.Query().Get("disabled") == "true"
// INSERT ON CONFLICT UPDATE RETURNING id
var id string
err = state.Pool.QueryRow(
d.Context,
"INSERT INTO guild_module_configurations (guild_id, module, disabled) VALUES ($1, $2, $3) ON CONFLICT (guild_id, module) DO UPDATE SET disabled = $3 RETURNING id",
guildId,
module,
disabled,
).Scan(&id)
if err != nil {
state.Logger.Error("Failed to insert guild_module_configuration", zap.Error(err))
return uapi.HttpResponse{
Json: types.ApiError{
Message: "Failed to insert guild_module_configuration: " + err.Error(),
},
Status: http.StatusInternalServerError,
}
}
resps, err := state.AnimusMagicClient.Request(
d.Context,
state.Rueidis,
animusmagic.BotAnimusMessage{
ExecutePerModuleFunction: &struct {
Module string `json:"module"`
Toggle string `json:"toggle"`
Options map[string]any `json:"options,omitempty"`
}{
Module: "settings",
Toggle: "toggle_module",
Options: map[string]any{
"guild_id": guildId,
"module": module,
"enabled": !disabled,
},
},
},
&animusmagic.RequestOptions{
ClusterID: utils.Pointer(uint16(clusterId)),
},
)
if err != nil {
return uapi.HttpResponse{
Status: http.StatusInternalServerError,
Json: types.ApiError{
Message: "Error sending request to animus magic: " + err.Error(),
},
Headers: map[string]string{
"Retry-After": "10",
},
}
}
if len(resps) != 1 {
return uapi.HttpResponse{
Status: http.StatusInternalServerError,
Json: types.ApiError{
Message: "Error sending request to animus magic: [unexpected response count of " + strconv.Itoa(len(resps)) + "]",
},
Headers: map[string]string{
"Retry-After": "10",
},
}
}
return uapi.DefaultResponse(http.StatusNoContent)
}