2017-07-15 10:01:39 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'singleton'
|
|
|
|
|
|
|
|
class ActivityPub::TagManager
|
|
|
|
include Singleton
|
|
|
|
include RoutingHelper
|
|
|
|
|
2017-08-09 04:52:15 +09:00
|
|
|
CONTEXT = 'https://www.w3.org/ns/activitystreams'
|
|
|
|
|
2017-07-15 10:01:39 +09:00
|
|
|
COLLECTIONS = {
|
|
|
|
public: 'https://www.w3.org/ns/activitystreams#Public',
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def url_for(target)
|
|
|
|
return target.url if target.respond_to?(:local?) && !target.local?
|
|
|
|
|
2020-03-26 09:56:41 +09:00
|
|
|
return unless target.respond_to?(:object_type)
|
|
|
|
|
2017-07-15 10:01:39 +09:00
|
|
|
case target.object_type
|
|
|
|
when :person
|
2019-07-19 08:44:42 +09:00
|
|
|
target.instance_actor? ? about_more_url(instance_actor: true) : short_account_url(target)
|
2017-07-15 10:01:39 +09:00
|
|
|
when :note, :comment, :activity
|
2017-08-24 23:21:42 +09:00
|
|
|
return activity_account_status_url(target.account, target) if target.reblog?
|
2017-07-15 10:01:39 +09:00
|
|
|
short_account_status_url(target.account, target)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def uri_for(target)
|
|
|
|
return target.uri if target.respond_to?(:local?) && !target.local?
|
|
|
|
|
|
|
|
case target.object_type
|
|
|
|
when :person
|
2019-07-19 08:44:42 +09:00
|
|
|
target.instance_actor? ? instance_actor_url : account_url(target)
|
2017-07-15 10:01:39 +09:00
|
|
|
when :note, :comment, :activity
|
2017-08-24 23:21:42 +09:00
|
|
|
return activity_account_status_url(target.account, target) if target.reblog?
|
2017-07-15 10:01:39 +09:00
|
|
|
account_status_url(target.account, target)
|
2017-10-08 00:43:42 +09:00
|
|
|
when :emoji
|
|
|
|
emoji_url(target)
|
2017-07-15 10:01:39 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-05 04:14:34 +09:00
|
|
|
def generate_uri_for(_target)
|
|
|
|
URI.join(root_url, 'payloads', SecureRandom.uuid)
|
|
|
|
end
|
|
|
|
|
2017-08-24 23:21:42 +09:00
|
|
|
def activity_uri_for(target)
|
2017-09-17 20:51:34 +09:00
|
|
|
raise ArgumentError, 'target must be a local activity' unless %i(note comment activity).include?(target.object_type) && target.local?
|
2017-08-24 23:21:42 +09:00
|
|
|
|
|
|
|
activity_account_status_url(target.account, target)
|
|
|
|
end
|
|
|
|
|
2019-03-01 02:16:34 +09:00
|
|
|
def replies_uri_for(target, page_params = nil)
|
|
|
|
raise ArgumentError, 'target must be a local activity' unless %i(note comment activity).include?(target.object_type) && target.local?
|
|
|
|
|
2019-07-08 19:03:45 +09:00
|
|
|
account_status_replies_url(target.account, target, page_params)
|
2019-03-01 02:16:34 +09:00
|
|
|
end
|
|
|
|
|
2017-07-15 10:01:39 +09:00
|
|
|
# Primary audience of a status
|
|
|
|
# Public statuses go out to primarily the public collection
|
|
|
|
# Unlisted and private statuses go out primarily to the followers collection
|
|
|
|
# Others go out only to the people they mention
|
|
|
|
def to(status)
|
|
|
|
case status.visibility
|
|
|
|
when 'public'
|
|
|
|
[COLLECTIONS[:public]]
|
|
|
|
when 'unlisted', 'private'
|
|
|
|
[account_followers_url(status.account)]
|
2018-10-18 00:13:04 +09:00
|
|
|
when 'direct', 'limited'
|
2019-05-10 05:05:43 +09:00
|
|
|
if status.account.silenced?
|
|
|
|
# Only notify followers if the account is locally silenced
|
|
|
|
account_ids = status.active_mentions.pluck(:account_id)
|
2019-12-05 04:36:33 +09:00
|
|
|
to = status.account.followers.where(id: account_ids).each_with_object([]) do |account, result|
|
|
|
|
result << uri_for(account)
|
|
|
|
result << account.followers_url if account.group?
|
|
|
|
end
|
|
|
|
to.concat(FollowRequest.where(target_account_id: status.account_id, account_id: account_ids).each_with_object([]) do |request, result|
|
|
|
|
result << uri_for(request.account)
|
|
|
|
result << request.account.followers_url if request.account.group?
|
|
|
|
end)
|
2019-05-10 05:05:43 +09:00
|
|
|
else
|
2019-12-05 04:36:33 +09:00
|
|
|
status.active_mentions.each_with_object([]) do |mention, result|
|
|
|
|
result << uri_for(mention.account)
|
|
|
|
result << mention.account.followers_url if mention.account.group?
|
|
|
|
end
|
2019-05-10 05:05:43 +09:00
|
|
|
end
|
2017-07-15 10:01:39 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Secondary audience of a status
|
|
|
|
# Public statuses go out to followers as well
|
|
|
|
# Unlisted statuses go to the public as well
|
|
|
|
# Both of those and private statuses also go to the people mentioned in them
|
|
|
|
# Direct ones don't have a secondary audience
|
|
|
|
def cc(status)
|
|
|
|
cc = []
|
|
|
|
|
2018-01-09 08:47:43 +09:00
|
|
|
cc << uri_for(status.reblog.account) if status.reblog?
|
|
|
|
|
2017-07-15 10:01:39 +09:00
|
|
|
case status.visibility
|
|
|
|
when 'public'
|
|
|
|
cc << account_followers_url(status.account)
|
|
|
|
when 'unlisted'
|
|
|
|
cc << COLLECTIONS[:public]
|
|
|
|
end
|
|
|
|
|
2019-05-10 05:05:43 +09:00
|
|
|
unless status.direct_visibility? || status.limited_visibility?
|
|
|
|
if status.account.silenced?
|
|
|
|
# Only notify followers if the account is locally silenced
|
|
|
|
account_ids = status.active_mentions.pluck(:account_id)
|
2019-12-05 04:36:33 +09:00
|
|
|
cc.concat(status.account.followers.where(id: account_ids).each_with_object([]) do |account, result|
|
|
|
|
result << uri_for(account)
|
|
|
|
result << account.followers_url if account.group?
|
|
|
|
end)
|
|
|
|
cc.concat(FollowRequest.where(target_account_id: status.account_id, account_id: account_ids).each_with_object([]) do |request, result|
|
|
|
|
result << uri_for(request.account)
|
|
|
|
result << request.account.followers_url if request.account.group?
|
|
|
|
end)
|
2019-05-10 05:05:43 +09:00
|
|
|
else
|
2019-12-05 04:36:33 +09:00
|
|
|
cc.concat(status.active_mentions.each_with_object([]) do |mention, result|
|
|
|
|
result << uri_for(mention.account)
|
|
|
|
result << mention.account.followers_url if mention.account.group?
|
|
|
|
end)
|
2019-05-10 05:05:43 +09:00
|
|
|
end
|
|
|
|
end
|
2017-07-15 10:01:39 +09:00
|
|
|
|
|
|
|
cc
|
|
|
|
end
|
2017-08-09 04:52:15 +09:00
|
|
|
|
|
|
|
def local_uri?(uri)
|
2018-05-06 01:22:34 +09:00
|
|
|
return false if uri.nil?
|
|
|
|
|
2017-08-18 10:21:59 +09:00
|
|
|
uri = Addressable::URI.parse(uri)
|
|
|
|
host = uri.normalized_host
|
|
|
|
host = "#{host}:#{uri.port}" if uri.port
|
|
|
|
|
2017-08-18 08:03:18 +09:00
|
|
|
!host.nil? && (::TagManager.instance.local_domain?(host) || ::TagManager.instance.web_domain?(host))
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def uri_to_local_id(uri, param = :id)
|
|
|
|
path_params = Rails.application.routes.recognize_path(uri)
|
2019-07-19 08:44:42 +09:00
|
|
|
path_params[:username] = Rails.configuration.x.local_domain if path_params[:controller] == 'instance_actors'
|
2017-08-09 04:52:15 +09:00
|
|
|
path_params[param]
|
|
|
|
end
|
|
|
|
|
|
|
|
def uri_to_resource(uri, klass)
|
2018-05-06 01:22:34 +09:00
|
|
|
return if uri.nil?
|
|
|
|
|
2017-08-09 04:52:15 +09:00
|
|
|
if local_uri?(uri)
|
|
|
|
case klass.name
|
|
|
|
when 'Account'
|
|
|
|
klass.find_local(uri_to_local_id(uri, :username))
|
|
|
|
else
|
2017-09-09 01:20:03 +09:00
|
|
|
StatusFinder.new(uri).status
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
2017-09-20 01:08:08 +09:00
|
|
|
elsif OStatus::TagManager.instance.local_id?(uri)
|
|
|
|
klass.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, klass.to_s))
|
2017-08-09 04:52:15 +09:00
|
|
|
else
|
2017-08-22 05:57:34 +09:00
|
|
|
klass.find_by(uri: uri.split('#').first)
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
2017-09-09 01:20:03 +09:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
nil
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
2017-07-15 10:01:39 +09:00
|
|
|
end
|