From 1d2a8a7898e8c5e1fdea30b684b03e89f283c041 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 18 Aug 2020 14:43:07 -0500 Subject: [PATCH] fanbox: don't raise error on age-restricted posts. Prevent age-restricted fanbox posts from raising errors when source data is fetched. This prevents error messages from being shown to users when switching to the edit tab on a post. This will cause uploads of age-restricted posts to fail with an unrelated error because we either can't find the image url (if we were given only the html page) or we can't download the image (because we're not logged in to Fanbox). --- app/logical/sources/strategies/fanbox.rb | 22 ++++++++++------------ test/functional/uploads_controller_test.rb | 3 +++ test/unit/sources/fanbox.rb | 9 +++++++++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/logical/sources/strategies/fanbox.rb b/app/logical/sources/strategies/fanbox.rb index c6099ca0b..31e1f2803 100644 --- a/app/logical/sources/strategies/fanbox.rb +++ b/app/logical/sources/strategies/fanbox.rb @@ -105,11 +105,7 @@ module Sources end def display_name - if api_response.present? - api_response["user"]["name"] - elsif artist_api_response.present? - artist_api_response["user"]["name"] - end + api_response.dig("user", "name") || artist_api_response.dig("user", "name") end def other_names @@ -125,8 +121,9 @@ module Sources end def artist_commentary_desc - return if api_response.blank? body = api_response["body"] + return if body.blank? + if body["text"].present? body["text"] elsif body["blocks"].present? @@ -160,11 +157,12 @@ module Sources return {} if illust_id.blank? resp = client.get("https://api.fanbox.cc/post.info?postId=#{illust_id}") json_response = JSON.parse(resp)["body"] - if json_response["restrictedFor"] == 2 && json_response["body"].blank? - # Pixiv Fanbox login is protected by Google Recaptcha, so it's not possible for us to extract anything from them (save for the title). - # Other projects like PixivUtils ask the user to periodically extract cookies from the browser, but this is not feasible for Danbooru. - raise Sources::Error, "Age-restricted posts from Pixiv Fanbox are not supported." - end + + # Pixiv Fanbox login is protected by Google Recaptcha, so it's not + # possible for us to extract anything from them (save for the title). + # Other projects like PixivUtils ask the user to periodically extract + # cookies from the browser, but this is not feasible for Danbooru. + return {} if json_response["restrictedFor"] == 2 && json_response["body"].blank? json_response rescue JSON::ParserError @@ -181,7 +179,7 @@ module Sources end def client - Danbooru::Http.headers(Origin: "https://fanbox.cc").cache(1.minute) + @client ||= http.headers(Origin: "https://fanbox.cc").cache(1.minute) end end end diff --git a/test/functional/uploads_controller_test.rb b/test/functional/uploads_controller_test.rb index 17bbb071d..498b18c54 100644 --- a/test/functional/uploads_controller_test.rb +++ b/test/functional/uploads_controller_test.rb @@ -371,6 +371,9 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest should_upload_successfully("https://art.ngfiles.com/images/1254000/1254722_natthelich_pandora.jpg") should_upload_successfully("https://art.ngfiles.com/comments/57000/iu_57615_7115981.jpg") should_upload_successfully("https://www.newgrounds.com/art/view/puddbytes/costanza-at-bat") + + should_upload_successfully("https://www.fanbox.cc/@tsukiori/posts/1080657") + should_upload_successfully("https://downloads.fanbox.cc/images/post/1080657/SaakPC251KafLL6jIo1WPPmr.png") end end end diff --git a/test/unit/sources/fanbox.rb b/test/unit/sources/fanbox.rb index cfd1d7519..c7244b03e 100644 --- a/test/unit/sources/fanbox.rb +++ b/test/unit/sources/fanbox.rb @@ -90,6 +90,15 @@ module Sources end end + context "an age-restricted fanbox post" do + should "not raise an error" do + @source = Sources::Strategies.find("https://mfr.fanbox.cc/posts/1306390") + + assert_nothing_raised { @source.to_h } + assert_equal("mfr", @source.artist_name) + end + end + context "A link in the old format" do should "still work" do post = Sources::Strategies.find("https://www.pixiv.net/fanbox/creator/1566167/post/39714")