From 4f36aad6e8e1dad6b9907b62a200919fbe0d4ebe Mon Sep 17 00:00:00 2001 From: Surinna Curtis Date: Sun, 25 Jun 2017 22:23:53 -0500 Subject: [PATCH] don't count bio metadata against bio length on server --- app/models/account.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/models/account.rb b/app/models/account.rb index 14c90cfc037..49d2c88f604 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -60,7 +60,7 @@ class Account < ApplicationRecord validates :username, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }, 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, length: { maximum: 500 }, if: -> { local? && will_save_change_to_note? } + validate :note_length_does_not_exceed_length_limit, if: -> { local? && will_save_change_to_note? } # Timelines has_many :stream_entries, inverse_of: :account, dependent: :destroy @@ -251,6 +251,22 @@ class Account < ApplicationRecord self.public_key = keypair.public_key.to_pem end + YAML_START = "---\r\n" + YAML_END = "\r\n...\r\n" + + def note_length_does_not_exceed_length_limit + note_without_metadata = note + if note.start_with? YAML_START + idx = note.index YAML_END + unless idx.nil? + note_without_metadata = note[(idx + YAML_END.length) .. -1] + end + end + if note_without_metadata.mb_chars.grapheme_length > 500 + errors.add(:note, "can't be longer than 500 graphemes") + end + end + def normalize_domain return if local?