New users can only post newuser_max_replies_per_topic times per topic.

This commit is contained in:
Robin Ward
2013-12-19 13:45:55 -05:00
parent 39e711783d
commit 1cac9fa257
8 changed files with 68 additions and 1 deletions

View File

@ -18,8 +18,12 @@
@include box-shadow(3px 3px 3px rgba($black, 0.14)); @include box-shadow(3px 3px 3px rgba($black, 0.14));
h3 {
margin-bottom: 10px;
}
p { p {
margin: 0 0 10px 0; margin-bottom: 10px;
} }
a.close { a.close {

View File

@ -143,6 +143,7 @@ class User < ActiveRecord::Base
where(username_lower: username.downcase).first where(username_lower: username.downcase).first
end end
def enqueue_welcome_message(message_type) def enqueue_welcome_message(message_type)
return unless SiteSetting.send_welcome_message? return unless SiteSetting.send_welcome_message?
Jobs.enqueue(:send_system_message, user_id: id, message_type: message_type) Jobs.enqueue(:send_system_message, user_id: id, message_type: message_type)
@ -340,6 +341,10 @@ class User < ActiveRecord::Base
topics_allowed.where(archetype: Archetype.private_message).count topics_allowed.where(archetype: Archetype.private_message).count
end end
def posted_too_much_in_topic?(topic_id)
trust_level == TrustLevel.levels[:newuser] && (Post.where(topic_id: topic_id, user_id: id).count >= SiteSetting.newuser_max_replies_per_topic)
end
def bio_excerpt def bio_excerpt
excerpt = PrettyText.excerpt(bio_cooked, 350) excerpt = PrettyText.excerpt(bio_cooked, 350)
return excerpt if excerpt.blank? || has_trust_level?(:basic) return excerpt if excerpt.blank? || has_trust_level?(:basic)

View File

@ -27,6 +27,8 @@ en:
site_under_maintenance: 'Site is currently undergoing maintenance.' site_under_maintenance: 'Site is currently undergoing maintenance.'
operation_already_running: "An %{operation} is currently running. Can't start a new %{operation} job right now." operation_already_running: "An %{operation} is currently running. Can't start a new %{operation} job right now."
too_many_replies: "Sorry you can't reply any more times in that topic."
too_many_mentions: too_many_mentions:
zero: "Sorry, you can't mention other users." zero: "Sorry, you can't mention other users."
one: "Sorry, you can only mention one other user in a post." one: "Sorry, you can only mention one other user in a post."
@ -132,6 +134,13 @@ en:
Are you sure you're providing adequate time for other people to share their points of view, too? Are you sure you're providing adequate time for other people to share their points of view, too?
too_many_replies: |
### You've replied too many times to this topic
Sorry, but new users are limited to %{newuser_max_replies_per_topic} replies in a topic.
You should consider editing one of your previous replies instead of a new reply.
activerecord: activerecord:
attributes: attributes:
category: category:
@ -667,6 +676,7 @@ en:
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"
newuser_max_attachments: "How many attachments a new user can add to a post" newuser_max_attachments: "How many attachments a new user can add to a post"
newuser_max_mentions_per_post: "Maximum number of @name notifications a new user can use in a post" newuser_max_mentions_per_post: "Maximum number of @name notifications a new user can use in a post"
newuser_max_replies_per_topic: "Maximum number of replies a new user can make in a single topic"
max_mentions_per_post: "Maximum number of @name notifications you can use in a post" max_mentions_per_post: "Maximum number of @name notifications you can use in a post"
create_thumbnails: "Create thumbnails for lightboxed images" create_thumbnails: "Create thumbnails for lightboxed images"

View File

@ -177,6 +177,7 @@ posting:
default: true default: true
post_undo_action_window_mins: 10 post_undo_action_window_mins: 10
max_mentions_per_post: 10 max_mentions_per_post: 10
newuser_max_replies_per_topic: 3
newuser_max_mentions_per_post: 2 newuser_max_mentions_per_post: 2
onebox_max_chars: 5000 onebox_max_chars: 5000
title_min_entropy: 10 title_min_entropy: 10

View File

@ -7,6 +7,7 @@ class ComposerMessagesFinder
def find def find
check_education_message || check_education_message ||
check_new_user_many_replies ||
check_avatar_notification || check_avatar_notification ||
check_sequential_replies || check_sequential_replies ||
check_dominating_topic check_dominating_topic
@ -32,6 +33,12 @@ class ComposerMessagesFinder
nil nil
end end
# New users have a limited number of replies in a topic
def check_new_user_many_replies
return unless replying? && @user.posted_too_much_in_topic?(@details[:topic_id])
{templateName: 'composer/education', body: PrettyText.cook(I18n.t('education.too_many_replies', newuser_max_replies_per_topic: SiteSetting.newuser_max_replies_per_topic)) }
end
# Should a user be contacted to update their avatar? # Should a user be contacted to update their avatar?
def check_avatar_notification def check_avatar_notification

View File

@ -5,6 +5,7 @@ class Validators::PostValidator < ActiveModel::Validator
presence(record) presence(record)
stripped_length(record) stripped_length(record)
raw_quality(record) raw_quality(record)
max_posts_validator(record)
max_mention_validator(record) max_mention_validator(record)
max_images_validator(record) max_images_validator(record)
max_attachments_validator(record) max_attachments_validator(record)
@ -40,6 +41,12 @@ class Validators::PostValidator < ActiveModel::Validator
end end
end end
def max_posts_validator(post)
if post.acting_user.present? && post.acting_user.posted_too_much_in_topic?(post.topic_id)
post.errors.add(:base, I18n.t(:too_many_replies))
end
end
# Ensure new users can not put too many images in a post # Ensure new users can not put too many images in a post
def max_images_validator(post) def max_images_validator(post)
add_error_if_count_exceeded(post, :too_many_images, post.image_count, SiteSetting.newuser_max_images) unless acting_user_is_trusted?(post) add_error_if_count_exceeded(post, :too_many_images, post.image_count, SiteSetting.newuser_max_images) unless acting_user_is_trusted?(post)

View File

@ -10,6 +10,7 @@ describe ComposerMessagesFinder do
it "calls all the message finders" do it "calls all the message finders" do
finder.expects(:check_education_message).once finder.expects(:check_education_message).once
finder.expects(:check_new_user_many_replies).once
finder.expects(:check_avatar_notification).once finder.expects(:check_avatar_notification).once
finder.expects(:check_sequential_replies).once finder.expects(:check_sequential_replies).once
finder.expects(:check_dominating_topic).once finder.expects(:check_dominating_topic).once
@ -56,6 +57,24 @@ describe ComposerMessagesFinder do
finder.check_education_message.should be_blank finder.check_education_message.should be_blank
end end
end end
end
context '.check_new_user_many_replies' do
let(:user) { Fabricate.build(:user) }
context 'replying' do
let(:finder) { ComposerMessagesFinder.new(user, composerAction: 'reply') }
it "has no message when `posted_too_much_in_topic?` is false" do
user.expects(:posted_too_much_in_topic?).returns(false)
finder.check_new_user_many_replies.should be_blank
end
it "has a message when a user has posted too much" do
user.expects(:posted_too_much_in_topic?).returns(true)
finder.check_new_user_many_replies.should be_present
end
end
end end

View File

@ -24,6 +24,20 @@ describe Validators::PostValidator do
end end
end end
context "too_many_posts" do
it "should be invalid when the user has posted too much" do
post.user.expects(:posted_too_much_in_topic?).returns(true)
validator.max_posts_validator(post)
expect(post.errors.count).to be > 0
end
it "should be valid when the user hasn't posted too much" do
post.user.expects(:posted_too_much_in_topic?).returns(false)
validator.max_posts_validator(post)
expect(post.errors.count).to be(0)
end
end
context "invalid post" do context "invalid post" do
it "should be invalid" do it "should be invalid" do
validator.validate(post) validator.validate(post)