New setting: minimum trust level to embed images in a post

This commit is contained in:
Robin Ward 2018-02-20 20:00:06 -05:00
parent 80ec6ce4fd
commit 3ea272f4f1
4 changed files with 40 additions and 2 deletions

View File

@ -207,6 +207,7 @@ en:
too_many_mentions_newuser:
one: "Sorry, new users can only mention one other user in a post."
other: "Sorry, new users can only mention %{count} users in a post."
no_images_allowed_trust: "Sorry, you can't put images in a post"
no_images_allowed: "Sorry, new users can't put images in posts."
too_many_images:
one: "Sorry, new users can only put one image in a post."
@ -1303,6 +1304,7 @@ en:
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"
min_trust_to_post_images: "The minimum trust level required to include images in 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."

View File

@ -911,6 +911,9 @@ trust:
min_trust_to_post_links:
default: 0
enum: 'TrustLevelSetting'
min_trust_to_post_images:
default: 0
enum: 'TrustLevelSetting'
allow_flagging_staff: true
tl1_requires_topics_entered: 5
tl1_requires_read_posts:

View File

@ -82,8 +82,25 @@ class Validators::PostValidator < ActiveModel::Validator
# Ensure new users can not put too many images in a post
def max_images_validator(post)
return if acting_user_is_trusted?(post) || private_message?(post)
add_error_if_count_exceeded(post, :no_images_allowed, :too_many_images, post.image_count, SiteSetting.newuser_max_images)
return if post.acting_user.blank?
if post.acting_user.trust_level < TrustLevel[SiteSetting.min_trust_to_post_images]
add_error_if_count_exceeded(
post,
:no_images_allowed_trust,
:no_images_allowed_trust,
post.image_count,
0
)
elsif post.acting_user.trust_level == TrustLevel[0]
add_error_if_count_exceeded(
post,
:no_images_allowed,
:too_many_images,
post.image_count,
SiteSetting.newuser_max_images
)
end
end
# Ensure new users can not put too many attachments in a post

View File

@ -188,6 +188,22 @@ describe Post do
expect(post_with_avatars.image_count).to eq(0)
end
it "allows images by default" do
expect(post_one_image).to be_valid
end
it "doesn't allow more than `min_trust_to_post_images`" do
SiteSetting.min_trust_to_post_images = 4
post_one_image.user.trust_level = 3
expect(post_one_image).not_to be_valid
end
it "doesn't allow more than `min_trust_to_post_images`" do
SiteSetting.min_trust_to_post_images = 4
post_one_image.user.trust_level = 4
expect(post_one_image).to be_valid
end
it "doesn't count favicons as images" do
PrettyText.stubs(:cook).returns(post_with_favicon.raw)
expect(post_with_favicon.image_count).to eq(0)