Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c131d01

Browse files
chore: disallow inbox as default method (#17093)
Disallow setting `inbox` as the default notifications method.
1 parent cf10d98 commit c131d01

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

coderd/notifications/enqueuer.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package notifications
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"slices"
78
"strings"
89
"text/template"
@@ -25,6 +26,14 @@ var (
2526
ErrDuplicate = xerrors.New("duplicate notification")
2627
)
2728

29+
type InvalidDefaultNotificationMethodError struct {
30+
Method string
31+
}
32+
33+
func (e InvalidDefaultNotificationMethodError) Error() string {
34+
return fmt.Sprintf("given default notification method %q is invalid", e.Method)
35+
}
36+
2837
type StoreEnqueuer struct {
2938
store Store
3039
log slog.Logger
@@ -43,8 +52,13 @@ type StoreEnqueuer struct {
4352
// NewStoreEnqueuer creates an Enqueuer implementation which can persist notification messages in the store.
4453
func NewStoreEnqueuer(cfg codersdk.NotificationsConfig, store Store, helpers template.FuncMap, log slog.Logger, clock quartz.Clock) (*StoreEnqueuer, error) {
4554
var method database.NotificationMethod
46-
if err := method.Scan(cfg.Method.String()); err != nil {
47-
return nil, xerrors.Errorf("given notification method %q is invalid", cfg.Method)
55+
// TODO(DanielleMaywood):
56+
// Currently we do not want to allow setting `inbox` as the default notification method.
57+
// As of 2025-03-25, setting this to `inbox` would cause a crash on the deployment
58+
// notification settings page. Until we make a future decision on this we want to disallow
59+
// setting it.
60+
if err := method.Scan(cfg.Method.String()); err != nil || method == database.NotificationMethodInbox {
61+
return nil, InvalidDefaultNotificationMethodError{Method: cfg.Method.String()}
4862
}
4963

5064
return &StoreEnqueuer{

coderd/notifications/notifications_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,18 @@ func TestNotificationDuplicates(t *testing.T) {
18561856
require.NoError(t, err)
18571857
}
18581858

1859+
func TestNotificationMethodCannotDefaultToInbox(t *testing.T) {
1860+
t.Parallel()
1861+
1862+
store, _ := dbtestutil.NewDB(t)
1863+
logger := testutil.Logger(t)
1864+
1865+
cfg := defaultNotificationsConfig(database.NotificationMethodInbox)
1866+
1867+
_, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewMock(t))
1868+
require.ErrorIs(t, err, notifications.InvalidDefaultNotificationMethodError{Method: string(database.NotificationMethodInbox)})
1869+
}
1870+
18591871
func TestNotificationTargetMatrix(t *testing.T) {
18601872
t.Parallel()
18611873

0 commit comments

Comments
 (0)