A platform for community discussion. Free, open, simple.
FEATURE: Remap channel hashtags when a chat channel slug changes
Régis Hanold347c2ad4474
diff --git a/app/services/hashtag_remapper.rb b/app/services/hashtag_remapper.rbindex daa98e7bf0d..52e1c819107 100644--- a/app/services/hashtag_remapper.rb+++ b/app/services/hashtag_remapper.rb@@ -13,7 +13,7 @@ class HashtagRemapperTagLocalizationStore,SiteSettingLocalizationStore,CategoryLocalizationStore,- ]+ ] | DiscoursePluginRegistry.hashtag_content_stores.to_aenddef self.enqueue(remaps)diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rbindex 10f0e8dac97..be6fcec0f56 100644--- a/lib/discourse_plugin_registry.rb+++ b/lib/discourse_plugin_registry.rb@@ -119,6 +119,7 @@ class DiscoursePluginRegistrydefine_filtered_register :user_destroyer_on_content_deletion_callbacksdefine_filtered_register :hashtag_autocomplete_data_sources+ define_filtered_register :hashtag_content_storesdefine_filtered_register :hashtag_autocomplete_contextual_type_prioritiesdefine_filtered_register :search_groups_set_query_callbacksdiff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rbindex 90e0464aa34..797447bb766 100644--- a/lib/plugin/instance.rb+++ b/lib/plugin/instance.rb@@ -1448,6 +1448,14 @@ class Plugin::InstanceDiscoursePluginRegistry.register_hashtag_autocomplete_data_source(klass, self)end+ def register_hashtag_content_store(klass)+ if !(klass < HashtagRemapper::Store)+ raise ArgumentError.new("Hashtag content stores must inherit from HashtagRemapper::Store")+ end++ DiscoursePluginRegistry.register_hashtag_content_store(klass, self)+ end+### Used to set up the priority ordering of hashtag autocomplete results by# type using HashtagAutocompleteService.diff --git a/plugins/chat/app/models/chat/channel.rb b/plugins/chat/app/models/chat/channel.rbindex 7e666ffaac9..b1ec9d91258 100644--- a/plugins/chat/app/models/chat/channel.rb+++ b/plugins/chat/app/models/chat/channel.rb@@ -46,6 +46,7 @@ module Chatvalidates :emoji, length: { maximum: 100 }validate :ensure_slug_ok, if: :slug_changed?before_validation :generate_auto_slug+ after_update :enqueue_channel_hashtag_remap, if: :saved_change_to_slug?scope :with_categories,-> do@@ -111,6 +112,13 @@ module Chat%i[allowed_user_ids allowed_group_ids chatable_url].each { |name| define_method(name) { nil } }+ def enqueue_channel_hashtag_remap+ old_ref = slug_before_last_save+ return if old_ref.blank? || old_ref.casecmp?(slug.to_s)++ HashtagRemapper.enqueue([{ type: Chat::ChannelHashtagDataSource.type, id:, old_ref: }])+ end+def ensure_slug_okif self.slug.present?# if we don't unescape it first we strip the % from the encoded versiondiff --git a/plugins/chat/app/models/chat/message.rb b/plugins/chat/app/models/chat/message.rbindex b823644f815..c61cefba88f 100644--- a/plugins/chat/app/models/chat/message.rb+++ b/plugins/chat/app/models/chat/message.rb@@ -282,10 +282,8 @@ module Chatentity]- def self.cook(message, opts = {})+ def self.markdown_options(opts = {})bot = opts[:user_id] && opts[:user_id].negative?- slash_command = match_slash_command(message, opts[:author_username])- message_to_cook = slash_command ? slash_command[:content] : messagefeatures = MARKDOWN_FEATURES.dupfeatures << "image-grid" if bot@@ -300,15 +298,20 @@ module Chat# this when cooking #hashtags to determine whether we should render# the found hashtag based on whether the user can access the channel it# is referencing.- cooked =- PrettyText.cook(- message_to_cook,- features_override: features + DiscoursePluginRegistry.chat_markdown_features.to_a,- markdown_it_rules: rules,- force_quote_link: true,- user_id: opts[:user_id],- hashtag_context: "chat-composer",- )+ {+ features_override: features + DiscoursePluginRegistry.chat_markdown_features.to_a,+ markdown_it_rules: rules,+ force_quote_link: true,+ user_id: opts[:user_id],+ hashtag_context: "chat-composer",+ }+ end++ def self.cook(message, opts = {})+ slash_command = match_slash_command(message, opts[:author_username])+ message_to_cook = slash_command ? slash_command[:content] : message++ cooked = PrettyText.cook(message_to_cook, markdown_options(opts))cooked = format_slash_command(cooked, slash_command, opts[:author_username]) if slash_commandresult =diff --git a/plugins/chat/lib/chat/channel_hashtag_data_source.rb b/plugins/chat/lib/chat/channel_hashtag_data_source.rbindex 4ac7cc58733..f2a0959e378 100644--- a/plugins/chat/lib/chat/channel_hashtag_data_source.rb+++ b/plugins/chat/lib/chat/channel_hashtag_data_source.rb@@ -14,6 +14,10 @@ module Chat"channel"end+ def self.ref_for(channel_id)+ Chat::Channel.where(id: channel_id, deleted_at: nil).pick(:slug)+ end+def self.channel_to_hashtag_item(guardian, channel)HashtagAutocompleteService::HashtagItem.new.tap do |item|item.text = channel.titlediff --git a/plugins/chat/lib/chat/message_hashtag_store.rb b/plugins/chat/lib/chat/message_hashtag_store.rbnew file mode 100644index 00000000000..d6ecaf99cb9--- /dev/null+++ b/plugins/chat/lib/chat/message_hashtag_store.rb@@ -0,0 +1,21 @@+# frozen_string_literal: true++module Chat+ class MessageHashtagStore < HashtagRemapper::Store+ def self.key = "chat_message"+ def self.relation = Chat::Message.where(deleted_at: nil)+ def self.raw_column = :message+ def self.cooked(message) = message.cooked+ def self.hashtag_context = "chat-composer"+ def self.cook_options(message) = Chat::Message.markdown_options(user_id: message.last_editor_id)++ def self.write!(message, raw)+ message.message = raw+ message.cook+ message.excerpt = message.build_excerpt+ message.save!(validate: false)+ message.rebake!(skip_notifications: true, priority: :ultra_low)+ SearchIndexer.index(message)+ end+ end+enddiff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rbindex a529093629d..2f4f54c7332 100644--- a/plugins/chat/plugin.rb+++ b/plugins/chat/plugin.rb@@ -613,6 +613,7 @@ after_initialize do# Make sure to update spec/system/hashtag_autocomplete_spec.rb when changing this.register_hashtag_data_source(Chat::ChannelHashtagDataSource)+ register_hashtag_content_store(Chat::MessageHashtagStore)register_hashtag_type_priority_for_context("channel", "chat-composer", 200)register_hashtag_type_priority_for_context("category", "chat-composer", 100)register_hashtag_type_priority_for_context("tag", "chat-composer", 50)diff --git a/plugins/chat/spec/lib/chat/message_hashtag_store_spec.rb b/plugins/chat/spec/lib/chat/message_hashtag_store_spec.rbnew file mode 100644index 00000000000..5697f0a4d27--- /dev/null+++ b/plugins/chat/spec/lib/chat/message_hashtag_store_spec.rb@@ -0,0 +1,106 @@+# frozen_string_literal: true++RSpec.describe Chat::MessageHashtagStore do+ fab!(:channel) { Fabricate(:category_channel, slug: "support") }++ before do+ SiteSetting.chat_enabled = true+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ end++ def remap_channel(old_ref)+ HashtagRemapper.new(+ type: "channel",+ record_id: channel.id,+ old_ref:,+ new_ref: channel.reload.slug,+ ).remap!+ end++ describe "Chat::ChannelHashtagDataSource.ref_for" do+ it "returns the channel slug" do+ expect(Chat::ChannelHashtagDataSource.ref_for(channel.id)).to eq("support")+ end++ it "returns nothing for a trashed channel" do+ channel.trash!++ expect(Chat::ChannelHashtagDataSource.ref_for(channel.id)).to be_nil+ end+ end++ describe "the rename callback" do+ it "enqueues a remap when the slug changes" do+ expect_enqueued_with(+ job: :remap_hashtag,+ args: {+ remaps: [{ type: "channel", id: channel.id, old_ref: "support" }],+ },+ ) { channel.update!(slug: "help") }+ end++ it "does not enqueue a remap for unrelated changes" do+ expect_not_enqueued_with(job: :remap_hashtag) { channel.update!(description: "hello") }+ end++ it "does not rewrite anything to the tombstone slug when a channel is trashed" do+ post = create_post(raw: "See #support for details.")++ Chat::TrashChannel.call(+ params: {+ channel_id: channel.id,+ },+ guardian: Discourse.system_user.guardian,+ )++ Jobs::RemapHashtag.new.execute(+ remaps: [{ "type" => "channel", "id" => channel.id, "old_ref" => "support" }],+ )++ expect(post.reload.raw).to eq("See #support for details.")+ end+ end++ describe "the chat message store" do+ it "rewrites a channel reference in a chat message" do+ message = Fabricate(:chat_message, chat_channel: channel, message: "See #support here.")++ expect(message.cooked).to include(%{data-type="channel"}, %{data-id="#{channel.id}"})++ channel.update!(slug: "help")+ remap_channel("support")++ message.reload+ expect(message.message).to eq("See #help here.")+ expect(message.cooked).to include(%{data-id="#{channel.id}"})+ expect(message.excerpt).to be_present+ end++ it "leaves a reference inside a code fence alone" do+ fenced =+ Fabricate(+ :chat_message,+ chat_channel: channel,+ message: "Live #support\n\n```\nfence #support\n```",+ )++ channel.update!(slug: "help")+ remap_channel("support")++ expect(fenced.reload.message).to eq("Live #help\n\n```\nfence #support\n```")+ end+ end++ describe "across stores" do+ it "rewrites a channel reference in a post" do+ post = create_post(raw: "See #support for details.")++ expect(post.cooked).to include(%{data-type="channel"})++ channel.update!(slug: "help")+ remap_channel("support")++ expect(post.reload.raw).to eq("See #help for details.")+ end+ end+end