Don't allow users to double flag stuff

Correct seed fu to match model
This commit is contained in:
Sam Saffron 2013-02-07 10:45:58 +11:00
parent 204a8a8b52
commit f79f0e740a
5 changed files with 26 additions and 4 deletions

View File

@ -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

View File

@ -1,4 +1,5 @@
class PostActionType < ActiveRecord::Base
attr_accessible :id, :is_flag, :name_key, :icon
def self.ordered

View File

@ -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

View File

@ -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'

View File

@ -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)