From 88eb96b3156dacda61922959ff676c1442a4ae17 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Mon, 4 Nov 2024 17:19:44 +0900 Subject: [PATCH] DEV: sets an icon_upload_id on a channel (#29566) There's no UI for it at the moment but when creating a channel or updating it, it's now possible to pass `icon_upload_id` as param. This will be available on the channel as `icon_upload_url`. --- plugins/chat/app/models/chat/channel.rb | 6 ++++-- .../chat/app/serializers/chat/channel_serializer.rb | 7 ++++++- .../chat/app/services/chat/create_category_channel.rb | 10 +++++++++- .../services/chat/create_direct_message_channel.rb | 9 ++++++--- plugins/chat/app/services/chat/update_channel.rb | 2 ++ .../discourse/components/channel-icon/index.gjs | 6 +++++- .../discourse/initializers/chat-sidebar.js | 8 ++++++-- .../javascripts/discourse/models/chat-channel.js | 4 ++++ .../assets/stylesheets/common/chat-channel-icon.scss | 6 ++++++ ...241104053309_add_icon_upload_id_to_chat_channel.rb | 7 +++++++ plugins/chat/lib/chat/channel_fetcher.rb | 1 + .../services/chat/create_category_channel_spec.rb | 7 +++++-- .../chat/create_direct_message_channel_spec.rb | 11 ++++++++++- .../chat/spec/services/chat/update_channel_spec.rb | 3 +++ 14 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 plugins/chat/db/migrate/20241104053309_add_icon_upload_id_to_chat_channel.rb diff --git a/plugins/chat/app/models/chat/channel.rb b/plugins/chat/app/models/chat/channel.rb index 808fac24df2..29fc80979c6 100644 --- a/plugins/chat/app/models/chat/channel.rb +++ b/plugins/chat/app/models/chat/channel.rb @@ -28,6 +28,7 @@ module Chat class_name: "Chat::Message", foreign_key: :last_message_id, optional: true + has_one :icon_upload, class_name: "Upload", foreign_key: :id, primary_key: :icon_upload_id def last_message super || NullMessage.new @@ -300,12 +301,13 @@ end # user_count :integer default(0), not null # auto_join_users :boolean default(FALSE), not null # user_count_stale :boolean default(FALSE), not null -# slug :string # type :string +# slug :string # allow_channel_wide_mentions :boolean default(TRUE), not null # messages_count :integer default(0), not null # threading_enabled :boolean default(FALSE), not null # last_message_id :bigint +# icon_upload_id :integer # # Indexes # @@ -313,6 +315,6 @@ end # index_chat_channels_on_chatable_id_and_chatable_type (chatable_id,chatable_type) # index_chat_channels_on_last_message_id (last_message_id) # index_chat_channels_on_messages_count (messages_count) -# index_chat_channels_on_slug (slug) UNIQUE +# index_chat_channels_on_slug (slug) UNIQUE WHERE ((slug)::text <> ''::text) # index_chat_channels_on_status (status) # diff --git a/plugins/chat/app/serializers/chat/channel_serializer.rb b/plugins/chat/app/serializers/chat/channel_serializer.rb index a6c4b87bea7..c0193a17b12 100644 --- a/plugins/chat/app/serializers/chat/channel_serializer.rb +++ b/plugins/chat/app/serializers/chat/channel_serializer.rb @@ -21,7 +21,8 @@ module Chat :memberships_count, :current_user_membership, :meta, - :threading_enabled + :threading_enabled, + :icon_upload_url has_one :last_message, serializer: Chat::LastMessageSerializer, embed: :objects @@ -32,6 +33,10 @@ module Chat @current_user_membership = opts[:membership] end + def icon_upload_url + object.icon_upload&.url + end + def include_description? object.description.present? end diff --git a/plugins/chat/app/services/chat/create_category_channel.rb b/plugins/chat/app/services/chat/create_category_channel.rb index 3484f80f5a1..c884c00442f 100644 --- a/plugins/chat/app/services/chat/create_category_channel.rb +++ b/plugins/chat/app/services/chat/create_category_channel.rb @@ -38,6 +38,7 @@ module Chat attribute :category_id, :integer attribute :auto_join_users, :boolean, default: false attribute :threading_enabled, :boolean, default: false + attribute :icon_upload_id, :integer before_validation do self.auto_join_users = auto_join_users.presence || false @@ -76,7 +77,14 @@ module Chat def create_channel(category:, params:) category.create_chat_channel( user_count: 1, - **params.slice(:name, :slug, :description, :auto_join_users, :threading_enabled), + **params.slice( + :name, + :slug, + :icon_upload_id, + :description, + :auto_join_users, + :threading_enabled, + ), ) end diff --git a/plugins/chat/app/services/chat/create_direct_message_channel.rb b/plugins/chat/app/services/chat/create_direct_message_channel.rb index 06a412607dd..79c28e56a15 100644 --- a/plugins/chat/app/services/chat/create_direct_message_channel.rb +++ b/plugins/chat/app/services/chat/create_direct_message_channel.rb @@ -30,6 +30,7 @@ module Chat attribute :target_usernames, :array attribute :target_groups, :array attribute :upsert, :boolean, default: false + attribute :icon_upload_id, :integer validate :target_presence @@ -47,7 +48,7 @@ module Chat class_name: Chat::DirectMessageChannel::Policy::CanCommunicateAllParties model :direct_message, :fetch_or_create_direct_message model :channel, :fetch_or_create_channel - step :set_optional_name + step :set_optional_params step :update_memberships step :recompute_users_count @@ -93,8 +94,10 @@ module Chat ::Chat::DirectMessageChannel.find_or_create_by(chatable: direct_message) end - def set_optional_name(channel:, params:) - channel.update!(params.slice(:name)) if params.name&.size&.positive? + def set_optional_params(channel:, params:) + optional_params = + params.slice(:name, :icon_upload_id).reject { |_, value| value.nil? || value == "" } + channel.update!(optional_params) if !optional_params.empty? end def update_memberships(channel:, target_users:) diff --git a/plugins/chat/app/services/chat/update_channel.rb b/plugins/chat/app/services/chat/update_channel.rb index 3539555f656..2159c556444 100644 --- a/plugins/chat/app/services/chat/update_channel.rb +++ b/plugins/chat/app/services/chat/update_channel.rb @@ -29,6 +29,7 @@ module Chat # @option params [String,nil] :name # @option params [String,nil] :description # @option params [String,nil] :slug + # @option params [Integer,nil] :icon_upload_id # @option params [Boolean] :threading_enabled # @option params [Boolean] :auto_join_users Only valid for {CategoryChannel}. Whether active users with permission to see the category should automatically join the channel. # @option params [Boolean] :allow_channel_wide_mentions Allow the use of @here and @all in the channel. @@ -43,6 +44,7 @@ module Chat attribute :threading_enabled, :boolean, default: false attribute :auto_join_users, :boolean, default: false attribute :allow_channel_wide_mentions, :boolean, default: true + attribute :icon_upload_id, :integer, default: nil before_validation do assign_attributes( diff --git a/plugins/chat/assets/javascripts/discourse/components/channel-icon/index.gjs b/plugins/chat/assets/javascripts/discourse/components/channel-icon/index.gjs index fc463d4e4cc..a115ccd2bc4 100644 --- a/plugins/chat/assets/javascripts/discourse/components/channel-icon/index.gjs +++ b/plugins/chat/assets/javascripts/discourse/components/channel-icon/index.gjs @@ -26,7 +26,11 @@ export default class ChatChannelIcon extends Component {