From 6e3ddb6ed698c38753db0a1eed41f62fc302216c Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 25 Feb 2017 16:54:08 -0600 Subject: [PATCH] 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