pull moderator into own column, rename trust levels

This commit is contained in:
Sam 2013-03-19 21:05:19 -07:00
parent 0f5046a9cc
commit 62c60540be
21 changed files with 75 additions and 62 deletions

View File

@ -67,7 +67,7 @@ class Admin::UsersController < Admin::AdminController
def revoke_moderation
@moderator = User.where(id: params[:user_id]).first
guardian.ensure_can_revoke_moderation!(@moderator)
@moderator.change_trust_level(:advanced)
@moderator.moderator = false
@moderator.save
render nothing: true
end
@ -75,7 +75,7 @@ class Admin::UsersController < Admin::AdminController
def grant_moderation
@user = User.where(id: params[:user_id]).first
guardian.ensure_can_grant_moderation!(@user)
@user.change_trust_level(:moderator)
@user.moderator = true
@user.save
render_serialized(@user, AdminUserSerializer)
end

View File

@ -71,8 +71,8 @@ class PostActionsController < ApplicationController
requires_parameter(:id)
finder = Post.where(id: params[:id])
# Include deleted posts if the user is a moderator
finder = finder.with_deleted if current_user.try(:has_trust_level?, :moderator)
# Include deleted posts if the user is a moderator (to guardian ?)
finder = finder.with_deleted if current_user.moderator?
@post = finder.first
guardian.ensure_can_see!(@post)

View File

@ -153,7 +153,7 @@ class PostsController < ApplicationController
finder = Post.where(id: params[:id] || params[:post_id])
# Include deleted posts if the user is a moderator
finder = finder.with_deleted if current_user.try(:has_trust_level?, :moderator)
finder = finder.with_deleted if current_user.try(:moderator?)
post = finder.first
guardian.ensure_can_see!(post)

View File

@ -237,7 +237,10 @@ class User < ActiveRecord::Base
end
def moderator?
has_trust_level?(:moderator)
# this saves us from checking both, admins are always moderators
#
# in future we may split this out
admin || moderator
end
def regular?
@ -399,7 +402,7 @@ class User < ActiveRecord::Base
# Takes into account admin, etc.
def has_trust_level?(level)
raise "Invalid trust level #{level}" unless TrustLevel.valid_level?(level)
admin? || TrustLevel.compare(trust_level, level)
admin? || moderator? || TrustLevel.compare(trust_level, level)
end
# a touch faster than automatic

View File

@ -36,7 +36,7 @@ class AdminDetailedUserSerializer < AdminUserSerializer
end
def moderator
object.has_trust_level?(:moderator)
object.moderator
end
end

View File

@ -27,7 +27,7 @@ class CurrentUserSerializer < BasicUserSerializer
end
def moderator?
object.has_trust_level?(:moderator)
object.moderator?
end
def site_flagged_posts_count

View File

@ -53,7 +53,7 @@ class PostSerializer < ApplicationSerializer
end
def moderator?
object.user.has_trust_level?(:moderator)
object.user.moderator?
end
def avatar_template

View File

@ -80,18 +80,16 @@ en:
post_template: "%{replace_paragraph}\n\nUse this space below for a longer description, as well as to establish any rules or discussion!"
trust_levels:
new:
title: "new user"
visitor:
title: "visitor"
basic:
title: "basic user"
regular:
title: "regular user"
experienced:
title: "experienced user"
advanced:
title: "advanced user"
moderator:
title: "moderator"
leader:
title: "leader"
elder:
title: "elder"
rate_limiter:
too_many_requests: "You're doing that too often. Please wait %{time_left} before trying again."

View File

@ -0,0 +1,10 @@
class AddModeratorToUser < ActiveRecord::Migration
def up
add_column :users, :moderator, :boolean, default: false
execute "UPDATE users SET trust_level = 1, moderator = 't' where trust_level = 5"
end
def down
remove_column :users, :moderator
end
end

View File

