From a2aea63ae9306d4f29713a6e31bcf263dd775afc Mon Sep 17 00:00:00 2001 From: chr Date: Mon, 12 Nov 2018 20:30:47 -0800 Subject: [PATCH] Bio length -> 413 characters Increase the cybre.space profile bio text length limit to 413 characters. Also modifies the account settings page to automatically resize the textbox to the size of the contained text, so that it's easier to type longer bios. --- app/javascript/packs/public.js | 33 ++++++++++++++++++++++ app/models/account.rb | 13 +++++---- app/views/settings/profiles/show.html.haml | 2 +- spec/models/account_spec.rb | 8 +++--- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/app/javascript/packs/public.js b/app/javascript/packs/public.js index 4ab27c769..c412f8263 100644 --- a/app/javascript/packs/public.js +++ b/app/javascript/packs/public.js @@ -18,6 +18,12 @@ window.addEventListener('message', e => { id: data.id, height: document.getElementsByTagName('html')[0].scrollHeight, }, '*'); + + if (document.fonts && document.fonts.ready) { + document.fonts.ready.then(sizeBioText); + } else { + sizeBioText(); + } }); }); @@ -116,6 +122,17 @@ function main() { document.head.appendChild(scrollbarWidthStyle); scrollbarWidthStyle.sheet.insertRule(`body.with-modals--active { margin-right: ${scrollbarWidth}px; }`, 0); } + + [].forEach.call(document.querySelectorAll('[data-component="Card"]'), (content) => { + const props = JSON.parse(content.getAttribute('data-props')); + ReactDOM.render(, content); + }); + + if (document.fonts && document.fonts.ready) { + document.fonts.ready.then(sizeBioText); + } else { + sizeBioText(); + } }); delegate(document, '.webapp-btn', 'click', ({ target, button }) => { @@ -214,6 +231,22 @@ function main() { console.error(err); } }); + + delegate(document, '#account_note', 'input', sizeBioText); + + function sizeBioText() { + const noteCounter = document.querySelector('.note-counter'); + const bioTextArea = document.querySelector('#account_note'); + + if (noteCounter) { + noteCounter.textContent = 413 - length(bioTextArea.value); + } + + if (bioTextArea) { + bioTextArea.style.height = 'auto'; + bioTextArea.style.height = (bioTextArea.scrollHeight+3) + 'px'; + } + } } loadPolyfills().then(main).catch(error => { diff --git a/app/models/account.rb b/app/models/account.rb index 11a3c21fe..cc247262b 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -75,7 +75,8 @@ class Account < ApplicationRecord validates_with UniqueUsernameValidator, if: -> { local? && will_save_change_to_username? } validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? } validates :display_name, length: { maximum: 30 }, if: -> { local? && will_save_change_to_display_name? } - validates :note, note_length: { maximum: 160 }, if: -> { local? && will_save_change_to_note? } + validates :note, note_length: { maximum: 413 }, if: -> { local? && will_save_change_to_note? } + validate :note_has_eight_newlines?, if: -> { local? && will_save_change_to_note? } validates :fields, length: { maximum: 4 }, if: -> { local? && will_save_change_to_fields? } scope :remote, -> { where.not(domain: nil) } @@ -294,10 +295,8 @@ class Account < ApplicationRecord def save_with_optional_media! save! rescue ActiveRecord::RecordInvalid - self.avatar = nil - self.header = nil - self[:avatar_remote_url] = '' - self[:header_remote_url] = '' + self.avatar = nil if errors[:avatar].present? + self.header = nil if errors[:header].present? save! end @@ -321,6 +320,10 @@ class Account < ApplicationRecord shared_inbox_url.presence || inbox_url end + def note_has_eight_newlines? + errors.add(:note, 'Bio can\'t have more then 8 newlines') unless note.count("\n") <= 8 + end + class Field < ActiveModelSerializers::Model attributes :name, :value, :verified_at, :account, :errors diff --git a/app/views/settings/profiles/show.html.haml b/app/views/settings/profiles/show.html.haml index eb232dc57..23c77e680 100644 --- a/app/views/settings/profiles/show.html.haml +++ b/app/views/settings/profiles/show.html.haml @@ -7,7 +7,7 @@ .fields-row .fields-row__column.fields-group.fields-row__column-6 = f.input :display_name, wrapper: :with_label, input_html: { maxlength: 30 }, hint: false - = f.input :note, wrapper: :with_label, input_html: { maxlength: 160 }, hint: false + = f.input :note, wrapper: :with_label, input_html: { maxlength: 413 }, hint: false .fields-row .fields-row__column.fields-row__column-6 diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index f7f78d34c..c98530b58 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -596,8 +596,8 @@ RSpec.describe Account, type: :model do expect(account).to model_have_error_on_field(:display_name) end - it 'is invalid if the note is longer than 160 characters' do - account = Fabricate.build(:account, note: Faker::Lorem.characters(161)) + it 'is invalid if the note is longer than 413 characters' do + account = Fabricate.build(:account, note: Faker::Lorem.characters(414)) account.valid? expect(account).to model_have_error_on_field(:note) end @@ -642,8 +642,8 @@ RSpec.describe Account, type: :model do expect(account).not_to model_have_error_on_field(:display_name) end - it 'is valid even if the note is longer than 160 characters' do - account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(161)) + it 'is valid even if the note is longer than 413 characters' do + account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(414)) account.valid? expect(account).not_to model_have_error_on_field(:note) end