FEATURE: revert disallowing putting URLs in titles for TL0 users (#13970)

FEATURE: revert disallowing putting URLs in titles for TL0 users (#13970)

This reverts a part of changes introduced by FEATURE: Disallow putting urls in the title for TL-0 users by AndrewPrigorshnev · Pull Request #13947 · discourse/discourse · GitHub

In that PR I:

  1. Disallowed topic feature links for TL-0 users
  2. Additionally, disallowed just putting any URL in topic titles for TL-0 users

Actually, we don’t need the second part. It introduced unnecessary complexity for no good reason. In fact, it tries to do the job that anti-spam plugins (like Akismet plugin) should be doing.

This PR reverts this second change.

diff --git a/app/models/topic.rb b/app/models/topic.rb
index ce78ed1..6b89da4 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -164,7 +164,6 @@ class Topic < ActiveRecord::Base
                     topic_title_length: true,
                     censored_words: true,
                     watched_words: true,
-                    urls_in_topic_title: true,
                     quality_title: { unless: :private_message? },
                     max_emojis: true,
                     unique_among: { unless: Proc.new { |t| (SiteSetting.allow_duplicate_topic_titles? || t.private_message?) },
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index c5c22c7..04e3d20 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -358,7 +358,6 @@ en:
     other: "Sorry, new users can only put %{count} attachments in a post."
   no_links_allowed: "Sorry, new users can't put links in posts."
   links_require_trust: "Sorry, you can't include links in your posts."
-  urls_in_title_require_trust_level: "Sorry, new users can't include links in topic titles."
   too_many_links:
     one: "Sorry, new users can only put one link in a post."
     other: "Sorry, new users can only put %{count} links in a post."
diff --git a/lib/guardian.rb b/lib/guardian.rb
index 77e32aa..54822b9 100644
--- a/lib/guardian.rb
+++ b/lib/guardian.rb
@@ -540,10 +540,6 @@ class Guardian
     !SiteSetting.login_required? || authenticated?
   end
 
-  def can_put_urls_in_topic_title?
-    @user.trust_level >= TrustLevel.levels[:basic]
-  end
-
   def auth_token
     if cookie = request&.cookies[Auth::DefaultCurrentUserProvider::TOKEN_COOKIE]
       UserAuthToken.hash_token(cookie)
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index 1cfc60a..42b70d0 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -77,12 +77,7 @@ class PostRevisor
   end
 
   track_topic_field(:title) do |topic_changes, attribute|
-    if UrlHelper.contains_url?(attribute) && !topic_changes.guardian.can_put_urls_in_topic_title?
-      topic_changes.topic.errors.add(:base, I18n.t("urls_in_title_require_trust_level"))
-      topic_changes.check_result(false)
-    else
-      track_and_revise topic_changes, :title, attribute
-    end
+    track_and_revise topic_changes, :title, attribute
   end
 
   track_topic_field(:archetype) do |topic_changes, attribute|
diff --git a/lib/url_helper.rb b/lib/url_helper.rb
index f55b40e..b1f10df 100644
--- a/lib/url_helper.rb
+++ b/lib/url_helper.rb
@@ -65,11 +65,6 @@ class UrlHelper
     Addressable::URI.normalized_encode(uri)
   end
 
-  def self.contains_url?(string)
-    uri_regexp = Discourse::Utils::URI_REGEXP
-    uri_regexp.match?(string)
-  end
-
   def self.rails_route_from_url(url)
     path = URI.parse(encode(url)).path
     Rails.application.routes.recognize_path(path)
diff --git a/lib/validators/urls_in_topic_title_validator.rb b/lib/validators/urls_in_topic_title_validator.rb
deleted file mode 100644
index 1bb03bc..0000000
--- a/lib/validators/urls_in_topic_title_validator.rb
+++ /dev/null
@@ -1,20 +0,0 @@
-# frozen_string_literal: true
-
-class UrlsInTopicTitleValidator < ActiveModel::Validator
-  def validate(record)
-    if UrlHelper.contains_url?(record.title) && !can_put_urls?(record)
-      record.errors.add(:base, error_message)
-    end
-  end
-
-  private
-
-  def can_put_urls?(topic)
-    guardian = Guardian.new(topic.acting_user)
-    guardian.can_put_urls_in_topic_title?
-  end
-
-  def error_message
-    I18n.t("urls_in_title_require_trust_level")
-  end
-end
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 424fa12..ec5bf41 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -1459,33 +1459,6 @@ describe PostsController do
       end
     end
 
-    describe "urls in the title" do
-      let!(:title_with_url) { "A title with the URL https://google.com" }
-
-      it "doesn't allow TL0 users to put urls into the title" do
-        sign_in(user_trust_level_0)
-
-        post "/posts.json", params: {
-          raw: 'this is the test content',
-          title: title_with_url
-        }
-
-        expect(response.status).to eq(422)
-        expect(response.body).to include(I18n.t('urls_in_title_require_trust_level'))
-      end
-
-      it "allows TL1 users to put urls into the title" do
-        sign_in(user_trust_level_1)
-
-        post "/posts.json", params: {
-          raw: 'this is the test content',
-          title: title_with_url
-        }
-
-        expect(response.status).to eq(200)
-      end
-    end
-
     describe "featured links" do
       it "allows to create topics with featured links" do
         sign_in(user_trust_level_1)
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 9cd8c36..0bbafc8 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -1653,29 +1653,6 @@ RSpec.describe TopicsController do
       end
     end
 
-    describe "urls in the title" do
-      let!(:title_with_url) { "A title with the URL https://google.com" }
-
-      it "doesn't allow TL0 users to put urls into the title" do
-        sign_in(trust_level_0)
-        topic = Fabricate(:topic, user: trust_level_0)
-        Fabricate(:post, topic: topic)
-        put "/t/#{topic.slug}/#{topic.id}.json", params: { title: title_with_url }
-
-        expect(response.status).to eq(422)
-        expect(response.body).to include(I18n.t('urls_in_title_require_trust_level'))
-      end
-
-      it "allows TL1 users to put urls into the title" do
-        sign_in(trust_level_1)
-        topic = Fabricate(:topic, user: trust_level_1)
-        Fabricate(:post, topic: topic)
-        put "/t/#{topic.slug}/#{topic.id}.json", params: { title: title_with_url }
-
-        expect(response.status).to eq(200)
-      end
-    end
-
     describe "featured links" do
       def fabricate_topic(user, category = nil)
         topic = Fabricate(:topic, user: user, category: category)

GitHub sha: 09ad3ed41d98e1df358f4da89145e6e7d27936e3

This commit appears in #13970 which was approved by eviltrout. It was merged by AndrewPrigorshnev.