@ -50,7 +50,7 @@ class Guardian
def can_moderate?(obj)
return false if obj.blank?
return false if @user.blank?
@user.has_trust_level?(:moderator)
@user.moderator?
end
alias :can_move_posts? :can_moderate?
alias :can_see_flags? :can_moderate?
@ -99,7 +99,7 @@ class Guardian
return false if target.blank?
return false if @user.blank?
return false if target.approved?
@user.has_trust_level?(:moderator)
@user.moderator?
end
def can_ban?(user)
@ -112,7 +112,7 @@ class Guardian
def can_clear_flags?(post)
return false if @user.blank?
return false if post.blank?
@user.has_trust_level?(:moderator)
@user.moderator?
end
def can_revoke_admin?(admin)
@ -135,7 +135,7 @@ class Guardian
return false unless @user.try(:admin?)
return false if moderator.blank?
return false if @user.id == moderator.id
return false unless moderator.trust_level == TrustLevel.levels[:moderator]
return false unless moderator.moderator?
true
end
@ -144,7 +144,7 @@ class Guardian
return false if user.blank?
return false if @user.id == user.id
return false if user.admin?
return false if user.has_trust_level?(:moderator)
return false if user.moderator?
true
end
@ -175,7 +175,7 @@ class Guardian
return false if @user.blank?
return false unless can_see?(object)
return false if SiteSetting.must_approve_users?
@user.has_trust_level?(:moderator)
@user.moderator?
end
@ -218,11 +218,11 @@ class Guardian
# Creating Methods
def can_create_category?(parent)
@user.has_trust_level?(:moderator)
@user.moderator?
end
def can_create_post_on_topic?(topic)
return true if @user.has_trust_level?(:moderator)
return true if @user.moderator?
return false if topic.closed?
return false if topic.archived?
true
@ -230,11 +230,11 @@ class Guardian
# Editing Methods
def can_edit_category?(category)
@user.has_trust_level?(:moderator)
@user.moderator?
end
def can_edit_post?(post)
return true if @user.has_trust_level?(:moderator)
return true if @user.moderator?
return false if post.topic.archived?
(post.user == @user)
end
@ -245,7 +245,7 @@ class Guardian
end
def can_edit_topic?(topic)
return true if @user.has_trust_level?(:moderator)
return true if @user.moderator?
return true if topic.user == @user
false
end
@ -258,22 +258,22 @@ class Guardian
# You can delete your own posts
return !post.user_deleted? if post.user == @user
@user.has_trust_level?(:moderator)
@user.moderator?
end
# Recovery Method
def can_recover_post?(post)
return false if @user.blank?
@user.has_trust_level?(:moderator)
@user.moderator?
end
def can_delete_category?(category)
return false unless @user.has_trust_level?(:moderator)
return false unless @user.moderator?
return category.topic_count == 0
end
def can_delete_topic?(topic)
return false unless @user.has_trust_level?(:moderator)
return false unless @user.moderator?
return false if Category.exists?(topic_id: topic.id)
true
end

View File

@ -21,7 +21,7 @@ class Promotion
false
end
def review_new
def review_visitor
return false if @user.topics_entered < SiteSetting.basic_requires_topics_entered
return false if @user.posts_read_count < SiteSetting.basic_requires_read_posts
return false if (@user.time_read / 60) < SiteSetting.basic_requires_time_spent_mins

View File

@ -22,7 +22,7 @@ class RateLimiter
def can_perform?
return true if RateLimiter.disabled?
return true if @user.has_trust_level?(:moderator)
return true if @user.moderator?
result = $redis.get(@key)
return true if result.blank?
@ -32,7 +32,7 @@ class RateLimiter
def performed!
return if RateLimiter.disabled?
return if @user.has_trust_level?(:moderator)
return if @user.moderator?
result = $redis.incr(@key).to_i
$redis.expire(@key, @secs) if result == 1

View File

@ -6,7 +6,7 @@ class TrustLevel
class << self
def levels
@levels ||= Enum.new(
:new, :basic, :regular, :experienced, :advanced, :moderator, start: 0
:visitor, :basic, :regular, :leader, :elder, start: 0
)
end
@ -21,7 +21,7 @@ class TrustLevel
end
def compare(current_level, level)
(current_level || levels[:new]) >= levels[level] rescue binding.pry
(current_level || levels[:visitor]) >= levels[level] rescue binding.pry
end
end

View File

@ -126,7 +126,7 @@ describe PostCreator do
end
it "returns a post for moderators" do
user.trust_level = TrustLevel.levels[:moderator]
user.moderator = true
new_post_creator.create
new_post_creator.errors.should be_blank
end

View File

@ -3,9 +3,9 @@ require 'promotion'
describe Promotion do
context "new user" do
context "visitor" do
let(:user) { Fabricate(:user, trust_level: TrustLevel.levels[:new])}
let(:user) { Fabricate(:user, trust_level: TrustLevel.levels[:visitor])}
let(:promotion) { Promotion.new(user) }
it "doesn't raise an error with a nil user" do
@ -20,7 +20,7 @@ describe Promotion do
end
it "has not changed the user's trust level" do
user.trust_level.should == TrustLevel.levels[:new]
user.trust_level.should == TrustLevel.levels[:visitor]
end
end

