From 9e901dbfbf4c26e0b5346f795fb5691985b4a0fb Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Fri, 16 Jun 2023 14:37:16 -0300 Subject: [PATCH] FIX: Serialize channel title for DMs (#90) --- app/serializers/ai_chat_channel_serializer.rb | 8 +++++- .../ai_chat_channel_serializer_spec.rb | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 spec/serializers/ai_chat_channel_serializer_spec.rb diff --git a/app/serializers/ai_chat_channel_serializer.rb b/app/serializers/ai_chat_channel_serializer.rb index ae5fafb5..260f4b3f 100644 --- a/app/serializers/ai_chat_channel_serializer.rb +++ b/app/serializers/ai_chat_channel_serializer.rb @@ -1,5 +1,11 @@ # frozen_string_literal: true class AiChatChannelSerializer < ApplicationSerializer - attributes :id, :chatable, :chatable_type, :chatable_url, :title, :slug + attributes :id, :chatable, :chatable_type, :chatable_url, :slug + + def title + # Display all participants for a DM. + # For category channels, the argument is ignored. + object.title(nil) + end end diff --git a/spec/serializers/ai_chat_channel_serializer_spec.rb b/spec/serializers/ai_chat_channel_serializer_spec.rb new file mode 100644 index 00000000..0ef48983 --- /dev/null +++ b/spec/serializers/ai_chat_channel_serializer_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +RSpec.describe AiChatChannelSerializer do + fab!(:admin) { Fabricate(:admin) } + + describe "#title" do + context "when the channel is a DM" do + fab!(:dm_channel) { Fabricate(:direct_message_channel) } + + it "display every participant" do + serialized = described_class.new(dm_channel, scope: Guardian.new(admin), root: nil) + + expect(serialized.title).to eq(dm_channel.title(nil)) + end + end + + context "when the channel is a regular one" do + fab!(:channel) { Fabricate(:chat_channel) } + + it "displays the category title" do + serialized = described_class.new(channel, scope: Guardian.new(admin), root: nil) + + expect(serialized.title).to eq(channel.title) + end + end + end +end