Browse Source

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.
kɜ:ʳ cybredragon 10 months ago
parent
commit
9cd0511ba9

+ 33
- 0
app/javascript/packs/public.js View File

@@ -18,6 +18,12 @@ window.addEventListener('message', e => {
18 18
       id: data.id,
19 19
       height: document.getElementsByTagName('html')[0].scrollHeight,
20 20
     }, '*');
21
+
22
+    if (document.fonts && document.fonts.ready) {
23
+      document.fonts.ready.then(sizeBioText);
24
+    } else {
25
+      sizeBioText();
26
+    }
21 27
   });
22 28
 });
23 29
 
@@ -116,6 +122,17 @@ function main() {
116 122
       document.head.appendChild(scrollbarWidthStyle);
117 123
       scrollbarWidthStyle.sheet.insertRule(`body.with-modals--active { margin-right: ${scrollbarWidth}px; }`, 0);
118 124
     }
125
+
126
+    [].forEach.call(document.querySelectorAll('[data-component="Card"]'), (content) => {
127
+      const props = JSON.parse(content.getAttribute('data-props'));
128
+      ReactDOM.render(<CardContainer locale={locale} {...props} />, content);
129
+    });
130
+
131
+    if (document.fonts && document.fonts.ready) {
132
+      document.fonts.ready.then(sizeBioText);
133
+    } else {
134
+      sizeBioText();
135
+    }
119 136
   });
120 137
 
121 138
   delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
@@ -237,6 +254,22 @@ function main() {
237 254
 
238 255
     input.readonly = oldReadOnly;
239 256
   });
257
+
258
+  delegate(document, '#account_note', 'input', sizeBioText);
259
+
260
+  function sizeBioText() {
261
+    const noteCounter = document.querySelector('.note-counter');
262
+    const bioTextArea = document.querySelector('#account_note');
263
+
264
+    if (noteCounter) {
265
+      noteCounter.textContent = 413 - length(bioTextArea.value);
266
+    }
267
+
268
+    if (bioTextArea) {
269
+      bioTextArea.style.height = 'auto';
270
+      bioTextArea.style.height = (bioTextArea.scrollHeight+3) + 'px';
271
+    }
272
+  }
240 273
 }
241 274
 
242 275
 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, note_length: { maximum: 160 }, if: -> { local? && will_save_change_to_note? }
78
+  validates :note, 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
   scope :remote, -> { where.not(domain: nil) }
@@ -299,10 +300,8 @@ class Account < ApplicationRecord
299 300
   def save_with_optional_media!
300 301
     save!
301 302
   rescue ActiveRecord::RecordInvalid
302
-    self.avatar              = nil
303
-    self.header              = nil
304
-    self[:avatar_remote_url] = ''
305
-    self[:header_remote_url] = ''
303
+    self.avatar = nil if errors[:avatar].present?
304
+    self.header = nil if errors[:header].present?
306 305
     save!
307 306
   end
308 307
 
@@ -326,6 +325,10 @@ class Account < ApplicationRecord
326 325
     shared_inbox_url.presence || inbox_url
327 326
   end
328 327
 
328
+  def note_has_eight_newlines?
329
+    errors.add(:note, 'Bio can\'t have more then 8 newlines') unless note.count("\n") <= 8
330
+  end
331
+
329 332
   class Field < ActiveModelSerializers::Model
330 333
     attributes :name, :value, :verified_at, :account, :errors
331 334
 

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

@@ -7,7 +7,7 @@
7 7
   .fields-row
8 8
     .fields-row__column.fields-group.fields-row__column-6
9 9
       = f.input :display_name, wrapper: :with_label, input_html: { maxlength: 30 }, hint: false
10
-      = f.input :note, wrapper: :with_label, input_html: { maxlength: 160 }, hint: false
10
+      = f.input :note, wrapper: :with_label, input_html: { maxlength: 413 }, hint: false
11 11
 
12 12
   .fields-row
13 13
     .fields-row__column.fields-row__column-6

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

@@ -601,8 +601,8 @@ RSpec.describe Account, type: :model do
601 601
         expect(account).to model_have_error_on_field(:display_name)
602 602
       end
603 603
 
604
-      it 'is invalid if the note is longer than 160 characters' do
605
-        account = Fabricate.build(:account, note: Faker::Lorem.characters(161))
604
+      it 'is invalid if the note is longer than 413 characters' do
605
+        account = Fabricate.build(:account, note: Faker::Lorem.characters(414))
606 606
         account.valid?
607 607
         expect(account).to model_have_error_on_field(:note)
608 608
       end
@@ -647,8 +647,8 @@ RSpec.describe Account, type: :model do
647 647
         expect(account).not_to model_have_error_on_field(:display_name)
648 648
       end
649 649
 
650
-      it 'is valid even if the note is longer than 160 characters' do
651
-        account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(161))
650
+      it 'is valid even if the note is longer than 413 characters' do
651
+        account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(414))
652 652
         account.valid?
653 653
         expect(account).not_to model_have_error_on_field(:note)
654 654
       end

Loading…
Cancel
Save