View File

@ -66,12 +66,12 @@ describe RateLimiter do
end
it "returns true for can_perform if the user is a mod" do
user.trust_level = TrustLevel.levels[:moderator]
user.moderator = true
rate_limiter.can_perform?.should be_true
end
it "doesn't raise an error when a moderator performs the task" do
user.trust_level = TrustLevel.levels[:moderator]
user.moderator = true
lambda { rate_limiter.performed! }.should_not raise_error
end

View File

@ -134,7 +134,7 @@ describe Admin::UsersController do
it 'updates the moderator flag' do
xhr :put, :revoke_moderation, user_id: @moderator.id
@moderator.reload
@moderator.has_trust_level?(:moderator).should_not be_true
@moderator.moderator.should_not be_true
end
end
@ -157,7 +157,7 @@ describe Admin::UsersController do
it 'updates the moderator flag' do
xhr :put, :grant_moderation, user_id: @another_user.id
@another_user.reload
@another_user.has_trust_level?(:moderator).should be_true
@another_user.moderator.should be_true
end
end

View File

@ -326,7 +326,7 @@ describe TopicsController do
end
it "reviews the user for a promotion if they're new" do
user.update_column(:trust_level, TrustLevel.levels[:new])
user.update_column(:trust_level, TrustLevel.levels[:visitor])
Promotion.any_instance.expects(:review)
get :show, id: topic.id
end

View File

@ -32,7 +32,7 @@ Fabricator(:moderator, from: :user) do
name 'A. Moderator'
username 'moderator'
email 'moderator@discourse.org'
trust_level TrustLevel.levels[:moderator]
moderator true
end
Fabricator(:admin, from: :user) do

View File

@ -161,8 +161,8 @@ describe Invite do
context 'invite trust levels' do
it "returns the trust level in default_invitee_trust_level" do
SiteSetting.stubs(:default_invitee_trust_level).returns(TrustLevel.levels[:experienced])
invite.redeem.trust_level.should == TrustLevel.levels[:experienced]
SiteSetting.stubs(:default_invitee_trust_level).returns(TrustLevel.levels[:leader])
invite.redeem.trust_level.should == TrustLevel.levels[:leader]
end
end

View File

@ -225,11 +225,13 @@ describe User do
end
describe "trust levels" do
let(:user) { Fabricate(:user, trust_level: TrustLevel.levels[:new]) }
# NOTE be sure to use build to avoid db calls
let(:user) { Fabricate.build(:user, trust_level: TrustLevel.levels[:visitor]) }
it "sets to the default trust level setting" do
SiteSetting.expects(:default_trust_level).returns(TrustLevel.levels[:advanced])
User.new.trust_level.should == TrustLevel.levels[:advanced]
SiteSetting.expects(:default_trust_level).returns(TrustLevel.levels[:elder])
User.new.trust_level.should == TrustLevel.levels[:elder]
end
describe 'has_trust_level?' do
@ -239,39 +241,39 @@ describe User do
end
it "is true for your basic level" do
user.has_trust_level?(:new).should be_true
user.has_trust_level?(:visitor).should be_true
end
it "is false for a higher level" do
user.has_trust_level?(:moderator).should be_false
user.has_trust_level?(:regular).should be_false
end
it "is true if you exceed the level" do
user.trust_level = TrustLevel.levels[:advanced]
user.has_trust_level?(:basic).should be_true
user.trust_level = TrustLevel.levels[:elder]
user.has_trust_level?(:visitor).should be_true
end
it "is true for an admin even with a low trust level" do
user.trust_level = TrustLevel.levels[:new]
user.admin = true
user.has_trust_level?(:advanced).should be_true
user.has_trust_level?(:elder).should be_true
end
end
describe 'moderator' do
it "isn't a moderator by default" do
user.has_trust_level?(:moderator).should be_false
user.moderator?.should be_false
end
it "is a moderator if the user level is moderator" do
user.trust_level = TrustLevel.levels[:moderator]
user.has_trust_level?(:moderator).should be_true
user.moderator = true
user.has_trust_level?(:elder).should be_true
end
it "is a moderator if the user is an admin" do
user.admin = true
user.has_trust_level?(:moderator).should be_true
user.moderator?.should be_true
end
end