Skip to content
Snippets Groups Projects
Unverified Commit f135ea4f authored by Ngoc Khuat's avatar Ngoc Khuat Committed by GitHub
Browse files

Tidy pulse code, remove unused methods (#44603)

parent 4bda2029
No related branches found
No related tags found
No related merge requests found
......@@ -53,12 +53,12 @@
:tab-title
(text->markdown-block (format "# %s" (:text payload)))))
(def slack-width
(def ^:private slack-width
"Maximum width of the rendered PNG of HTML to be sent to Slack. Content that exceeds this width (e.g. a table with
many columns) is truncated."
1200)
(defn create-and-upload-slack-attachments!
(defn- create-and-upload-slack-attachments!
"Create an attachment in Slack for a given Card by rendering its content into an image and uploading
it. Slack-attachment-uploader is a function which takes image-bytes and an attachment name, uploads the file, and
returns an image url, defaulting to slack/upload-file!.
......
......@@ -4,7 +4,6 @@
[metabase.api.common :as api]
[metabase.channel.core :as channel]
[metabase.events :as events]
[metabase.integrations.slack :as slack]
[metabase.models.dashboard :as dashboard :refer [Dashboard]]
[metabase.models.dashboard-card :as dashboard-card]
[metabase.models.database :refer [Database]]
......@@ -12,7 +11,6 @@
[metabase.models.pulse :as pulse :refer [Pulse]]
[metabase.models.serialization :as serdes]
[metabase.pulse.parameters :as pulse-params]
[metabase.pulse.render :as render]
[metabase.pulse.util :as pu]
[metabase.query-processor.timezone :as qp.timezone]
[metabase.server.middleware.session :as mw.session]
......@@ -180,33 +178,6 @@
(or (some->> card database-id (t2/select-one Database :id) qp.timezone/results-timezone-id)
(qp.timezone/system-timezone-id)))
(def slack-width
"Maximum width of the rendered PNG of HTML to be sent to Slack. Content that exceeds this width (e.g. a table with
many columns) is truncated."
1200)
(defn create-and-upload-slack-attachments!
"Create an attachment in Slack for a given Card by rendering its content into an image and uploading
it. Slack-attachment-uploader is a function which takes image-bytes and an attachment name, uploads the file, and
returns an image url, defaulting to slack/upload-file!.
Nested `blocks` lists containing text cards are passed through unmodified."
([attachments] (create-and-upload-slack-attachments! attachments slack/upload-file!))
([attachments slack-attachment-uploader]
(letfn [(f [a] (select-keys a [:title :title_link :fallback]))]
(reduce (fn [processed {:keys [rendered-info attachment-name channel-id] :as attachment-data}]
(conj processed (if (:blocks attachment-data)
attachment-data
(if (:render/text rendered-info)
(-> (f attachment-data)
(assoc :text (:render/text rendered-info)))
(let [image-bytes (render/png-from-render-info rendered-info slack-width)
image-url (slack-attachment-uploader image-bytes attachment-name channel-id)]
(-> (f attachment-data)
(assoc :image_url image-url)))))))
[]
attachments))))
(defn- are-all-parts-empty?
"Do none of the cards have any results?"
[results]
......
(ns metabase.channel.slack-test
(:require
[clojure.test :refer :all]
[metabase.channel.slack :as channel.slack]
[metabase.integrations.slack :as slack]))
(deftest create-and-upload-slack-attachments!-test
(let [slack-uploader (fn [storage]
(fn [_bytes attachment-name _channel-id]
(swap! storage conj attachment-name)
(str "http://uploaded/" attachment-name)))]
(testing "Uploads files"
(let [titles (atom [])
attachments [{:title "a"
:attachment-name "a.png"
:rendered-info {:attachments nil
:content [:div "hi"]}
:channel-id "FOO"}
{:title "b"
:attachment-name "b.png"
:rendered-info {:attachments nil
:content [:div "hi again"]}
:channel-id "FOO"}]
processed (with-redefs [slack/upload-file! (slack-uploader titles)]
(#'channel.slack/create-and-upload-slack-attachments! attachments))]
(is (= [{:title "a", :image_url "http://uploaded/a.png"}
{:title "b", :image_url "http://uploaded/b.png"}]
processed))
(is (= @titles ["a.png" "b.png"]))))
(testing "Uses the raw text when present"
(let [titles (atom [])
attachments [{:title "a"
:attachment-name "a.png"
:rendered-info {:attachments nil
:content [:div "hi"]}
:channel-id "FOO"}
{:title "b"
:attachment-name "b.png"
:rendered-info {:attachments nil
:content [:div "hi again"]
:render/text "hi again"}
:channel-id "FOO"}]
processed (with-redefs [slack/upload-file! (slack-uploader titles)]
(#'channel.slack/create-and-upload-slack-attachments! attachments))]
(is (= [{:title "a", :image_url "http://uploaded/a.png"}
{:title "b", :text "hi again"}]
processed))
(is (= @titles ["a.png"]))))))
......@@ -697,47 +697,6 @@
(is (mt/received-email-body? :rasta #"Manage your subscriptions"))
(is (mt/received-email-body? "nonuser@metabase.com" #"Unsubscribe"))))))
(deftest create-and-upload-slack-attachments!-test
(let [slack-uploader (fn [storage]
(fn [_bytes attachment-name _channel-id]
(swap! storage conj attachment-name)
(str "http://uploaded/" attachment-name)))]
(testing "Uploads files"
(let [titles (atom [])
attachments [{:title "a"
:attachment-name "a.png"
:rendered-info {:attachments nil
:content [:div "hi"]}
:channel-id "FOO"}
{:title "b"
:attachment-name "b.png"
:rendered-info {:attachments nil
:content [:div "hi again"]}
:channel-id "FOO"}]
processed (metabase.pulse/create-and-upload-slack-attachments! attachments (slack-uploader titles))]
(is (= [{:title "a", :image_url "http://uploaded/a.png"}
{:title "b", :image_url "http://uploaded/b.png"}]
processed))
(is (= @titles ["a.png" "b.png"]))))
(testing "Uses the raw text when present"
(let [titles (atom [])
attachments [{:title "a"
:attachment-name "a.png"
:rendered-info {:attachments nil
:content [:div "hi"]}
:channel-id "FOO"}
{:title "b"
:attachment-name "b.png"
:rendered-info {:attachments nil
:content [:div "hi again"]
:render/text "hi again"}
:channel-id "FOO"}]
processed (metabase.pulse/create-and-upload-slack-attachments! attachments (slack-uploader titles))]
(is (= [{:title "a", :image_url "http://uploaded/a.png"}
{:title "b", :text "hi again"}]
processed))
(is (= @titles ["a.png"]))))))
(deftest pulse-permissions-test
(testing "Pulses should be sent with the Permissions of the user that created them."
(letfn [(send-pulse-created-by-user!* [user-kw]
......
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