FIX: only ONE user on site could have a list of muted users

This commit is contained in:
Sam 2015-03-31 10:16:11 +11:00
parent 4e3bea67e4
commit 14741b5dac
2 changed files with 30 additions and 5 deletions

View File

@ -75,17 +75,13 @@ class UserUpdater
end end
end end
private
attr_reader :user, :guardian
def update_muted_users(usernames) def update_muted_users(usernames)
usernames ||= "" usernames ||= ""
desired_ids = User.where(username: usernames.split(",")).pluck(:id) desired_ids = User.where(username: usernames.split(",")).pluck(:id)
if desired_ids.empty? if desired_ids.empty?
MutedUser.where(user_id: user.id).destroy_all MutedUser.where(user_id: user.id).destroy_all
else else
MutedUser.where('id not in (?)', desired_ids).destroy_all MutedUser.where('user_id = ? AND muted_user_id not in (?)', user.id, desired_ids).destroy_all
# SQL is easier here than figuring out how to do the same in AR # SQL is easier here than figuring out how to do the same in AR
MutedUser.exec_sql("INSERT into muted_users(user_id, muted_user_id, created_at, updated_at) MutedUser.exec_sql("INSERT into muted_users(user_id, muted_user_id, created_at, updated_at)
@ -102,6 +98,10 @@ class UserUpdater
end end
end end
private
attr_reader :user, :guardian
def format_url(website) def format_url(website)
if website =~ /^http/ if website =~ /^http/
website website

View File

@ -4,6 +4,31 @@ describe UserUpdater do
let(:acting_user) { Fabricate.build(:user) } let(:acting_user) { Fabricate.build(:user) }
describe '#update_muted_users' do
it 'has no cross talk' do
u1 = Fabricate(:user)
u2 = Fabricate(:user)
u3 = Fabricate(:user)
updater = UserUpdater.new(u1, u1)
updater.update_muted_users("#{u2.username},#{u3.username}")
updater = UserUpdater.new(u2, u2)
updater.update_muted_users("#{u3.username},#{u1.username}")
updater = UserUpdater.new(u3, u3)
updater.update_muted_users("")
expect(MutedUser.where(user_id: u2.id).count).to eq 2
expect(MutedUser.where(user_id: u1.id).count).to eq 2
expect(MutedUser.where(user_id: u3.id).count).to eq 0
end
end
describe '#update' do describe '#update' do
it 'saves user' do it 'saves user' do
user = Fabricate(:user, name: 'Billy Bob') user = Fabricate(:user, name: 'Billy Bob')