listInboxNotifications lists the notifications for the user. @Summary List inbox notifications @ID list-inbox-notifications @Security CoderSessionToken @Produce json @Tags Notifications @Param targets query string false "Comma-separated list of target IDs to filter notifications" @Param templates qu
(rw http.ResponseWriter, r *http.Request)
| 288 | // @Success 200 {object} codersdk.ListInboxNotificationsResponse |
| 289 | // @Router /api/v2/notifications/inbox [get] |
| 290 | func (api *API) listInboxNotifications(rw http.ResponseWriter, r *http.Request) { |
| 291 | p := httpapi.NewQueryParamParser() |
| 292 | vals := r.URL.Query() |
| 293 | |
| 294 | var ( |
| 295 | ctx = r.Context() |
| 296 | apikey = httpmw.APIKey(r) |
| 297 | |
| 298 | targets = p.UUIDs(vals, nil, "targets") |
| 299 | templates = p.UUIDs(vals, nil, "templates") |
| 300 | readStatus = p.String(vals, "all", "read_status") |
| 301 | startingBefore = p.UUID(vals, uuid.Nil, "starting_before") |
| 302 | ) |
| 303 | p.ErrorExcessParams(vals) |
| 304 | if len(p.Errors) > 0 { |
| 305 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 306 | Message: "Query parameters have invalid values.", |
| 307 | Validations: p.Errors, |
| 308 | }) |
| 309 | return |
| 310 | } |
| 311 | |
| 312 | if !slices.Contains([]string{ |
| 313 | string(database.InboxNotificationReadStatusAll), |
| 314 | string(database.InboxNotificationReadStatusRead), |
| 315 | string(database.InboxNotificationReadStatusUnread), |
| 316 | }, readStatus) { |
| 317 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 318 | Message: "starting_before query parameter should be any of 'all', 'read', 'unread'.", |
| 319 | }) |
| 320 | return |
| 321 | } |
| 322 | |
| 323 | createdBefore := dbtime.Now() |
| 324 | if startingBefore != uuid.Nil { |
| 325 | lastNotif, err := api.Database.GetInboxNotificationByID(ctx, startingBefore) |
| 326 | if err == nil { |
| 327 | createdBefore = lastNotif.CreatedAt |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | notifs, err := api.Database.GetFilteredInboxNotificationsByUserID(ctx, database.GetFilteredInboxNotificationsByUserIDParams{ |
| 332 | UserID: apikey.UserID, |
| 333 | Templates: templates, |
| 334 | Targets: targets, |
| 335 | ReadStatus: database.InboxNotificationReadStatus(readStatus), |
| 336 | CreatedAtOpt: createdBefore, |
| 337 | }) |
| 338 | if err != nil { |
| 339 | api.Logger.Error(ctx, "failed to get filtered inbox notifications", slog.Error(err)) |
| 340 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 341 | Message: "Failed to get filtered inbox notifications.", |
| 342 | }) |
| 343 | return |
| 344 | } |
| 345 | |
| 346 | unreadCount, err := api.Database.CountUnreadInboxNotificationsByUserID(ctx, apikey.UserID) |
| 347 | if err != nil { |
nothing calls this directly
no test coverage detected