Skip to content

A platform for community discussion. Free, open, simple.

FIX: Update Teams integration to use Adaptive Card (#37696)

Osama Sayegh1f4bc3e6c0cd
diff --git a/plugins/discourse-chat-integration/lib/discourse_chat_integration/provider/teams/teams_provider.rb b/plugins/discourse-chat-integration/lib/discourse_chat_integration/provider/teams/teams_provider.rb
index 35093161564..2417ad94811 100644
--- a/plugins/discourse-chat-integration/lib/discourse_chat_integration/provider/teams/teams_provider.rb
+++ b/plugins/discourse-chat-integration/lib/discourse_chat_integration/provider/teams/teams_provider.rb
@@ -37,51 +37,105 @@ module DiscourseChatIntegration::Provider::TeamsProvider
def self.get_message(post)
display_name = "@#{post.user.username}"
- full_name =
- if SiteSetting.enable_names && post.user.name.present?
- post.user.name
- else
- ""
- end
+ if SiteSetting.enable_names && post.user.name.present?
+ display_name = "#{post.user.name} (#{display_name})"
+ end
topic = post.topic
- category = ""
+ category = nil
if topic.category&.uncategorized?
- category = "[#{I18n.t("uncategorized_category_name")}]"
+ category = "#{I18n.t("uncategorized_category_name")}"
elsif topic.category
category =
(
if (topic.category.parent_category)
- "[#{topic.category.parent_category.name}/#{topic.category.name}]"
+ "#{topic.category.parent_category.name}/#{topic.category.name}"
else
- "[#{topic.category.name}]"
+ topic.category.name
end
)
end
+ tags = topic.tags.map(&:name).join(", ") if topic.tags.present?
+ category_and_tags_line = [category, tags].compact.join(" | ").presence
+
+ body = [
+ {
+ type: "RichTextBlock",
+ inlines: [
+ {
+ type: "TextRun",
+ text: topic.title,
+ size: "Large",
+ weight: "Bolder",
+ selectAction: {
+ type: "Action.OpenUrl",
+ url: post.full_url,
+ },
+ },
+ ],
+ },
+ ]
- message = {
- "@type": "MessageCard",
- summary: topic.title,
- sections: [
+ if category_and_tags_line
+ body.push(
{
- activityTitle:
- "[#{topic.title} #{category} #{topic.tags.present? ? topic.tags.map(&:name).join(", ") : ""}](#{post.full_url})",
- activitySubtitle:
- post.excerpt(
- SiteSetting.chat_integration_teams_excerpt_length,
- text_entities: true,
- strip_links: true,
- remap_emoji: true,
- ),
- activityImage: post.user.small_avatar_url,
- facts: [{ name: full_name, value: display_name }],
- markdown: true,
+ type: "TextBlock",
+ text: category_and_tags_line,
+ wrap: true,
+ size: "Small",
+ isSubtle: true,
+ weight: "Lighter",
+ spacing: "Small",
},
- ],
- }
+ )
+ end
- message
+ body.push(
+ {
+ type: "ColumnSet",
+ columns: [
+ {
+ type: "Column",
+ items: [
+ {
+ type: "Image",
+ style: "Person",
+ url: "http:#{post.user.small_avatar_url}",
+ altText: display_name,
+ size: "Small",
+ },
+ ],
+ width: "auto",
+ },
+ {
+ type: "Column",
+ width: "stretch",
+ items: [{ type: "TextBlock", weight: "Bolder", text: display_name, wrap: true }],
+ verticalContentAlignment: "Center",
+ horizontalAlignment: "Left",
+ },
+ ],
+ },
+ {
+ type: "TextBlock",
+ text:
+ post.excerpt(
+ SiteSetting.chat_integration_teams_excerpt_length,
+ text_entities: true,
+ strip_links: true,
+ remap_emoji: true,
+ ),
+ wrap: true,
+ },
+ )
+
+ {
+ type: "AdaptiveCard",
+ "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
+ version: "1.5",
+ body: body,
+ }
end
def self.get_channel_by_name(name)
diff --git a/plugins/discourse-chat-integration/spec/lib/discourse_chat_integration/provider/teams/teams_provider_spec.rb b/plugins/discourse-chat-integration/spec/lib/discourse_chat_integration/provider/teams/teams_provider_spec.rb
index 5600368e827..65b6fbe1944 100644
--- a/plugins/discourse-chat-integration/spec/lib/discourse_chat_integration/provider/teams/teams_provider_spec.rb
+++ b/plugins/discourse-chat-integration/spec/lib/discourse_chat_integration/provider/teams/teams_provider_spec.rb
@@ -35,10 +35,18 @@ RSpec.describe DiscourseChatIntegration::Provider::TeamsProvider do
describe "with nil user.name" do
before { post.user.update!(name: nil) }
- it "handles nil username correctly" do
+ it "handles nil user name correctly" do
message = described_class.get_message(post)
- name = message[:sections].first[:facts].first[:name]
- expect(name).to eq("")
+ name = message[:body][2][:columns][1][:items][0][:text]
+ expect(name).to eq("@#{post.user.username}")
+ end
+ end
+
+ describe "with present user.name" do
+ it "handles user name correctly" do
+ message = described_class.get_message(post)
+ name = message[:body][2][:columns][1][:items][0][:text]
+ expect(name).to eq("#{post.user.name} (@#{post.user.username})")
end
end
end