forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscord_handler_validator.go
More file actions
156 lines (137 loc) · 4.42 KB
/
discord_handler_validator.go
File metadata and controls
156 lines (137 loc) · 4.42 KB
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
package validators
import (
"context"
"fmt"
"net/url"
"github.com/NdoleStudio/httpsms/pkg/discord"
"github.com/palantir/stacktrace"
"github.com/NdoleStudio/httpsms/pkg/requests"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
"github.com/thedevsaddam/govalidator"
)
// DiscordHandlerValidator validates models used in handlers.DiscordHandler
type DiscordHandlerValidator struct {
validator
client *discord.Client
logger telemetry.Logger
tracer telemetry.Tracer
}
// NewDiscordHandlerValidator creates a new handlers.DiscordHandler validator
func NewDiscordHandlerValidator(
logger telemetry.Logger,
tracer telemetry.Tracer,
client *discord.Client,
) (v *DiscordHandlerValidator) {
return &DiscordHandlerValidator{
logger: logger.WithService(fmt.Sprintf("%T", v)),
tracer: tracer,
client: client,
}
}
// ValidateIndex validates the requests.DiscordIndex request
func (validator *DiscordHandlerValidator) ValidateIndex(_ context.Context, request requests.DiscordIndex) url.Values {
v := govalidator.New(govalidator.Options{
Data: &request,
Rules: govalidator.MapData{
"limit": []string{
"required",
"numeric",
"min:1",
"max:100",
},
"skip": []string{
"required",
"numeric",
"min:0",
},
"query": []string{
"max:100",
},
},
})
return v.ValidateStruct()
}
// ValidateStore validates the requests.DiscordStore request
func (validator *DiscordHandlerValidator) ValidateStore(ctx context.Context, request requests.DiscordStore) url.Values {
ctx, span, ctxLogger := validator.tracer.StartWithLogger(ctx, validator.logger)
defer span.End()
v := govalidator.New(govalidator.Options{
Data: &request,
Rules: govalidator.MapData{
"name": []string{
"required",
"min:1",
"max:255",
},
"server_id": []string{
"required",
"numeric",
"max:255",
},
"incoming_channel_id": []string{
"required",
"max:255",
"numeric",
},
},
})
result := v.ValidateStruct()
if len(result) > 0 {
return result
}
if _, _, err := validator.client.Channel.Get(ctx, request.IncomingChannelID); err != nil {
msg := fmt.Sprintf("cannot fetch discord channel with ID [%s]", request.IncomingChannelID)
ctxLogger.Error(validator.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)))
result.Add("incoming_channel_id", fmt.Sprintf("cannot fetch discord channel with ID [%s] make sure the bot has access to the channel", request.IncomingChannelID))
}
if _, _, err := validator.client.Guild.Get(ctx, request.ServerID); err != nil {
msg := fmt.Sprintf("cannot fetch discord channel with ID [%s]", request.IncomingChannelID)
ctxLogger.Error(validator.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)))
result.Add("server_id", fmt.Sprintf("cannot fetch discord server with ID [%s] make sure the bot has access to the channel", request.ServerID))
}
return result
}
// ValidateUpdate validates the requests.DiscordUpdate request
func (validator *DiscordHandlerValidator) ValidateUpdate(ctx context.Context, request requests.DiscordUpdate) url.Values {
ctx, span, ctxLogger := validator.tracer.StartWithLogger(ctx, validator.logger)
defer span.End()
v := govalidator.New(govalidator.Options{
Data: &request,
Rules: govalidator.MapData{
"name": []string{
"required",
"min:1",
"max:255",
},
"server_id": []string{
"required",
"numeric",
"max:255",
},
"incoming_channel_id": []string{
"required",
"max:255",
"numeric",
},
"discordID": []string{
"required",
"uuid",
},
},
})
result := v.ValidateStruct()
if len(result) > 0 {
return result
}
if _, _, err := validator.client.Channel.Get(ctx, request.IncomingChannelID); err != nil {
msg := fmt.Sprintf("cannot fetch discord channel with ID [%s]", request.IncomingChannelID)
ctxLogger.Error(validator.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)))
result.Add("incoming_channel_id", fmt.Sprintf("cannot fetch discord channel with ID [%s] make sure the bot has access to the channel", request.IncomingChannelID))
}
if _, _, err := validator.client.Guild.Get(ctx, request.ServerID); err != nil {
msg := fmt.Sprintf("cannot fetch discord channel with ID [%s]", request.IncomingChannelID)
ctxLogger.Error(validator.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)))
result.Add("server_id", fmt.Sprintf("cannot fetch discord server with ID [%s] make sure the bot has access to the channel", request.ServerID))
}
return result
}