Add misskey searchability support

This commit is contained in:
KMY 2023-08-04 13:54:55 +09:00
parent 82d61dad96
commit 753ef0f8e0
3 changed files with 60 additions and 19 deletions

View file

@ -504,37 +504,45 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
end end
SCAN_SEARCHABILITY_RE = /\[searchability:(public|followers|reactors|private)\]/ SCAN_SEARCHABILITY_RE = /\[searchability:(public|followers|reactors|private)\]/
SCAN_SEARCHABILITY_FEDIBIRD_RE = /searchable_by_(all_users|followers_only|reacted_users_only|nobody)/
def searchability def searchability
searchability = searchability_from_audience searchability = searchability_from_audience || searchability_from_bio || (marked_as_misskey_searchability? ? :public : nil)
return nil if searchability.nil?
if searchability.nil?
note = @account&.note
return nil if note.blank?
searchability_bio = note.scan(SCAN_SEARCHABILITY_RE).first
return nil unless searchability_bio
searchability = searchability_bio[0]
return nil if searchability.nil?
searchability = :public if searchability == 'public'
searchability = :private if searchability == 'followers'
searchability = :limited if searchability == 'private'
searchability = :direct if searchability == 'reactors'
end
visibility = visibility_from_audience_with_silence visibility = visibility_from_audience_with_silence
if searchability == visibility || searchability == :limited || searchability == :direct if searchability == visibility || searchability == :limited || searchability == :direct
searchability searchability
elsif [:public, :unlisted, :private].include?(searchability) && [:public, :public_unlisted, :unlisted, :login, :private].include?(visibility) elsif [:public, :unlisted, :private].include?(searchability) && [:private].include?(visibility)
:private :private
else else
:direct :direct
end end
end end
def searchability_from_bio
note = @account&.note
return nil if note.blank?
searchability_bio = note.scan(SCAN_SEARCHABILITY_RE).first || note.scan(SCAN_SEARCHABILITY_FEDIBIRD_RE).first
return nil unless searchability_bio
searchability = searchability_bio[0]
return nil if searchability.nil?
searchability = :public if %w(public all_users).include?(searchability)
searchability = :private if %w(followers followers_only).include?(searchability)
searchability = :limited if %w(private reacted_users_only).include?(searchability)
searchability = :direct if %w(reactors nobody).include?(searchability)
searchability
end
def marked_as_misskey_searchability?
@marked_as_misskey_searchability ||= DomainBlock.detect_invalid_subscription?(@account.domain)
end
def visibility_from_audience def visibility_from_audience
if audience_to.any? { |to| ActivityPub::TagManager.instance.public_collection?(to) } if audience_to.any? { |to| ActivityPub::TagManager.instance.public_collection?(to) }
:public :public

View file

@ -110,6 +110,10 @@ class DomainBlock < ApplicationRecord
!!rule_for(domain)&.reject_new_follow? !!rule_for(domain)&.reject_new_follow?
end end
def detect_invalid_subscription?(domain)
!!rule_for(domain)&.detect_invalid_subscription?
end
def reject_reports?(domain) def reject_reports?(domain)
!!rule_for(domain)&.reject_reports? !!rule_for(domain)&.reject_reports?
end end

View file

@ -8,6 +8,8 @@ class ActivityPub::ProcessAccountService < BaseService
SUBDOMAINS_RATELIMIT = 10 SUBDOMAINS_RATELIMIT = 10
DISCOVERIES_PER_REQUEST = 400 DISCOVERIES_PER_REQUEST = 400
SCAN_SEARCHABILITY_RE = /\[searchability:(public|followers|reactors|private)\]/
SCAN_SEARCHABILITY_FEDIBIRD_RE = /searchable_by_(all_users|followers_only|reacted_users_only|nobody)/
# Should be called with confirmed valid JSON # Should be called with confirmed valid JSON
# and WebFinger-resolved username and domain # and WebFinger-resolved username and domain
@ -252,7 +254,12 @@ class ActivityPub::ProcessAccountService < BaseService
end end
def searchability_from_audience def searchability_from_audience
return :private if audience_searchable_by.nil? if audience_searchable_by.nil?
bio = searchability_from_bio
return bio unless bio.nil?
return marked_as_misskey_searchability? ? :public : :private
end
if audience_searchable_by.any? { |uri| ActivityPub::TagManager.instance.public_collection?(uri) } if audience_searchable_by.any? { |uri| ActivityPub::TagManager.instance.public_collection?(uri) }
:public :public
@ -263,6 +270,28 @@ class ActivityPub::ProcessAccountService < BaseService
end end
end end
def searchability_from_bio
note = @json['summary'] || ''
return nil if note.blank?
searchability_bio = note.scan(SCAN_SEARCHABILITY_RE).first || note.scan(SCAN_SEARCHABILITY_FEDIBIRD_RE).first
return nil unless searchability_bio
searchability = searchability_bio[0]
return nil if searchability.nil?
searchability = :public if %w(public all_users).include?(searchability)
searchability = :private if %w(followers followers_only).include?(searchability)
searchability = :limited if %w(private reacted_users_only).include?(searchability)
searchability = :direct if %w(reactors nobody).include?(searchability)
searchability
end
def marked_as_misskey_searchability?
domain_block&.detect_invalid_subscription
end
def subscribable_by def subscribable_by
return nil if @json['subscribableBy'].nil? return nil if @json['subscribableBy'].nil?