Skip to content

Commit 009069c

Browse files
feat: allow notification templates to be disabled by default (#16093)
Change as part of #16071 It has been decided that we want to be able to have some notification templates be disabled _by default_ #16071 (comment). This adds a new column (`enabled_by_default`) to `notification_templates` that defaults to `TRUE`. It also modifies the `inhibit_enqueue_if_disabled` function to reject notifications for templates that have `enabled_by_default = FALSE` with the user not explicitly enabling it.
1 parent 22236f2 commit 009069c

20 files changed

+231
-62
lines changed

coderd/apidoc/docs.go

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

coderd/apidoc/swagger.json

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

coderd/database/dump.sql

Lines changed: 20 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
ALTER TABLE notification_templates DROP COLUMN enabled_by_default;
2+
3+
CREATE OR REPLACE FUNCTION inhibit_enqueue_if_disabled()
4+
RETURNS TRIGGER AS
5+
$$
6+
BEGIN
7+
-- Fail the insertion if the user has disabled this notification.
8+
IF EXISTS (SELECT 1
9+
FROM notification_preferences
10+
WHERE disabled = TRUE
11+
AND user_id = NEW.user_id
12+
AND notification_template_id = NEW.notification_template_id) THEN
13+
RAISE EXCEPTION 'cannot enqueue message: user has disabled this notification';
14+
END IF;
15+
16+
RETURN NEW;
17+
END;
18+
$$ LANGUAGE plpgsql;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
ALTER TABLE notification_templates ADD COLUMN enabled_by_default boolean DEFAULT TRUE NOT NULL;
2+
3+
CREATE OR REPLACE FUNCTION inhibit_enqueue_if_disabled()
4+
RETURNS TRIGGER AS
5+
$$
6+
BEGIN
7+
-- Fail the insertion if one of the following:
8+
-- * the user has disabled this notification.
9+
-- * the notification template is disabled by default and hasn't
10+
-- been explicitly enabled by the user.
11+
IF EXISTS (
12+
SELECT 1 FROM notification_templates
13+
LEFT JOIN notification_preferences
14+
ON notification_preferences.notification_template_id = notification_templates.id
15+
AND notification_preferences.user_id = NEW.user_id
16+
WHERE notification_templates.id = NEW.notification_template_id AND (
17+
-- Case 1: The user has explicitly disabled this template
18+
notification_preferences.disabled = TRUE
19+
OR
20+
-- Case 2: The template is disabled by default AND the user hasn't enabled it
21+
(notification_templates.enabled_by_default = FALSE AND notification_preferences.notification_template_id IS NULL)
22+
)
23+
) THEN
24+
RAISE EXCEPTION 'cannot enqueue message: notification is not enabled';
25+
END IF;
26+
27+
RETURN NEW;
28+
END;
29+
$$ LANGUAGE plpgsql;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Enable 'workspace created' notification by default
2+
UPDATE notification_templates
3+
SET enabled_by_default = TRUE
4+
WHERE id = '281fdf73-c6d6-4cbb-8ff5-888baf8a2fff';
5+
6+
-- Enable 'workspace manually updated' notification by default
7+
UPDATE notification_templates
8+
SET enabled_by_default = TRUE
9+
WHERE id = 'd089fe7b-d5c5-4c0c-aaf5-689859f7d392';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Disable 'workspace created' notification by default
2+
UPDATE notification_templates
3+
SET enabled_by_default = FALSE
4+
WHERE id = '281fdf73-c6d6-4cbb-8ff5-888baf8a2fff';
5+
6+
-- Disable 'workspace manually updated' notification by default
7+
UPDATE notification_templates
8+
SET enabled_by_default = FALSE
9+
WHERE id = 'd089fe7b-d5c5-4c0c-aaf5-689859f7d392';

coderd/database/models.go

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

coderd/database/queries.sql.go

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

coderd/notifications.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,15 @@ func (api *API) putUserNotificationPreferences(rw http.ResponseWriter, r *http.R
271271
func convertNotificationTemplates(in []database.NotificationTemplate) (out []codersdk.NotificationTemplate) {
272272
for _, tmpl := range in {
273273
out = append(out, codersdk.NotificationTemplate{
274-
ID: tmpl.ID,
275-
Name: tmpl.Name,
276-
TitleTemplate: tmpl.TitleTemplate,
277-
BodyTemplate: tmpl.BodyTemplate,
278-
Actions: string(tmpl.Actions),
279-
Group: tmpl.Group.String,
280-
Method: string(tmpl.Method.NotificationMethod),
281-
Kind: string(tmpl.Kind),
274+
ID: tmpl.ID,
275+
Name: tmpl.Name,
276+
TitleTemplate: tmpl.TitleTemplate,
277+
BodyTemplate: tmpl.BodyTemplate,
278+
Actions: string(tmpl.Actions),
279+
Group: tmpl.Group.String,
280+
Method: string(tmpl.Method.NotificationMethod),
281+
Kind: string(tmpl.Kind),
282+
EnabledByDefault: tmpl.EnabledByDefault,
282283
})
283284
}
284285

0 commit comments

Comments
 (0)