dont create dmails when mentioned in a quote block #2466

This commit is contained in:
r888888888
2015-11-06 15:01:26 -08:00
parent f103adbb50
commit 2f55353cfc
2 changed files with 59 additions and 11 deletions

View File

@@ -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*@)|(?:[:;,.!?\)\]<>]$)/, "")

View File

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