Skip to content

Commit 657a230

Browse files
committed
chore: address comments
1 parent 80c6b22 commit 657a230

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

coderd/database/modelmethods.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,6 @@ func (u User) RBACObject() rbac.Object {
376376
return rbac.ResourceUserObject(u.ID)
377377
}
378378

379-
func (u User) IsSystemUser() bool {
380-
return u.IsSystem
381-
}
382-
383379
func (u GetUsersRow) RBACObject() rbac.Object {
384380
return rbac.ResourceUserObject(u.ID)
385381
}

coderd/notifications.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/http"
8+
"time"
89

910
"github.com/google/uuid"
1011

@@ -132,7 +133,7 @@ func (api *API) notificationTemplatesByKind(rw http.ResponseWriter, r *http.Requ
132133
templates, err := api.Database.GetNotificationTemplatesByKind(ctx, kind)
133134
if err != nil {
134135
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
135-
Message: fmt.Sprintf("Failed to retrieve '%s' notifications templates.", kind),
136+
Message: fmt.Sprintf("Failed to retrieve %q notifications templates.", kind),
136137
Detail: err.Error(),
137138
})
138139
return
@@ -386,7 +387,7 @@ func (api *API) postCustomNotification(rw http.ResponseWriter, r *http.Request)
386387
})
387388
return
388389
}
389-
if user.IsSystemUser() {
390+
if user.IsSystem {
390391
api.Logger.Error(ctx, "send custom notification: system user is not allowed",
391392
slog.F("id", user.ID.String()), slog.F("name", user.Name))
392393
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{
@@ -407,12 +408,12 @@ func (api *API) postCustomNotification(rw http.ResponseWriter, r *http.Request)
407408
},
408409
map[string]any{
409410
// Current dedupe is done via an hash of (template, user, method, payload, targets, day).
410-
// We intentionally include a timestamp to bypass the per-day dedupe so the caller can
411-
// resend identical content to themselves multiple times in one day.
412-
// TODO(ssncferreira): When we support sending custom notifications to multiple users/roles,
411+
// Include a minute-bucketed timestamp to bypass per-day dedupe for self-sends,
412+
// letting the caller resend identical content the same day (but not more than
413+
// once per minute).
414+
// TODO(ssncferreira): When custom notifications can target multiple users/roles,
413415
// enforce proper deduplication across recipients to reduce noise and prevent spam.
414-
// See https://github.com/coder/coder/issues/19768
415-
"dedupe_bypass_ts": api.Clock.Now().UTC(),
416+
"dedupe_bypass_ts": api.Clock.Now().UTC().Truncate(time.Minute),
416417
},
417418
user.ID.String(),
418419
); err != nil {

codersdk/notifications.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ type CustomNotificationRequest struct {
293293
// See: https://github.com/coder/coder/issues/19768
294294
}
295295

296+
const (
297+
maxCustomNotificationTitleLen = 120
298+
maxCustomNotificationMessageLen = 2000
299+
)
300+
296301
func (c CustomNotificationRequest) Validate() error {
297302
if c.Content == nil {
298303
return xerrors.Errorf("content is required")
@@ -301,6 +306,14 @@ func (c CustomNotificationRequest) Validate() error {
301306
strings.TrimSpace(c.Content.Message) == "" {
302307
return xerrors.Errorf("provide a non-empty 'content.title' or 'content.message'")
303308
}
309+
310+
if len(c.Content.Title) > maxCustomNotificationTitleLen {
311+
return xerrors.Errorf("'content.title' must be less than %d characters", maxCustomNotificationTitleLen)
312+
}
313+
if len(c.Content.Message) > maxCustomNotificationMessageLen {
314+
return xerrors.Errorf("'content.message' must be less than %d characters", maxCustomNotificationMessageLen)
315+
}
316+
304317
return nil
305318
}
306319

site/src/api/typesGenerated.ts

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)