From 28e5ef1d12410c37033c435ed92ebb75298fb837 Mon Sep 17 00:00:00 2001 From: Mac Siri Date: Tue, 9 Jul 2019 17:34:21 -0400 Subject: [PATCH] Make RSS's self-referential link an option (#3428) * Create migration * Run migration * Update validations * Add additional form * Update RssReader::Assembler#thorough_parsing * Add spec for referential --- app/models/user.rb | 1 + app/policies/user_policy.rb | 119 +++++++++--------- app/services/rss_reader/assembler.rb | 2 +- app/views/users/_publishing_from_rss.html.erb | 8 ++ ...2214_add_feed_referential_link_to_users.rb | 5 + db/schema.rb | 3 +- spec/services/rss_reader_spec.rb | 11 ++ 7 files changed, 89 insertions(+), 60 deletions(-) create mode 100644 db/migrate/20190709192214_add_feed_referential_link_to_users.rb diff --git a/app/models/user.rb b/app/models/user.rb index ff7535669..3c39bfb34 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -132,6 +132,7 @@ class User < ApplicationRecord length: { maximum: 500 } validates :inbox_type, inclusion: { in: %w[open private] } validates :currently_streaming_on, inclusion: { in: %w[twitch] }, allow_nil: true + validates :feed_referential_link, inclusion: [true, false] validate :conditionally_validate_summary validate :validate_mastodon_url validate :validate_feed_url, if: :feed_url_changed? diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index 0772ef18d..cabcfe69b 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -48,64 +48,67 @@ class UserPolicy < ApplicationPolicy end def permitted_attributes - %i[available_for - behance_url - bg_color_hex - config_theme - config_font - contact_consent - currently_hacking_on - currently_learning - display_sponsors - dribbble_url - education - email - email_badge_notifications - email_comment_notifications - email_digest_periodic - email_follower_notifications - email_membership_newsletter - email_tag_mod_newsletter - email_community_mod_newsletter - email_mention_notifications - email_connect_messages - email_newsletter - email_public - editor_version - email_unread_notifications - mobile_comment_notifications - employer_name - employer_url - employment_title - experience_level - facebook_url - feed_admin_publish_permission - feed_mark_canonical - feed_url - gitlab_url - inbox_guidelines - instagram_url - linkedin_url - location - looking_for_work - looking_for_work_publicly - mastodon_url - medium_url - mostly_work_with - name - inbox_type - permit_adjacent_sponsors - password - password_confirmation - profile_image - stackoverflow_url - summary - text_color_hex - twitch_url - twitch_username - username - website_url - export_requested] + %i[ + available_for + behance_url + bg_color_hex + config_font + config_theme + contact_consent + currently_hacking_on + currently_learning + display_sponsors + dribbble_url + editor_version + education + email + email_badge_notifications + email_comment_notifications + email_community_mod_newsletter + email_connect_messages + email_digest_periodic + email_follower_notifications + email_membership_newsletter + email_mention_notifications + email_newsletter + email_public + email_tag_mod_newsletter + email_unread_notifications + employer_name + employer_url + employment_title + experience_level + export_requested + facebook_url + feed_admin_publish_permission + feed_mark_canonical + feed_referential_link + feed_url + gitlab_url + inbox_guidelines + inbox_type + instagram_url + linkedin_url + location + looking_for_work + looking_for_work_publicly + mastodon_url + medium_url + mobile_comment_notifications + mostly_work_with + name + password + password_confirmation + permit_adjacent_sponsors + profile_image + stackoverflow_url + summary + text_color_hex + twitch_url + twitch_username + username + website_url + ] end private diff --git a/app/services/rss_reader/assembler.rb b/app/services/rss_reader/assembler.rb index e56e1df20..71e3ce7f0 100644 --- a/app/services/rss_reader/assembler.rb +++ b/app/services/rss_reader/assembler.rb @@ -56,7 +56,7 @@ class RssReader def thorough_parsing(content, feed_url) html_doc = Nokogiri::HTML(content) - find_and_replace_possible_links!(html_doc) + find_and_replace_possible_links!(html_doc) if @user.feed_referential_link if feed_url.include?("medium.com") parse_and_translate_gist_iframe!(html_doc) parse_and_translate_youtube_iframe!(html_doc) diff --git a/app/views/users/_publishing_from_rss.html.erb b/app/views/users/_publishing_from_rss.html.erb index 7ca5699dd..663e97d82 100644 --- a/app/views/users/_publishing_from_rss.html.erb +++ b/app/views/users/_publishing_from_rss.html.erb @@ -47,6 +47,14 @@ If you check this box, the post will automatically mark the feed source as the canonical URL. + +
+ <%= f.check_box :feed_referential_link %> + <%= f.label :feed_referential_link, "Replace self-referential links with DEV-specific links" %> +
+ If you check this box, the post will automatically change any URLs included in the post to refer to the version of that article on DEV if available. This is primarily meant for folks migrating their entire blog onto DEV. +
+
diff --git a/db/migrate/20190709192214_add_feed_referential_link_to_users.rb b/db/migrate/20190709192214_add_feed_referential_link_to_users.rb new file mode 100644 index 000000000..a7f77b4f1 --- /dev/null +++ b/db/migrate/20190709192214_add_feed_referential_link_to_users.rb @@ -0,0 +1,5 @@ +class AddFeedReferentialLinkToUsers < ActiveRecord::Migration[5.2] + def change + add_column :users, :feed_referential_link, :boolean, null: false, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 1de83b0a2..b2a2b83cc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_07_04_105143) do +ActiveRecord::Schema.define(version: 2019_07_09_192214) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -963,6 +963,7 @@ ActiveRecord::Schema.define(version: 2019_07_04_105143) do t.boolean "feed_admin_publish_permission", default: true t.datetime "feed_fetched_at", default: "2017-01-01 05:00:00" t.boolean "feed_mark_canonical", default: false + t.boolean "feed_referential_link", default: true, null: false t.string "feed_url" t.integer "following_orgs_count", default: 0, null: false t.integer "following_tags_count", default: 0, null: false diff --git a/spec/services/rss_reader_spec.rb b/spec/services/rss_reader_spec.rb index 28611e3b6..b3a37d308 100644 --- a/spec/services/rss_reader_spec.rb +++ b/spec/services/rss_reader_spec.rb @@ -73,6 +73,17 @@ RSpec.describe RssReader, vcr: vcr_option do end end + context "when feed_referential_link is false" do + it "does not self-reference links for user" do + # Article.find_by is used by find_and_replace_possible_links! + # checking it's invocation s a shortcut to testing the functionality. + allow(Article).to receive(:find_by).and_call_original + create(:user, feed_url: nonpermanent_link, feed_referential_link: false) + described_class.get_all_articles + expect(Article).not_to have_received(:find_by) + end + end + describe "#fetch_user" do before do [link, nonmedium_link, nonpermanent_link].each do |feed_url|