FEATURE: A site setting for a minimum TL to post links

This commit is contained in:
Robin Ward 2018-02-06 18:07:24 -05:00
parent ce26f48f97
commit 1bab15c757
5 changed files with 25 additions and 7 deletions

View File

@ -215,6 +215,7 @@ en:
one: "Sorry, new users can only put one attachment in a post." one: "Sorry, new users can only put one attachment in a post."
other: "Sorry, new users can only put %{count} attachments in a post." other: "Sorry, new users can only put %{count} attachments in a post."
no_links_allowed: "Sorry, new users can't put links in posts." no_links_allowed: "Sorry, new users can't put links in posts."
links_require_trust: "Sorry, you can't include links in your posts."
too_many_links: too_many_links:
one: "Sorry, new users can only put one link in a post." one: "Sorry, new users can only put one link in a post."
other: "Sorry, new users can only put %{count} links in a post." other: "Sorry, new users can only put %{count} links in a post."
@ -1292,6 +1293,7 @@ en:
min_trust_to_send_email_messages: "The minimum trust level required to send new personal messages via email (to staged users)." min_trust_to_send_email_messages: "The minimum trust level required to send new personal messages via email (to staged users)."
min_trust_to_flag_posts: "The minimum trust level required to flag posts" min_trust_to_flag_posts: "The minimum trust level required to flag posts"
min_trust_to_post_links: "The minimum trust level required to include links in posts"
newuser_max_links: "How many links a new user can add to a post." newuser_max_links: "How many links a new user can add to a post."
newuser_max_images: "How many images a new user can add to a post." newuser_max_images: "How many images a new user can add to a post."

View File

@ -897,6 +897,9 @@ trust:
min_trust_to_flag_posts: min_trust_to_flag_posts:
default: 1 default: 1
enum: 'TrustLevelSetting' enum: 'TrustLevelSetting'
min_trust_to_post_links:
default: 0
enum: 'TrustLevelSetting'
tl1_requires_topics_entered: 5 tl1_requires_topics_entered: 5
tl1_requires_read_posts: tl1_requires_read_posts:
default: 30 default: 30

View File

@ -15,7 +15,8 @@ class Validators::PostValidator < ActiveModel::Validator
max_mention_validator(record) max_mention_validator(record)
max_images_validator(record) max_images_validator(record)
max_attachments_validator(record) max_attachments_validator(record)
max_links_validator(record) can_post_links_validator(record)
newuser_links_validator(record)
unique_post_validator(record) unique_post_validator(record)
end end
@ -91,8 +92,14 @@ class Validators::PostValidator < ActiveModel::Validator
add_error_if_count_exceeded(post, :no_attachments_allowed, :too_many_attachments, post.attachment_count, SiteSetting.newuser_max_attachments) add_error_if_count_exceeded(post, :no_attachments_allowed, :too_many_attachments, post.attachment_count, SiteSetting.newuser_max_attachments)
end end
def can_post_links_validator(post)
return if acting_user_is_trusted?(post, SiteSetting.min_trust_to_post_links) || private_message?(post)
post.errors.add(:base, I18n.t(:links_require_trust))
end
# Ensure new users can not put too many links in a post # Ensure new users can not put too many links in a post
def max_links_validator(post) def newuser_links_validator(post)
return if acting_user_is_trusted?(post) || private_message?(post) return if acting_user_is_trusted?(post) || private_message?(post)
add_error_if_count_exceeded(post, :no_links_allowed, :too_many_links, post.link_count, SiteSetting.newuser_max_links) add_error_if_count_exceeded(post, :no_links_allowed, :too_many_links, post.link_count, SiteSetting.newuser_max_links)
end end
@ -113,8 +120,8 @@ class Validators::PostValidator < ActiveModel::Validator
private private
def acting_user_is_trusted?(post) def acting_user_is_trusted?(post, level = 1)
post.acting_user.present? && post.acting_user.has_trust_level?(TrustLevel[1]) post.acting_user.present? && post.acting_user.has_trust_level?(TrustLevel[level])
end end
def private_message?(post) def private_message?(post)

View File

@ -184,7 +184,7 @@ describe Validators::PostValidator do
validator.expects(:max_mention_validator).never validator.expects(:max_mention_validator).never
validator.expects(:max_images_validator).never validator.expects(:max_images_validator).never
validator.expects(:max_attachments_validator).never validator.expects(:max_attachments_validator).never
validator.expects(:max_links_validator).never validator.expects(:newuser_links_validator).never
validator.expects(:unique_post_validator).never validator.expects(:unique_post_validator).never
validator.validate(post) validator.validate(post)
end end

View File

@ -352,7 +352,7 @@ describe Post do
end end
describe "maximum links" do describe "maximums" do
let(:newuser) { Fabricate(:user, trust_level: TrustLevel[0]) } let(:newuser) { Fabricate(:user, trust_level: TrustLevel[0]) }
let(:post_one_link) { post_with_body("[sherlock](http://www.bbc.co.uk/programmes/b018ttws)", newuser) } let(:post_one_link) { post_with_body("[sherlock](http://www.bbc.co.uk/programmes/b018ttws)", newuser) }
let(:post_two_links) { post_with_body("<a href='http://discourse.org'>discourse</a> <a href='http://twitter.com'>twitter</a>", newuser) } let(:post_two_links) { post_with_body("<a href='http://discourse.org'>discourse</a> <a href='http://twitter.com'>twitter</a>", newuser) }
@ -391,11 +391,17 @@ describe Post do
end end
end end
it "allows multiple images for basic accounts" do it "allows multiple links for basic accounts" do
post_two_links.user.trust_level = TrustLevel[1] post_two_links.user.trust_level = TrustLevel[1]
expect(post_two_links).to be_valid expect(post_two_links).to be_valid
end end
it "doesn't allow allow links if `min_trust_to_post_links` is not met" do
SiteSetting.min_trust_to_post_links = 2
post_two_links.user.trust_level = TrustLevel[1]
expect(post_one_link).not_to be_valid
end
end end
end end