Fix and refactor hide user content (#12307)

This commit is contained in:
Ben Halpern 2021-01-18 11:08:23 -05:00 committed by GitHub
parent 0cac61024f
commit 472c3d2922
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 52 additions and 12 deletions

View file

@ -55,7 +55,7 @@ class AsyncInfoController < ApplicationController
methods: [:points]),
followed_podcast_ids: @user.cached_following_podcasts_ids,
reading_list_ids: @user.cached_reading_list_article_ids,
blocked_user_ids: @user.all_blocking.pluck(:blocked_id),
blocked_user_ids: UserBlock.cached_blocked_ids_for_blocker(@user.id),
saw_onboarding: @user.saw_onboarding,
checked_code_of_conduct: @user.checked_code_of_conduct,
checked_terms_and_conditions: @user.checked_terms_and_conditions,

View file

@ -168,13 +168,11 @@ class StoriesController < ApplicationController
def handle_base_index
@home_page = true
assign_feed_stories
assign_feed_stories unless user_signed_in? # Feed fetched async for signed-in users
assign_hero_html
assign_podcasts
get_latest_campaign_articles if Campaign.current.show_in_sidebar?
@article_index = true
@featured_story = (featured_story || Article.new)&.decorate
@stories = ArticleDecorator.decorate_collection(@stories)
set_surrogate_key_header "main_app_home_page"
set_cache_control_headers(600,
stale_while_revalidate: 30,
@ -279,6 +277,8 @@ class StoriesController < ApplicationController
@default_home_feed = true
@featured_story, @stories = feed.default_home_feed_and_featured_story(user_signed_in: user_signed_in?)
end
@featured_story = (featured_story || Article.new)&.decorate
@stories = ArticleDecorator.decorate_collection(@stories)
end
def assign_article_show_variables

View file

@ -13,7 +13,7 @@ export default function hideBlockedContent() {
});
divsToHide.forEach((div) => {
if (div.className.includes('single-article')) {
if (div.className.includes('crayons-story')) {
div.style.display = 'none';
} else if (div.className.includes('single-comment-node')) {
const divInnerComment = div.getElementsByClassName('inner-comment')[0];

View file

@ -435,10 +435,6 @@ class User < ApplicationRecord
def block; end
def all_blocking
UserBlock.where(blocker_id: id)
end
def all_blocked_by
UserBlock.where(blocked_id: id)
end

View file

@ -10,10 +10,21 @@ class UserBlock < ApplicationRecord
counter_culture :blocker, column_name: "blocking_others_count"
counter_culture :blocked, column_name: "blocked_by_count"
after_create :bust_blocker_cache
before_destroy :bust_blocker_cache
BLOCKED_IDS_CACHE_KEY = "blocked_ids_for_blocker/".freeze
class << self
def blocking?(blocker_id, blocked_id)
exists?(blocker_id: blocker_id, blocked_id: blocked_id)
end
def cached_blocked_ids_for_blocker(blocker_id)
Rails.cache.fetch("#{BLOCKED_IDS_CACHE_KEY}#{blocker_id}", expires_in: 48.hours) do
where(blocker_id: blocker_id).pluck(:blocked_id)
end
end
end
private
@ -21,4 +32,8 @@ class UserBlock < ApplicationRecord
def blocker_cannot_be_same_as_blocked
errors.add(:blocker_id, "can't be the same as the blocked_id") if blocker_id == blocked_id
end
def bust_blocker_cache
Rails.cache.delete("#{BLOCKED_IDS_CACHE_KEY}#{blocker_id}")
end
end

View file

@ -16,6 +16,7 @@ module Articles
.limited_column_select.includes(top_comments: :user)
return articles unless @user
articles = articles.where.not(user_id: UserBlock.cached_blocked_ids_for_blocker(@user.id))
articles.sort_by.with_index do |article, index|
article_tags = article.decorate.cached_tag_list_array
tag_score = user_followed_tags.sum do |tag|

View file

@ -128,6 +128,7 @@ module Articles
def globally_hot_articles(user_signed_in)
if user_signed_in
hot_stories = experimental_hot_story_grab
hot_stories = hot_stories.where.not(user_id: UserBlock.cached_blocked_ids_for_blocker(@user.id))
featured_story = hot_stories.where.not(main_image: nil).first
new_stories = Article.published
.where("score > ?", -15)

View file

@ -1,7 +1,8 @@
require "rails_helper"
RSpec.describe UserBlock, type: :model do
let(:blocker) { build(:user) }
let(:blocker) { create(:user) }
let(:blocked_user) { create(:user) }
describe "validations" do
it { is_expected.to validate_inclusion_of(:config).in_array(%w[default]) }
@ -11,5 +12,18 @@ RSpec.describe UserBlock, type: :model do
expect(user_block).not_to be_valid
expect(user_block.errors.full_messages).to include("Blocker can't be the same as the blocked_id")
end
it "returns ids blocked by user" do
create(:user_block, blocker: blocker, blocked: blocked_user, config: "default")
expect(described_class.cached_blocked_ids_for_blocker(blocker)).to eq([blocked_user.id])
end
it "busts user block cache" do
allow(Rails.cache).to receive(:delete).and_call_original
block = create(:user_block, blocker: blocker, blocked: blocked_user, config: "default")
expect(Rails.cache).to have_received(:delete).with("blocked_ids_for_blocker/#{blocker.id}").once
block.destroy
expect(Rails.cache).to have_received(:delete).with("blocked_ids_for_blocker/#{blocker.id}").twice
end
end
end

View file

@ -2,9 +2,10 @@ require "rails_helper"
RSpec.describe Articles::Feeds::Basic, type: :service do
let(:user) { create(:user) }
let(:second_user) { create(:user) }
let(:unique_tag_name) { "foo" }
let!(:article) { create(:article, hotness_score: 10) }
let!(:hot_story) { create(:article, hotness_score: 1000, score: 1000, published_at: 3.hours.ago) }
let!(:hot_story) { create(:article, hotness_score: 1000, score: 1000, published_at: 3.hours.ago, user_id: second_user.id) }
let!(:old_story) { create(:article, hotness_score: 500, published_at: 3.days.ago, tags: unique_tag_name) }
let!(:low_scoring_article) { create(:article, score: -1000) }
let!(:month_old_story) { create(:article, published_at: 1.month.ago) } # rubocop:disable RSpec/LetSetup
@ -36,5 +37,11 @@ RSpec.describe Articles::Feeds::Basic, type: :service do
expect(result.third).to eq article
expect(result).not_to include(low_scoring_article)
end
it "does not load blocked articles" do
create(:user_block, blocker: user, blocked: second_user, config: "default")
result = feed.feed
expect(result).not_to include(hot_story)
end
end
end

View file

@ -2,9 +2,10 @@ require "rails_helper"
RSpec.describe Articles::Feeds::LargeForemExperimental, type: :service do
let(:user) { create(:user) }
let(:second_user) { create(:user)}
let!(:feed) { described_class.new(user: user, number_of_articles: 100, page: 1) }
let!(:article) { create(:article) }
let!(:hot_story) { create(:article, hotness_score: 1000, score: 1000, published_at: 3.hours.ago) }
let!(:hot_story) { create(:article, hotness_score: 1000, score: 1000, published_at: 3.hours.ago, user_id: second_user.id) }
let!(:old_story) { create(:article, published_at: 3.days.ago) }
let!(:low_scoring_article) { create(:article, score: -1000) }
let!(:month_old_story) { create(:article, published_at: 1.month.ago) }
@ -76,6 +77,11 @@ RSpec.describe Articles::Feeds::LargeForemExperimental, type: :service do
expect(stories).to include(article)
expect(stories).to include(hot_story)
end
it "does not load blocked articles" do
create(:user_block, blocker: user, blocked: second_user, config: "default")
expect(result).not_to include(hot_story)
end
end
context "when ranking is true" do