Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Remove links from all alerts/subscriptions related emails in modular embeddings (#69014)

Mahatthana (Kelvin) Nomsawadi2028e4af8cff
diff --git a/src/metabase/channel/email/_header.hbs b/src/metabase/channel/email/_header.hbs
index dd0634fcdd4..ff77b605b42 100644
--- a/src/metabase/channel/email/_header.hbs
+++ b/src/metabase/channel/email/_header.hbs
@@ -1,7 +1,8 @@
<html>
<head>
- <link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,900;1,100;1,300;1,400;1,700;1,900" rel="stylesheet">
<style>
+ @import url("https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,900;1,100;1,300;1,400;1,700;1,900");
+
* {
mso-line-height-rule: exactly;
}
diff --git a/src/metabase/channel/email/broken_subscription_notification.hbs b/src/metabase/channel/email/broken_subscription_notification.hbs
index 7f731d6d85e..fccdadd210f 100644
--- a/src/metabase/channel/email/broken_subscription_notification.hbs
+++ b/src/metabase/channel/email/broken_subscription_notification.hbs
@@ -65,6 +65,7 @@
{{/each}}
</table>
+ {{#unless disable_links}}
<div style="text-align: center;">
<a
href="{{{dashboardUrl}}}"
@@ -77,5 +78,6 @@
<div
style="height: 1px; width: 100%; background-color: #F0F0F0; margin-top: 2em;"
></div>
+ {{/unless}}
</div>
{{> metabase/channel/email/_footer.hbs }}
diff --git a/src/metabase/channel/email/card_notification_archived.hbs b/src/metabase/channel/email/card_notification_archived.hbs
index 2a5841ead06..f92a68446f8 100644
--- a/src/metabase/channel/email/card_notification_archived.hbs
+++ b/src/metabase/channel/email/card_notification_archived.hbs
@@ -1,4 +1,4 @@
{{> metabase/channel/email/_header.hbs }}
<p>Alerts about {{card.name}} (#{{card.id}}) have stopped because the question was archived by {{actor.first_name}} {{actor.last_name}}.</p>
- <p>If you want to restore the alert, go to the <a href="{{{trash-url}}}">trash</a>, restore the question and re-create the alert.</p>
+ {{#unless disable_links}}<p>If you want to restore the alert, go to the <a href="{{{trash-url}}}">trash</a>, restore the question and re-create the alert.</p>{{/unless}}
{{> metabase/channel/email/_footer.hbs }}
diff --git a/src/metabase/channel/email/card_notification_changed_stopped.hbs b/src/metabase/channel/email/card_notification_changed_stopped.hbs
index c010fc3814c..e98ced24626 100644
--- a/src/metabase/channel/email/card_notification_changed_stopped.hbs
+++ b/src/metabase/channel/email/card_notification_changed_stopped.hbs
@@ -1,3 +1,3 @@
{{> metabase/channel/email/_header.hbs }}
- <p>Alerts about <a href="{{{card-url card.id}}}">{{card.name}}</a> have stopped because the question was edited by {{actor.first_name}} {{actor.last_name}}.</p>
+ <p>Alerts about <a {{#unless disable_links}}href="{{{card-url card.id}}}"{{/unless}}>{{card.name}}</a> have stopped because the question was edited by {{actor.first_name}} {{actor.last_name}}.</p>
{{> metabase/channel/email/_footer.hbs }}
diff --git a/src/metabase/channel/email/messages.clj b/src/metabase/channel/email/messages.clj
index 84806b844fa..3efb3f553de 100644
--- a/src/metabase/channel/email/messages.clj
+++ b/src/metabase/channel/email/messages.clj
@@ -352,23 +352,33 @@
(defn send-alert-stopped-because-archived-email!
"Email to notify users when a card associated to their alert has been archived"
- [card recipient-emails archiver]
+ [card recipient-emails recipient-emails-with-no-links archiver]
(send-email! recipient-emails not-working-subject archived-template
{:card card
:actor archiver}
+ true)
+ (send-email! recipient-emails-with-no-links not-working-subject archived-template
+ {:card card
+ :actor archiver
+ :disable_links true}
true))
(defn send-alert-stopped-because-changed-email!
"Email to notify users when a card associated to their alert changed in a way that invalidates their alert"
- [card recipient-emails archiver]
+ [card recipient-emails recipient-emails-with-no-links archiver]
(send-email! recipient-emails not-working-subject changed-stopped-template
{:card card
:actor archiver}
+ true)
+ (send-email! recipient-emails-with-no-links not-working-subject changed-stopped-template
+ {:card card
+ :actor archiver
+ :disable_links true}
true))
(defn send-broken-subscription-notification!
"Email dashboard and subscription creators information about a broken subscription due to bad parameters"
- [{:keys [dashboard-id dashboard-name pulse-creator dashboard-creator affected-users bad-parameters]}]
+ [{:keys [dashboard-id dashboard-name pulse-creator dashboard-creator affected-users bad-parameters disable_links]}]
(let [{:keys [siteUrl] :as context} (common-context)]
(email/send-message!
:subject (trs "Subscription to {0} removed" dashboard-name)
@@ -399,4 +409,5 @@
:recipient (:common_name pulse-creator)
:role "Subscription Creator"}]
(map #(assoc % :role "Subscriber") affected-users)))
- :dashboardUrl (format "%s/dashboard/%s" siteUrl dashboard-id)})))))
+ :dashboardUrl (format "%s/dashboard/%s" siteUrl dashboard-id)
+ :disable_links disable_links})))))
diff --git a/src/metabase/channel/email/notification_card_new_confirmation.hbs b/src/metabase/channel/email/notification_card_new_confirmation.hbs
index 978b20787ff..7b9f90c9348 100644
--- a/src/metabase/channel/email/notification_card_new_confirmation.hbs
+++ b/src/metabase/channel/email/notification_card_new_confirmation.hbs
@@ -1,4 +1,4 @@
{{> metabase/channel/email/_header.hbs }}
- <p>This is just a confirmation that your alert about <a href="{{{card-url payload.event_info.object.payload.card_id}}}">{{payload.event_info.object.payload.card.name}}</a> has been set up.</p>
+ <p>This is just a confirmation that your alert about <a {{#unless payload.event_info.object.payload.disable_links}}href="{{{card-url payload.event_info.object.payload.card_id}}}"{{/unless}}>{{payload.event_info.object.payload.card.name}}</a> has been set up.</p>
<p>This alert will be sent {{> metabase/channel/email/_notification_card_condition.hbs send_condition=payload.event_info.object.payload.send_condition}}.</p>
{{> metabase/channel/email/_footer.hbs}}
diff --git a/src/metabase/channel/email/notification_card_you_were_removed.hbs b/src/metabase/channel/email/notification_card_you_were_removed.hbs
index 5972e71edd8..26eb52b2b10 100644
--- a/src/metabase/channel/email/notification_card_you_were_removed.hbs
+++ b/src/metabase/channel/email/notification_card_you_were_removed.hbs
@@ -1,3 +1,3 @@
{{> metabase/channel/email/_header.hbs }}
- <p>Just letting you know that {{actor_name}} unsubscribed you from alerts about <a href="{{{card-url payload.card_id}}}">{{payload.card.name}}</a>.</p>
+ <p>Just letting you know that {{actor_name}} unsubscribed you from alerts about <a {{#unless payload.disable_links}}href="{{{card-url payload.card_id}}}"{{/unless}}>{{payload.card.name}}</a>.</p>
{{> metabase/channel/email/_footer.hbs }}
diff --git a/src/metabase/dashboards_rest/api.clj b/src/metabase/dashboards_rest/api.clj
index fd4edb511ff..6c30b3db568 100644
--- a/src/metabase/dashboards_rest/api.clj
+++ b/src/metabase/dashboards_rest/api.clj
@@ -939,7 +939,9 @@
:dashboard-id dashboard-id
:dashboard-name dashboard-name
:dashboard-description dashboard-description
- :dashboard-creator (select-keys dashboard-creator [:first_name :last_name :email :common_name]))))))
+ :dashboard-creator (select-keys dashboard-creator [:first_name :last_name :email :common_name])
+ ;; We will not include links to Metabase for subscriptions created in modular embeddings
+ :disable_links (:disable_links broken-pulse))))))
(defn- handle-broken-subscriptions
"Given a dashboard id and original parameters, determine if any of the subscriptions are broken (we've removed params
diff --git a/src/metabase/queries/events/cards_notification_deleted_on_card_save.clj b/src/metabase/queries/events/cards_notification_deleted_on_card_save.clj
index f75df11e18d..a3bf254305f 100644
--- a/src/metabase/queries/events/cards_notification_deleted_on_card_save.clj
+++ b/src/metabase/queries/events/cards_notification_deleted_on_card_save.clj
@@ -22,6 +22,7 @@
:event/card-update.notification-deleted.card-changed
messages/send-alert-stopped-because-changed-email!)
recipients (->> notifications
+ (filter #((complement true?) (get-in % [:payload :disable_links])))
(mapcat :handlers)
(filter #(= :channel/email (:channel_type %)))
(mapcat :recipients)
@@ -31,8 +32,21 @@
(-> recipient :user :email)
:notification-recipient/raw-value
(-> recipient :details :value)
- (throw (ex-info "Unknown recipient type" {:recipient recipient}))))))]
- (when (seq recipients)
- (send-message! card recipients actor)))
+ (throw (ex-info "Unknown recipient type" {:recipient recipient}))))))
+
+ recipients-with-no-links (->> notifications
+ (filter #(true? (get-in % [:payload :disable_links])))
+ (mapcat :handlers)
+ (filter #(= :channel/email (:channel_type %)))
+ (mapcat :recipients)
+ (keep (fn [recipient]
+ (case (:type recipient)
+ :notification-recipient/user
+ (-> recipient :user :email)
+ :notification-recipient/raw-value
+ (-> recipient :details :value)
+ (throw (ex-info "Unknown recipient type" {:recipient recipient}))))))]
+
+ (send-message! card recipients recipients-with-no-links actor))
(catch Throwable e
(log/error e "Error sending notification email"))))
diff --git a/test/metabase/dashboards_rest/api_test.clj b/test/metabase/dashboards_rest/api_test.clj
index a3491800c64..8044527507a 100644
--- a/test/metabase/dashboards_rest/api_test.clj
+++ b/test/metabase/dashboards_rest/api_test.clj
@@ -4540,7 +4540,8 @@
(is (true? (:archived (models.pulse/update-pulse! {:id bad-pulse-id :archived true})))))))))))
(deftest handle-broken-subscriptions-due-to-bad-parameters-test
- (testing "When a subscriptions is broken, archive it and notify the dashboard and subscription creator (#30100)"
+ (defn- test-handle-broken-subscription-notification!
+ [{:keys [disable-links? email-body-pattern match-email-body-pattern?]}]
(let [param {:name "Source"
:slug "source"
:id "_SOURCE_PARAM_ID_"
@@ -4571,10 +4572,11 @@
[:field (mt/id :people :source)
{:base-type :type/Text}]]}]}
- :model/Pulse {bad-pulse-id :id} {:name "Bad Pulse"
- :dashboard_id dash-id
- :creator_id (mt/user->id :trashbird)
- :parameters [(assoc param :value ["Twitter", "Facebook"])]}
+ :model/Pulse {bad-pulse-id :id} {:name "Bad Pulse"
+ :dashboard_id dash-id
+ :creator_id (mt/user->id :trashbird)
+ :parameters [(assoc param :value ["Twitter", "Facebook"])]
+ :disable_links disable-links?}
:model/PulseCard _ {:pulse_id bad-pulse-id
:card_id card-id
:dashboard_card_id dash-card-id}
@@ -4586,10 +4588,11 @@
:model/PulseChannelRecipient _ {:pulse_channel_id pulse-channel-id
:user_id (mt/user->id :crowberto)}
;; Broken slack pulse
- :model/Pulse {bad-slack-pulse-id :id} {:name "Bad Slack Pulse"
- :dashboard_id dash-id
- :creator_id (mt/user->id :trashbird)
- :parameters [(assoc param :value ["LinkedIn"])]}
+ :model/Pulse {bad-slack-pulse-id :id} {:name "Bad Slack Pulse"
+ :dashboard_id dash-id
+ :creator_id (mt/user->id :trashbird)
+ :parameters [(assoc param :value ["LinkedIn"])]
+ :disable_links disable-links?}
:model/PulseCard _ {:pulse_id bad-slack-pulse-id
:card_id card-id
:dashboard_card_id dash-card-id}
@@ -4630,8 +4633,8 @@
(is (true? (some-> (get-in inbox [recipient-email 0 :body 0 :content])
(str/includes? title)))))
(testing "The second email (about the broken slack pulse) was received"
- (is (true? (some-> (get-in inbox [recipient-email 1 :body 0 :content])
- (str/includes? "#my-channel"))))))]
+ (is (= match-email-body-pattern? (some-> (get-in inbox [recipient-email 1 :body 0 :content])
+ (str/includes? email-body-pattern))))))]
(testing "The dashboard parameters were removed"
(is (empty? parameters)))
(testing "The broken pulse was archived"
@@ -4643,7 +4646,25 @@
(set (keys inbox)))))
(testing "Notification emails were sent to the dashboard and pulse creators"
(emails-received? "[email protected]")
- (emails-received? "[email protected]")))))))))
+ (emails-received? "[email protected]"))))))))
+
+ (testing "When a subscriptions is broken, archive it and notify the dashboard and subscription creator (#30100)"
+ (test-handle-broken-subscription-notification!
+ {:disable-links? false
+ :email-body-pattern "#my-channel"
+ :match-email-body-pattern? true}))
+
+ (testing "When a subscriptions is broken, archive it and notify the dashboard and subscription creator (#30100) with email links when disable_links: false"
+ (test-handle-broken-subscription-notification!
+ {:disable-links? false
+ :email-body-pattern "href="
+ :match-email-body-pattern? true}))
+
+ (testing "When a subscriptions is broken, archive it and notify the dashboard and subscription creator (#30100) without email links when disable_links: true"
+ (test-handle-broken-subscription-notification!
+ {:disable-links? true
+ :email-body-pattern "href="
+ :match-email-body-pattern? false})))
(deftest run-mlv2-dashcard-query-test
(testing "POST /api/dashboard/:dashboard-id/dashcard/:dashcard-id/card/:card-id"
diff --git a/test/metabase/notification/api/notification_test.clj b/test/metabase/notification/api/notification_test.clj
index 6cd103d7ed5..e7b5a5f7bdc 100644
--- a/test/metabase/notification/api/notification_test.clj
+++ b/test/metabase/notification/api/notification_test.clj
@@ -1021,6 +1021,21 @@
:expected-subject "You’ve been unsubscribed from an alert"
:card-url-tag card-url-tag))))
+ (testing "when notification is archived (active -> inactive) with disable_links value:"
+ (let [has-link? (fn [disable_links]
+ (notification.tu/with-card-notification
+ [{noti-id :id :as notification} (assoc-in base-notification [:notification-card :disable_links] disable_links)]
+ (->> (update-notification! noti-id notification {:active false})
+ first :body first :content
+ (re-find #"href=")
+ (= "href="))))]
+ (testing "false will keep links in the alert unsubscribe email"
+ (is (true? (has-link? false))))
+ (testing "nil will keep links in the alert unsubscribe email"
+ (is (true? (has-link? nil))))
+ (testing "true will remove all links in the alert unsubscribe email"
+ (is (false? (has-link? true))))))
+
(testing "when notification is unarchived (inactive -> active)"
(notification.tu/with-card-notification
[{noti-id :id :as notification} (assoc-in base-notification [:notification :active] false)]
diff --git a/test/metabase/notification/payload/impl/system_event_test.clj b/test/metabase/notification/payload/impl/system_event_test.clj
index 5cf7bc5b0b0..16208c8d680 100644
--- a/test/metabase/notification/payload/impl/system_event_test.clj
+++ b/test/metabase/notification/payload/impl/system_event_test.clj
@@ -224,7 +224,25 @@
#"This alert will be sent\s+when this question meets its goal"]
[:goal_below
#"This alert will be sent\s+when this question goes below its goal"]]]
- (check send-condition condition-regex)))))
+ (check send-condition condition-regex))))
+
+ (notification.tu/with-notification-testing-setup!
+ (notification.tu/with-card-notification
+ [notification {:card {:name "A Card"}
+ :notification {:creator_id (mt/user->id :rasta)}}]
+ (let [has-link? (fn [notification]
+ (->> (notification.tu/with-captured-channel-send!
+ (events/publish-event! :event/notification-create {:object notification
+ :user-id (:id (mt/user->id :rasta))}))
+ :channel/email first :message first :content
+ (re-find #"href=")
+ (= "href=")))]
+ (testing "test that disable_links: false will keep links in the alert confirmation email"
+ (is (true? (has-link? (assoc-in notification [:payload :disable_links] false)))))
+ (testing "test that disable_links: nil will keep links in the alert confirmation email"
+ (is (true? (has-link? (assoc-in notification [:payload :disable_links] nil)))))
+ (testing "test that disable_links: true will disable all links in the alert confirmation email"
+ (is (false? (has-link? (assoc-in notification [:payload :disable_links] true)))))))))
(deftest slack-error-token-email-test
(let [check (fn [recipients regexes]
diff --git a/test/metabase/queries_rest/api/card_test.clj b/test/metabase/queries_rest/api/card_test.clj
index f9bad7dbe7a..d404aef3c84 100644
--- a/test/metabase/queries_rest/api/card_test.clj
+++ b/test/metabase/queries_rest/api/card_test.clj
@@ -2003,7 +2003,7 @@
;;; | Card updates that impact alerts |
;;; +----------------------------------------------------------------------------------------------------------------+
-(defn- test-alert-deletion! [{:keys [message card deleted? expected-email-re f]}]
+(defn- test-alert-deletion! [{:keys [message card deleted? disable-links? expected-email-re should-re-not-match? f]}]
(testing message
(notification.tu/with-channel-fixtures [:channel/email]
(api.notification-test/with-send-messages-sync!
@@ -2015,13 +2015,14 @@
{:type :notification-recipient/user
:user_id (mt/user->id :rasta)}
{:type :notification-recipient/raw-value
- :details {:value "[email protected]"}}]}]}]
+ :details {:value "[email protected]"}}]}]
+ :notification-card {:disable_links (boolean disable-links?)}}]
(when deleted?
(let [[email] (notification.tu/with-mock-inbox-email!
(f (->> notification :payload :card_id (t2/select-one :model/Card))))]
:subject "One of your alerts has stopped working"
- :body [{(str expected-email-re) true}]}
+ :body [{(str expected-email-re) (not should-re-not-match?)}]}
(mt/summarize-multipart-single-email email expected-email-re)))))
(if deleted?
(is (not (t2/exists? :model/Notification :id (:id notification)))
@@ -2035,7 +2036,25 @@
:deleted? true
:expected-email-re #"Alerts about [A-Za-z]+ \(#\d+\) have stopped because the question was archived by Rasta Toucan"
:f (fn [card]
- (mt/user-http-request :rasta :put 200 (str "card/" (u/the-id card)) {:archived true}))}))
+ (mt/user-http-request :rasta :put 200 (str "card/" (u/the-id card)) {:archived true}))})
+
+ (test-alert-deletion!
+ {:message "Archiving a Card should trigger Alert deletion with email links when disable_links: false"
+ :deleted? true
+ :disable-links? false
+ :expected-email-re #"If you want to restore the alert, go to the <a href=\".*\">trash</a>"
+ :should-re-not-match? false
+ :f (fn [card]
+ (mt/user-http-request :rasta :put 200 (str "card/" (u/the-id card)) {:archived true}))})
+
+ (test-alert-deletion!
+ {:message "Archiving a Card should trigger Alert deletion without email links when disable_links: true"
+ :deleted? true
+ :disable-links? true
+ :expected-email-re #"If you want to restore the alert, go to the <a href=\".*\">trash</a>"
+ :should-re-not-match? true
+ :f (fn [card]
+ (mt/user-http-request :rasta :put 200 (str "card/" (u/the-id card)) {:archived true}))}))
(deftest alert-deletion-test-2
(test-alert-deletion!
@@ -2044,7 +2063,17 @@
:deleted? true
:expected-email-re #"Alerts about <a href=\"https?://[^\/]+\/question/\d+\">([^<]+)<\/a> have stopped because the question was edited by Rasta Toucan"
:f (fn [card]
- (mt/user-http-request :rasta :put 200 (str "card/" (u/the-id card)) {:display :line}))}))
+ (mt/user-http-request :rasta :put 200 (str "card/" (u/the-id card)) {:display :line}))})
+
+ (test-alert-deletion!
+ {:message "Validate changing display type triggers alert deletion without email links when disable_links: true"
+ :card {:display :table}
+ :deleted? true
+ :disable-links? true
+ :expected-email-re #"Alerts about <a href=\"https?://[^\/]+\/question/\d+\">([^<]+)<\/a> have stopped because the question was edited by Rasta Toucan"
+ :should-re-not-match? true
+ :f (fn [card]
+ (mt/user-http-request :rasta :put 200 (str "card/" (u/the-id card)) {:display :line}))}))
(deftest alert-deletion-test-3
(test-alert-deletion!