diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 5c62edd7da2..089dc0d5fd5 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -2,6 +2,8 @@ require_dependency 'rate_limiter' require_dependency 'system_message' class PostAction < ActiveRecord::Base + class AlreadyFlagged < StandardError; end + include RateLimiter::OnCreateRecord attr_accessible :deleted_at, :post_action_type_id, :post_id, :user_id, :post, :user, :post_action_type, :message @@ -115,6 +117,15 @@ class PostAction < ActiveRecord::Base end end + before_create do + if is_flag? + if PostAction.where('user_id = ? and post_id = ? and post_action_type_id in (?) and deleted_at is null', + self.user_id, self.post_id, PostActionType.FlagTypes).exists? + raise AlreadyFlagged + end + end + end + after_save do # Update denormalized counts diff --git a/app/models/post_action_type.rb b/app/models/post_action_type.rb index 39b58a2a9f6..a347c4fba32 100644 --- a/app/models/post_action_type.rb +++ b/app/models/post_action_type.rb @@ -1,4 +1,5 @@ class PostActionType < ActiveRecord::Base + attr_accessible :id, :is_flag, :name_key, :icon def self.ordered diff --git a/config/application.rb b/config/application.rb index 0f3b40e2edf..85723bf2180 100644 --- a/config/application.rb +++ b/config/application.rb @@ -93,10 +93,6 @@ module Discourse # So open id logs somewhere sane config.after_initialize do OpenID::Util.logger = Rails.logger - - # latest possible so earliest in the stack - # require 'rack/message_bus' - # config.middleware.insert(0, Rack::MessageBus) end end end diff --git a/db/fixtures/post_action_types.rb b/db/fixtures/post_action_types.rb index 863fc8c3c51..820b05f76f6 100644 --- a/db/fixtures/post_action_types.rb +++ b/db/fixtures/post_action_types.rb @@ -27,6 +27,13 @@ PostActionType.seed do |s| s.position = 4 end +PostActionType.seed do |s| + s.id = PostActionType.Types[:vote] + s.name_key = 'vote' + s.is_flag = false + s.position = 5 +end + PostActionType.seed do |s| s.id = PostActionType.Types[:spam] s.name_key = 'spam' diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 10c63990000..6eb78277288 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -111,6 +111,13 @@ describe PostAction do describe 'flagging' do + it 'does not allow you to flag stuff with 2 reasons' do + post = Fabricate(:post) + u1 = Fabricate(:evil_trout) + PostAction.act(u1, post, PostActionType.Types[:spam]) + lambda { PostAction.act(u1, post, PostActionType.Types[:off_topic]) }.should raise_error(PostAction::AlreadyFlagged) + end + it 'should update counts when you clear flags' do post = Fabricate(:post) u1 = Fabricate(:evil_trout)