@@ -53,20 +53,12 @@ func convertInboxNotificationParameters(ctx context.Context, logger slog.Logger,
53
53
}
54
54
}
55
55
56
+ readStatus := string (database .InboxNotificationReadStatusAll )
56
57
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
67
59
}
68
60
69
- return targets , templates , readStatusParam , nil
61
+ return targets , templates , readStatus , nil
70
62
}
71
63
72
64
// 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
110
102
// @Success 200 {object} codersdk.GetInboxNotificationResponse
111
103
// @Router /notifications/inbox/watch [get]
112
104
func (api * API ) watchInboxNotifications (rw http.ResponseWriter , r * http.Request ) {
113
- ctx := r .Context ()
114
105
115
106
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 )
120
109
)
121
110
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 )
123
117
if err != nil {
124
118
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
125
119
Message : "Invalid query parameter." ,
@@ -233,17 +227,17 @@ func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request)
233
227
// @Success 200 {object} codersdk.ListInboxNotificationsResponse
234
228
// @Router /notifications/inbox [get]
235
229
func (api * API ) listInboxNotifications (rw http.ResponseWriter , r * http.Request ) {
236
- ctx := r .Context ()
237
-
238
230
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 )
244
233
)
245
234
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 )
247
241
if err != nil {
248
242
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
249
243
Message : "Invalid query parameter." ,
@@ -253,15 +247,8 @@ func (api *API) listInboxNotifications(rw http.ResponseWriter, r *http.Request)
253
247
}
254
248
255
249
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 )
265
252
if err != nil {
266
253
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
267
254
Message : "Failed to get notification by id." ,
0 commit comments