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

This reverts a part of changes introduced by https://github.com/discourse/discourse/pull/13947

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.
This commit is contained in:
Andrei Prigorshnev 2021-08-06 20:07:42 +04:00 committed by GitHub
parent 0d8fd9ace6
commit 09ad3ed41d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 1 additions and 87 deletions

View File

@ -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?) },

View File

@ -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."

View File

@ -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)

View File

@ -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|

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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)