10000 feat(coderd): add inbox notifications endpoints by defelmnq · Pull Request #16889 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat(coderd): add inbox notifications endpoints #16889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix parameters validation
  • Loading branch information
defelmnq committed Mar 13, 2025
commit 796bcd07d58f0c955a6d73a44722bd221ca1e51e
55 changes: 21 additions & 34 deletions coderd/inboxnotifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,12 @@ func convertInboxNotificationParameters(ctx context.Context, logger slog.Logger,
}
}

readStatus := string(database.InboxNotificationReadStatusAll)
if readStatusParam != "" {
readOptions := []string{
string(database.InboxNotificationReadStatusRead),
string(database.InboxNotificationReadStatusUnread),
string(database.InboxNotificationReadStatusAll),
}

if !slices.Contains(readOptions, readStatusParam) {
logger.Error(ctx, "unable to parse read status")
return nil, nil, "", xerrors.New("unable to parse read status")
}
readStatus = readStatusParam
}

return targets, templates, readStatusParam, nil
return targets, templates, readStatus, nil
}

// convertInboxNotificationResponse works as a util function to transform a database.InboxNotification to codersdk.InboxNotification
Expand Down Expand Up @@ -110,16 +102,18 @@ func convertInboxNotificationResponse(ctx context.Context, logger slog.Logger, n
// @Success 200 {object} codersdk.GetInboxNotificationResponse
// @Router /notifications/inbox/watch [get]
func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bet you want to write some tests for this API endpoint 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some tests to cover most of the endpoint. ✅

ctx := r.Context()

var (
apikey = httpmw.APIKey(r)
targetsParam = r.URL.Query().Get("targets")
templatesParam = r.URL.Query().Get("templates")
readStatusParam = r.URL.Query().Get("read_status")
ctx = r.Context()
apikey = httpmw.APIKey(r)
)

targets, templates, readStatusParam, err := convertInboxNotificationParameters(ctx, api.Logger, targetsParam, templatesParam, readStatusParam)
var req codersdk.WatchInboxNotificationsRequest
if !httpapi.Read(ctx, rw, r, &req) {
return
}

targets, templates, readStatusParam, err := convertInboxNotificationParameters(ctx, api.Logger, req.Targets, req.Templates, req.Targets)
if err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid query parameter.",
Expand Down Expand Up @@ -233,17 +227,17 @@ func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request)
// @Success 200 {object} codersdk.ListInboxNotificationsResponse
// @Router /notifications/inbox [get]
func (api *API) listInboxNotifications(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()

var (
apikey = httpmw.APIKey(r)
targetsParam = r.URL.Query().Get("targets")
templatesParam = r.URL.Query().Get("templates")
readStatusParam = r.URL.Query().Get("read_status")
startingBeforeParam = r.URL.Query().Get("starting_before")
ctx = r.Context()
apikey = httpmw.APIKey(r)
)

targets, templates, readStatus, err := convertInboxNotificationParameters(ctx, api.Logger, targetsParam, templatesParam, readStatusParam)
var req codersdk.ListInboxNotificationsRequest
if !httpapi.Read(ctx, rw, r, &req) {
return
}

targets, templates, readStatus, err := convertInboxNotificationParameters(ctx, api.Logger, req.Targets, req.Templates, req.ReadStatus)
if err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid query parameter.",
Expand All @@ -253,15 +247,8 @@ func (api *API) listInboxNotifications(rw http.ResponseWriter, r *http.Request)
}

startingBefore := dbtime.Now()
if startingBeforeParam != "" {
lastNotifID, err := uuid.Parse(startingBeforeParam)
if err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid starting before.",
})
return
}
lastNotif, err := api.Database.GetInboxNotificationByID(ctx, lastNotifID)
if req.StartingBefore != uuid.Nil {
lastNotif, err := api.Database.GetInboxNotificationByID(ctx, req.StartingBefore)
if err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Failed to get notification by id.",
Expand Down
14 changes: 10 additions & 4 deletions codersdk/inboxnotification.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@
URL string `json:"url"`
}

type WatchInboxNotificationsRequest struct {
Targets string `json:"targets,omitempty"`
Templates string `json:"templates,omitempty"`
ReadStatus string `json:"read_status,omitempty" validate:"omitempty,oneof=read unread all"`
}

type GetInboxNotificationResponse struct {
Notification InboxNotification `json:"notification"`
UnreadCount int `json:"unread_count"`
}

type ListInboxNotificationsRequest struct {
Targets []uuid.UUID
Templates []uuid.UUID
ReadStatus string
StartingBefore uuid.UUID
Targets string `json:"targets,omitempty"`
Templates string `json:"templates,omitempty"`
ReadStatus string `json:"read_status,omitempty" validate:"omitempty,oneof=read unread all"`
StartingBefore uuid.UUID `json:"starting_before,omitempty" validate:"omitempty" format:"uuid"`
}

type ListInboxNotificationsResponse struct {
Expand All @@ -50,10 +56,10 @@
func ListInboxNotificationsRequestToQueryParams(req ListInboxNotificationsRequest) []RequestOption {
var opts []RequestOption
if len(req.Targets) > 0 {
opts = append(opts, WithQueryParam("targets", utiluuid.FromSliceToString(req.Targets, ",")))

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / gen

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / offlinedocs

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-e2e-premium

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-e2e

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / lint

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / lint

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / lint

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / lint

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / build-dylib

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-cli (macos-latest)

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-cli (macos-latest)

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-race

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-race

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 59 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

cannot use req.Targets (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString
}
if len(req.Templates) > 0 {
opts = append(opts, WithQueryParam("templates", utiluuid.FromSliceToString(req.Templates, ",")))

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / gen

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / offlinedocs

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-e2e-premium

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-e2e

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / lint

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / lint

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString) (typecheck)

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / lint

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString) (typecheck)

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / build-dylib

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-cli (macos-latest)

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-cli (macos-latest)

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-race

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-race

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString

Check failure on line 62 in codersdk/inboxnotification.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

cannot use req.Templates (variable of type string) as []"github.com/google/uuid".UUID value in argument to utiluuid.FromSliceToString
}
if req.ReadStatus != "" {
opts = append(opts, WithQueryParam("read_status", req.ReadStatus))
Expand Down
Loading
0