Skip to content

feat(coderd): add mark-all-as-read endpoint for inbox notifications #16976

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 12 commits into from
Mar 20, 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 dbauthz
  • Loading branch information
defelmnq committed Mar 18, 2025
commit 8aaace39d8c59ba068637128cca622f480f37a4b
4 changes: 2 additions & 2 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -3555,7 +3555,10 @@ func (q *querier) ListWorkspaceAgentPortShares(ctx context.Context, workspaceID
}

func (q *querier) MarkAllInboxNotificationsAsRead(ctx context.Context, arg database.MarkAllInboxNotificationsAsReadParams) error {
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceInboxNotification); err != nil {
resource := rbac.ResourceInboxNotification
resource.Owner = arg.UserID.String()

if err := q.authorizeContext(ctx, policy.ActionUpdate, resource); err != nil {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion coderd/database/queries/notificationsinbox.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ UPDATE
inbox_notifications
SET
read_at = $1
WHERE user_id = $2 and read_at IS NULL;
WHERE
user_id = $2 and read_at IS NULL;
6 changes: 3 additions & 3 deletions coderd/inboxnotifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ func (api *API) updateInboxNotificationReadStatus(rw http.ResponseWriter, r *htt
})
}

// markAllInboxNotificationsAsRead marks as read all unread notifications.
// @Summary Make all unread notifications as read
// @ID make-all-unread-notifications-as-read
// markAllInboxNotificationsAsRead marks as read all unread notifications for authenticated user.
// @Summary Mark all unread notifications as read
// @ID mark-all-unread-notifications-as-read
// @Security CoderSessionToken
// @Produce json
// @Tags Notifications
Expand Down
54 changes: 54 additions & 0 deletions coderd/inboxnotifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,57 @@ func TestInboxNotifications_ReadStatus(t *testing.T) {
require.Empty(t, updatedNotif.Notification)
})
}
func TestInboxNotifications_MarkAllAsRead(t *testing.T) {
t.Parallel()

// I skip these tests specifically on windows as for now they are flaky - only on Windows.
// For now the idea is that the runner takes too long to insert the entries, could be worth
// investigating a manual Tx.
if runtime.GOOS == "windows" {
t.Skip("our runners are randomly taking too long to insert entries")
}

t.Run("ok", func(t *testing.T) {
t.Parallel()
client, _, api := coderdtest.NewWithAPI(t, &coderdtest.Options{})
firstUser := coderdtest.CreateFirstUser(t, client)
client, member := coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

notifs, err := client.ListInboxNotifications(ctx, codersdk.ListInboxNotificationsRequest{})
require.NoError(t, err)
require.NotNil(t, notifs)
require.Equal(t, 0, notifs.UnreadCount)
require.Empty(t, notifs.Notifications)

for i := range 20 {
dbgen.NotificationInbox(t, api.Database, database.InsertInboxNotificationParams{
ID: uuid.New(),
UserID: member.ID,
TemplateID: notifications.TemplateWorkspaceOutOfMemory,
Title: fmt.Sprintf("Notification %d", i),
Actions: json.RawMessage("[]"),
Content: fmt.Sprintf("Content of the notif %d", i),
CreatedAt: dbtime.Now(),
})
}

notifs, err = client.ListInboxNotifications(ctx, codersdk.ListInboxNotificationsRequest{})
require.NoError(t, err)
require.NotNil(t, notifs)
require.Equal(t, 20, notifs.UnreadCount)
require.Len(t, notifs.Notifications, 20)

resp, err := client.MarkAllInboxNotificationsAsRead(ctx)
require.NoError(t, err)
require.Zero(t, resp.UnreadCount)

notifs, err = client.ListInboxNotifications(ctx, codersdk.ListInboxNotificationsRequest{})
require.NoError(t, err)
require.NotNil(t, notifs)
require.Equal(t, 0, notifs.UnreadCount)
require.Len(t, notifs.Notifications, 20)
})
}
2 changes: 1 addition & 1 deletion docs/reference/api/notifications.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading