diff --git a/app/logical/mentionable.rb b/app/logical/mentionable.rb index ad52df56f..a0034ec36 100644 --- a/app/logical/mentionable.rb +++ b/app/logical/mentionable.rb @@ -16,10 +16,32 @@ module Mentionable end end + def strip_quote_blocks(str) + stripped = "" + str.gsub!(/\s*\[quote\](?!\])\s*/m, "\n\n[quote]\n\n") + str.gsub!(/\s*\[\/quote\]\s*/m, "\n\n[/quote]\n\n") + str.gsub!(/(?:\r?\n){3,}/, "\n\n") + str.strip! + nest = 0 + str.split(/\n{2}/).each do |block| + if block == "[quote]" + nest += 1 + + elsif block == "[/quote]" + nest -= 1 + + elsif nest == 0 + stripped << "#{block}\n" + end + end + + stripped + end + def queue_mention_messages title = self.class.mentionable_option(:title) from_id = read_attribute(self.class.mentionable_option(:user_field)) - text = read_attribute(self.class.mentionable_option(:message_field)) + text = strip_quote_blocks(read_attribute(self.class.mentionable_option(:message_field))) text.scan(DText::MENTION_REGEXP).each do |mention| mention.gsub!(/(?:^\s*@)|(?:[:;,.!?\)\]<>]$)/, "") diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb index 4836d8fde..a20a69612 100644 --- a/test/unit/forum_post_test.rb +++ b/test/unit/forum_post_test.rb @@ -15,18 +15,44 @@ class ForumPostTest < ActiveSupport::TestCase end context "that mentions a user" do - setup do - @user2 = FactoryGirl.create(:user) - @post = FactoryGirl.build(:forum_post, :topic_id => @topic.id, :body => "Hey @#{@user2.name} check this out!") - end - - should "create a dmail" do - assert_difference("Dmail.count", 1) do - @post.save + context "in a quote block" do + setup do + @user2 = FactoryGirl.create(:user) end - dmail = Dmail.last - assert_equal("You were mentioned in the forum topic \"#{@topic.title}\":#{Danbooru.config.hostname}/forum_topics/#{@topic.id}?page=1\n\n---\n\nHey @#{@user2.name} check this out!", dmail.body) + should "not create a dmail" do + assert_difference("Dmail.count", 0) do + FactoryGirl.create(:forum_post, :topic_id => @topic.id, :body => "[quote]@#{@user2.name}[/quote]") + end + + assert_difference("Dmail.count", 0) do + FactoryGirl.create(:forum_post, :topic_id => @topic.id, :body => "[quote]@#{@user2.name}[/quote] blah [quote]@#{@user2.name}[/quote]") + end + + assert_difference("Dmail.count", 0) do + FactoryGirl.create(:forum_post, :topic_id => @topic.id, :body => "[quote][quote]@#{@user2.name}[/quote][/quote]") + end + + assert_difference("Dmail.count", 1) do + FactoryGirl.create(:forum_post, :topic_id => @topic.id, :body => "[quote]@#{@user2.name}[/quote] @#{@user2.name}") + end + end + end + + context "outside a quote block" do + setup do + @user2 = FactoryGirl.create(:user) + @post = FactoryGirl.build(:forum_post, :topic_id => @topic.id, :body => "Hey @#{@user2.name} check this out!") + end + + should "create a dmail" do + assert_difference("Dmail.count", 1) do + @post.save + end + + dmail = Dmail.last + assert_equal("You were mentioned in the forum topic \"#{@topic.title}\":http://#{Danbooru.config.hostname}/forum_topics/#{@topic.id}?page=1\n\n---\n\nHey @#{@user2.name} check this out!", dmail.body) + end end end