From db880d8ed71a0c05ecc0bf8fd1a3a4d08f9a70df Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Fri, 27 Oct 2023 10:27:34 +0200 Subject: [PATCH] DEV: adds a `:chat_thread_created` trigger (#24133) Usage: ```ruby DiscourseEvent.on(:chat_thread_created) do |thread| end ``` --- plugins/chat/app/services/chat/create_thread.rb | 5 +++++ plugins/chat/spec/services/chat/create_thread_spec.rb | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/plugins/chat/app/services/chat/create_thread.rb b/plugins/chat/app/services/chat/create_thread.rb index 683d895bb6d..090009bb960 100644 --- a/plugins/chat/app/services/chat/create_thread.rb +++ b/plugins/chat/app/services/chat/create_thread.rb @@ -26,6 +26,7 @@ module Chat step :associate_thread_to_message step :fetch_membership step :publish_new_thread + step :trigger_chat_thread_created_event end # @!visibility private @@ -84,5 +85,9 @@ module Chat def publish_new_thread(channel:, original_message:, **) ::Chat::Publisher.publish_thread_created!(channel, original_message, context.thread.id) end + + def trigger_chat_thread_created_event + ::DiscourseEvent.trigger(:chat_thread_created, context.thread) + end end end diff --git a/plugins/chat/spec/services/chat/create_thread_spec.rb b/plugins/chat/spec/services/chat/create_thread_spec.rb index c73a202fa89..c7cb6cc9d43 100644 --- a/plugins/chat/spec/services/chat/create_thread_spec.rb +++ b/plugins/chat/spec/services/chat/create_thread_spec.rb @@ -51,6 +51,12 @@ RSpec.describe Chat::CreateThread do expect(message.data["type"]).to eq("thread_created") end + it "triggers a discourse event `chat_thread_created`" do + event = DiscourseEvent.track_events(:chat_thread_created) { result }.first + + expect(event[:params][0]).to eq(result.thread) + end + it "sets the title when existing" do params[:title] = "Restaurant for Saturday" result