uploads: fix sources for 4chan uploads.

Fix a bug where, if you were uploading an entire 4chan thread, then the source of each post would
get set to the 4chan thread, rather than to the individual 4chan post.
This commit is contained in:
evazion
2022-11-18 21:27:10 -06:00
parent 902cd0bbdf
commit f8d2758f8b
2 changed files with 63 additions and 11 deletions

View File

@@ -67,17 +67,27 @@ class UploadMediaAsset < ApplicationRecord
source_url.starts_with?("file://")
end
# The source of the post after upload.
# The source of the post after upload. This is either the image URL, if the image URL is convertible to a page URL
# (e.g. Pixiv), or the page URL if it's not (e.g. Twitter).
def canonical_url
return source_url if file_upload?
# If the image URL is convertible to a page URL, or the page URL couldn't
# be found, then use the image URL as the source of the post. Otherwise,
# use the page URL.
if Source::URL.page_url(source_url).present? || page_url.blank?
if file_upload?
source_url
else
# If the source is an image URL that is convertible to a page URL, then use the image URL as the post source.
elsif Source::URL.page_url(source_url).present?
source_url
# If a better page URL can be found by the extractor (potentially with an API call), then use that as the source.
elsif source_extractor.page_url.present?
source_extractor.page_url
# If we can't find any better page URL, then just use the one we already have.
elsif page_url.present?
page_url
# Otherwise if we can't find a page URL at all, then just use the image URL.
else
source_url
end
end

View File

@@ -5,10 +5,19 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
assert_equal(expected, response.parsed_body.css("link[rel=canonical]").attribute("href").value)
end
def create_post!(user: create(:user), media_asset: build(:media_asset), rating: "q", tag_string: "tagme", **params)
def assert_post_source_equals(expected_source, source_url, page_url = nil)
post = create_post!(source_url: source_url, page_url: page_url)
assert_response :redirect
assert_equal(expected_source, post.source)
end
def create_post!(user: create(:user), media_asset: build(:media_asset), rating: "q", tag_string: "tagme", source_url: nil, page_url: nil, **params)
upload = build(:upload, uploader: user, media_asset_count: 1, status: "completed")
asset = create(:upload_media_asset, upload: upload, media_asset: media_asset)
post_auth posts_path, user, params: { upload_media_asset_id: asset.id, post: { rating: rating, tag_string: tag_string, **params }}
asset = create(:upload_media_asset, upload: upload, media_asset: media_asset, **{ source_url: source_url, page_url: page_url }.compact_blank)
RateLimit.delete_all
post_auth posts_path, user, params: { upload_media_asset_id: asset.id, post: { rating: rating, source: asset.canonical_url, tag_string: tag_string, **params }}
Post.last
end
@@ -784,6 +793,39 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
assert_equal(false, @post.artist_commentary.present?)
end
should "set the correct source after upload" do
assert_post_source_equals("https://i.pximg.net/img-original/img/2017/08/18/00/09/21/64476642_p0.jpg", "https://i.pximg.net/img-original/img/2017/08/18/00/09/21/64476642_p0.jpg")
assert_post_source_equals("https://i.pximg.net/img-original/img/2017/08/18/00/09/21/64476642_p0.jpg", "https://i.pximg.net/img-original/img/2017/08/18/00/09/21/64476642_p0.jpg", "https://www.pixiv.net/en/artworks/64476642")
assert_post_source_equals("https://pbs.twimg.com/media/DCdZ_FhUIAAYKFN.jpg:orig", "https://pbs.twimg.com/media/DCdZ_FhUIAAYKFN.jpg:orig")
assert_post_source_equals("https://twitter.com/noizave/status/875768175136317440", "https://pbs.twimg.com/media/DCdZ_FhUIAAYKFN.jpg:orig", "https://twitter.com/noizave/status/875768175136317440")
assert_post_source_equals("https://noizave.tumblr.com/post/162206271767", "https://media.tumblr.com/3bbfcbf075ddf969c996641b264086fd/tumblr_os2buiIOt51wsfqepo1_1280.png")
assert_post_source_equals(
"https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/intermediary/f/8b472d70-a0d6-41b5-9a66-c35687090acc/d23jbr4-8a06af02-70cb-46da-8a96-42a6ba73cdb4.jpg/v1/fill/w_786,h_1017,q_70,strp/silverhawks_quicksilver_by_edsfox_d23jbr4-pre.jpg",
"https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/intermediary/f/8b472d70-a0d6-41b5-9a66-c35687090acc/d23jbr4-8a06af02-70cb-46da-8a96-42a6ba73cdb4.jpg/v1/fill/w_786,h_1017,q_70,strp/silverhawks_quicksilver_by_edsfox_d23jbr4-pre.jpg"
)
assert_post_source_equals(
"https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/intermediary/f/8b472d70-a0d6-41b5-9a66-c35687090acc/d23jbr4-8a06af02-70cb-46da-8a96-42a6ba73cdb4.jpg/v1/fill/w_786,h_1017,q_70,strp/silverhawks_quicksilver_by_edsfox_d23jbr4-pre.jpg",
"https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/intermediary/f/8b472d70-a0d6-41b5-9a66-c35687090acc/d23jbr4-8a06af02-70cb-46da-8a96-42a6ba73cdb4.jpg/v1/fill/w_786,h_1017,q_70,strp/silverhawks_quicksilver_by_edsfox_d23jbr4-pre.jpg",
"https://www.deviantart.com/edsfox/art/Silverhawks-Quicksilver-126872896"
)
assert_post_source_equals("https://cdna.artstation.com/p/assets/images/images/000/705/368/large/jey-rain-one1.jpg?1443931773", "https://cdna.artstation.com/p/assets/images/images/000/705/368/large/jey-rain-one1.jpg?1443931773")
assert_post_source_equals("https://jeyrain.artstation.com/projects/04XA4", "https://cdna.artstation.com/p/assets/images/images/000/705/368/large/jey-rain-one1.jpg?1443931773", "https://www.artstation.com/artwork/04XA4")
assert_post_source_equals("https://i0.hdslb.com/bfs/album/669c0974a2a7508cbbb60b185eddaa0ccf8c5b7a.jpg", "https://i0.hdslb.com/bfs/album/669c0974a2a7508cbbb60b185eddaa0ccf8c5b7a.jpg")
assert_post_source_equals("https://h.bilibili.com/83341894", "https://i0.hdslb.com/bfs/album/669c0974a2a7508cbbb60b185eddaa0ccf8c5b7a.jpg", "https://h.bilibili.com/83341894")
assert_post_source_equals("https://i0.hdslb.com/bfs/new_dyn/675526fd8baa2f75d7ea0e7ea957bc0811742550.jpg", "https://i0.hdslb.com/bfs/new_dyn/675526fd8baa2f75d7ea0e7ea957bc0811742550.jpg")
assert_post_source_equals("https://t.bilibili.com/686082748803186697", "https://i0.hdslb.com/bfs/new_dyn/675526fd8baa2f75d7ea0e7ea957bc0811742550.jpg", "https://t.bilibili.com/686082748803186697")
assert_post_source_equals("https://i.4cdn.org/vt/1611919211191.jpg", "https://i.4cdn.org/vt/1611919211191.jpg")
assert_post_source_equals("https://boards.4channel.org/vt/thread/1#p1", "https://i.4cdn.org/vt/1611919211191.jpg", "https://boards.4channel.org/vt/thread/1")
end
end
context "update action" do