FIX: Group can't be deleted if certain users are demoted.

This commit is contained in:
Guo Xiang Tan 2018-04-10 14:19:08 +08:00
parent 9df4f7bf8e
commit 874003b7b1
3 changed files with 76 additions and 50 deletions

View File

@ -82,15 +82,8 @@ class GroupUser < ActiveRecord::Base
.includes(:group) .includes(:group)
.maximum("groups.grant_trust_level") .maximum("groups.grant_trust_level")
if highest_level.nil?
# If the user no longer has a group with a trust level,
# unlock them, start at 0 and consider promotions.
user.update!(group_locked_trust_level: nil)
Promotion.recalculate(user)
else
user.update!(group_locked_trust_level: highest_level) user.update!(group_locked_trust_level: highest_level)
user.change_trust_level!(highest_level) Promotion.recalculate(user)
end
end end
end end

View File

@ -111,16 +111,16 @@ class Promotion
def self.recalculate(user, performed_by = nil) def self.recalculate(user, performed_by = nil)
# First, use the manual locked level # First, use the manual locked level
unless user.manual_locked_trust_level.nil? unless user.manual_locked_trust_level.nil?
user.trust_level = user.manual_locked_trust_level return user.update!(
user.save trust_level: user.manual_locked_trust_level
return )
end end
# Then consider the group locked level # Then consider the group locked level
if user.group_locked_trust_level if user.group_locked_trust_level
user.trust_level = user.group_locked_trust_level return user.update!(
user.save trust_level: user.group_locked_trust_level
return )
end end
user.update_column(:trust_level, TrustLevel[0]) user.update_column(:trust_level, TrustLevel[0])

View File

@ -541,6 +541,7 @@ describe Group do
end end
end end
describe 'trust level management' do
it "correctly grants a trust level to members" do it "correctly grants a trust level to members" do
group = Fabricate(:group, grant_trust_level: 2) group = Fabricate(:group, grant_trust_level: 2)
u0 = Fabricate(:user, trust_level: 0) u0 = Fabricate(:user, trust_level: 0)
@ -553,6 +554,37 @@ describe Group do
expect(u3.reload.trust_level).to eq(3) expect(u3.reload.trust_level).to eq(3)
end end
describe 'when a user has qualified for trust level 1' do
let(:user) do
Fabricate(:user,
trust_level: 1,
created_at: Time.zone.now - 10.years
)
end
let(:group) { Fabricate(:group, grant_trust_level: 1) }
let(:group2) { Fabricate(:group, grant_trust_level: 0) }
before do
user.user_stat.update!(
topics_entered: 999,
posts_read_count: 999,
time_read: 999
)
end
it "should not demote the user" do
group.add(user)
group2.add(user)
expect(user.reload.trust_level).to eq(1)
group.remove(user)
expect(user.reload.trust_level).to eq(0)
end
end
it "adjusts the user trust level" do it "adjusts the user trust level" do
g0 = Fabricate(:group, grant_trust_level: 2) g0 = Fabricate(:group, grant_trust_level: 2)
g1 = Fabricate(:group, grant_trust_level: 3) g1 = Fabricate(:group, grant_trust_level: 3)
@ -586,6 +618,7 @@ describe Group do
expect(user.group_locked_trust_level).to be_nil expect(user.group_locked_trust_level).to be_nil
expect(user.trust_level).to eq(0) expect(user.trust_level).to eq(0)
end end
end
it 'should cook the bio' do it 'should cook the bio' do
group = Fabricate(:group) group = Fabricate(:group)