From df44937c57b12389ec9331123ef59ee7980bb576 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 4 Jan 2021 15:59:36 -0600 Subject: [PATCH] post regenerations: replace PostRegeneration model with mod actions. * Remove the PostRegeneration model. Instead just use a mod action to log when a post is regenerated. * Change it so that IQDB is also updated when the image samples are regenerated. This is necessary because when the images samples are regenerated, the thumbnail may change, which means IQDB needs to be updated too. This can happen when regenerating old images with transparent backgrounds where the transparency was flattened to black instead of white in the thumbnail. * Only display one "Regenerate image" option in the post sidebar, to regenerate both the images and IQDB. Regenerating IQDB only can be done through the API. Having two options in the sidebar is too much clutter, and it's too confusing for Mods who don't know the difference between an IQDB-only regeneration and a full image regeneration. * Add a confirm prompt to the "Regenerate image" link. --- .../post_regenerations_controller.rb | 16 ++--- app/models/mod_action.rb | 2 + app/models/post.rb | 16 +++++ app/models/post_regeneration.rb | 31 --------- app/policies/post_policy.rb | 4 ++ app/policies/post_regeneration_policy.rb | 9 --- app/views/post_regenerations/create.js.erb | 6 +- app/views/post_regenerations/index.html.erb | 17 ----- .../posts/partials/show/_options.html.erb | 5 +- app/views/posts/show.html.erb | 1 - config/routes.rb | 2 +- ...0201121180345_create_post_regenerations.rb | 13 ---- db/structure.sql | 63 ------------------- test/factories/post_regeneration.rb | 5 -- .../post_regenerations_controller_test.rb | 33 ++-------- 15 files changed, 34 insertions(+), 189 deletions(-) delete mode 100644 app/models/post_regeneration.rb delete mode 100644 app/policies/post_regeneration_policy.rb delete mode 100644 app/views/post_regenerations/index.html.erb delete mode 100644 db/migrate/20201121180345_create_post_regenerations.rb delete mode 100644 test/factories/post_regeneration.rb diff --git a/app/controllers/post_regenerations_controller.rb b/app/controllers/post_regenerations_controller.rb index f7466e0cc..1d5b60244 100644 --- a/app/controllers/post_regenerations_controller.rb +++ b/app/controllers/post_regenerations_controller.rb @@ -1,18 +1,10 @@ class PostRegenerationsController < ApplicationController - respond_to :html, :xml, :json, :js + respond_to :xml, :json, :js def create - @post_regeneration = authorize PostRegeneration.new(creator: CurrentUser.user, **permitted_attributes(PostRegeneration)) - @post_regeneration.execute_category_action! - @post_regeneration.save + @post = authorize Post.find(params[:post_id]), :regenerate? + @post.regenerate!(params[:category], CurrentUser.user) - respond_with(@post_regeneration, location: @post_regeneration.post) - end - - def index - @post_regenerations = authorize PostRegeneration.paginated_search(params) - @post_regenerations = @post_regenerations.includes(:creator, :post) if request.format.html? - - respond_with(@post_regenerations) + respond_with(@post) end end diff --git a/app/models/mod_action.rb b/app/models/mod_action.rb index 437d3efea..a0c98ab16 100644 --- a/app/models/mod_action.rb +++ b/app/models/mod_action.rb @@ -29,6 +29,8 @@ class ModAction < ApplicationRecord post_unban: 45, post_permanent_delete: 46, post_move_favorites: 47, + post_regenerate: 48, + post_regenerate_iqdb: 49, pool_delete: 62, pool_undelete: 63, artist_ban: 184, diff --git a/app/models/post.rb b/app/models/post.rb index a4bfc70e8..1a67e0a64 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1325,6 +1325,22 @@ class Post < ApplicationRecord end end + concerning :RegenerationMethods do + def regenerate!(category, user) + if category == "iqdb" + update_iqdb_async + + ModAction.log("<@#{user.name}> regenerated IQDB for post ##{id}", :post_regenerate_iqdb, user) + else + media_file = MediaFile.open(file, frame_data: pixiv_ugoira_frame_data) + UploadService::Utils.process_resizes(self, nil, id, media_file: media_file) + update_iqdb_async + + ModAction.log("<@#{user.name}> regenerated image samples for post ##{id}", :post_regenerate, user) + end + end + end + module IqdbMethods extend ActiveSupport::Concern diff --git a/app/models/post_regeneration.rb b/app/models/post_regeneration.rb deleted file mode 100644 index 729f8740c..000000000 --- a/app/models/post_regeneration.rb +++ /dev/null @@ -1,31 +0,0 @@ -class PostRegeneration < ApplicationRecord - belongs_to :creator, :class_name => "User" - belongs_to :post - - validates :category, inclusion: %w[iqdb resizes] - - module SearchMethods - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :category, :creator, :post) - q.apply_default_order(params) - end - end - - extend SearchMethods - - def execute_category_action! - if category == "iqdb" - post.update_iqdb_async - elsif category == "resizes" - media_file = MediaFile.open(post.file, frame_data: post.pixiv_ugoira_frame_data) - UploadService::Utils.process_resizes(post, nil, post.id, media_file: media_file) - else - # should never happen - raise Error, "Unknown category: #{category}" - end - end - - def self.searchable_includes - [:creator, :post] - end -end diff --git a/app/policies/post_policy.rb b/app/policies/post_policy.rb index 3bfa5d05c..d23e268e1 100644 --- a/app/policies/post_policy.rb +++ b/app/policies/post_policy.rb @@ -27,6 +27,10 @@ class PostPolicy < ApplicationPolicy user.is_approver? && record.fav_count > 0 && record.parent_id.present? end + def regenerate? + user.is_moderator? + end + def delete? user.is_approver? && !record.is_deleted? end diff --git a/app/policies/post_regeneration_policy.rb b/app/policies/post_regeneration_policy.rb deleted file mode 100644 index 25cc57956..000000000 --- a/app/policies/post_regeneration_policy.rb +++ /dev/null @@ -1,9 +0,0 @@ -class PostRegenerationPolicy < ApplicationPolicy - def create? - user.is_moderator? - end - - def permitted_attributes_for_create - [:post_id, :category] - end -end diff --git a/app/views/post_regenerations/create.js.erb b/app/views/post_regenerations/create.js.erb index d45a54f1e..c891928ae 100644 --- a/app/views/post_regenerations/create.js.erb +++ b/app/views/post_regenerations/create.js.erb @@ -1,5 +1 @@ -<% if @post_regeneration.errors.any? %> - Danbooru.error("<%= j @post_regeneration.errors.full_messages.join(',') %>"); -<% else %> - Danbooru.notice("Post regenerated"); -<% end %> +Danbooru.notice("Post regenerated"); diff --git a/app/views/post_regenerations/index.html.erb b/app/views/post_regenerations/index.html.erb deleted file mode 100644 index cea7ac3a1..000000000 --- a/app/views/post_regenerations/index.html.erb +++ /dev/null @@ -1,17 +0,0 @@ -
-
-

