From 1abc27645144d8cf8380331c9a7cdec0430a471e Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 19 Dec 2017 12:53:59 +1100 Subject: [PATCH] FIX: properly omit inactive and silenced users from directory --- app/models/directory_item.rb | 4 ++-- spec/models/directory_item_spec.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/models/directory_item.rb b/app/models/directory_item.rb index 91672f7d21a..a31453bc022 100644 --- a/app/models/directory_item.rb +++ b/app/models/directory_item.rb @@ -44,7 +44,7 @@ class DirectoryItem < ActiveRecord::Base # Delete records that belonged to users who have been deleted exec_sql "DELETE FROM directory_items USING directory_items di - LEFT JOIN users u ON u.id = user_id + LEFT JOIN users u ON (u.id = user_id AND u.active AND u.silenced_till IS NULL AND u.id > 0) WHERE di.id = directory_items.id AND u.id IS NULL AND di.period_type = :period_type", period_type: period_types[period_type] @@ -63,7 +63,7 @@ class DirectoryItem < ActiveRecord::Base 0 FROM users u LEFT JOIN directory_items di ON di.user_id = u.id AND di.period_type = :period_type - WHERE di.id IS NULL AND u.id > 0 + WHERE di.id IS NULL AND u.id > 0 AND u.silenced_till IS NULL and u.active ", period_type: period_types[period_type] # Calculate new values and update records diff --git a/spec/models/directory_item_spec.rb b/spec/models/directory_item_spec.rb index 78f2245671f..4f9b8d5cfb6 100644 --- a/spec/models/directory_item_spec.rb +++ b/spec/models/directory_item_spec.rb @@ -18,6 +18,35 @@ describe DirectoryItem do end end + context 'inactive and silenced users' do + it 'removes silenced users correctly' do + post = create_post + DirectoryItem.refresh_period!(:daily) + + count = DirectoryItem.where(user_id: post.user_id).count + expect(count).to eq(1) + + post.user.update_columns(active: false) + DirectoryItem.refresh_period!(:daily) + + count = DirectoryItem.where(user_id: post.user_id).count + expect(count).to eq(0) + + post.user.update_columns(active: true) + DirectoryItem.refresh_period!(:daily) + + count = DirectoryItem.where(user_id: post.user_id).count + expect(count).to eq(1) + + post.user.update_columns(silenced_till: 1.year.from_now) + DirectoryItem.refresh_period!(:daily) + + count = DirectoryItem.where(user_id: post.user_id).count + expect(count).to eq(0) + + end + end + context 'refresh' do before do UserActionCreator.enable