Skip to content
Snippets Groups Projects
Unverified Commit f2823e4c authored by Jerry Huang's avatar Jerry Huang Committed by GitHub
Browse files

Add subscription-send and alert-send events (#35559)

parent f685f1c1
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,7 @@
[metabase.api.alert :as api.alert]
[metabase.email :as email]
[metabase.email.messages :as messages]
[metabase.events.audit-log-test :as audit-log-test]
[metabase.models
:refer [Card Pulse PulseCard PulseChannel PulseChannelRecipient]]
[metabase.models.pulse :as pulse]
......@@ -84,6 +85,61 @@
(m/dissoc-in results ["rasta@metabase.com" 0 :body])))
(get-in results ["rasta@metabase.com" 0 :body 0 :result])))))))
(deftest pulse-send-event-test
(testing "When we send a pulse, we also log the event:"
(mt/with-model-cleanup [:model/AuditLog]
(t2.with-temp/with-temp [Card pulse-card {}
Pulse pulse {:creator_id (mt/user->id :crowberto)
:name "Test Pulse"}
PulseCard _ {:pulse_id (:id pulse)
:card_id (:id pulse-card)}
PulseChannel pc {:channel_type :email
:pulse_id (:id pulse)
:enabled true}
PulseChannelRecipient _ {:pulse_channel_id (:id pc)
:user_id (mt/user->id :rasta)}]
(mt/with-temporary-setting-values [email-from-address "metamailman@metabase.com"]
(mt/with-fake-inbox
(with-redefs [messages/render-pulse-email (fn [_ _ _ [{:keys [result]}] _]
[{:result result}])]
(mt/with-test-user :lucky
(metabase.pulse/send-pulse! pulse)))
(is (= {:topic :subscription-send
:user_id (mt/user->id :crowberto)
:model "Pulse"
:model_id (:id pulse)
:details {:recipients [[(dissoc (mt/fetch-user :rasta) :last_login :is_qbnewb :is_superuser :date_joined)]]
:filters []}}
(audit-log-test/latest-event :subscription-send (:id pulse))))))))))
(deftest alert-send-event-test
(testing "When we send a pulse, we also log the event:"
(mt/with-model-cleanup [:model/AuditLog]
(t2.with-temp/with-temp [Card pulse-card {:dataset_query (mt/mbql-query venues)}
Pulse pulse {:creator_id (mt/user->id :crowberto)
:name "Test Pulse"
:alert_condition "rows"}
PulseCard _ {:pulse_id (:id pulse)
:card_id (:id pulse-card)}
PulseChannel pc {:channel_type :email
:pulse_id (:id pulse)
:enabled true}
PulseChannelRecipient _ {:pulse_channel_id (:id pc)
:user_id (mt/user->id :rasta)}]
(mt/with-temporary-setting-values [email-from-address "metamailman@metabase.com"]
(mt/with-fake-inbox
(with-redefs [messages/render-pulse-email (fn [_ _ _ [{:keys [result]}] _]
[{:result result}])]
(mt/with-test-user :lucky
(metabase.pulse/send-pulse! pulse)))
(is (= {:topic :alert-send
:user_id (mt/user->id :crowberto)
:model "Pulse"
:model_id (:id pulse)
:details {:recipients [[(dissoc (mt/fetch-user :rasta) :last_login :is_qbnewb :is_superuser :date_joined)]]
:filters []}}
(audit-log-test/latest-event :alert-send (:id pulse))))))))))
(deftest e2e-sandboxed-pulse-test
(testing "Sending Pulses w/ sandboxing, end-to-end"
(met/with-gtaps {:gtaps {:venues {:query (mt/mbql-query venues
......
......@@ -101,6 +101,8 @@
(derive :event/alert-unsubscribe ::pulse-event)
(derive :event/subscription-create ::pulse-event)
(derive :event/subscription-update ::pulse-event)
(derive :event/subscription-send ::pulse-event)
(derive :event/alert-send ::pulse-event)
(defn- create-details-map [pulse name is-alert parent]
(let [channels (:channels pulse)
......@@ -114,16 +116,17 @@
:recipients (map :recipients channels)}))
(methodical/defmethod events/publish-event! ::pulse-event
[topic {:keys [object user-id] :as _event}]
;; Check if topic is a pulse or not (can be an unsubscribe event, which only contains email)
[topic {:keys [id object user-id] :as _event}]
;; Check if object contains the keys that we want populated, if not then may be a unsubscribe/send event
(let [details-map (if (some? (:id object))
(create-details-map object (:name object) false (:dashboard_id object))
object)]
object)
model-id (or id (:id object))]
(audit-log/record-event! topic
{:details details-map
:user-id user-id
:model :model/Pulse
:model-id (:id object)})))
:model-id model-id})))
(derive ::alert-event ::event)
(derive :event/alert-create ::alert-event)
......
......@@ -6,6 +6,7 @@
[metabase.config :as config]
[metabase.email :as email]
[metabase.email.messages :as messages]
[metabase.events :as events]
[metabase.integrations.slack :as slack]
[metabase.models.card :refer [Card]]
[metabase.models.dashboard :as dashboard :refer [Dashboard]]
......@@ -486,6 +487,14 @@
(defn- parts->notifications [{:keys [channels channel-ids], pulse-id :id, :as pulse} parts]
(let [channel-ids (or channel-ids (mapv :id channels))]
(when (should-send-notification? pulse parts)
(let [event-type (if (= :pulse (alert-or-pulse pulse))
:event/subscription-send
:event/alert-send)]
(events/publish-event! event-type {:id (:id pulse)
:user-id (:creator_id pulse)
:object {:recipients (map :recipients (:channels pulse))
:filters (:parameters pulse)}}))
(when (:alert_first_only pulse)
(t2/delete! Pulse :id pulse-id))
;; `channel-ids` is the set of channels to send to now, so only send to those. Note the whole set of channels
......
......@@ -401,6 +401,40 @@
:details {:email "test"}}
(latest-event "subscription-unsubscribe-undo"))))))
(deftest subscription-send-event-test
(testing :subscription-send
(mt/with-model-cleanup [:model/AuditLog]
(let [id 1]
(mt/with-test-user :rasta
(events/publish-event! :event/subscription-send {:id id
:user-id (mt/user->id :lucky)
:object {:recipients [[{:email "test"}]]
:filters []}}))
(is (= {:topic :subscription-send
:user_id (mt/user->id :lucky)
:model "Pulse"
:model_id id
:details {:recipients [[{:email "test"}]]
:filters []}}
(latest-event "subscription-send" id)))))))
(deftest alert-send-event-test
(testing :alert-send
(mt/with-model-cleanup [:model/AuditLog]
(let [id 1]
(mt/with-test-user :rasta
(events/publish-event! :event/alert-send {:id id
:user-id (mt/user->id :lucky)
:object {:recipients [[{:email "test"}]]
:filters []}}))
(is (= {:topic :alert-send
:user_id (mt/user->id :lucky)
:model "Pulse"
:model_id id
:details {:recipients [[{:email "test"}]]
:filters []}}
(latest-event "alert-send" id)))))))
(deftest alert-unsubscribe-event-test
(testing :alert-unsubscribe
(mt/with-test-user :rasta
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment