Browse Source

Feature: increase bio length to 413 characters

Andrew 1 year ago
parent
commit
61183ac419

+ 21
- 7
app/javascript/packs/public.js View File

@@ -17,6 +17,12 @@ window.addEventListener('message', e => {
17 17
       id: data.id,
18 18
       height: document.getElementsByTagName('html')[0].scrollHeight,
19 19
     }, '*');
20
+
21
+    if (document.fonts && document.fonts.ready) {
22
+      document.fonts.ready.then(sizeBioText);
23
+    } else {
24
+      sizeBioText();
25
+    }
20 26
   });
21 27
 });
22 28
 
@@ -142,13 +148,7 @@ function main() {
142 148
     }
143 149
   });
144 150
 
145
-  delegate(document, '#account_note', 'input', ({ target }) => {
146
-    const noteCounter = document.querySelector('.note-counter');
147
-
148
-    if (noteCounter) {
149
-      noteCounter.textContent = 160 - length(target.value);
150
-    }
151
-  });
151
+  delegate(document, '#account_note', 'input', sizeBioText);
152 152
 
153 153
   delegate(document, '#account_avatar', 'change', ({ target }) => {
154 154
     const avatar = document.querySelector('.card .avatar img');
@@ -175,6 +175,20 @@ function main() {
175 175
       lock.style.display = 'none';
176 176
     }
177 177
   });
178
+
179
+  function sizeBioText() {
180
+    const noteCounter = document.querySelector('.note-counter');
181
+    const bioTextArea = document.querySelector('#account_note');
182
+
183
+    if (noteCounter) {
184
+      noteCounter.textContent = 413 - length(bioTextArea.value);
185
+    }
186
+
187
+    if (bioTextArea) {
188
+      bioTextArea.style.height = 'auto';
189
+      bioTextArea.style.height = (bioTextArea.scrollHeight+3) + 'px';
190
+    }
191
+  }
178 192
 }
179 193
 
180 194
 loadPolyfills().then(main).catch(error => {

+ 8
- 5
app/models/account.rb View File

@@ -75,7 +75,8 @@ class Account < ApplicationRecord
75 75
   validates_with UniqueUsernameValidator, if: -> { local? && will_save_change_to_username? }
76 76
   validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? }
77 77
   validates :display_name, length: { maximum: 30 }, if: -> { local? && will_save_change_to_display_name? }
78
-  validates :note, length: { maximum: 160 }, if: -> { local? && will_save_change_to_note? }
78
+  validates :note, length: { maximum: 413 }, if: -> { local? && will_save_change_to_note? }
79
+  validate :note_has_eight_newlines?, if: -> { local? && will_save_change_to_note? }
79 80
   validates :fields, length: { maximum: 4 }, if: -> { local? && will_save_change_to_fields? }
80 81
 
81 82
   # Timelines
@@ -266,10 +267,8 @@ class Account < ApplicationRecord
266 267
   def save_with_optional_media!
267 268
     save!
268 269
   rescue ActiveRecord::RecordInvalid
269
-    self.avatar              = nil
270
-    self.header              = nil
271
-    self[:avatar_remote_url] = ''
272
-    self[:header_remote_url] = ''
270
+    self.avatar = nil if errors[:avatar].present?
271
+    self.header = nil if errors[:header].present?
273 272
     save!
274 273
   end
275 274
 
@@ -293,6 +292,10 @@ class Account < ApplicationRecord
293 292
     shared_inbox_url.presence || inbox_url
294 293
   end
295 294
 
295
+  def note_has_eight_newlines?
296
+    errors.add(:note, 'Bio can\'t have more then 8 newlines') unless note.count("\n") <= 8
297
+  end
298
+
296 299
   class Field < ActiveModelSerializers::Model
297 300
     attributes :name, :value, :account, :errors
298 301
 

+ 1
- 1
app/views/settings/profiles/show.html.haml View File

@@ -6,7 +6,7 @@
6 6
 
7 7
   .fields-group
8 8
     = f.input :display_name, placeholder: t('simple_form.labels.defaults.display_name'), hint: t('simple_form.hints.defaults.display_name', count: 30 - @account.display_name.size).html_safe
9
-    = f.input :note, placeholder: t('simple_form.labels.defaults.note'), hint: t('simple_form.hints.defaults.note', count: 160 - @account.note.size).html_safe
9
+    = f.input :note, placeholder: t('simple_form.labels.defaults.note'), hint: t('simple_form.hints.defaults.note', count: 413 - @account.note.size).html_safe
10 10
 
11 11
   = render 'application/card', account: @account
12 12
 

+ 4
- 4
spec/models/account_spec.rb View File

@@ -596,8 +596,8 @@ RSpec.describe Account, type: :model do
596 596
         expect(account).to model_have_error_on_field(:display_name)
597 597
       end
598 598
 
599
-      it 'is invalid if the note is longer than 160 characters' do
600
-        account = Fabricate.build(:account, note: Faker::Lorem.characters(161))
599
+      it 'is invalid if the note is longer than 413 characters' do
600
+        account = Fabricate.build(:account, note: Faker::Lorem.characters(414))
601 601
         account.valid?
602 602
         expect(account).to model_have_error_on_field(:note)
603 603
       end
@@ -636,8 +636,8 @@ RSpec.describe Account, type: :model do
636 636
         expect(account).not_to model_have_error_on_field(:display_name)
637 637
       end
638 638
 
639
-      it 'is valid even if the note is longer than 160 characters' do
640
-        account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(161))
639
+      it 'is valid even if the note is longer than 413 characters' do
640
+        account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(414))
641 641
         account.valid?
642 642
         expect(account).not_to model_have_error_on_field(:note)
643 643
       end