A platform for community discussion. Free, open, simple.
FIX: Cap bookmark name at 100 chars and truncate existing names (#10189)
Martin Brennan6be7a66ba7ab
diff --git a/app/assets/javascripts/discourse/app/templates/modal/bookmark.hbs b/app/assets/javascripts/discourse/app/templates/modal/bookmark.hbsindex d3811ac9abd..17f41b174ef 100644--- a/app/assets/javascripts/discourse/app/templates/modal/bookmark.hbs+++ b/app/assets/javascripts/discourse/app/templates/modal/bookmark.hbs@@ -9,7 +9,7 @@{{/if}}<div class="control-group bookmark-name-wrap">- {{input id="bookmark-name" value=model.name name="bookmark-name" class="bookmark-name" enter=(action "saveAndClose") placeholder=(i18n "post.bookmarks.name_placeholder")}}+ {{input id="bookmark-name" value=model.name name="bookmark-name" class="bookmark-name" enter=(action "saveAndClose") placeholder=(i18n "post.bookmarks.name_placeholder") maxlength="100"}}{{d-button icon="cog" action=(action "toggleOptionsPanel") class="bookmark-options-button"}}</div>diff --git a/app/models/bookmark.rb b/app/models/bookmark.rbindex c90af0f147b..32ef5169ff5 100644--- a/app/models/bookmark.rb+++ b/app/models/bookmark.rb@@ -12,6 +12,7 @@ class Bookmark < ActiveRecord::Basevalidate :unique_per_post_for_uservalidate :ensure_sane_reminder_at_time+ validates :name, length: { maximum: 100 }# we don't care whether the post or topic is deleted,# they hold important information about the bookmark@@ -83,7 +84,7 @@ end# user_id :bigint not null# topic_id :bigint not null# post_id :bigint not null-# name :string+# name :string(100)# reminder_type :integer# reminder_at :datetime# created_at :datetime not nulldiff --git a/db/migrate/20200708051009_cap_bookmark_name_at_100_characters.rb b/db/migrate/20200708051009_cap_bookmark_name_at_100_characters.rbnew file mode 100644index 00000000000..1428778531e--- /dev/null+++ b/db/migrate/20200708051009_cap_bookmark_name_at_100_characters.rb@@ -0,0 +1,12 @@+# frozen_string_literal: true++class CapBookmarkNameAt100Characters < ActiveRecord::Migration[6.0]+ def up+ DB.exec("UPDATE bookmarks SET name = LEFT(name, 100) WHERE name IS NOT NULL AND name <> LEFT(name, 100)")+ change_column :bookmarks, :name, :string, limit: 100+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/spec/lib/bookmark_manager_spec.rb b/spec/lib/bookmark_manager_spec.rbindex 6aabf798240..9d2a05f6db2 100644--- a/spec/lib/bookmark_manager_spec.rb+++ b/spec/lib/bookmark_manager_spec.rb@@ -75,6 +75,13 @@ RSpec.describe BookmarkManager doendend+ context "when the bookmark name is too long" do+ it "adds an error to the manager" do+ subject.create(post_id: post.id, name: "test" * 100)+ expect(subject.errors.full_messages).to include("Name is too long (maximum is 100 characters)")+ end+ end+context "when the reminder time is not provided when it needs to be" dolet(:reminder_at) { nil }it "adds an error to the manager" do