Post regenerations

- <%= table_for @post_regenerations, width: "100%" do |t| %> - <% t.column "Post", width: "1%" do |regeneration| %> - <%= PostPresenter.preview(regeneration.post, show_deleted: true) %> - <% end %> - <% t.column :category %> - <% t.column "Creator", width: "10%" do |regeneration| %> - <%= compact_time regeneration.created_at %> -
by <%= link_to_user regeneration.creator %> - <% end %> - <% end %> - - <%= numbered_paginator(@post_regenerations) %> -
-
diff --git a/app/views/posts/partials/show/_options.html.erb b/app/views/posts/partials/show/_options.html.erb index 2b336a854..852a55205 100644 --- a/app/views/posts/partials/show/_options.html.erb +++ b/app/views/posts/partials/show/_options.html.erb @@ -87,8 +87,7 @@ <% end %> <% end %> - <% if policy(PostRegeneration).create? %> -
  • <%= link_to "Regenerate IQDB", post_regenerations_path(post_regeneration: {post_id: post.id, category: "iqdb"}), remote: true, method: :post %>
  • -
  • <%= link_to "Regenerate image sizes", post_regenerations_path(post_regeneration: {post_id: post.id, category: "resizes"}), remote: true, method: :post %>
  • + <% if policy(post).regenerate? %> +
  • <%= link_to "Regenerate image", post_regenerations_path(post_id: post.id, category: "resizes"), remote: true, method: :post, "data-confirm": "This will regenerate the posts's thumbnail images. Are you sure?" %>
  • <% end %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 6f5152225..a2f202f27 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -33,7 +33,6 @@
  • <%= link_to "Moderation", post_events_path(@post.id) %>
  • <%= link_to "Commentary", artist_commentary_versions_path(search: { post_id: @post.id }) %>
  • <%= link_to "Replacements", post_replacements_path(search: {post_id: @post.id }) %>
  • -
  • <%= link_to "Regenerations", post_regenerations_path(search: {post_id: @post.id }) %>
  • <% end %> diff --git a/config/routes.rb b/config/routes.rb index 5fa7b3238..7b32daaf4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -178,7 +178,7 @@ Rails.application.routes.draw do get :search end end - resources :post_regenerations, :only => [:index, :create] + resources :post_regenerations, :only => [:create] resources :post_replacements, :only => [:index, :new, :create, :update] resources :post_votes, only: [:index] diff --git a/db/migrate/20201121180345_create_post_regenerations.rb b/db/migrate/20201121180345_create_post_regenerations.rb deleted file mode 100644 index ec88f7bbe..000000000 --- a/db/migrate/20201121180345_create_post_regenerations.rb +++ /dev/null @@ -1,13 +0,0 @@ -class CreatePostRegenerations < ActiveRecord::Migration[6.0] - def change - create_table :post_regenerations do |t| - t.timestamps - t.integer :creator_id, null: false - t.integer :post_id, null: false - t.string :category, null: false - - t.index :creator_id - t.index :post_id - end - end -end diff --git a/db/structure.sql b/db/structure.sql index 3afb26718..f57281afa 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2724,39 +2724,6 @@ CREATE SEQUENCE public.post_flags_id_seq ALTER SEQUENCE public.post_flags_id_seq OWNED BY public.post_flags.id; --- --- Name: post_regenerations; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE public.post_regenerations ( - id bigint NOT NULL, - created_at timestamp(6) without time zone NOT NULL, - updated_at timestamp(6) without time zone NOT NULL, - creator_id integer NOT NULL, - post_id integer NOT NULL, - category character varying NOT NULL -); - - --- --- Name: post_regenerations_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE public.post_regenerations_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: post_regenerations_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - --- - -ALTER SEQUENCE public.post_regenerations_id_seq OWNED BY public.post_regenerations.id; - - -- -- Name: post_replacements; Type: TABLE; Schema: public; Owner: - -- @@ -4170,13 +4137,6 @@ ALTER TABLE ONLY public.post_disapprovals ALTER COLUMN id SET DEFAULT nextval('p ALTER TABLE ONLY public.post_flags ALTER COLUMN id SET DEFAULT nextval('public.post_flags_id_seq'::regclass); --- --- Name: post_regenerations id; Type: DEFAULT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.post_regenerations ALTER COLUMN id SET DEFAULT nextval('public.post_regenerations_id_seq'::regclass); - - -- -- Name: post_replacements id; Type: DEFAULT; Schema: public; Owner: - -- @@ -4539,14 +4499,6 @@ ALTER TABLE ONLY public.post_flags ADD CONSTRAINT post_flags_pkey PRIMARY KEY (id); --- --- Name: post_regenerations post_regenerations_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.post_regenerations - ADD CONSTRAINT post_regenerations_pkey PRIMARY KEY (id); - - -- -- Name: post_replacements post_replacements_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -6800,20 +6752,6 @@ CREATE INDEX index_post_flags_on_reason_tsvector ON public.post_flags USING gin CREATE INDEX index_post_flags_on_status ON public.post_flags USING btree (status); --- --- Name: index_post_regenerations_on_creator_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX index_post_regenerations_on_creator_id ON public.post_regenerations USING btree (creator_id); - - --- --- Name: index_post_regenerations_on_post_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX index_post_regenerations_on_post_id ON public.post_regenerations USING btree (post_id); - - -- -- Name: index_post_replacements_on_creator_id; Type: INDEX; Schema: public; Owner: - -- @@ -7582,7 +7520,6 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200520060951'), ('20200803022359'), ('20200816175151'), -('20201121180345'), ('20201201211748'), ('20201213052805'), ('20201219201007'), diff --git a/test/factories/post_regeneration.rb b/test/factories/post_regeneration.rb deleted file mode 100644 index 00087ac02..000000000 --- a/test/factories/post_regeneration.rb +++ /dev/null @@ -1,5 +0,0 @@ -FactoryBot.define do - factory(:post_regeneration) do - post factory: :post, source: FFaker::Internet.http_url - end -end diff --git a/test/functional/post_regenerations_controller_test.rb b/test/functional/post_regenerations_controller_test.rb index 119d5741a..b02e9efdd 100644 --- a/test/functional/post_regenerations_controller_test.rb +++ b/test/functional/post_regenerations_controller_test.rb @@ -6,43 +6,18 @@ class PostRegenerationsControllerTest < ActionDispatch::IntegrationTest @mod = create(:moderator_user, name: "yukari", created_at: 1.month.ago) as(@mod) do @post = create(:post, source: "https://google.com", tag_string: "touhou") - @post_regeneration = create(:post_regeneration, creator: @mod, category: "iqdb") end end context "create action" do should "render" do - assert_difference("PostRegeneration.count") do - post_auth post_regenerations_path, @mod, params: {format: :json, post_regeneration: {post_id: @post.id, category: "iqdb"}} - assert_response :success - end - end - - should "not allow non-mods to regenerate posts" do - assert_difference("PostRegeneration.count", 0) do - post_auth post_regenerations_path, create(:user), params: {format: :json, post_regeneration: {post_id: @post.id, category: "iqdb"}} - assert_response 403 - end - end - end - - context "index action" do - setup do - @admin = create(:admin_user) - as(@admin) { @admin_regeneration = create(:post_regeneration, post: @post, creator: @admin, category: "resizes") } - end - - should "render" do - get post_regenerations_path + post_auth post_regenerations_path, @mod, params: { post_id: @post.id, category: "iqdb" } assert_response :success end - should respond_to_search({}).with { [@admin_regeneration, @post_regeneration] } - should respond_to_search(category: "iqdb").with { @post_regeneration } - - context "using includes" do - should respond_to_search(post_tags_match: "touhou").with { @admin_regeneration } - should respond_to_search(creator: {level: User::Levels::ADMIN}).with { @admin_regeneration } + should "not allow non-mods to regenerate posts" do + post_auth post_regenerations_path, create(:user), params: { post_id: @post.id, category: "iqdb" } + assert_response 403 end end end