Skip to content

Commit 796bcd0

Browse files
committed
fix parameters validation
1 parent d72d1f2 commit 796bcd0

File tree

2 files changed

+31
-38
lines changed

2 files changed

+31
-38
lines changed

coderd/inboxnotifications.go

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,12 @@ func convertInboxNotificationParameters(ctx context.Context, logger slog.Logger,
5353
}
5454
}
5555

56+
readStatus := string(database.InboxNotificationReadStatusAll)
5657
if readStatusParam != "" {
57-
readOptions := []string{
58-
string(database.InboxNotificationReadStatusRead),
59-
string(database.InboxNotificationReadStatusUnread),
60-
string(database.InboxNotificationReadStatusAll),
61-
}
62-
63-
if !slices.Contains(readOptions, readStatusParam) {
64-
logger.Error(ctx, "unable to parse read status")
65-
return nil, nil, "", xerrors.New("unable to parse read status")
66-
}
58+
readStatus = readStatusParam
6759
}
6860

69-
return targets, templates, readStatusParam, nil
61+
return targets, templates, readStatus, nil
7062
}
7163

7264
// convertInboxNotificationResponse works as a util function to transform a database.InboxNotification to codersdk.InboxNotification
@@ -110,16 +102,18 @@ func convertInboxNotificationResponse(ctx context.Context, logger slog.Logger, n
110102
// @Success 200 {object} codersdk.GetInboxNotificationResponse
111103
// @Router /notifications/inbox/watch [get]
112104
func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request) {
113-
ctx := r.Context()
114105

115106
var (
116-
apikey = httpmw.APIKey(r)
117-
targetsParam = r.URL.Query().Get("targets")
118-
templatesParam = r.URL.Query().Get("templates")
119-
readStatusParam = r.URL.Query().Get("read_status")
107+
ctx = r.Context()
108+
apikey = httpmw.APIKey(r)
120109
)
121110

122-
targets, templates, readStatusParam, err := convertInboxNotificationParameters(ctx, api.Logger, targetsParam, templatesParam, readStatusParam)
111+
var req codersdk.WatchInboxNotificationsRequest
112+
if !httpapi.Read(ctx, rw, r, &req) {
113+
return
114+
}
115+
116+
targets, templates, readStatusParam, err := convertInboxNotificationParameters(ctx, api.Logger, req.Targets, req.Templates, req.Targets)
123117
if err != nil {
124118
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
125119
Message: "Invalid query parameter.",
@@ -233,17 +227,17 @@ func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request)
233227
// @Success 200 {object} codersdk.ListInboxNotificationsResponse
234228
// @Router /notifications/inbox [get]
235229
func (api *API) listInboxNotifications(rw http.ResponseWriter, r *http.Request) {
236-
ctx := r.Context()
237-
238230
var (
239-
apikey = httpmw.APIKey(r)
240-
targetsParam = r.URL.Query().Get("targets")
241-
templatesParam = r.URL.Query().Get("templates")
242-
readStatusParam = r.URL.Query().Get("read_status")
243-
startingBeforeParam = r.URL.Query().Get("starting_before")
231+
ctx = r.Context()
232+
apikey = httpmw.APIKey(r)
244233
)
245234

246-
targets, templates, readStatus, err := convertInboxNotificationParameters(ctx, api.Logger, targetsParam, templatesParam, readStatusParam)
235+
var req codersdk.ListInboxNotificationsRequest
236+
if !httpapi.Read(ctx, rw, r, &req) {
237+
return
238+
}
239+
240+
targets, templates, readStatus, err := convertInboxNotificationParameters(ctx, api.Logger, req.Targets, req.Templates, req.ReadStatus)
247241
if err != nil {
248242
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
249243
Message: "Invalid query parameter.",
@@ -253,15 +247,8 @@ func (api *API) listInboxNotifications(rw http.ResponseWriter, r *http.Request)
253247
}
254248

255249
startingBefore := dbtime.Now()
256-
if startingBeforeParam != "" {
257-
lastNotifID, err := uuid.Parse(startingBeforeParam)
258-
if err != nil {
259-
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
260-
Message: "Invalid starting before.",
261-
})
262-
return
263-
}
264-
lastNotif, err := api.Database.GetInboxNotificationByID(ctx, lastNotifID)
250+
if req.StartingBefore != uuid.Nil {
251+
lastNotif, err := api.Database.GetInboxNotificationByID(ctx, req.StartingBefore)
265252
if err != nil {
266253
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
267254
Message: "Failed to get notification by id.",

codersdk/inboxnotification.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,22 @@ type InboxNotificationAction struct {
3030
URL string `json:"url"`
3131
}
3232

33+
type WatchInboxNotificationsRequest struct {
34+
Targets string `json:"targets,omitempty"`
35+
Templates string `json:"templates,omitempty"`
36+
ReadStatus string `json:"read_status,omitempty" validate:"omitempty,oneof=read unread all"`
37+
}
38+
3339
type GetInboxNotificationResponse struct {
3440
Notification InboxNotification `json:"notification"`
3541
UnreadCount int `json:"unread_count"`
3642
}
3743

3844
type ListInboxNotificationsRequest struct {
39-
Targets []uuid.UUID
40-
Templates []uuid.UUID
41-
ReadStatus string
42-
StartingBefore uuid.UUID
45+
Targets string `json:"targets,omitempty"`
46+
Templates string `json:"templates,omitempty"`
47+
ReadStatus string `json:"read_status,omitempty" validate:"omitempty,oneof=read unread all"`
48+
StartingBefore uuid.UUID `json:"starting_before,omitempty" validate:"omitempty" format:"uuid"`
4349
}
4450

4551
type ListInboxNotificationsResponse struct {

0 commit comments

Comments
 (0)