A platform for community discussion. Free, open, simple.
DEV: Add informative blocks with category elements to chat (#37932)
Gabriel Grubbab4925714f541
diff --git a/plugins/chat/app/serializers/chat/block_serializer.rb b/plugins/chat/app/serializers/chat/block_serializer.rbindex bc508ef9ed1..c07af08c8a6 100644--- a/plugins/chat/app/serializers/chat/block_serializer.rb+++ b/plugins/chat/app/serializers/chat/block_serializer.rb@@ -19,6 +19,8 @@ module Chatcase typewhen "button"Chat::Blocks::Elements::ButtonSerializer+ when "category"+ Chat::Blocks::Elements::CategorySerializerelseraise "no serializer for #{type}"enddiff --git a/plugins/chat/app/serializers/chat/blocks/elements/category_serializer.rb b/plugins/chat/app/serializers/chat/blocks/elements/category_serializer.rbnew file mode 100644index 00000000000..b7a2a205784--- /dev/null+++ b/plugins/chat/app/serializers/chat/blocks/elements/category_serializer.rb@@ -0,0 +1,55 @@+# frozen_string_literal: true++module Chat+ module Blocks+ module Elements+ class CategorySerializer < ApplicationSerializer+ attributes :type, :title, :color, :description, :url, :parent_name, :parent_color++ def type+ object["type"]+ end++ def title+ object["title"]+ end++ def color+ object["color"]+ end++ def description+ object["description"]+ end++ def include_description?+ object["description"].present?+ end++ def url+ object["url"]+ end++ def include_url?+ object["url"].present?+ end++ def parent_name+ object["parent_name"]+ end++ def include_parent_name?+ object["parent_name"].present?+ end++ def parent_color+ object["parent_color"]+ end++ def include_parent_color?+ object["parent_color"].present?+ end+ end+ end+ end+enddiff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/block.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/block.gjsindex c7ec6422f7a..0ff13a30d2f 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/block.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/block.gjs@@ -1,11 +1,14 @@import Component from "@glimmer/component";import Actions from "./actions";+import Informative from "./informative";export default class Block extends Component {get blockForType() {switch (this.args.definition.type) {case "actions":return Actions;+ case "informative":+ return Informative;default:throw new Error(`Unknown block type: ${this.args.definition.type}`);}diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/element.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/element.gjsindex da88e987d01..d8bec1b7466 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/element.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/element.gjs@@ -1,11 +1,14 @@import Component from "@glimmer/component";import Button from "./elements/button";+import CategoryElement from "./elements/category";export default class Element extends Component {get elementForType() {switch (this.args.definition.type) {case "button":return Button;+ case "category":+ return CategoryElement;default:throw new Error(`Unknown element type: ${this.args.definition.type}`);}diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/elements/category.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/elements/category.gjsnew file mode 100644index 00000000000..bb29eb83d69--- /dev/null+++ b/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/elements/category.gjs@@ -0,0 +1,57 @@+import Component from "@glimmer/component";+import { htmlSafe } from "@ember/template";++export default class CategoryElement extends Component {+ get boxStyle() {+ let style = `--category-badge-color: #${this.args.definition.color}`;+ if (this.args.definition.parent_color) {+ style += `; --parent-category-badge-color: #${this.args.definition.parent_color}`;+ }+ return htmlSafe(style);+ }++ get parentBadgeStyle() {+ return this.args.definition.parent_color+ ? htmlSafe(+ `--category-badge-color: #${this.args.definition.parent_color}`+ )+ : null;+ }++ <template>+ <div class="block__category" style={{this.boxStyle}}>+ <div class="block__category-inner">+ <div class="block__category-heading">+ {{#if @definition.url}}+ <a href={{@definition.url}} class="block__category-link">+ <span class="badge-category --style-square">+ <span class="badge-category__name">{{@definition.title}}</span>+ </span>+ </a>+ {{else}}+ <span class="badge-category --style-square">+ <span class="badge-category__name">{{@definition.title}}</span>+ </span>+ {{/if}}+ </div>+ {{#if @definition.parent_name}}+ <div class="block__category-parent">+ <span+ class="badge-category --style-square"+ style={{this.parentBadgeStyle}}+ >+ <span+ class="badge-category__name"+ >{{@definition.parent_name}}</span>+ </span>+ </div>+ {{/if}}+ {{#if @definition.description}}+ <div class="block__category-description">+ {{@definition.description}}+ </div>+ {{/if}}+ </div>+ </div>+ </template>+}diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/informative.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/informative.gjsnew file mode 100644index 00000000000..9298c11fe24--- /dev/null+++ b/plugins/chat/assets/javascripts/discourse/components/chat-message/blocks/informative.gjs@@ -0,0 +1,15 @@+import Element from "./element";++const Informative = <template>+ <div class="block__informative-wrapper">+ <div class="block__informative">+ {{#each @definition.elements as |elementDefinition|}}+ <div class="block__informative-item">+ <Element @definition={{elementDefinition}} />+ </div>+ {{/each}}+ </div>+ </div>+</template>;++export default Informative;diff --git a/plugins/chat/assets/stylesheets/common/chat-message-blocks.scss b/plugins/chat/assets/stylesheets/common/chat-message-blocks.scssindex 331eadac509..122a4a1609c 100644--- a/plugins/chat/assets/stylesheets/common/chat-message-blocks.scss+++ b/plugins/chat/assets/stylesheets/common/chat-message-blocks.scss@@ -17,4 +17,84 @@width: 18px;}}++ .block__informative-wrapper {+ margin: 0.5em 0;+ }++ .block__informative {+ display: flex;+ flex-direction: column;+ gap: 0.5em;+ padding: 0.5em 0;+ }++ .block__informative-item {+ display: flex;+ align-items: stretch;+ }++ .block__category {+ position: relative;+ display: flex;+ flex-direction: row;+ box-sizing: border-box;+ border-width: 0;+ border-radius: var(--d-border-radius);+ background-color: var(--d-category-box-background-color);+ border-left-width: var(--d-category-border-accent-width);+ border-style: solid;+ border-color: var(--category-badge-color, var(--primary-low));+ width: 100%;+ }++ .block__category-inner {+ box-sizing: border-box;+ width: 100%;+ padding: 1em;+ display: flex;+ flex-direction: column;+ gap: 0.5em;+ border-width: var(--d-category-border-box-width);+ border-left-width: 0;+ border-style: solid;+ border-color: var(--primary-low);+ border-top-right-radius: var(--d-border-radius);+ border-bottom-right-radius: var(--d-border-radius);+ }++ .block__category-heading {+ margin-bottom: 0.5em;++ .badge-category__wrapper {+ font-size: var(--font-up-2);+ }++ .badge-category__name {+ font-weight: bold;+ color: var(--primary);+ }++ .block__category-link {+ text-decoration: none;+ color: inherit;++ &:hover {+ .badge-category__name {+ color: var(--tertiary);+ }+ }+ }+ }++ .block__category-parent {+ font-size: var(--font-down-1);+ color: var(--primary-high);+ }++ .block__category-description {+ font-size: var(--font-0);+ color: var(--primary-high);+ line-height: var(--line-height-medium);+ }}diff --git a/plugins/chat/lib/chat/schemas/message_blocks.rb b/plugins/chat/lib/chat/schemas/message_blocks.rbindex a631a814ef9..672b37f9452 100644--- a/plugins/chat/lib/chat/schemas/message_blocks.rb+++ b/plugins/chat/lib/chat/schemas/message_blocks.rb@@ -47,6 +47,46 @@ module ChatadditionalProperties: false,}+ CategoryV1 = {+ type: "object",+ properties: {+ schema_version: {+ type: "integer",+ },+ type: {+ type: "string",+ enum: ["category"],+ },+ title: {+ type: "string",+ maxLength: 50,+ },+ color: {+ type: "string",+ pattern: "^[0-9A-Fa-f]{6}$",+ },+ description: {+ type: "string",+ maxLength: 500,+ },+ url: {+ type: "string",+ maxLength: 500,+ pattern: "^(https://|/)",+ },+ parent_name: {+ type: "string",+ maxLength: 50,+ },+ parent_color: {+ type: "string",+ pattern: "^[0-9A-Fa-f]{6}$",+ },+ },+ required: %w[schema_version type title color],+ additionalProperties: false,+ }+ActionsV1 = {type: "object",properties: {@@ -73,6 +113,32 @@ module ChatadditionalProperties: false,}- MessageBlocks = { type: "array", maxItems: 5, items: { oneOf: [ActionsV1] } }+ InformativeV1 = {+ type: "object",+ properties: {+ type: {+ type: "string",+ enum: ["informative"],+ },+ schema_version: {+ type: "integer",+ },+ block_id: {+ type: "string",+ maxLength: 255,+ },+ elements: {+ type: "array",+ maxItems: 10,+ items: {+ oneOf: [CategoryV1],+ },+ },+ },+ required: %w[schema_version type elements],+ additionalProperties: false,+ }++ MessageBlocks = { type: "array", maxItems: 5, items: { oneOf: [ActionsV1, InformativeV1] } }endenddiff --git a/plugins/chat/spec/models/chat/message_spec.rb b/plugins/chat/spec/models/chat/message_spec.rbindex 9b6d1298cee..759b87f905b 100644--- a/plugins/chat/spec/models/chat/message_spec.rb+++ b/plugins/chat/spec/models/chat/message_spec.rb@@ -85,6 +85,9 @@ describe Chat::Message do["value at `/0/elements/0/type` is not one of: [\"button\"]","object at `/0/elements/0` is missing required properties: text",+ "value at `/0/type` is not one of: [\"informative\"]",+ "value at `/0/elements/0/type` is not one of: [\"category\"]",+ "object at `/0/elements/0` is missing required properties: title, color",],)enddiff --git a/plugins/chat/spec/system/chat_informative_blocks_spec.rb b/plugins/chat/spec/system/chat_informative_blocks_spec.rbnew file mode 100644index 00000000000..28c595b07b0--- /dev/null+++ b/plugins/chat/spec/system/chat_informative_blocks_spec.rb@@ -0,0 +1,195 @@+# frozen_string_literal: true++RSpec.describe "Informative blocks in chat messages", type: :system do+ fab!(:user)+ fab!(:channel_1, :chat_channel)++ let(:chat_page) { PageObjects::Pages::Chat.new }++ before do+ chat_system_bootstrap+ channel_1.add(user)+ sign_in(user)+ end++ context "with a simple category element" do+ fab!(:message_1) do+ Fabricate(+ :chat_message,+ user: Discourse.system_user,+ chat_channel: channel_1,+ blocks: [+ {+ type: "informative",+ elements: [{ type: "category", title: "Support", color: "0088CC" }],+ },+ ],+ )+ end++ it "renders the category title" do+ chat_page.visit_channel(channel_1)++ expect(page).to have_css(".block__category")+ expect(page).to have_css(".badge-category__name", text: "Support")+ end+ end++ context "with a category element with description" do+ fab!(:message_1) do+ Fabricate(+ :chat_message,+ user: Discourse.system_user,+ chat_channel: channel_1,+ blocks: [+ {+ type: "informative",+ elements: [+ {+ type: "category",+ title: "Support",+ color: "0088CC",+ description: "Get help from the community",+ },+ ],+ },+ ],+ )+ end++ it "renders the category description" do+ chat_page.visit_channel(channel_1)++ expect(page).to have_css(".block__category-description", text: "Get help from the community")+ end+ end++ context "with a category element with a relative URL" do+ fab!(:message_1) do+ Fabricate(+ :chat_message,+ user: Discourse.system_user,+ chat_channel: channel_1,+ blocks: [+ {+ type: "informative",+ elements: [{ type: "category", title: "Support", color: "0088CC", url: "/c/support" }],+ },+ ],+ )+ end++ it "renders a link wrapping the category badge" do+ chat_page.visit_channel(channel_1)++ expect(page).to have_css("a.block__category-link[href='/c/support']")+ expect(page).to have_css("a.block__category-link .badge-category__name", text: "Support")+ end+ end++ context "with a category element with an https:// URL" do+ fab!(:message_1) do+ Fabricate(+ :chat_message,+ user: Discourse.system_user,+ chat_channel: channel_1,+ blocks: [+ {+ type: "informative",+ elements: [+ { type: "category", title: "Support", color: "0088CC", url: "https://discourse.org" },+ ],+ },+ ],+ )+ end++ it "renders a link wrapping the category badge" do+ chat_page.visit_channel(channel_1)++ expect(page).to have_css("a.block__category-link[href='https://discourse.org']")+ expect(page).to have_css("a.block__category-link .badge-category__name", text: "Support")+ end+ end++ context "with a category element with a javascript: URL" do+ it "rejects the message as invalid" do+ message =+ Chat::Message.new(+ user: Discourse.system_user,+ chat_channel: channel_1,+ message: "Here are some categories:",+ blocks: [+ {+ type: "informative",+ elements: [+ { type: "category", title: "Support", color: "0088CC", url: "javascript:alert(1)" },+ ],+ },+ ],+ )++ expect(message).not_to be_valid+ expect(message.errors[:blocks]).to be_present+ end+ end++ context "with a category element with a parent category" do+ fab!(:message_1) do+ Fabricate(+ :chat_message,+ user: Discourse.system_user,+ chat_channel: channel_1,+ blocks: [+ {+ type: "informative",+ elements: [+ {+ type: "category",+ title: "Dev",+ color: "E45735",+ description: "Development related discussions",+ parent_name: "Meta",+ parent_color: "0088CC",+ },+ ],+ },+ ],+ )+ end++ it "renders the child category in the heading and parent in its own row" do+ chat_page.visit_channel(channel_1)++ expect(page).to have_css(".block__category-heading .badge-category__name", text: "Dev")+ expect(page).to have_css(".block__category-parent .badge-category__name", text: "Meta")+ end+ end++ context "with multiple category elements in one block" do+ fab!(:message_1) do+ Fabricate(+ :chat_message,+ user: Discourse.system_user,+ chat_channel: channel_1,+ blocks: [+ {+ type: "informative",+ elements: [+ { type: "category", title: "Support", color: "0088CC" },+ { type: "category", title: "Feedback", color: "25AAE2" },+ ],+ },+ ],+ )+ end++ it "renders all category elements" do+ chat_page.visit_channel(channel_1)++ expect(page).to have_css(".block__informative-item", count: 2)+ expect(page).to have_css(".badge-category__name", text: "Support")+ expect(page).to have_css(".badge-category__name", text: "Feedback")+ end+ end+end