From aaab527baa329bdc51020e23e158d8299f0ae0b5 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 23 Nov 2019 17:59:18 -0600 Subject: [PATCH] Move Curated pool updater to Danbooru. * Move the Curated pool updater from Reportbooru to Danbooru. * Change the process for selecting curated posts. Previously it was every post from the last week with at least three supervotes. This was flawed because it included both super-upvotes and super-downvotes. Now it's the top 100 posts from the last week, ordered from most super-upvoted to least. --- app/logical/curated_pool_updater.rb | 16 ++++++++++++++++ app/logical/danbooru_maintenance.rb | 1 + config/danbooru_default_config.rb | 5 +++++ test/unit/danbooru_maintenance_test.rb | 9 ++++++++- 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 app/logical/curated_pool_updater.rb diff --git a/app/logical/curated_pool_updater.rb b/app/logical/curated_pool_updater.rb new file mode 100644 index 000000000..aa82f314f --- /dev/null +++ b/app/logical/curated_pool_updater.rb @@ -0,0 +1,16 @@ +module CuratedPoolUpdater + def self.curated_posts(date_range: (1.week.ago..Time.now), limit: 100) + posts = Post.where(created_at: date_range) + posts = posts.joins(:votes).where("post_votes.score": SuperVoter::MAGNITUDE) + posts = Post.where(id: posts.group(:id).order(Arel.sql("COUNT(*) DESC")).limit(limit)) + posts + end + + def self.update_pool!(pool_id = Danbooru.config.curated_pool_id) + return unless pool_id.present? + + CurrentUser.scoped(User.system, "127.0.0.1") do + Pool.find(pool_id).update!(post_ids: curated_posts.pluck(:id)) + end + end +end diff --git a/app/logical/danbooru_maintenance.rb b/app/logical/danbooru_maintenance.rb index 07d1654a0..6c4f842d0 100644 --- a/app/logical/danbooru_maintenance.rb +++ b/app/logical/danbooru_maintenance.rb @@ -21,6 +21,7 @@ module DanbooruMaintenance TagChangeRequestPruner.warn_all TagChangeRequestPruner.reject_all Ban.prune! + CuratedPoolUpdater.update_pool! ActiveRecord::Base.connection.execute("vacuum analyze") unless Rails.env.test? rescue Exception => exception diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 4e62a4982..b5fa78b14 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -41,6 +41,11 @@ module Danbooru ForumTopic.where(title: "Upload Feedback Thread").first end + # The ID of the "Curated" pool. If present, this pool will be updated daily with curated posts. + def curated_pool_id + nil + end + def source_code_url "https://github.com/r888888888/danbooru" end diff --git a/test/unit/danbooru_maintenance_test.rb b/test/unit/danbooru_maintenance_test.rb index 3ff2133b4..ffb990cfc 100644 --- a/test/unit/danbooru_maintenance_test.rb +++ b/test/unit/danbooru_maintenance_test.rb @@ -3,13 +3,20 @@ require 'test_helper' class DanbooruMaintenanceTest < ActiveSupport::TestCase context "daily maintenance" do setup do - FactoryBot.create(:admin_user) # for SuperVoter.init! + @admin = create(:admin_user) # for SuperVoter.init! end should "work" do assert_nothing_raised { DanbooruMaintenance.daily } end + should "update the curated pool" do + pool = as(@admin) { create(:pool, name: "curated") } + Danbooru.config.stubs(:curated_pool_id).returns(pool.id) + + assert_nothing_raised { DanbooruMaintenance.daily } + end + should "prune expired posts" do @pending = FactoryBot.create(:post, is_pending: true, created_at: 4.days.ago) @flagged = FactoryBot.create(:post, is_flagged: true, created_at: 4.days.ago)