From 16332682931d3a59c180ecff94e07d6bf650e5e0 Mon Sep 17 00:00:00 2001 From: multiple creatures Date: Thu, 12 Dec 2019 11:10:11 -0600 Subject: avoid posting when body only has mentions it --- app/lib/bangtags.rb | 6 +++++- app/services/post_status_service.rb | 5 +++++ config/locales/en.yml | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/lib/bangtags.rb b/app/lib/bangtags.rb index 1284b47e0..201ed9622 100644 --- a/app/lib/bangtags.rb +++ b/app/lib/bangtags.rb @@ -1001,7 +1001,7 @@ class Bangtags text.strip! text = text.split("\n").map { |chunk| chunk.strip }.join("\n") if @strip_lines - if text.blank? + if text.blank? || has_only_mentions?(text) RemoveStatusService.new.call(@status) else status.text = text @@ -1090,6 +1090,10 @@ class Bangtags end end + def has_only_mentions?(text) + text.match?(/^(?:\s*@((#{Account::USERNAME_RE})(?:@[a-z0-9\.\-]+[a-z0-9]+)?))+$/i) + end + def add_tags(to_status, *tags) valid_name = /^[[:word:]:._\-]*[[:alpha:]:._ยท\-][[:word:]:._\-]*$/ tags = tags.select {|t| t.present? && valid_name.match?(t)}.uniq diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb index dc1a3bcdc..2fe1f3e56 100644 --- a/app/services/post_status_service.rb +++ b/app/services/post_status_service.rb @@ -55,6 +55,7 @@ class PostStatusService < BaseService raise Mastodon::LengthValidationError, I18n.t('statuses.replies_rejected') if recipient_rejects_replies? raise Mastodon::LengthValidationError, I18n.t('statuses.kicked') if kicked? + raise Mastodon::LengthValidationError, I18n.t('statuses.no_body') if has_only_mentions? return idempotency_duplicate if idempotency_given? && idempotency_duplicate? @@ -98,6 +99,10 @@ class PostStatusService < BaseService @in_reply_to.present? && ConversationKick.where(account_id: @account.id, conversation_id: @in_reply_to.conversation_id).exists? end + def has_only_mentions? + @options[:nomentions].blank? && @text.match?(/^(?:\s*@((#{Account::USERNAME_RE})(?:@[a-z0-9\.\-]+[a-z0-9]+)?))+$/i) + end + def mark_recipient_known @in_reply_to.account.mark_known! unless !Setting.auto_mark_known || @in_reply_to.account.known? end diff --git a/config/locales/en.yml b/config/locales/en.yml index 7f026135f..af7921753 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -976,6 +976,7 @@ en: vote: Vote replies_rejected: 'The author is not accepting replies to this roar.' kicked: "You do not have the author's permission to reply to this thread." + no_body: "Your post has mentions without any body text." show_more: Show more sign_in_to_participate: Sign in to participate in the conversation title: '%{name}: "%{quote}"' -- cgit