-
Notifications
You must be signed in to change notification settings - Fork 7
Add richer Slack plan unfurls #157
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| module CoPlan | ||
| LinkPreview = Data.define( | ||
| :kind, :external_id, :canonical_url, :title, :description, | ||
| :context, :image_url, :cache_key | ||
| :context, :image_url, :author_name, :author_avatar_url, :cache_key | ||
| ) | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,22 @@ | ||
| module CoPlan | ||
| module Slack | ||
| class Renderer | ||
| BRAND_COLOR = "#136FF5" | ||
| PRIVATE_COLOR = "#8C4AF6" | ||
| ARCHIVED_COLOR = "#64748B" | ||
|
|
||
| def self.call(preview, url: preview.canonical_url) | ||
| section = { | ||
| type: "section", | ||
| text: { type: "mrkdwn", text: "*<#{escape_url(url)}|#{escape(preview.title)}>*#{description(preview)}" } | ||
| } | ||
| section[:accessory] = { type: "image", image_url: preview.image_url, alt_text: preview.title.to_s.first(2000) } if preview.image_url.present? | ||
| { | ||
| color: accent_color(preview), | ||
| fallback: [ preview.title, preview.description, preview.context ].compact.join(" — ").first(3000), | ||
| blocks: [ | ||
| section, | ||
| { type: "context", elements: [ { type: "mrkdwn", text: escape(preview.context) } ] } | ||
| { type: "context", elements: context_elements(preview) } | ||
| ], | ||
| preview: { | ||
| title: { type: "plain_text", text: preview.title.to_s.first(150) } | ||
|
|
@@ -30,7 +35,31 @@ def self.escape_url(value) | |
| def self.description(preview) | ||
| preview.description.present? ? "\n#{escape(preview.description)}" : "" | ||
| end | ||
| private_class_method :escape_url, :description | ||
|
|
||
| def self.accent_color(preview) | ||
| return ARCHIVED_COLOR if preview.context.to_s.start_with?("Archived") | ||
| return PRIVATE_COLOR if preview.context.to_s.start_with?("Private") | ||
|
|
||
| BRAND_COLOR | ||
| end | ||
|
|
||
| def self.decorated_context(preview) | ||
| context = escape(preview.context) | ||
| context = context.sub("by #{escape(preview.author_name)}", "by *#{escape(preview.author_name)}*") if preview.author_name.present? | ||
| return "📦 *Archived*#{context.delete_prefix("Archived")}" if preview.context.to_s.start_with?("Archived") | ||
| return "🔒 *Private*#{context.delete_prefix("Private")}" if preview.context.to_s.start_with?("Private") | ||
|
|
||
| "📄 #{context}" | ||
| end | ||
|
|
||
| def self.context_elements(preview) | ||
| elements = [] | ||
| if preview.author_avatar_url.present? | ||
| elements << { type: "image", image_url: preview.author_avatar_url, alt_text: preview.author_name.to_s.first(2000) } | ||
|
Comment on lines
+57
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| end | ||
| elements << { type: "mrkdwn", text: decorated_context(preview) } | ||
| end | ||
| private_class_method :escape_url, :description, :accent_color, :decorated_context, :context_elements | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,29 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe CoPlan::Slack::Renderer do | ||
| it "escapes mrkdwn and adds an image accessory and composer title" do | ||
| preview = CoPlan::LinkPreview.new(kind: "plan", external_id: "id", canonical_url: "https://example.test/p", title: "A < B & C", description: "> hello", context: "Live & ready", image_url: "https://example.test/i.png", cache_key: "x") | ||
| it "renders a branded published-plan unfurl" do | ||
| preview = CoPlan::LinkPreview.new(kind: "plan", external_id: "id", canonical_url: "https://example.test/p", title: "A < B & C", description: "> hello", context: "Live & ready · by Ada", image_url: "https://example.test/i.png", author_name: "Ada", author_avatar_url: "https://example.test/ada.png", cache_key: "x") | ||
| result = described_class.call(preview, url: "https://example.test/p?thread=123&view=full") | ||
| expect(result[:blocks][0][:text][:text]).to include("A < B & C", "> hello") | ||
| expect(result[:blocks][1][:elements][0][:text]).to eq("Live & ready") | ||
| expect(result[:blocks][1][:elements][0]).to eq(type: "image", image_url: preview.author_avatar_url, alt_text: "Ada") | ||
| expect(result[:blocks][1][:elements][1][:text]).to eq("📄 Live & ready · by *Ada*") | ||
| expect(result[:blocks][0][:text][:text]).to include("https://example.test/p?thread=123&view=full") | ||
| expect(result[:blocks][0][:accessory][:image_url]).to eq(preview.image_url) | ||
| expect(result[:color]).to eq("#136FF5") | ||
| expect(result[:fallback]).to include(preview.title) | ||
| expect(result.dig(:preview, :title, :text)).to eq(preview.title) | ||
| end | ||
|
|
||
| it "visually distinguishes private and archived plans" do | ||
| private_preview = CoPlan::LinkPreview.new(kind: "plan", external_id: "id", canonical_url: "https://example.test/p", title: "Private", description: nil, context: "Private · EDD · by Ada", image_url: nil, author_name: "Ada", author_avatar_url: nil, cache_key: "x") | ||
| archived_preview = private_preview.with(title: "Archived", context: "Archived · EDD · by Ada") | ||
|
|
||
| private_result = described_class.call(private_preview) | ||
| archived_result = described_class.call(archived_preview) | ||
|
|
||
| expect(private_result[:color]).to eq("#8C4AF6") | ||
| expect(private_result.dig(:blocks, 1, :elements, 0, :text)).to eq("🔒 *Private* · EDD · by *Ada*") | ||
| expect(archived_result[:color]).to eq("#64748B") | ||
| expect(archived_result.dig(:blocks, 1, :elements, 0, :text)).to eq("📦 *Archived* · EDD · by *Ada*") | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coplan