Preparing feeds for better filtering

This commit is contained in:
Eugen Rochko 2016-10-02 15:28:47 +02:00
parent 1fce687f8e
commit be86d4e0a3
5 changed files with 22 additions and 9 deletions

View file

@ -24,6 +24,7 @@
&:disabled { &:disabled {
background-color: #9baec8; background-color: #9baec8;
cursor: default;
} }
&.button-secondary { &.button-secondary {

View file

@ -9,10 +9,12 @@ class FeedManager
"feed:#{type}:#{id}" "feed:#{type}:#{id}"
end end
# Filter status out of the home feed if it is a reply to someone the user doesn't follow def filter?(timeline_type, status, receiver)
def filter_status?(status, follower) if timeline_type == :home
replied_to_user = status.reply? ? status.thread.account : nil filter_from_home?(status, receiver)
(status.reply? && !(follower.id == replied_to_user.id || replied_to_user.id == status.account_id || follower.following?(replied_to_user))) else
filter_from_mentions?(status, receiver)
end
end end
def push(timeline_type, account, status) def push(timeline_type, account, status)
@ -37,6 +39,16 @@ class FeedManager
$redis $redis
end end
# Filter status out of the home feed if it is a reply to someone the user doesn't follow
def filter_from_home?(status, follower)
replied_to_user = status.reply? ? status.thread.account : nil
(status.reply? && !(follower.id == replied_to_user.id || replied_to_user.id == status.account_id || follower.following?(replied_to_user)))
end
def filter_from_mentions?(status, follower)
false
end
def inline_render(target_account, status) def inline_render(target_account, status)
rabl_scope = Class.new do rabl_scope = Class.new do
include RoutingHelper include RoutingHelper

View file

@ -15,7 +15,7 @@ class FanOutOnWriteService < BaseService
def deliver_to_followers(status) def deliver_to_followers(status)
status.account.followers.each do |follower| status.account.followers.each do |follower|
next if !follower.local? || FeedManager.instance.filter_status?(status, follower) next if !follower.local? || FeedManager.instance.filter?(:home, status, follower)
FeedManager.instance.push(:home, follower, status) FeedManager.instance.push(:home, follower, status)
end end
end end
@ -23,7 +23,7 @@ class FanOutOnWriteService < BaseService
def deliver_to_mentioned(status) def deliver_to_mentioned(status)
status.mentions.each do |mention| status.mentions.each do |mention|
mentioned_account = mention.account mentioned_account = mention.account
next if !mentioned_account.local? || mentioned_account.id == status.account_id next if !mentioned_account.local? || mentioned_account.id == status.account_id || FeedManager.instance.filter?(:mentions, status, mentioned_account)
FeedManager.instance.push(:mentions, mentioned_account, status) FeedManager.instance.push(:mentions, mentioned_account, status)
end end
end end

View file

@ -7,7 +7,7 @@ class PrecomputeFeedService < BaseService
instant_return = [] instant_return = []
Status.send("as_#{type}_timeline", account).order('created_at desc').limit(FeedManager::MAX_ITEMS).find_each do |status| Status.send("as_#{type}_timeline", account).order('created_at desc').limit(FeedManager::MAX_ITEMS).find_each do |status|
next if type == :home && FeedManager.instance.filter_status?(status, account) next FeedManager.instance.filter?(type, status, account)
redis.zadd(FeedManager.instance.key(type, account.id), status.id, status.id) redis.zadd(FeedManager.instance.key(type, account.id), status.id, status.id)
instant_return << status unless instant_return.size > limit instant_return << status unless instant_return.size > limit
end end

View file

@ -9,12 +9,12 @@ RSpec.describe FeedManager do
end end
end end
describe '#filter_status?' do describe '#filter?' do
let(:followee) { Fabricate(:account, username: 'alice') } let(:followee) { Fabricate(:account, username: 'alice') }
let(:status) { Fabricate(:status, text: 'Hello world', account: followee) } let(:status) { Fabricate(:status, text: 'Hello world', account: followee) }
let(:follower) { Fabricate(:account, username: 'bob') } let(:follower) { Fabricate(:account, username: 'bob') }
subject { FeedManager.instance.filter_status?(status, follower) } subject { FeedManager.instance.filter?(:home, status, follower) }
it 'returns a boolean value' do it 'returns a boolean value' do
expect(subject).to be false expect(subject).to be false