Auto-promote DanbooruBot to Mod. Auto-create DanbooruBot if it doesn't exist.

This commit is contained in:
evazion
2017-12-15 14:51:24 -06:00
parent b48211cd4a
commit e2eb45a5a3
8 changed files with 21 additions and 9 deletions

View File

@@ -86,7 +86,7 @@ class Dmail < ApplicationRecord
end end
def create_automated(params) 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.owner = dmail.to
dmail.save dmail.save
dmail dmail
@@ -234,7 +234,7 @@ class Dmail < ApplicationRecord
end end
def is_automated? def is_automated?
from == Danbooru.config.system_user from == User.system
end end
def filtered? def filtered?

View File

@@ -316,7 +316,7 @@ class User < ApplicationRecord
module ClassMethods module ClassMethods
def system def system
Danbooru.config.system_user User.find_by!(name: Danbooru.config.system_user)
end end
def level_hash def level_hash
@@ -366,7 +366,7 @@ class User < ApplicationRecord
def promote_to_admin_if_first_user def promote_to_admin_if_first_user
return if Rails.env.test? return if Rails.env.test?
if User.count == 0 if User.admins.count == 0
self.level = Levels::ADMIN self.level = Levels::ADMIN
self.can_approve_posts = true self.can_approve_posts = true
self.can_upload_free = true self.can_upload_free = true

View File

@@ -37,8 +37,10 @@ module Danbooru
end end
# System actions, such as sending automated dmails, will be performed with this account. # 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 def system_user
User.find_by_name("DanbooruBot") || User.admins.first "DanbooruBot"
end end
def upload_feedback_topic def upload_feedback_topic

View 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

View File

@@ -177,7 +177,7 @@ class DmailTest < ActiveSupport::TestCase
context "that is automated" do context "that is automated" do
setup do setup do
@bot = FactoryGirl.create(:user) @bot = FactoryGirl.create(:user)
Danbooru.config.stubs(:system_user).returns(@bot) User.stubs(:system).returns(@bot)
end end
should "only create a copy for the recipient" do should "only create a copy for the recipient" do

View File

@@ -85,7 +85,7 @@ class PostDisapprovalTest < ActiveSupport::TestCase
should "dmail the uploaders" do should "dmail the uploaders" do
bot = FactoryGirl.create(:user) 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 assert_difference(["@uploaders[0].dmails.count", "@uploaders[1].dmails.count"], 1) do
PostDisapproval.dmail_messages! PostDisapproval.dmail_messages!

View File

@@ -20,7 +20,7 @@ class PostReplacementTest < ActiveSupport::TestCase
Delayed::Worker.delay_jobs = true # don't delete the old images right away Delayed::Worker.delay_jobs = true # don't delete the old images right away
@system = FactoryGirl.create(:user, created_at: 2.weeks.ago) @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) @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) @replacer = FactoryGirl.create(:user, created_at: 2.weeks.ago, can_approve_posts: true)

View File

@@ -28,7 +28,7 @@ class UserTest < ActiveSupport::TestCase
should "send an automated dmail to the user" do should "send an automated dmail to the user" do
bot = FactoryGirl.create(:user) bot = FactoryGirl.create(:user)
Danbooru.config.stubs(:system_user).returns(bot) User.stubs(:system).returns(bot)
assert_difference("Dmail.count", 1) do assert_difference("Dmail.count", 1) do
@user.promote_to!(User::Levels::GOLD) @user.promote_to!(User::Levels::GOLD)