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 1 month ago
parent
commit
df0855a91b

+ 34
- 1
app/javascript/packs/public.js View File

@@ -1,4 +1,4 @@
1
-import loadPolyfills from '../mastodon/load_polyfills';
1
+                    import loadPolyfills from '../mastodon/load_polyfills';
2 2
 import ready from '../mastodon/ready';
3 3
 import { start } from '../mastodon/common';
4 4
 
@@ -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
 
@@ -93,6 +99,17 @@ function main() {
93 99
       detailedStatuses[0].scrollIntoView();
94 100
       history.replace(location.pathname, { ...location.state, scrolledToDetailedStatus: true });
95 101
     }
102
+
103
+    [].forEach.call(document.querySelectorAll('[data-component="Card"]'), (content) => {
104
+      const props = JSON.parse(content.getAttribute('data-props'));
105
+      ReactDOM.render(<CardContainer locale={locale} {...props} />, content);
106
+    });
107
+
108
+    if (document.fonts && document.fonts.ready) {
109
+      document.fonts.ready.then(sizeBioText);
110
+    } else {
111
+      sizeBioText();
112
+    }
96 113
   });
97 114
 
98 115
   delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
@@ -188,6 +205,22 @@ function main() {
188 205
       console.error(err);
189 206
     }
190 207
   });
208
+
209
+  delegate(document, '#account_note', 'input', sizeBioText);
210
+
211
+  function sizeBioText() {
212
+    const noteCounter = document.querySelector('.note-counter');
213
+    const bioTextArea = document.querySelector('#account_note');
214
+
215
+    if (noteCounter) {
216
+      noteCounter.textContent = 413 - length(bioTextArea.value);
217
+    }
218
+
219
+    if (bioTextArea) {
220
+      bioTextArea.style.height = 'auto';
221
+      bioTextArea.style.height = (bioTextArea.scrollHeight+3) + 'px';
222
+    }
223
+  }
191 224
 }
192 225
 
193 226
 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
@@ -279,10 +280,8 @@ class Account < ApplicationRecord
279 280
   def save_with_optional_media!
280 281
     save!
281 282
   rescue ActiveRecord::RecordInvalid
282
-    self.avatar              = nil
283
-    self.header              = nil
284
-    self[:avatar_remote_url] = ''
285
-    self[:header_remote_url] = ''
283
+    self.avatar = nil if errors[:avatar].present?
284
+    self.header = nil if errors[:header].present?
286 285
     save!
287 286
   end
288 287
 
@@ -306,6 +305,10 @@ class Account < ApplicationRecord
306 305
     shared_inbox_url.presence || inbox_url
307 306
   end
308 307
 
308
+  def note_has_eight_newlines?
309
+    errors.add(:note, 'Bio can\'t have more then 8 newlines') unless note.count("\n") <= 8
310
+  end
311
+
309 312
   class Field < ActiveModelSerializers::Model
310 313
     attributes :name, :value, :verified_at, :account, :errors
311 314
 

+ 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

@@ -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