From dadd6aed47f400c5751492d5be7b885b3ffc0255 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 29 Jan 2022 22:09:23 -0600 Subject: [PATCH] uploads: fix not being able to change the source field during upload. Fix not being able to change the post's source when submitting the upload. For example, if you were uploading a Twitter image from a direct Twitter image URL, and you tried to change the source to the tweet URL on the upload page before creating the post, then the source would be ignored when the post was created. --- app/models/post.rb | 4 +++- app/views/uploads/show.html.erb | 2 +- test/functional/posts_controller_test.rb | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index b82aa67aa..5c417a4ae 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,4 +1,6 @@ # frozen_string_literal: true +# normalize unicode in non-web sources +# normalize percent-encode unicode in source urls class Post < ApplicationRecord class RevertError < StandardError; end @@ -96,7 +98,7 @@ class Post < ApplicationRecord file_size: media_asset.file_size, image_width: media_asset.image_width, image_height: media_asset.image_height, - source: Sources::Strategies.find(upload.source, upload.referer_url).canonical_url || upload.source, + source: params[:source].to_s, tag_string: params[:tag_string], rating: params[:rating], parent_id: params[:parent_id], diff --git a/app/views/uploads/show.html.erb b/app/views/uploads/show.html.erb index 02612d334..50d0b1f9b 100644 --- a/app/views/uploads/show.html.erb +++ b/app/views/uploads/show.html.erb @@ -56,7 +56,7 @@ <%= f.input :media_asset_id, as: :hidden, input_html: { value: @media_asset.id } %> <%= f.input :upload_media_asset_id, as: :hidden, input_html: { value: @upload_media_asset.id } %> - <%= f.input :source, as: :string %> + <%= f.input :source, as: :string, input_html: { value: @upload.source_strategy&.canonical_url } %> <%= f.input :rating, collection: [["Explicit", "e"], ["Questionable", "q"], ["Safe", "s"]], as: :radio_buttons, selected: @post.rating %> <%= f.input :parent_id, label: "Parent ID", as: :string, input_html: { value: @post.parent_id } %> diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index 7b92a486b..43a32a51a 100644 --- a/test/functional/posts_controller_test.rb +++ b/test/functional/posts_controller_test.rb @@ -663,6 +663,13 @@ class PostsControllerTest < ActionDispatch::IntegrationTest assert_equal("tagme", @post.tag_string) end + should "set the source" do + @post = create_post!(source: "https://www.example.com") + + assert_redirected_to @post + assert_equal("https://www.example.com", @post.source) + end + should "autoban the post when it is tagged banned_artist" do @post = create_post!(tag_string: "banned_artist") assert_equal(true, @post.is_banned?)