activitypub-academy/app/lib/activity_log_audience_helper.rb

60 lines
1.6 KiB
Ruby
Raw Normal View History

2023-01-05 01:29:24 +09:00
# frozen_string_literal: true
class ActivityLogAudienceHelper
def self.audience(activity_log_event)
2023-01-05 22:33:43 +09:00
domain = Rails.configuration.x.web_domain
2023-01-05 01:29:24 +09:00
if activity_log_event.type == 'outbound'
actor = activity_log_event.data['actor']
2023-01-05 22:33:43 +09:00
if actor and match = actor.match(Regexp.new("https://#{domain}/users/([^/]*)"))
2023-01-05 01:29:24 +09:00
return [match.captures[0]]
else
return []
end
end
if activity_log_event.type == 'inbound'
2023-01-05 22:33:43 +09:00
if match = activity_log_event.path.match(Regexp.new("https://#{domain}/users/([^/]*)/inbox"))
2023-01-05 01:29:24 +09:00
return [match.captures[0]]
elsif activity_log_event.path == "https://#{domain}/inbox"
2023-01-05 22:57:43 +09:00
return ['to', 'bto', 'cc', 'bcc']
.map { |target| actors(activity_log_event.data[target]) }
.flatten
2023-01-05 01:29:24 +09:00
end
return []
end
return []
2023-01-05 01:29:24 +09:00
end
2023-01-05 22:33:43 +09:00
private
def self.actors(string_or_array)
domain = Rails.configuration.x.web_domain
if string_or_array.nil?
[]
elsif string_or_array.is_a?(String)
self.actors([string_or_array])
else
string_or_array.map do |string|
if match = string.match(Regexp.new("https://#{domain}/users/([^/]*)"))
match.captures[0]
2023-01-05 22:57:43 +09:00
elsif string.ends_with?("/followers")
Account
.joins(
"JOIN follows ON follows.account_id = accounts.id
JOIN accounts AS followed ON follows.target_account_id = followed.id
WHERE followed.followers_url = '#{string}'")
.map { |account| account.username }
2023-01-05 22:33:43 +09:00
else
nil
end
2023-01-05 22:57:43 +09:00
end.flatten.compact
2023-01-05 22:33:43 +09:00
end
end
2023-01-05 01:29:24 +09:00
end