Skip to content

feat: allow notification templates to be disabled by default #16093

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

Conversation

DanielleMaywood
Copy link
Contributor

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.

@DanielleMaywood DanielleMaywood marked this pull request as ready for review January 13, 2025 11:35
Copy link
Contributor

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

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

LGTM modulo a couple nits
Could you please deploy this to the preview env? I'd like to play around with the frontend.

Comment on lines 3 to 11
-- Disable 'workspace created' notification by default
UPDATE notification_templates
SET enabled_by_default = FALSE
WHERE id = '281fdf73-c6d6-4cbb-8ff5-888baf8a2fff';

-- Disable 'workspace manually updated' notification by default
UPDATE notification_templates
SET enabled_by_default = FALSE
WHERE id = 'd089fe7b-d5c5-4c0c-aaf5-689859f7d392';
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: this might've been better suited in its own migration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do! 👍

Comment on lines 33 to 36
AND (EXISTS (SELECT 1
FROM notification_templates
WHERE id = NEW.notification_template_id
AND enabled_by_default = FALSE)) THEN
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: could this not be an OR on the other branch?
If we're gonna keep this separate branch I'd rather we raised a more specific error since this one is no longer accurate.

Perhaps notification is not enabled covers both.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do!

So make this an OR to the other branch and rename the error message to notification is not enabled?

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks! Yes please, I think that'd be a more precise error message

Copy link

github-actions bot commented Jan 13, 2025


✔️ PR 16093 Updated successfully.
🚀 Access the credentials here.

cc: @DanielleMaywood

RAISE EXCEPTION 'cannot enqueue message: user has disabled this notification';
END IF;

-- Fails if the notification template is disabled by default and the
Copy link
Member

Choose a reason for hiding this comment

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

pinging @mafredri to review SQL changes

Comment on lines 7 to 26
-- Fail the insertion if one of the following:
-- * the user has disabled this notification.
-- * the notification template is disabled by default and hasn't
-- been explicitly enabled by the user.
IF EXISTS (SELECT 1
FROM notification_preferences
WHERE disabled = TRUE
AND user_id = NEW.user_id
AND notification_template_id = NEW.notification_template_id)
OR (NOT EXISTS (SELECT 1
FROM notification_preferences
WHERE disabled = FALSE
AND user_id = NEW.user_id
AND notification_template_id = NEW.notification_template_id))
AND (EXISTS (SELECT 1
FROM notification_templates
WHERE id = NEW.notification_template_id
AND enabled_by_default = FALSE) ) THEN
RAISE EXCEPTION 'cannot enqueue message: notification is not enabled';
END IF;
Copy link
Member

@mafredri mafredri Jan 13, 2025

Choose a reason for hiding this comment

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

How about simplifying this with a join instead?

Suggested change
-- Fail the insertion if one of the following:
-- * the user has disabled this notification.
-- * the notification template is disabled by default and hasn't
-- been explicitly enabled by the user.
IF EXISTS (SELECT 1
FROM notification_preferences
WHERE disabled = TRUE
AND user_id = NEW.user_id
AND notification_template_id = NEW.notification_template_id)
OR (NOT EXISTS (SELECT 1
FROM notification_preferences
WHERE disabled = FALSE
AND user_id = NEW.user_id
AND notification_template_id = NEW.notification_template_id))
AND (EXISTS (SELECT 1
FROM notification_templates
WHERE id = NEW.notification_template_id
AND enabled_by_default = FALSE) ) THEN
RAISE EXCEPTION 'cannot enqueue message: notification is not enabled';
END IF;
IF EXISTS (
SELECT 1
FROM notification_templates nt
LEFT JOIN notification_preferences np
ON np.notification_template_id = nt.id
AND np.user_id = NEW.user_id
WHERE nt.id = NEW.notification_template_id
AND (
-- Case 1: The user explicitly disabled this template
np.disabled = TRUE
OR
-- Case 2: Template is disabled by default
-- AND user never explicitly enabled it
(np.notification_template_id IS NULL AND nt.enabled_by_default = FALSE)
)
)
THEN
RAISE EXCEPTION 'cannot enqueue message: notification is not enabled';
END IF;

I feel this is a bit more readable and concise. What do you think? (PS. Feel free to double check the logic, I actually used ChatGPT to rewrite it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I agree with you, this definitely feels easier (at least for me) to read

@DanielleMaywood DanielleMaywood merged commit 009069c into main Jan 13, 2025
34 of 36 checks passed
@DanielleMaywood DanielleMaywood deleted the dm-allow-notification-templates-to-be-disabled-by-default branch January 13, 2025 15:01
@github-actions github-actions bot locked and limited conversation to collaborators Jan 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants