improve filtering

staging
multiple creatures 2019-02-16 22:13:00 +00:00
parent 10b20607ac
commit 28c9b9ce6a
3 changed files with 28 additions and 33 deletions

View File

@ -11,7 +11,9 @@ class StatusFilter
def filtered? def filtered?
return false if !account.nil? && account.id == status.account_id return false if !account.nil? && account.id == status.account_id
blocked_by_policy? || (account_present? && filtered_status?) || silenced_account? return true if blocked_by_policy? || (account_present? && filtered_status?) || silenced_account?
# filter non-op posts replying to something marked no replies
non_self_reply? && reply_to_no_replies?
end end
private private
@ -25,13 +27,11 @@ class StatusFilter
end end
def filtered_reference? def filtered_reference?
filtered_reply = reply_to_blocked? || reply_to_muted? # filter muted/blocked
return true if reply_to_blocked? || reply_to_muted?
# I don't think this should happen, but just in case... # kajiht has no filters if status has no mentions
return filtered_reply if status&.mentions.nil? return false if status&.mentions.nil?
# filter non-op posts replying to something marked no replies
return true if reply_to_no_replies?
# Grab a list of account IDs mentioned in the status. # Grab a list of account IDs mentioned in the status.
mentioned_account_ids = status.mentions.pluck(:account_id) mentioned_account_ids = status.mentions.pluck(:account_id)
@ -39,22 +39,16 @@ class StatusFilter
# Don't filter statuses mentioning you. # Don't filter statuses mentioning you.
return false if mentioned_account_ids.include?(account.id) return false if mentioned_account_ids.include?(account.id)
# Otherwise, filter replies to someone you've muted or blocked. # Otherwise, filter the status if it mentions someone you've muted.
return true if filtered_reply
# Otherwise, filter the status if it mentions someone in the preloaded muting relation.
return true if @preloaded_relations[:muting] && mentioned_account_ids.any? do |mentioned_account_id| return true if @preloaded_relations[:muting] && mentioned_account_ids.any? do |mentioned_account_id|
@preloaded_relations[:muting][mentioned_account_id] @preloaded_relations[:muting][mentioned_account_id]
end end
# Otherwise, filter the status if it mentions someone you've muted.
return true if account.muting?(mentioned_account_ids) return true if account.muting?(mentioned_account_ids)
# Same as above, but for blocks: # Same as above, but for blocks:
return true if @preloaded_relations[:blocking] && mentioned_account_ids.any? do |mentioned_account_id| return true if @preloaded_relations[:blocking] && mentioned_account_ids.any? do |mentioned_account_id|
@preloaded_relations[:blocking][mentioned_account_id] @preloaded_relations[:blocking][mentioned_account_id]
end end
account.blocking?(mentioned_account_ids) account.blocking?(mentioned_account_ids)
end end
@ -66,14 +60,6 @@ class StatusFilter
@preloaded_relations[:muting] ? @preloaded_relations[:muting][status.in_reply_to_account_id] : account.muting?(status.in_reply_to_account_id) @preloaded_relations[:muting] ? @preloaded_relations[:muting][status.in_reply_to_account_id] : account.muting?(status.in_reply_to_account_id)
end end
def reply_to_no_replies?
status.reply? &&
!status.in_reply_to_account_id.nil? &&
!status.in_reply_to_id.nil? &&
status.account_id != status.in_reply_to_account_id &&
Status.find(status.in_reply_to_id)&.marked_no_replies?
end
def blocking_account? def blocking_account?
@preloaded_relations[:blocking] ? @preloaded_relations[:blocking][status.account_id] : account.blocking?(status.account_id) @preloaded_relations[:blocking] ? @preloaded_relations[:blocking][status.account_id] : account.blocking?(status.account_id)
end end
@ -86,6 +72,19 @@ class StatusFilter
@preloaded_relations[:muting] ? @preloaded_relations[:muting][status.account_id] : account.muting?(status.account_id) @preloaded_relations[:muting] ? @preloaded_relations[:muting][status.account_id] : account.muting?(status.account_id)
end end
def account_following_status_account?
@preloaded_relations[:following] ? @preloaded_relations[:following][status.account_id] : account&.following?(status.account_id)
end
def non_self_reply?
status.reply? && status.in_reply_to_account_id != status.account_id
end
def reply_to_no_replies?
parent_status = Status.find(status.in_reply_to_id)
parent_status&.marked_no_replies? && !parent_status.mentions.pluck(:account_id).include?(status.account_id)
end
def silenced_account? def silenced_account?
!account&.silenced? && status_account_silenced? && !account_following_status_account? !account&.silenced? && status_account_silenced? && !account_following_status_account?
end end
@ -94,10 +93,6 @@ class StatusFilter
status.account.silenced? status.account.silenced?
end end
def account_following_status_account?
@preloaded_relations[:following] ? @preloaded_relations[:following][status.account_id] : account&.following?(status.account_id)
end
def blocked_by_policy? def blocked_by_policy?
!policy_allows_show? !policy_allows_show?
end end

View File

@ -295,7 +295,7 @@ class Status < ApplicationRecord
after_create :set_poll_id after_create :set_poll_id
after_find :limit_visibility after_find :limit_domain_visibility
class << self class << self
def selectable_visibilities def selectable_visibilities
@ -551,11 +551,11 @@ class Status < ApplicationRecord
self.sensitive = false if sensitive.nil? self.sensitive = false if sensitive.nil?
end end
def limit_visibility def limit_domain_visibility
return unless has_attribute?(:uri) && !uri.nil? return unless has_attribute?(:uri) && !uri.nil?
domain = Addressable::URI.parse(uri).host domain = Addressable::URI.parse(uri).host
self.sensitive = true if domain.in?(FORCE_SENSITIVE) self.sensitive = true if domain.in?(FORCE_SENSITIVE)
self.visibility = :unlisted if domain.in?(FORCE_UNLISTED) self.visibility = :unlisted if public_visibility? && domain.in?(FORCE_UNLISTED)
end end
def set_locality def set_locality

View File

@ -14,7 +14,7 @@ class StatusPolicy < ApplicationPolicy
def show? def show?
return false if local_only? && (current_account.nil? || !current_account.local?) return false if local_only? && (current_account.nil? || !current_account.local?)
if requires_mention? if direct?
owned? || mention_exists? owned? || mention_exists?
elsif private? elsif private?
owned? || following_author? || mention_exists? owned? || following_author? || mention_exists?
@ -24,7 +24,7 @@ class StatusPolicy < ApplicationPolicy
end end
def reblog? def reblog?
!requires_mention? && (!private? || owned?) && show? && !blocking_author? !direct? && (!private? || owned?) && show? && !blocking_author?
end end
def favourite? def favourite?
@ -43,8 +43,8 @@ class StatusPolicy < ApplicationPolicy
private private
def requires_mention? def direct?
record.direct_visibility? || record.limited_visibility? record.direct_visibility?
end end
def owned? def owned?