Auto-promote DanbooruBot to Mod. Auto-create DanbooruBot if it doesn't exist.
This commit is contained in:
@@ -86,7 +86,7 @@ class Dmail < ApplicationRecord
|
||||
end
|
||||
|
||||
def create_automated(params)
|
||||
dmail = Dmail.new(from: Danbooru.config.system_user, **params)
|
||||
dmail = Dmail.new(from: User.system, **params)
|
||||
dmail.owner = dmail.to
|
||||
dmail.save
|
||||
dmail
|
||||
@@ -234,7 +234,7 @@ class Dmail < ApplicationRecord
|
||||
end
|
||||
|
||||
def is_automated?
|
||||
from == Danbooru.config.system_user
|
||||
from == User.system
|
||||
end
|
||||
|
||||
def filtered?
|
||||
|
||||
@@ -316,7 +316,7 @@ class User < ApplicationRecord
|
||||
|
||||
module ClassMethods
|
||||
def system
|
||||
Danbooru.config.system_user
|
||||
User.find_by!(name: Danbooru.config.system_user)
|
||||
end
|
||||
|
||||
def level_hash
|
||||
@@ -366,7 +366,7 @@ class User < ApplicationRecord
|
||||
def promote_to_admin_if_first_user
|
||||
return if Rails.env.test?
|
||||
|
||||
if User.count == 0
|
||||
if User.admins.count == 0
|
||||
self.level = Levels::ADMIN
|
||||
self.can_approve_posts = true
|
||||
self.can_upload_free = true
|
||||
|
||||
@@ -37,8 +37,10 @@ module Danbooru
|
||||
end
|
||||
|
||||
# System actions, such as sending automated dmails, will be performed with this account.
|
||||
# This account will be created automatically if it doesn't exist. It will
|
||||
# be promoted to Moderator if it isn't already a Moderator.
|
||||
def system_user
|
||||
User.find_by_name("DanbooruBot") || User.admins.first
|
||||
"DanbooruBot"
|
||||
end
|
||||
|
||||
def upload_feedback_topic
|
||||
|
||||
10
config/initializers/system_user.rb
Normal file
10
config/initializers/system_user.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
require "securerandom"
|
||||
|
||||
system = User.find_or_create_by!(name: Danbooru.config.system_user) do |user|
|
||||
user.password = SecureRandom.base64(32)
|
||||
end
|
||||
|
||||
unless system.is_moderator?
|
||||
system.level = User::Levels::MODERATOR
|
||||
system.save
|
||||
end
|
||||
@@ -177,7 +177,7 @@ class DmailTest < ActiveSupport::TestCase
|
||||
context "that is automated" do
|
||||
setup do
|
||||
@bot = FactoryGirl.create(:user)
|
||||
Danbooru.config.stubs(:system_user).returns(@bot)
|
||||
User.stubs(:system).returns(@bot)
|
||||
end
|
||||
|
||||
should "only create a copy for the recipient" do
|
||||
|
||||
@@ -85,7 +85,7 @@ class PostDisapprovalTest < ActiveSupport::TestCase
|
||||
|
||||
should "dmail the uploaders" do
|
||||
bot = FactoryGirl.create(:user)
|
||||
Danbooru.config.stubs(:system_user).returns(bot)
|
||||
User.stubs(:system).returns(bot)
|
||||
|
||||
assert_difference(["@uploaders[0].dmails.count", "@uploaders[1].dmails.count"], 1) do
|
||||
PostDisapproval.dmail_messages!
|
||||
|
||||
@@ -20,7 +20,7 @@ class PostReplacementTest < ActiveSupport::TestCase
|
||||
Delayed::Worker.delay_jobs = true # don't delete the old images right away
|
||||
|
||||
@system = FactoryGirl.create(:user, created_at: 2.weeks.ago)
|
||||
Danbooru.config.stubs(:system_user).returns(@system)
|
||||
User.stubs(:system).returns(@system)
|
||||
|
||||
@uploader = FactoryGirl.create(:user, created_at: 2.weeks.ago, can_upload_free: true)
|
||||
@replacer = FactoryGirl.create(:user, created_at: 2.weeks.ago, can_approve_posts: true)
|
||||
|
||||
@@ -28,7 +28,7 @@ class UserTest < ActiveSupport::TestCase
|
||||
|
||||
should "send an automated dmail to the user" do
|
||||
bot = FactoryGirl.create(:user)
|
||||
Danbooru.config.stubs(:system_user).returns(bot)
|
||||
User.stubs(:system).returns(bot)
|
||||
|
||||
assert_difference("Dmail.count", 1) do
|
||||
@user.promote_to!(User::Levels::GOLD)
|
||||
|
||||
Reference in New Issue
Block a user