activitypub-academy/app/lib/activity_log_audience_helper.rb

57 lines
1.5 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'
2023-02-05 03:07:01 +09:00
sender = activity_log_event.sender
2023-01-05 01:29:24 +09:00
2023-02-05 03:07:01 +09:00
if sender and match = sender.match(Regexp.new("https://#{domain}/users/([^/]*)"))
2023-01-05 23:15:25 +09:00
[match.captures[0]]
2023-01-05 01:29:24 +09:00
else
2023-01-05 23:15:25 +09:00
[]
2023-01-05 01:29:24 +09:00
end
2023-01-05 23:15:25 +09:00
elsif 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 23:15:25 +09:00
[match.captures[0]]
elsif activity_log_event.path == "https://#{domain}/inbox"
2023-01-05 23:15:25 +09:00
['to', 'bto', 'cc', 'bcc']
2023-01-05 22:57:43 +09:00
.map { |target| actors(activity_log_event.data[target]) }
.flatten
2023-01-05 23:02:09 +09:00
.uniq
2023-01-05 23:15:25 +09:00
else
[]
2023-01-05 01:29:24 +09:00
end
2023-01-05 23:15:25 +09:00
else
[]
2023-01-05 01:29:24 +09:00
end
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)
if match = string_or_array.match(Regexp.new("https://#{domain}/users/([^/]*)"))
[match.captures[0]]
elsif string_or_array.ends_with?("/followers")
Account
.joins(
"JOIN follows ON follows.account_id = accounts.id
2023-01-05 22:57:43 +09:00
JOIN accounts AS followed ON follows.target_account_id = followed.id
WHERE followed.followers_url = '#{string_or_array}'")
.map { |account| account.username }
else
[]
end
else
string_or_array.flat_map { |inner| self.actors(inner) }
2023-01-05 22:33:43 +09:00
end
end
2023-01-05 01:29:24 +09:00
end