From 6e672557faa92391c16dd53f824982cbdb7f6f15 Mon Sep 17 00:00:00 2001 From: David Battersby Date: Tue, 12 Sep 2023 14:52:50 +0800 Subject: [PATCH] DEV: add maxlength to additional chat text columns (#23505) Add additional limits to text columns for chat. --- plugins/chat/app/models/chat/channel.rb | 2 ++ plugins/chat/app/models/chat/channel_archive.rb | 3 +++ plugins/chat/app/models/chat/incoming_webhook.rb | 6 ++++++ plugins/chat/app/models/chat/message_reaction.rb | 2 ++ plugins/chat/app/models/chat/reviewable_message.rb | 3 +++ plugins/chat/spec/models/chat/category_channel_spec.rb | 2 ++ plugins/chat/spec/models/chat/channel_archive_spec.rb | 6 ++++++ plugins/chat/spec/models/chat/channel_spec.rb | 2 ++ .../chat/spec/models/chat/direct_message_channel_spec.rb | 2 ++ plugins/chat/spec/models/chat/incoming_webhook_spec.rb | 6 ++++++ plugins/chat/spec/models/chat/message_reaction_spec.rb | 5 +++++ .../chat/spec/models/chat/reviewable_chat_message_spec.rb | 3 +++ 12 files changed, 42 insertions(+) create mode 100644 plugins/chat/spec/models/chat/channel_archive_spec.rb create mode 100644 plugins/chat/spec/models/chat/message_reaction_spec.rb diff --git a/plugins/chat/app/models/chat/channel.rb b/plugins/chat/app/models/chat/channel.rb index e9d6f1a300f..55e7f7dd0a7 100644 --- a/plugins/chat/app/models/chat/channel.rb +++ b/plugins/chat/app/models/chat/channel.rb @@ -37,6 +37,8 @@ module Chat presence: true, allow_nil: true validates :description, length: { maximum: 500 } + validates :chatable_type, length: { maximum: 100 } + validates :type, length: { maximum: 100 } validates :slug, length: { maximum: 100 } validate :ensure_slug_ok, if: :slug_changed? before_validation :generate_auto_slug diff --git a/plugins/chat/app/models/chat/channel_archive.rb b/plugins/chat/app/models/chat/channel_archive.rb index e8c88b4b932..c2ca472c824 100644 --- a/plugins/chat/app/models/chat/channel_archive.rb +++ b/plugins/chat/app/models/chat/channel_archive.rb @@ -6,6 +6,9 @@ module Chat belongs_to :archived_by, class_name: "User" belongs_to :destination_topic, class_name: "Topic" + validates :archive_error, length: { maximum: 1000 } + validates :destination_topic_title, length: { maximum: 1000 } + self.table_name = "chat_channel_archives" def complete? diff --git a/plugins/chat/app/models/chat/incoming_webhook.rb b/plugins/chat/app/models/chat/incoming_webhook.rb index a7f447fbd68..34a62c90a22 100644 --- a/plugins/chat/app/models/chat/incoming_webhook.rb +++ b/plugins/chat/app/models/chat/incoming_webhook.rb @@ -12,6 +12,12 @@ module Chat before_create { self.key = SecureRandom.hex(12) } + validates :name, presence: true, length: { maximum: 100 } + validates :key, length: { maximum: 100 } + validates :username, length: { maximum: 100 } + validates :description, length: { maximum: 500 } + validates :emoji, length: { maximum: 100 } + def url "#{Discourse.base_url}/chat/hooks/#{key}.json" end diff --git a/plugins/chat/app/models/chat/message_reaction.rb b/plugins/chat/app/models/chat/message_reaction.rb index 3b378dd0481..cb1e69cdd9d 100644 --- a/plugins/chat/app/models/chat/message_reaction.rb +++ b/plugins/chat/app/models/chat/message_reaction.rb @@ -6,6 +6,8 @@ module Chat belongs_to :chat_message, class_name: "Chat::Message" belongs_to :user + + validates :emoji, length: { maximum: 100 } end end diff --git a/plugins/chat/app/models/chat/reviewable_message.rb b/plugins/chat/app/models/chat/reviewable_message.rb index 3110f1de73b..942e74c4aea 100644 --- a/plugins/chat/app/models/chat/reviewable_message.rb +++ b/plugins/chat/app/models/chat/reviewable_message.rb @@ -2,6 +2,9 @@ module Chat class ReviewableMessage < Reviewable + validates :type, length: { maximum: 100 } + validates :target_type, length: { maximum: 100 } + def serializer Chat::ReviewableMessageSerializer end diff --git a/plugins/chat/spec/models/chat/category_channel_spec.rb b/plugins/chat/spec/models/chat/category_channel_spec.rb index 5e275db5096..b527bae77da 100644 --- a/plugins/chat/spec/models/chat/category_channel_spec.rb +++ b/plugins/chat/spec/models/chat/category_channel_spec.rb @@ -9,6 +9,8 @@ RSpec.describe Chat::CategoryChannel do it { is_expected.to delegate_method(:url).to(:chatable).with_prefix } it { is_expected.to validate_length_of(:description).is_at_most(500) } it { is_expected.to validate_length_of(:slug).is_at_most(100) } + it { is_expected.to validate_length_of(:chatable_type).is_at_most(100) } + it { is_expected.to validate_length_of(:type).is_at_most(100) } describe "#category_channel?" do it "always returns true" do diff --git a/plugins/chat/spec/models/chat/channel_archive_spec.rb b/plugins/chat/spec/models/chat/channel_archive_spec.rb new file mode 100644 index 00000000000..9d99abc1715 --- /dev/null +++ b/plugins/chat/spec/models/chat/channel_archive_spec.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +RSpec.describe Chat::ChannelArchive do + it { is_expected.to validate_length_of(:archive_error).is_at_most(1000) } + it { is_expected.to validate_length_of(:destination_topic_title).is_at_most(1000) } +end diff --git a/plugins/chat/spec/models/chat/channel_spec.rb b/plugins/chat/spec/models/chat/channel_spec.rb index 9f4da9ce9f8..a596f84d417 100644 --- a/plugins/chat/spec/models/chat/channel_spec.rb +++ b/plugins/chat/spec/models/chat/channel_spec.rb @@ -8,6 +8,8 @@ RSpec.describe Chat::Channel do it { is_expected.to validate_length_of(:description).is_at_most(500) } it { is_expected.to validate_length_of(:slug).is_at_most(100) } + it { is_expected.to validate_length_of(:chatable_type).is_at_most(100) } + it { is_expected.to validate_length_of(:type).is_at_most(100) } describe ".find_by_id_or_slug" do subject(:find_channel) { described_class.find_by_id_or_slug(channel_id) } diff --git a/plugins/chat/spec/models/chat/direct_message_channel_spec.rb b/plugins/chat/spec/models/chat/direct_message_channel_spec.rb index f3674bcd580..83a9642a1e2 100644 --- a/plugins/chat/spec/models/chat/direct_message_channel_spec.rb +++ b/plugins/chat/spec/models/chat/direct_message_channel_spec.rb @@ -7,6 +7,8 @@ RSpec.describe Chat::DirectMessageChannel do it { is_expected.to delegate_method(:allowed_user_ids).to(:direct_message).as(:user_ids) } it { is_expected.to validate_length_of(:description).is_at_most(500) } + it { is_expected.to validate_length_of(:chatable_type).is_at_most(100) } + it { is_expected.to validate_length_of(:type).is_at_most(100) } describe "#category_channel?" do it "always returns false" do diff --git a/plugins/chat/spec/models/chat/incoming_webhook_spec.rb b/plugins/chat/spec/models/chat/incoming_webhook_spec.rb index 197d0b1ccd6..37c82e07d57 100644 --- a/plugins/chat/spec/models/chat/incoming_webhook_spec.rb +++ b/plugins/chat/spec/models/chat/incoming_webhook_spec.rb @@ -7,4 +7,10 @@ RSpec.describe Chat::IncomingWebhook do .class_name("Chat::WebhookEvent") .dependent(:delete_all) end + + it { is_expected.to validate_length_of(:name).is_at_most(100) } + it { is_expected.to validate_length_of(:key).is_at_most(100) } + it { is_expected.to validate_length_of(:username).is_at_most(100) } + it { is_expected.to validate_length_of(:description).is_at_most(500) } + it { is_expected.to validate_length_of(:emoji).is_at_most(100) } end diff --git a/plugins/chat/spec/models/chat/message_reaction_spec.rb b/plugins/chat/spec/models/chat/message_reaction_spec.rb new file mode 100644 index 00000000000..49801e8ad16 --- /dev/null +++ b/plugins/chat/spec/models/chat/message_reaction_spec.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +RSpec.describe Chat::MessageReaction do + it { is_expected.to validate_length_of(:emoji).is_at_most(100) } +end diff --git a/plugins/chat/spec/models/chat/reviewable_chat_message_spec.rb b/plugins/chat/spec/models/chat/reviewable_chat_message_spec.rb index d7f6a951018..a8f5607a9ed 100644 --- a/plugins/chat/spec/models/chat/reviewable_chat_message_spec.rb +++ b/plugins/chat/spec/models/chat/reviewable_chat_message_spec.rb @@ -11,6 +11,9 @@ RSpec.describe Chat::ReviewableMessage, type: :model do Fabricate(:chat_reviewable_message, target: chat_message, created_by: moderator) end + it { is_expected.to validate_length_of(:type).is_at_most(100) } + it { is_expected.to validate_length_of(:target_type).is_at_most(100) } + it "agree_and_keep agrees with the flag and doesn't delete the message" do reviewable.perform(moderator, :agree_and_keep_message)