Skip to content

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

Send dashboard subscription with attachments only (#61784)

Ngoc Khuat49389aea8049
diff --git a/e2e/test/scenarios/sharing/subscriptions.cy.spec.js b/e2e/test/scenarios/sharing/subscriptions.cy.spec.js
index eaaf037fab9..64be8222c73 100644
--- a/e2e/test/scenarios/sharing/subscriptions.cy.spec.js
+++ b/e2e/test/scenarios/sharing/subscriptions.cy.spec.js
@@ -327,6 +327,29 @@ describe("scenarios > dashboard > subscriptions", () => {
});
});
+ it("should send only attachments without email content when 'Send only attachments' is enabled", () => {
+ assignRecipient();
+
+ cy.findByLabelText("Attach results").click();
+ cy.findByLabelText("Questions to attach").click();
+ cy.findByLabelText("Send only attachments").click();
+ cy.findByLabelText("Send only attachments").should("be.checked");
+
+ H.sendEmailAndAssert((email) => {
+ expect(email.attachments).to.not.be.empty;
+ const csvAttachment = email.attachments.find(
+ (attachment) => attachment.contentType === "text/csv",
+ );
+ expect(csvAttachment).to.exist;
+ expect(csvAttachment.fileName).to.include("Orders");
+ expect(email.html).to.not.include("Orders chart");
+ expect(email.html).to.include(
+ "Dashboard content available in attached files",
+ );
+ expect(email.html).to.include("Orders in a dashboard");
+ });
+ });
+
it("should not display 'null' day of the week (metabase#14405)", () => {
assignRecipient();
// eslint-disable-next-line no-unscoped-text-selectors -- deprecated usage
diff --git a/frontend/src/metabase/notifications/EmailAttachmentPicker.jsx b/frontend/src/metabase/notifications/EmailAttachmentPicker.jsx
index 8f478e82d0c..662d708ca67 100644
--- a/frontend/src/metabase/notifications/EmailAttachmentPicker.jsx
+++ b/frontend/src/metabase/notifications/EmailAttachmentPicker.jsx
@@ -20,6 +20,7 @@ export default class EmailAttachmentPicker extends Component {
isEnabled: false,
isFormattingEnabled: true,
isPivotingEnabled: false,
+ isAttachmentOnly: false,
selectedAttachmentType: this.DEFAULT_ATTACHMENT_TYPE,
selectedCardIds: new Set(),
};
@@ -55,6 +56,7 @@ export default class EmailAttachmentPicker extends Component {
calculateStateFromCards() {
const selectedCards = this._getCardsWithAttachments();
+ const { pulse } = this.props;
return {
isEnabled: selectedCards.length > 0,
@@ -65,6 +67,7 @@ export default class EmailAttachmentPicker extends Component {
),
isFormattingEnabled: getInitialFormattingState(selectedCards),
isPivotingEnabled: getInitialPivotingState(selectedCards),
+ isAttachmentOnly: getInitialAttachmentOnlyState(pulse),
};
}
@@ -86,7 +89,8 @@ export default class EmailAttachmentPicker extends Component {
*/
updatePulseCards(attachmentType, selectedCardIds) {
const { pulse, setPulse } = this.props;
- const { isFormattingEnabled, isPivotingEnabled } = this.state;
+ const { isFormattingEnabled, isPivotingEnabled, isAttachmentOnly } =
+ this.state;
const isXls = attachmentType === "xlsx",
isCsv = attachmentType === "csv";
@@ -102,6 +106,13 @@ export default class EmailAttachmentPicker extends Component {
card.pivot_results = card.display === "pivot" && isPivotingEnabled;
return card;
}),
+ channels: pulse.channels.map((channel) => ({
+ ...channel,
+ details: {
+ ...channel.details,
+ attachment_only: isAttachmentOnly,
+ },
+ })),
});
}
@@ -139,7 +150,10 @@ export default class EmailAttachmentPicker extends Component {
this.disableAllCards();
}
- this.setState({ isEnabled: includeAttachment });
+ this.setState({
+ isEnabled: includeAttachment,
+ isAttachmentOnly: includeAttachment ? this.state.isAttachmentOnly : false,
+ });
};
/*
@@ -213,6 +227,21 @@ export default class EmailAttachmentPicker extends Component {
);
};
+ onToggleAttachmentOnly = () => {
+ this.setState(
+ (prevState) => ({
+ ...prevState,
+ isAttachmentOnly: !prevState.isAttachmentOnly,
+ }),
+ () => {
+ this.updatePulseCards(
+ this.state.selectedAttachmentType,
+ this.state.selectedCardIds,
+ );
+ },
+ );
+ };
+
disableAllCards() {
const selectedCardIds = new Set();
this.updatePulseCards(this.state.selectedAttachmentType, selectedCardIds);
@@ -233,6 +262,7 @@ export default class EmailAttachmentPicker extends Component {
isEnabled,
isFormattingEnabled,
isPivotingEnabled,
+ isAttachmentOnly,
selectedAttachmentType,
selectedCardIds,
} = this.state;
@@ -244,7 +274,8 @@ export default class EmailAttachmentPicker extends Component {
<Text fw="bold">{t`Attach results as files`}</Text>
<Icon
name="info"
- className={cx(CS.textMedium, CS.ml1)}
+ c="gray.6"
+ ml="0.5rem"
size={12}
tooltip={t`Attachments can contain up to 2,000 rows of data.`}
/>
@@ -316,6 +347,23 @@ export default class EmailAttachmentPicker extends Component {
))}
</ul>
</div>
+ <Group justify="space-between" pt="1rem">
+ <Group position="left" gap="0">
+ <Text fw="bold">{t`Send only attachments (no charts)`}</Text>
+ <Icon
+ name="info"
+ c="gray"
+ ml="0.5rem"
+ size={12}
+ tooltip={t`When enabled, only file attachments will be sent (no email content).`}
+ />
+ </Group>
+ <Toggle
+ aria-label={t`Send only attachments`}
+ value={isAttachmentOnly}
+ onChange={this.onToggleAttachmentOnly}
+ />
+ </Group>
</div>
)}
</div>
@@ -336,3 +384,10 @@ function getInitialPivotingState(cards) {
}
return false;
}
+
+function getInitialAttachmentOnlyState(pulse) {
+ if (pulse && pulse.channels && pulse.channels.length > 0) {
+ return pulse.channels.some((channel) => !!channel.details?.attachment_only);
+ }
+ return false;
+}
diff --git a/src/metabase/channel/core.clj b/src/metabase/channel/core.clj
index c56f880055c..b9e821427aa 100644
--- a/src/metabase/channel/core.clj
+++ b/src/metabase/channel/core.clj
@@ -25,13 +25,13 @@
channel-type))
(defmulti render-notification
- "Given a notification payload, return a sequence of channel-specific messages.
+ "Given a notification payload, return a sequence of handler-specific messages.
The message format is channel-specific, one requirement is that it should be the same format that
the [[send!]] multimethod expects."
{:added "0.51.0"
- :arglists '([channel-type notification-payload template recipients])}
- (fn [channel-type notification-payload _template _recipients]
+ :arglists '([channel-type notification-payload handler])}
+ (fn [channel-type notification-payload _handler]
[channel-type (:payload_type notification-payload)]))
(defmulti send!
diff --git a/src/metabase/channel/impl/email.clj b/src/metabase/channel/impl/email.clj
index 54a1dce3d50..3b691100353 100644
--- a/src/metabase/channel/impl/email.clj
+++ b/src/metabase/channel/impl/email.clj
@@ -195,7 +195,7 @@
;; ------------------------------------------------------------------------------------------------;;
(mu/defmethod channel/render-notification [:channel/email :notification/card] :- [:sequential EmailMessage]
- [_channel-type {:keys [payload payload_type] :as notification-payload} template recipients]
+ [_channel-type {:keys [payload payload_type] :as notification-payload} {:keys [template recipients]}]
(let [{:keys [card_part
notification_card
subscriptions
@@ -238,7 +238,7 @@
;; ------------------------------------------------------------------------------------------------;;
(mu/defmethod channel/render-notification [:channel/email :notification/dashboard] :- [:sequential EmailMessage]
- [_channel-type {:keys [payload payload_type] :as notification-payload} template recipients]
+ [_channel-type {:keys [payload payload_type] :as notification-payload} {:keys [template recipients attachment_only]}]
(let [{:keys [dashboard_parts
dashboard_subscription
parameters
@@ -250,20 +250,26 @@
;; 1. Accumulate the attachments in an imperative way.
;; 2. Convert Hiccup structure into HTML immediately.
;; 3. Later, we combine all HTMLs using ordinary string mashing.
- merged-attachments (volatile! {})
- result-attachments (volatile! [])
- html-contents (->> dashboard_parts
- (assoc-attachment-booleans (:dashboard_subscription_dashcards dashboard_subscription))
- (mapv #(let [{:keys [attachments content]}
- (render-part timezone % {:channel.render/include-title? true})
- result-attachment (email.result-attachment/result-attachment %)]
- (vswap! merged-attachments merge attachments)
- (vswap! result-attachments into result-attachment)
- (html content))))
+ [merged-attachments
+ result-attachments
+ html-contents] (reduce
+ (fn [[merged-attachments result-attachments html-contents] part]
+ (let [{:keys [attachments content]} (render-part timezone part {:channel.render/include-title? true})
+ result-attachment (email.result-attachment/result-attachment part)]
+ [(merge merged-attachments attachments)
+ (into result-attachments result-attachment)
+ (when-not attachment_only
+ (conj html-contents (html content)))]))
+ [{} [] []]
+ (assoc-attachment-booleans (:dashboard_subscription_dashcards dashboard_subscription) dashboard_parts))
icon-attachment (make-message-attachment (first (icon-bundle :dashboard)))
- card-attachments (map make-message-attachment @merged-attachments)
- attachments (concat [icon-attachment] card-attachments @result-attachments)
- dashboard-content (str "<div>" (str/join html-contents) "</div>")
+ card-attachments (map make-message-attachment merged-attachments)
+ attachments (cond-> (into [icon-attachment] result-attachments)
+ (not attachment_only)
+ (concat card-attachments))
+ dashboard-content (if-not attachment_only
+ (str "<div>" (str/join html-contents) "</div>")
+ "<p>Dashboard content available in attached files</p>")
message-context-fn (fn [non-user-email]
(-> notification-payload
(assoc :computed {:dashboard_content dashboard-content
@@ -275,9 +281,10 @@
:management_url (if (nil? non-user-email)
(urls/notification-management-url)
(pulse-unsubscribe-url-for-non-user (:id dashboard_subscription) non-user-email))
- :filters (some-> (seq parameters)
- (impl.util/remove-inline-parameters dashboard_parts)
- (render.util/render-parameters))})
+ :filters (when-not attachment_only
+ (some-> (seq parameters)
+ (impl.util/remove-inline-parameters dashboard_parts)
+ (render.util/render-parameters)))})
(m/update-existing-in [:payload :dashboard :description] #(markdown/process-markdown % :html))))]
(construct-emails template message-context-fn attachments recipients)))
@@ -307,8 +314,9 @@
[:channel/email :notification/system-event]
[_channel-type
notification-payload #_:- #_notification/NotificationPayload
- template :- ::models.channel/ChannelTemplate
- recipients :- [:sequential ::models.notification/NotificationRecipient]]
+ {:keys [template recipients]} :- [:map
+ [:template ::models.channel/ChannelTemplate]
+ [:recipients [:sequential ::models.notification/NotificationRecipient]]]]
(assert (some? template) "Template is required for system event notifications")
[(construct-email (channel.params/substitute-params (-> template :details :subject) notification-payload)
(notification-recipients->emails recipients notification-payload)
diff --git a/src/metabase/channel/impl/http.clj b/src/metabase/channel/impl/http.clj
index 3204bfd10f9..097ee9bbfa6 100644
--- a/src/metabase/channel/impl/http.clj
+++ b/src/metabase/channel/impl/http.clj
@@ -91,7 +91,7 @@
:rows (:rows data)}))
(mu/defmethod channel/render-notification [:channel/http :notification/card]
- [_channel-type {:keys [payload creator]} _template _recipients]
+ [_channel-type {:keys [payload creator]} _handler]
(let [{:keys [card notification_card card_part]} payload
card_part (channel.shared/maybe-realize-data-rows card_part)
request-body {:type "alert"
diff --git a/src/metabase/channel/impl/slack.clj b/src/metabase/channel/impl/slack.clj
index 93be63012f3..8fe963006cf 100644
--- a/src/metabase/channel/impl/slack.clj
+++ b/src/metabase/channel/impl/slack.clj
@@ -138,7 +138,7 @@
;; ------------------------------------------------------------------------------------------------;;
(mu/defmethod channel/render-notification [:channel/slack :notification/card] :- [:sequential SlackMessage]
- [_channel-type {:keys [payload]} _template recipients]
+ [_channel-type {:keys [payload]} {:keys [recipients]}]
(let [blocks (concat [{:type "header"
:text {:type "plain_text"
:text (truncate (str "🔔 " (-> payload :card :name)) header-text-limit)
@@ -185,7 +185,7 @@
(filter some? [header-section filter-section link-section])))
(mu/defmethod channel/render-notification [:channel/slack :notification/dashboard] :- [:sequential SlackMessage]
- [_channel-type {:keys [payload creator]} _template recipients]
+ [_channel-type {:keys [payload creator]} {:keys [recipients]}]
(let [all-params (:parameters payload)
top-level-params (impl.util/remove-inline-parameters all-params (:dashboard_parts payload))
dashboard (:dashboard payload)
diff --git a/src/metabase/notification/send.clj b/src/metabase/notification/send.clj
index 5964beae8c8..92326aac6eb 100644
--- a/src/metabase/notification/send.clj
+++ b/src/metabase/notification/send.clj
@@ -167,8 +167,7 @@
messages (channel/render-notification
channel-type
notification-payload
- (:template handler)
- (:recipients handler))]
+ handler)]
(log/debugf "Got %d messages for channel %s with template %d"
(count messages)
(handler->channel-name handler)
diff --git a/src/metabase/pulse/send.clj b/src/metabase/pulse/send.clj
index 1506f95e458..3d6f5c2fbd8 100644
--- a/src/metabase/pulse/send.clj
+++ b/src/metabase/pulse/send.clj
@@ -50,9 +50,10 @@
[pulse-channel]
(let [channel (pc->channel pulse-channel)
channel-type (:type channel)]
- {:channel_type channel-type
- :channel channel
- :recipients (channel-recipients pulse-channel)}))
+ {:channel_type channel-type
+ :channel channel
+ :recipients (channel-recipients pulse-channel)
+ :attachment_only (boolean (get-in pulse-channel [:details :attachment_only]))}))
(defn- maybe-name [x] (some-> x name))
diff --git a/test/metabase/channel/impl/slack_test.clj b/test/metabase/channel/impl/slack_test.clj
index b2117d94934..034a82b0973 100644
--- a/test/metabase/channel/impl/slack_test.clj
+++ b/test/metabase/channel/impl/slack_test.clj
@@ -67,7 +67,7 @@
recipient {:type :notification-recipient/raw-value
:details {:value "#foo"}}
processed (with-redefs [channel.slack/header-text-limit char-limit]
- (channel/render-notification :channel/slack notification nil [recipient]))]
+ (channel/render-notification :channel/slack notification {:recipients [recipient]}))]
(-> processed first :blocks first :text :text)))]
(are [dashboard-name rendered-text]
(= rendered-text (render-dashboard-header dashboard-name 5))
@@ -84,7 +84,7 @@
recipient {:type :notification-recipient/raw-value
:details {:value "#foo"}}
processed (with-redefs [channel.slack/header-text-limit char-limit]
- (channel/render-notification :channel/slack notification nil [recipient]))]
+ (channel/render-notification :channel/slack notification {:recipients [recipient]}))]
(-> processed first :blocks first :text :text)))]
(are [card-name rendered-text]
(= rendered-text (render-card-header card-name 8))
@@ -105,7 +105,7 @@
recipient {:type :notification-recipient/raw-value
:details {:value "#foo"}}
processed (with-redefs [slack/upload-file! (constantly {:url "a.com", :id "id"})]
- (channel/render-notification :channel/slack notification nil [recipient]))]
+ (channel/render-notification :channel/slack notification {:recipients [recipient]}))]
(->> processed first :blocks last :fields (map :text))))))]
(when config/ee-available?
(testing "When whitelabeling is enabled, branding content should not be included"
diff --git a/test/metabase/notification/send_test.clj b/test/metabase/notification/send_test.clj
index 79560907500..270ec629149 100644
--- a/test/metabase/notification/send_test.clj
+++ b/test/metabase/notification/send_test.clj
@@ -54,12 +54,12 @@
[:context :map]
[:payload :map]])
renders (atom [])]
- (mt/with-dynamic-fn-redefs [channel/render-notification (fn [channel-type notification-payload template recipients]
+ (mt/with-dynamic-fn-redefs [channel/render-notification (fn [channel-type notification-payload {:keys [template recipients]}]
(swap! renders conj {:channel-type channel-type
:notification-payload notification-payload
:template template
:recipients recipients})
- ;; rendered messages are recipients
+ ;; rendered messages are recipients
recipients)]
(testing "channel/send! are called on rendered messages"
(is (=? {:channel/metabase-test [{:type :notification-recipient/user :user_id (mt/user->id :crowberto)}
diff --git a/test/metabase/notification/test_util.clj b/test/metabase/notification/test_util.clj
index 083ecf7fbb1..5344cd1fdbf 100644
--- a/test/metabase/notification/test_util.clj
+++ b/test/metabase/notification/test_util.clj
@@ -39,7 +39,7 @@
message)
(defmethod channel/render-notification [:channel/metabase-test :notification/testing]
- [_channel-type notification-info _template _recipients]
+ [_channel-type notification-info _handler]
[notification-info])
(defmethod notification.payload/payload :notification/testing
diff --git a/test/metabase/pulse/dashboard_subscription_test.clj b/test/metabase/pulse/dashboard_subscription_test.clj
index e7a6e81a6a2..939f70054d9 100644
--- a/test/metabase/pulse/dashboard_subscription_test.clj
+++ b/test/metabase/pulse/dashboard_subscription_test.clj
@@ -1294,3 +1294,32 @@
(is (=? {:channel "#general",
:blocks (default-slack-blocks dashboard-id [card-id])}
(pulse.test-util/thunk->boolean (first (:channel/slack pulse-results)))))))))))))
+
+(deftest dashboard-sub-attachment-only-test
+ (mt/with-temp [:model/Card {card-id :id} {:name pulse.test-util/card-name
+ :dataset_query (mt/mbql-query orders {:limit 1})}
+ :model/Dashboard {dashboard-id :id} {:name "Aviary KPIs"}
+ :model/DashboardCard _ {:dashboard_id dashboard-id
+ :card_id card-id}
+ :model/Pulse {pulse-id :id} {:name "Pulse Name"
+ :dashboard_id dashboard-id}
+ :model/PulseCard _ {:pulse_id pulse-id
+ :card_id card-id
+ :position 0
+ :include_csv true}
+ :model/PulseChannel {pc-id :id} {:pulse_id pulse-id
+ :channel_type "email"
+ :details {:attachment_only true}}
+ :model/PulseChannelRecipient _ {:user_id (pulse.test-util/rasta-id)
+ :pulse_channel_id pc-id}]
+ (let [pulse-results (pulse.test-util/with-captured-channel-send-messages!
+ (pulse.send/send-pulse! (t2/select-one :model/Pulse pulse-id)))]
+ (is (= (rasta-dashsub-message
+ {:message [{"Aviary KPIs" true
+ "Dashboard content available in attached files" true}
+ pulse.test-util/png-attachment
+ pulse.test-util/csv-attachment]})
+ (mt/summarize-multipart-single-email
+ (first (:channel/email pulse-results))
+ #"Aviary KPIs"
+ #"Dashboard content available in attached files"))))))