From 55512130e1ac9187beb180dbe4cf9d4fc5bdaf73 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Jun 2017 14:19:28 -0500 Subject: [PATCH 1/7] css: set text fields in dialogs to 100% width. * Fixes the flag reason field being too small in the flag dialog * Fixes the source field being too small in the post replacement dialog. --- app/assets/stylesheets/common/simple_form.scss | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/assets/stylesheets/common/simple_form.scss b/app/assets/stylesheets/common/simple_form.scss index d2926f181..7e3b606a7 100644 --- a/app/assets/stylesheets/common/simple_form.scss +++ b/app/assets/stylesheets/common/simple_form.scss @@ -66,3 +66,12 @@ form.inline-form { } } } + +div.ui-dialog { + div.input { + input[type="text"] { + width: 100%; + max-width: 100%; + } + } +} From c27668d2ef9ef09813415dbbeed3d7bb3f618f96 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Jun 2017 15:19:41 -0500 Subject: [PATCH 2/7] post replacements: add option to fix source after replacement. Adds a "Final Source" field to the post replacement dialog. If specified, the post's source field will be changed to this value after replacement. This makes fixing the source back to the HTML page after replacement easier. --- app/controllers/post_replacements_controller.rb | 2 +- app/models/post_replacement.rb | 5 +++-- app/views/post_replacements/_new.html.erb | 3 ++- test/unit/post_replacement_test.rb | 10 ++++++++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/controllers/post_replacements_controller.rb b/app/controllers/post_replacements_controller.rb index b9bd22984..d0c95cc8d 100644 --- a/app/controllers/post_replacements_controller.rb +++ b/app/controllers/post_replacements_controller.rb @@ -23,6 +23,6 @@ class PostReplacementsController < ApplicationController private def create_params - params.require(:post_replacement).permit(:replacement_url) + params.require(:post_replacement).permit(:replacement_url, :final_source) end end diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index 6eea498e5..1346f631c 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -4,7 +4,8 @@ class PostReplacement < ApplicationRecord belongs_to :post belongs_to :creator, class_name: "User" before_validation :initialize_fields - attr_accessible :replacement_url + attr_accessible :replacement_url, :final_source + attr_accessor :final_source def initialize_fields self.creator = CurrentUser.user @@ -37,7 +38,7 @@ class PostReplacement < ApplicationRecord post.image_width = upload.image_width post.image_height = upload.image_height post.file_size = upload.file_size - post.source = upload.source + post.source = final_source.presence || upload.source post.tag_string = upload.tag_string rescale_notes update_ugoira_frame_data(upload) diff --git a/app/views/post_replacements/_new.html.erb b/app/views/post_replacements/_new.html.erb index d226ca26d..a80dfce44 100644 --- a/app/views/post_replacements/_new.html.erb +++ b/app/views/post_replacements/_new.html.erb @@ -1,5 +1,6 @@ <%= format_text(WikiPage.titled(Danbooru.config.replacement_notice_wiki_page).first.try(&:body), ragel: true) %> <%= simple_form_for(post_replacement, url: post_replacements_path(post_id: post_replacement.post_id), method: :post) do |f| %> - <%= f.input :replacement_url, label: "New Source", input_html: { value: "" } %> + <%= f.input :replacement_url, label: "Replacement URL", hint: "The source URL to download the replacement from.", as: :string, input_html: { value: post_replacement.post.normalized_source } %> + <%= f.input :final_source, label: "Final Source", hint: "If present, the source field will be changed to this after replacement.", as: :string, input_html: { value: post_replacement.post.source } %> <% end %> diff --git a/test/unit/post_replacement_test.rb b/test/unit/post_replacement_test.rb index 23c48bd7e..bdc6017a2 100644 --- a/test/unit/post_replacement_test.rb +++ b/test/unit/post_replacement_test.rb @@ -190,5 +190,15 @@ class PostReplacementTest < ActiveSupport::TestCase assert(File.exists?(@post.large_file_path)) end end + + context "a post when given a final_source" do + should "change the source to the final_source" do + replacement_url = "http://data.tumblr.com/afed9f5b3c33c39dc8c967e262955de2/tumblr_orwwptNBCE1wsfqepo1_raw.png" + final_source = "https://noizave.tumblr.com/post/162094447052" + @post.replace!(replacement_url: replacement_url, final_source: final_source) + + assert_equal(final_source, @post.source) + end + end end end From d3e89377166c2e7c15643bee4de0a97123019907 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Jun 2017 15:49:27 -0500 Subject: [PATCH 3/7] post replacements: allow replacing with an uploaded file. --- app/controllers/post_replacements_controller.rb | 2 +- app/models/post_replacement.rb | 5 ++--- app/views/post_replacements/_new.html.erb | 1 + test/unit/post_replacement_test.rb | 12 ++++++++++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/controllers/post_replacements_controller.rb b/app/controllers/post_replacements_controller.rb index d0c95cc8d..8250329d1 100644 --- a/app/controllers/post_replacements_controller.rb +++ b/app/controllers/post_replacements_controller.rb @@ -23,6 +23,6 @@ class PostReplacementsController < ApplicationController private def create_params - params.require(:post_replacement).permit(:replacement_url, :final_source) + params.require(:post_replacement).permit(:replacement_url, :replacement_file, :final_source) end end diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index 1346f631c..df84e6a39 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -4,8 +4,7 @@ class PostReplacement < ApplicationRecord belongs_to :post belongs_to :creator, class_name: "User" before_validation :initialize_fields - attr_accessible :replacement_url, :final_source - attr_accessor :final_source + attr_accessor :replacement_file, :final_source def initialize_fields self.creator = CurrentUser.user @@ -24,7 +23,7 @@ class PostReplacement < ApplicationRecord end transaction do - upload = Upload.create!(source: replacement_url, rating: post.rating, tag_string: post.tag_string) + upload = Upload.create!(file: replacement_file, source: replacement_url, rating: post.rating, tag_string: post.tag_string) upload.process_upload upload.update(status: "completed", post_id: post.id) diff --git a/app/views/post_replacements/_new.html.erb b/app/views/post_replacements/_new.html.erb index a80dfce44..f1fbed06b 100644 --- a/app/views/post_replacements/_new.html.erb +++ b/app/views/post_replacements/_new.html.erb @@ -1,6 +1,7 @@ <%= format_text(WikiPage.titled(Danbooru.config.replacement_notice_wiki_page).first.try(&:body), ragel: true) %> <%= simple_form_for(post_replacement, url: post_replacements_path(post_id: post_replacement.post_id), method: :post) do |f| %> + <%= f.input :replacement_file, label: "File", as: :file %> <%= f.input :replacement_url, label: "Replacement URL", hint: "The source URL to download the replacement from.", as: :string, input_html: { value: post_replacement.post.normalized_source } %> <%= f.input :final_source, label: "Final Source", hint: "If present, the source field will be changed to this after replacement.", as: :string, input_html: { value: post_replacement.post.source } %> <% end %> diff --git a/test/unit/post_replacement_test.rb b/test/unit/post_replacement_test.rb index bdc6017a2..cfddbcfd0 100644 --- a/test/unit/post_replacement_test.rb +++ b/test/unit/post_replacement_test.rb @@ -191,6 +191,18 @@ class PostReplacementTest < ActiveSupport::TestCase end end + context "a post with an uploaded file" do + should "work" do + Tempfile.open do |file| + file.write(File.read("#{Rails.root}/test/files/test.png")) + file.seek(0) + + @post.replace!(replacement_file: file, replacement_url: "") + assert_equal(@post.md5, Digest::MD5.file(file).hexdigest) + end + end + end + context "a post when given a final_source" do should "change the source to the final_source" do replacement_url = "http://data.tumblr.com/afed9f5b3c33c39dc8c967e262955de2/tumblr_orwwptNBCE1wsfqepo1_raw.png" From 41b9a8a0443c3f0aa116d73794de273b490c2675 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Jun 2017 16:43:25 -0500 Subject: [PATCH 4/7] post replacements: fix `post_replacements_path` helper. Bug: `post_replacements_path` returned the wrong path because the `/posts/:post_id/replacements` route took precedence over the `/post_replacements` route. Fix: declare the `/post_replacements` route first so that it takes precedence instead. --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index ccec6af30..18664f7b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -196,6 +196,7 @@ Rails.application.routes.draw do get :diff end end + resources :post_replacements, :only => [:index, :new, :create] resources :posts do resources :events, :only => [:index], :controller => "post_events" resources :replacements, :only => [:index, :new, :create], :controller => "post_replacements" @@ -217,7 +218,6 @@ Rails.application.routes.draw do end resources :post_appeals resources :post_flags - resources :post_replacements, :only => [:index, :new, :create] resources :post_versions, :only => [:index, :search] do member do put :undo From 13d49467a205a2f83a7618f6d34e1829c918563f Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Jun 2017 16:47:52 -0500 Subject: [PATCH 5/7] post replacements: fix `search[creator_name]=...` param. --- app/models/post_replacement.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index df84e6a39..474247d61 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -77,7 +77,7 @@ class PostReplacement < ApplicationRecord end if params[:creator_name].present? - q = q.where(creator_name: User.name_to_id(params[:creator_name])) + q = q.where(creator_id: User.name_to_id(params[:creator_name])) end if params[:id].present? From 8b4e598a12f3e81ce22105b16e9c709f63bfc85e Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Jun 2017 16:42:56 -0500 Subject: [PATCH 6/7] post replacements: add /post_replacements view. --- app/helpers/application_helper.rb | 8 ++++ app/views/post_replacements/index.html.erb | 54 ++++++++++++++++++++++ app/views/posts/show.html.erb | 1 + app/views/static/site_map.html.erb | 1 + 4 files changed, 64 insertions(+) create mode 100644 app/views/post_replacements/index.html.erb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 21ec75472..1723d70ab 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -101,6 +101,14 @@ module ApplicationHelper time_tag(time.strftime("%Y-%m-%d %H:%M"), time) end + def external_link_to(url) + if url =~ %r!\Ahttps?://!i + link_to url, {}, {rel: :nofollow} + else + url + end + end + def link_to_ip(ip) link_to ip, moderator_ip_addrs_path(:search => {:ip_addr => ip}) end diff --git a/app/views/post_replacements/index.html.erb b/app/views/post_replacements/index.html.erb new file mode 100644 index 000000000..ce84f0af1 --- /dev/null +++ b/app/views/post_replacements/index.html.erb @@ -0,0 +1,54 @@ +
+
+

Post Replacements

+ + <%= render "posts/partials/common/inline_blacklist" %> + + <%= simple_form_for(:search, url: post_replacements_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %> + <%= f.input :creator_name, label: "Replacer", input_html: { value: params[:search][:creator_name] } %> + <%= f.submit "Search" %> + <% end %> + + + + + + + + + + + <% @post_replacements.each do |post_replacement| %> + + + + + + <% end %> + +
PostSourceReplacer
<%= PostPresenter.preview(post_replacement.post) %> +
+
Original Source
+
<%= external_link_to post_replacement.original_url %>
+
Replacement Source
+
+ <% if post_replacement.replacement_url.present? %> + <%= external_link_to post_replacement.replacement_url %> + <% else %> + file + <% end %> +
+
+
+ <%= compact_time post_replacement.created_at %> +
by <%= link_to_user post_replacement.creator %> + <%= link_to "ยป", post_replacements_path(search: params[:search].merge(creator_name: post_replacement.creator.name)) %> +
+ + <%= numbered_paginator(@post_replacements) %> +
+
+ +<% content_for(:page_title) do %> + Post Replacements - <%= Danbooru.config.app_name %> +<% end %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 6cb62c631..3a383c1ab 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -27,6 +27,7 @@
  • <%= fast_link_to "Notes", note_versions_path(:search => {:post_id => @post.id}) %>
  • <%= fast_link_to "Flags & Appeals", post_events_path(@post.id) %>
  • <%= fast_link_to "Commentary", artist_commentary_versions_path(:search => {:post_id => @post.id}) %>
  • +
  • <%= fast_link_to "Replacements", post_replacements_path(:search => {:post_id => @post.id}) %>
  • diff --git a/app/views/static/site_map.html.erb b/app/views/static/site_map.html.erb index 496d85807..b2706ff63 100644 --- a/app/views/static/site_map.html.erb +++ b/app/views/static/site_map.html.erb @@ -11,6 +11,7 @@
  • <%= link_to("Upload Listing", uploads_path) %>
  • <%= link_to("Appeals", post_appeals_path) %>
  • <%= link_to("Flags", post_flags_path) %>
  • +
  • <%= link_to("Replacements", post_replacements_path) %>
  • <% if CurrentUser.can_approve_posts? %>
  • <%= link_to("Moderate", moderator_post_queue_path) %>
  • <% end %> From 6329d089ee2d7fe3404245c8e57bec5c9bf59141 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Jun 2017 16:50:25 -0500 Subject: [PATCH 7/7] post replacements: add tag match field to search form. --- app/models/post_replacement.rb | 8 ++++++++ app/views/post_replacements/index.html.erb | 1 + 2 files changed, 9 insertions(+) diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index 474247d61..b41c314de 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -69,6 +69,10 @@ class PostReplacement < ApplicationRecord end module SearchMethods + def post_tags_match(query) + PostQueryBuilder.new(query).build(self.joins(:post)) + end + def search(params = {}) q = all @@ -88,6 +92,10 @@ class PostReplacement < ApplicationRecord q = q.where(post_id: params[:post_id].split(",").map(&:to_i)) end + if params[:post_tags_match].present? + q = q.post_tags_match(params[:post_tags_match]) + end + q = q.order("created_at DESC") q diff --git a/app/views/post_replacements/index.html.erb b/app/views/post_replacements/index.html.erb index ce84f0af1..b1cfaa87f 100644 --- a/app/views/post_replacements/index.html.erb +++ b/app/views/post_replacements/index.html.erb @@ -6,6 +6,7 @@ <%= simple_form_for(:search, url: post_replacements_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %> <%= f.input :creator_name, label: "Replacer", input_html: { value: params[:search][:creator_name] } %> + <%= f.input :post_tags_match, label: "Tags", input_html: { value: params[:search][:post_tags_match] } %> <%= f.submit "Search" %> <% end %>