From 359d59242ea178c90e6076b667b303d5de757111 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 29 Apr 2014 12:59:14 -0400 Subject: [PATCH] If a new user receives a mention, quote or response to their post, allow them to continue posting in a topic. --- app/models/user.rb | 12 +++++++++--- app/models/user_action.rb | 5 +++++ spec/models/user_spec.rb | 10 ++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 5ee6a10c586..b2e3361de0b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -386,10 +386,16 @@ class User < ActiveRecord::Base def posted_too_much_in_topic?(topic_id) - # Does not apply to staff or your own topics - return false if staff? || Topic.where(id: topic_id, user_id: id).exists? + # Does not apply to staff, non-new members or your own topics + return false if staff? || + (trust_level != TrustLevel.levels[:newuser]) || + Topic.where(id: topic_id, user_id: id).exists? - trust_level == TrustLevel.levels[:newuser] && (Post.where(topic_id: topic_id, user_id: id).count >= SiteSetting.newuser_max_replies_per_topic) + last_action_in_topic = UserAction.last_action_in_topic(id, topic_id) + since_reply = Post.where(user_id: id, topic_id: topic_id) + since_reply = since_reply.where('id > ?', last_action_in_topic) if last_action_in_topic + + (since_reply.count >= SiteSetting.newuser_max_replies_per_topic) end def bio_excerpt diff --git a/app/models/user_action.rb b/app/models/user_action.rb index 03841c91ef8..fc66f18800f 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -41,6 +41,11 @@ class UserAction < ActiveRecord::Base include ActiveModel::SerializerSupport end + def self.last_action_in_topic(user_id, topic_id) + UserAction.where(user_id: user_id, + target_topic_id: topic_id, + action_type: [RESPONSE, MENTION, QUOTE]).order('created_at DESC').pluck(:target_post_id).first + end def self.stats(user_id, guardian) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index ae13a2523eb..0e340837698 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -935,6 +935,16 @@ describe User do it "returns true when the user has posted too much" do user.posted_too_much_in_topic?(topic.id).should be_true end + + context "with a reply" do + before do + PostCreator.new(Fabricate(:user), raw: 'whatever this is a raw post', topic_id: topic.id, reply_to_post_number: post.post_number).create + end + + it "resets the `posted_too_much` threshold" do + user.posted_too_much_in_topic?(topic.id).should be_false + end + end end it "returns false for a user who created the topic" do