From c3c4f5a2a70b737500105206286f54696d643175 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 14 Jan 2022 22:42:19 -0600 Subject: [PATCH] Fix #4957: Autotag non-web_source. Autotag non-web_source on posts that have a non-http:// or https:// URL. Add a fix script to backfill old posts. Syntactically invalid URLs are still considered web sources. For example, `https://google,com` technically isn't a valid URL, but it's not considered a non-web source. --- app/models/post.rb | 6 +++++- script/fixes/089_add_non-web_source_tag.rb | 25 ++++++++++++++++++++++ test/unit/post_test.rb | 24 +++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100755 script/fixes/089_add_non-web_source_tag.rb diff --git a/app/models/post.rb b/app/models/post.rb index 3350988d0..ede050c04 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -404,7 +404,7 @@ class Post < ApplicationRecord end def add_automatic_tags(tags) - tags -= %w[incredibly_absurdres absurdres highres lowres flash video ugoira animated_gif animated_png exif_rotation non-repeating_animation] + tags -= %w[incredibly_absurdres absurdres highres lowres flash video ugoira animated_gif animated_png exif_rotation non-repeating_animation non-web_source] if tags.size >= 30 tags -= ["tagme"] @@ -443,6 +443,10 @@ class Post < ApplicationRecord tags << "ugoira" end + if source.present? && source !~ %r{\Ahttps?://}i + tags << "non-web_source" + end + # Allow only Flash files to be manually tagged as `animated`; GIFs, PNGs, videos, and ugoiras are automatically tagged. tags -= ["animated"] unless is_flash? tags << "animated" if media_asset.is_animated? diff --git a/script/fixes/089_add_non-web_source_tag.rb b/script/fixes/089_add_non-web_source_tag.rb new file mode 100755 index 000000000..d0b46bcf0 --- /dev/null +++ b/script/fixes/089_add_non-web_source_tag.rb @@ -0,0 +1,25 @@ +#!/usr/bin/env ruby + +require_relative "base" + +CurrentUser.scoped(User.system) do + Post.system_tag_match("-source:none -source:http://* -source:https://* -non-web_source").find_each do |post| + puts "post ##{post.id}: adding non-web_source" + post.save! + end + + Post.system_tag_match("source:none non-web_source").find_each do |post| + puts "post ##{post.id}: removing non-web_source" + post.save! + end + + Post.system_tag_match("source:http://* non-web_source").find_each do |post| + puts "post ##{post.id}: removing non-web_source" + post.save! + end + + Post.system_tag_match("source:https://* non-web_source").find_each do |post| + puts "post ##{post.id}: removing non-web_source" + post.save! + end +end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index cd5b2c8af..a34ae6637 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -1046,6 +1046,30 @@ class PostTest < ActiveSupport::TestCase end end + context "a post with a non-web source" do + should "automatically add the non-web_source tag" do + @post.update!(source: "this was once revealed to me in a dream") + @post.save! + assert_equal("non-web_source tag1 tag2", @post.tag_string) + end + end + + context "a post with a blank source" do + should "remove the non-web_source tag" do + @post.update!(source: "", tag_string: "non-web_source") + @post.save! + assert_equal("tagme", @post.tag_string) + end + end + + context "a post with a https:// source" do + should "remove the non-web_source tag" do + @post.update!(source: "https://www.google.com", tag_string: "non-web_source") + @post.save! + assert_equal("tagme", @post.tag_string) + end + end + should "have an array representation of its tags" do post = FactoryBot.create(:post) post.reload