about summary refs log tree commit diff
path: root/app/validators/status_length_validator.rb
diff options
context:
space:
mode:
authorkibigo! <marrus-sh@users.noreply.github.com>2017-08-01 13:07:43 -0700
committerkibigo! <marrus-sh@users.noreply.github.com>2017-08-01 13:20:29 -0700
commit8150689b48716bb016d492d28cef08600a4b315e (patch)
treea05a2539e894c79ef17698dce0da5a6af0c25bf6 /app/validators/status_length_validator.rb
parentb61e3daf983d87c6d2de7e54d420c2e8f5a531e6 (diff)
parent7ef848256871454a790a9b7cc725053c67ba3da4 (diff)
Merge upstream (#111)
Diffstat (limited to 'app/validators/status_length_validator.rb')
-rw-r--r--app/validators/status_length_validator.rb23
1 files changed, 22 insertions, 1 deletions
diff --git a/app/validators/status_length_validator.rb b/app/validators/status_length_validator.rb
index cd791e2f3..2ce5d1ee9 100644
--- a/app/validators/status_length_validator.rb
+++ b/app/validators/status_length_validator.rb
@@ -5,6 +5,27 @@ class StatusLengthValidator < ActiveModel::Validator
 
   def validate(status)
     return unless status.local? && !status.reblog?
-    status.errors.add(:text, I18n.t('statuses.over_character_limit', max: MAX_CHARS)) if [status.text, status.spoiler_text].join.mb_chars.grapheme_length > MAX_CHARS
+    status.errors.add(:text, I18n.t('statuses.over_character_limit', max: MAX_CHARS)) if too_long?(status)
+  end
+
+  private
+
+  def too_long?(status)
+    countable_length(status) > MAX_CHARS
+  end
+
+  def countable_length(status)
+    total_text(status).mb_chars.grapheme_length
+  end
+
+  def total_text(status)
+    [status.spoiler_text, countable_text(status)].join
+  end
+
+  def countable_text(status)
+    status.text.dup.tap do |new_text|
+      new_text.gsub!(FetchLinkCardService::URL_PATTERN, 'x' * 23)
+      new_text.gsub!(Account::MENTION_RE, '@\2')
+    end
   end
 end