From 6e3ddb6ed698c38753db0a1eed41f62fc302216c Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 25 Feb 2017 16:54:08 -0600 Subject: [PATCH 1/3] dmails: don't save copies of outgoing dmails sent by DanbooruBot. There's not much sense in saving copies of everything DanbooruBot sends in DanbooruBot's inbox. They probably won't be checked so it just bloats the dmails table. --- app/models/dmail.rb | 5 ++++- test/unit/dmail_test.rb | 21 +++++++++++++++++++++ test/unit/user_test.rb | 3 +-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/models/dmail.rb b/app/models/dmail.rb index d6263d1d6..977a63e18 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -61,7 +61,10 @@ class Dmail < ActiveRecord::Base end def create_automated(params) - create_split(from: Danbooru.config.system_user, **params) + dmail = Dmail.new(from: Danbooru.config.system_user, **params) + dmail.owner = dmail.to + dmail.save + dmail end end diff --git a/test/unit/dmail_test.rb b/test/unit/dmail_test.rb index f7d220277..05644707c 100644 --- a/test/unit/dmail_test.rb +++ b/test/unit/dmail_test.rb @@ -147,5 +147,26 @@ class DmailTest < ActiveSupport::TestCase recipient.reload assert(!recipient.has_mail?) end + + context "that is automated" do + setup do + @bot = FactoryGirl.create(:user) + Danbooru.config.stubs(:system_user).returns(@bot) + end + + should "only create a copy for the recipient" do + Dmail.create_automated(to: @user, title: "test", body: "test") + + assert @user.dmails.exists?(from: @bot, title: "test", body: "test") + assert !@bot.dmails.exists?(from: @bot, title: "test", body: "test") + end + + should "fail gracefully if recipient doesn't exist" do + assert_nothing_raised do + dmail = Dmail.create_automated(to_name: "this_name_does_not_exist", title: "test", body: "test") + assert_equal(["can't be blank"], dmail.errors[:to_id]) + end + end + end end end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index ffb505616..34253af5a 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -31,12 +31,11 @@ class UserTest < ActiveSupport::TestCase bot = FactoryGirl.create(:user) Danbooru.config.stubs(:system_user).returns(bot) - assert_difference("Dmail.count", 2) do + assert_difference("Dmail.count", 1) do @user.promote_to!(User::Levels::GOLD) end assert(@user.dmails.exists?(from: bot, to: @user, title: "You have been promoted")) - assert(bot.dmails.exists?(from: bot, to: @user, title: "You have been promoted")) end end From 699423180128ae0770344b89ce771137c3ba3696 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 25 Feb 2017 22:26:20 -0600 Subject: [PATCH 2/3] mentions: send dmails from DanbooruBot. --- app/logical/mentionable.rb | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/app/logical/mentionable.rb b/app/logical/mentionable.rb index ea777d2e8..b1624accd 100644 --- a/app/logical/mentionable.rb +++ b/app/logical/mentionable.rb @@ -50,18 +50,7 @@ module Mentionable end bodies.each do |name, text| - user = User.find_by_name(name) - - if user - dmail = Dmail.new( - from_id: from_id, - to_id: user.id, - title: title, - body: text - ) - dmail.owner_id = user.id - dmail.save - end + Dmail.create_automated(to_name: name, title: title, body: body) end end end From 46280f222751d957b34ad0b987ea330ca12d1bd7 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 25 Feb 2017 22:28:21 -0600 Subject: [PATCH 3/3] mentions: include mentioner in subject line. The template looks like this: Subject: #{creator_name} mentioned you in a comment on post ##{post_id} Body: @#{creator_name} mentioned you in a \"comment\":/posts/#{post_id}#comment-#{id} on post ##{post_id}: [quote] #{DText.excerpt(body, "@"+user_name)} [/quote] --- app/logical/d_text.rb | 7 +++++++ app/logical/mentionable.rb | 10 +++++----- app/models/comment.rb | 6 +++--- app/models/forum_post.rb | 4 ++-- test/unit/comment_test.rb | 8 +++++++- test/unit/forum_post_test.rb | 8 +++++++- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/app/logical/d_text.rb b/app/logical/d_text.rb index 269812418..e80591443 100644 --- a/app/logical/d_text.rb +++ b/app/logical/d_text.rb @@ -381,5 +381,12 @@ class DText }) ) end + + # extract the first paragraph `needle` occurs in. + def self.excerpt(dtext, needle) + dtext = dtext.gsub(/\r\n|\r|\n/, "\n") + excerpt = ActionController::Base.helpers.excerpt(dtext, needle, separator: "\n\n", radius: 1, omission: "") + excerpt + end end diff --git a/app/logical/mentionable.rb b/app/logical/mentionable.rb index b1624accd..daf8f2374 100644 --- a/app/logical/mentionable.rb +++ b/app/logical/mentionable.rb @@ -39,17 +39,17 @@ module Mentionable end def queue_mention_messages - title = self.class.mentionable_option(:title) from_id = read_attribute(self.class.mentionable_option(:user_field)) text = strip_quote_blocks(read_attribute(self.class.mentionable_option(:message_field))) - bodies = {} - text.scan(DText::MENTION_REGEXP).each do |mention| + names = text.scan(DText::MENTION_REGEXP).map do |mention| mention.gsub!(/(?:^\s*@)|(?:[:;,.!?\)\]<>]$)/, "") - bodies[mention] = self.class.mentionable_option(:body).call(self, mention) end - bodies.each do |name, text| + names.uniq.each do |name| + body = self.instance_exec(name, &self.class.mentionable_option(:body)) + title = self.instance_exec(name, &self.class.mentionable_option(:title)) + Dmail.create_automated(to_name: name, title: title, body: body) end end diff --git a/app/models/comment.rb b/app/models/comment.rb index e455e5cef..95a4494b1 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -23,9 +23,9 @@ class Comment < ActiveRecord::Base mentionable( :message_field => :body, :user_field => :creator_id, - :title => "You were mentioned in a comment", - :body => lambda {|rec, user_name| "You were mentioned in a \"comment\":/posts/#{rec.post_id}#comment-#{rec.id}\n\n---\n\n[i]#{rec.creator.name} said:[/i]\n\n#{ActionController::Base.helpers.excerpt(rec.body, user_name)}"} - ) + :title => lambda {|user_name| "#{creator_name} mentioned you in a comment on post ##{post_id}"}, + :body => lambda {|user_name| "@#{creator_name} mentioned you in a \"comment\":/posts/#{post_id}#comment-#{id} on post ##{post_id}:\n\n[quote]\n#{DText.excerpt(body, "@"+user_name)}\n[/quote]\n"}, + ) module SearchMethods def recent diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 89d9f8e41..5e299b75e 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -28,8 +28,8 @@ class ForumPost < ActiveRecord::Base mentionable( :message_field => :body, :user_field => :creator_id, - :title => "You were mentioned in a forum topic", - :body => lambda {|rec, user_name| "You were mentioned in the forum topic \"#{rec.topic.title}\":/forum_topics/#{rec.topic_id}?page=#{rec.forum_topic_page}\n\n---\n\n[i]#{rec.creator.name} said:[/i]\n\n#{ActionController::Base.helpers.excerpt(rec.body, user_name)}"} + :title => lambda {|user_name| %{#{creator_name} mentioned you in topic ##{topic_id} (#{topic.title})}}, + :body => lambda {|user_name| %{@#{creator_name} mentioned you in topic ##{topic_id} ("#{topic.title}":[/forum_topics/#{topic_id}?page=#{forum_topic_page}]):\n\n[quote]\n#{DText.excerpt(body, "@"+user_name)}\n[/quote]\n}}, ) module SearchMethods diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index b62f2fb5e..f55573b12 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -57,7 +57,13 @@ class CommentTest < ActiveSupport::TestCase end dmail = Dmail.last - assert_equal("You were mentioned in a \"comment\":/posts/#{@comment.post_id}#comment-#{@comment.id}\n\n---\n\n[i]#{CurrentUser.name} said:[/i]\n\nHey @#{@user2.name} check this out!", dmail.body) + assert_equal(<<-EOS.strip_heredoc, dmail.body) + @#{CurrentUser.name} mentioned you in a \"comment\":/posts/#{@comment.post_id}#comment-#{@comment.id} on post ##{@comment.post_id}: + + [quote] + Hey @#{@user2.name} check this out! + [/quote] + EOS end end end diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb index 714d41098..2b60d66d5 100644 --- a/test/unit/forum_post_test.rb +++ b/test/unit/forum_post_test.rb @@ -51,7 +51,13 @@ class ForumPostTest < ActiveSupport::TestCase end dmail = Dmail.last - assert_equal("You were mentioned in the forum topic \"#{@topic.title}\":/forum_topics/#{@topic.id}?page=1\n\n---\n\n[i]#{@user.name} said:[/i]\n\nHey @#{@user2.name} check this out!", dmail.body) + assert_equal(<<-EOS.strip_heredoc, dmail.body) + @#{CurrentUser.name} mentioned you in topic ##{@topic.id} (\"#{@topic.title}\":[/forum_topics/#{@topic.id}?page=1]): + + [quote] + Hey @#{@user2.name} check this out! + [/quote] + EOS end end end