2017-08-09 05:52:15 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::ProcessAccountService < BaseService
|
|
|
|
include JsonLdHelper
|
2019-07-09 11:27:35 +10:00
|
|
|
include DomainControlHelper
|
2022-04-29 01:47:34 +10:00
|
|
|
include Redisable
|
2022-05-13 08:02:35 +10:00
|
|
|
include Lockable
|
2017-08-09 05:52:15 +10:00
|
|
|
|
2022-12-07 10:15:24 +11:00
|
|
|
SUBDOMAINS_RATELIMIT = 10
|
|
|
|
DISCOVERIES_PER_REQUEST = 400
|
|
|
|
|
2017-08-09 05:52:15 +10:00
|
|
|
# Should be called with confirmed valid JSON
|
|
|
|
# and WebFinger-resolved username and domain
|
2018-08-27 04:21:03 +10:00
|
|
|
def call(username, domain, json, options = {})
|
2019-07-09 11:27:35 +10:00
|
|
|
return if json['inbox'].blank? || unsupported_uri_scheme?(json['id']) || domain_not_allowed?(domain)
|
2017-08-23 02:30:15 +10:00
|
|
|
|
2018-08-27 04:21:03 +10:00
|
|
|
@options = options
|
2017-09-08 20:00:17 +10:00
|
|
|
@json = json
|
|
|
|
@uri = @json['id']
|
|
|
|
@username = username
|
2022-12-07 10:15:24 +11:00
|
|
|
@domain = TagManager.instance.normalize_domain(domain)
|
2017-09-08 20:00:17 +10:00
|
|
|
@collections = {}
|
2017-08-09 05:52:15 +10:00
|
|
|
|
2022-12-07 10:15:24 +11:00
|
|
|
# The key does not need to be unguessable, it just needs to be somewhat unique
|
|
|
|
@options[:request_id] ||= "#{Time.now.utc.to_i}-#{username}@#{domain}"
|
|
|
|
|
2023-05-03 02:16:07 +10:00
|
|
|
with_redis_lock("process_account:#{@uri}") do
|
2022-05-13 08:02:35 +10:00
|
|
|
@account = Account.remote.find_by(uri: @uri) if @options[:only_key]
|
|
|
|
@account ||= Account.find_remote(@username, @domain)
|
|
|
|
@old_public_key = @account&.public_key
|
|
|
|
@old_protocol = @account&.protocol
|
|
|
|
@suspension_changed = false
|
|
|
|
|
2022-12-07 10:15:24 +11:00
|
|
|
if @account.nil?
|
|
|
|
with_redis do |redis|
|
|
|
|
return nil if redis.pfcount("unique_subdomains_for:#{PublicSuffix.domain(@domain, ignore_private: true)}") >= SUBDOMAINS_RATELIMIT
|
|
|
|
|
|
|
|
discoveries = redis.incr("discovery_per_request:#{@options[:request_id]}")
|
|
|
|
redis.expire("discovery_per_request:#{@options[:request_id]}", 5.minutes.seconds)
|
|
|
|
return nil if discoveries > DISCOVERIES_PER_REQUEST
|
|
|
|
end
|
|
|
|
|
|
|
|
create_account
|
|
|
|
end
|
|
|
|
|
2022-05-13 08:02:35 +10:00
|
|
|
update_account
|
|
|
|
process_tags
|
|
|
|
|
|
|
|
process_duplicate_accounts! if @options[:verified_webfinger]
|
2017-09-19 14:53:16 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
after_protocol_change! if protocol_changed?
|
2018-08-27 04:21:03 +10:00
|
|
|
after_key_change! if key_changed? && !@options[:signed_with_known_key]
|
2019-01-19 01:56:55 +11:00
|
|
|
clear_tombstones! if key_changed?
|
2020-11-08 10:28:39 +11:00
|
|
|
after_suspension_change! if suspension_changed?
|
2019-01-19 01:56:55 +11:00
|
|
|
|
2020-11-08 10:28:39 +11:00
|
|
|
unless @options[:only_key] || @account.suspended?
|
2019-01-08 07:45:13 +11:00
|
|
|
check_featured_collection! if @account.featured_collection_url.present?
|
2022-10-20 18:15:52 +11:00
|
|
|
check_featured_tags_collection! if @json['featuredTags'].present?
|
2022-11-09 18:24:21 +11:00
|
|
|
check_links! if @account.fields.any?(&:requires_verification?)
|
2019-01-08 07:45:13 +11:00
|
|
|
end
|
2017-08-09 05:52:15 +10:00
|
|
|
|
|
|
|
@account
|
|
|
|
rescue Oj::ParseError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_account
|
|
|
|
@account = Account.new
|
2020-11-08 10:28:39 +11:00
|
|
|
@account.protocol = :activitypub
|
|
|
|
@account.username = @username
|
|
|
|
@account.domain = @domain
|
|
|
|
@account.private_key = nil
|
|
|
|
@account.suspended_at = domain_block.created_at if auto_suspend?
|
|
|
|
@account.suspension_origin = :local if auto_suspend?
|
|
|
|
@account.silenced_at = domain_block.created_at if auto_silence?
|
2023-07-11 02:42:19 +10:00
|
|
|
|
|
|
|
set_immediate_protocol_attributes!
|
|
|
|
|
2023-07-21 02:23:48 +10:00
|
|
|
@account.save!
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_account
|
2019-01-08 07:45:13 +11:00
|
|
|
@account.last_webfingered_at = Time.now.utc unless @options[:only_key]
|
2017-08-09 05:52:15 +10:00
|
|
|
@account.protocol = :activitypub
|
2017-09-19 14:53:16 +10:00
|
|
|
|
2020-11-08 10:28:39 +11:00
|
|
|
set_suspension!
|
2020-12-19 09:26:26 +11:00
|
|
|
set_immediate_protocol_attributes!
|
|
|
|
set_fetchable_key! unless @account.suspended? && @account.suspension_origin_local?
|
2020-11-08 10:28:39 +11:00
|
|
|
set_immediate_attributes! unless @account.suspended?
|
2020-12-19 09:26:26 +11:00
|
|
|
set_fetchable_attributes! unless @options[:only_key] || @account.suspended?
|
2017-09-19 14:53:16 +10:00
|
|
|
|
2017-09-09 21:41:45 +10:00
|
|
|
@account.save_with_optional_media!
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|
|
|
|
|
2020-12-19 09:26:26 +11:00
|
|
|
def set_immediate_protocol_attributes!
|
2018-03-04 19:19:11 +11:00
|
|
|
@account.inbox_url = @json['inbox'] || ''
|
|
|
|
@account.outbox_url = @json['outbox'] || ''
|
|
|
|
@account.shared_inbox_url = (@json['endpoints'].is_a?(Hash) ? @json['endpoints']['sharedInbox'] : @json['sharedInbox']) || ''
|
|
|
|
@account.followers_url = @json['followers'] || ''
|
|
|
|
@account.url = url || @uri
|
2018-05-15 06:56:45 +10:00
|
|
|
@account.uri = @uri
|
2020-12-19 09:26:26 +11:00
|
|
|
@account.actor_type = actor_type
|
2021-05-07 22:33:19 +10:00
|
|
|
@account.created_at = @json['published'] if @json['published'].present?
|
2020-12-19 09:26:26 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_immediate_attributes!
|
|
|
|
@account.featured_collection_url = @json['featured'] || ''
|
|
|
|
@account.devices_url = @json['devices'] || ''
|
2018-03-04 19:19:11 +11:00
|
|
|
@account.display_name = @json['name'] || ''
|
|
|
|
@account.note = @json['summary'] || ''
|
|
|
|
@account.locked = @json['manuallyApprovesFollowers'] || false
|
2018-04-14 20:41:08 +10:00
|
|
|
@account.fields = property_values || {}
|
2018-12-29 12:24:36 +11:00
|
|
|
@account.also_known_as = as_array(@json['alsoKnownAs'] || []).map { |item| value_or_id(item) }
|
2019-08-30 08:14:36 +10:00
|
|
|
@account.discoverable = @json['discoverable'] || false
|
2023-08-15 02:54:51 +10:00
|
|
|
@account.indexable = @json['indexable'] || false
|
2023-08-23 16:27:24 +10:00
|
|
|
@account.memorial = @json['memorial'] || false
|
2017-09-19 14:53:16 +10:00
|
|
|
end
|
|
|
|
|
2020-12-19 09:26:26 +11:00
|
|
|
def set_fetchable_key!
|
2021-05-07 22:33:19 +10:00
|
|
|
@account.public_key = public_key || ''
|
2020-12-19 09:26:26 +11:00
|
|
|
end
|
|
|
|
|
2017-09-19 14:53:16 +10:00
|
|
|
def set_fetchable_attributes!
|
2021-05-11 22:19:22 +10:00
|
|
|
begin
|
|
|
|
@account.avatar_remote_url = image_url('icon') || '' unless skip_download?
|
2022-08-16 04:32:21 +10:00
|
|
|
@account.avatar = nil if @account.avatar_remote_url.blank?
|
2021-05-11 22:19:22 +10:00
|
|
|
rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError
|
|
|
|
RedownloadAvatarWorker.perform_in(rand(30..600).seconds, @account.id)
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
@account.header_remote_url = image_url('image') || '' unless skip_download?
|
2022-08-16 04:32:21 +10:00
|
|
|
@account.header = nil if @account.header_remote_url.blank?
|
2021-05-11 22:19:22 +10:00
|
|
|
rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError
|
|
|
|
RedownloadHeaderWorker.perform_in(rand(30..600).seconds, @account.id)
|
|
|
|
end
|
2017-09-19 14:53:16 +10:00
|
|
|
@account.statuses_count = outbox_total_items if outbox_total_items.present?
|
|
|
|
@account.following_count = following_total_items if following_total_items.present?
|
|
|
|
@account.followers_count = followers_total_items if followers_total_items.present?
|
2020-03-09 10:10:29 +11:00
|
|
|
@account.hide_collections = following_private? || followers_private?
|
2018-01-03 10:38:20 +11:00
|
|
|
@account.moved_to_account = @json['movedTo'].present? ? moved_account : nil
|
2017-09-19 14:53:16 +10:00
|
|
|
end
|
|
|
|
|
2020-11-08 10:28:39 +11:00
|
|
|
def set_suspension!
|
|
|
|
return if @account.suspended? && @account.suspension_origin_local?
|
|
|
|
|
|
|
|
if @account.suspended? && !@json['suspended']
|
|
|
|
@account.unsuspend!
|
|
|
|
@suspension_changed = true
|
|
|
|
elsif !@account.suspended? && @json['suspended']
|
|
|
|
@account.suspend!(origin: :remote)
|
|
|
|
@suspension_changed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-19 14:53:16 +10:00
|
|
|
def after_protocol_change!
|
2017-08-21 09:14:40 +10:00
|
|
|
ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
|
|
|
|
end
|
|
|
|
|
2017-09-19 14:53:16 +10:00
|
|
|
def after_key_change!
|
|
|
|
RefollowWorker.perform_async(@account.id)
|
|
|
|
end
|
|
|
|
|
2020-11-08 10:28:39 +11:00
|
|
|
def after_suspension_change!
|
|
|
|
if @account.suspended?
|
|
|
|
Admin::SuspensionWorker.perform_async(@account.id)
|
|
|
|
else
|
|
|
|
Admin::UnsuspensionWorker.perform_async(@account.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-04 19:19:11 +11:00
|
|
|
def check_featured_collection!
|
2022-12-07 10:15:24 +11:00
|
|
|
ActivityPub::SynchronizeFeaturedCollectionWorker.perform_async(@account.id, { 'hashtag' => @json['featuredTags'].blank?, 'request_id' => @options[:request_id] })
|
2022-10-20 18:15:52 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_featured_tags_collection!
|
|
|
|
ActivityPub::SynchronizeFeaturedTagsCollectionWorker.perform_async(@account.id, @json['featuredTags'])
|
2018-03-04 19:19:11 +11:00
|
|
|
end
|
|
|
|
|
2018-09-19 00:45:58 +10:00
|
|
|
def check_links!
|
2023-11-14 03:17:05 +11:00
|
|
|
VerifyAccountLinksWorker.perform_in(rand(10.minutes.to_i), @account.id)
|
2018-09-19 00:45:58 +10:00
|
|
|
end
|
|
|
|
|
2020-12-19 09:26:26 +11:00
|
|
|
def process_duplicate_accounts!
|
|
|
|
return unless Account.where(uri: @account.uri).where.not(id: @account.id).exists?
|
|
|
|
|
|
|
|
AccountMergingWorker.perform_async(@account.id)
|
|
|
|
end
|
|
|
|
|
2018-05-08 21:30:04 +10:00
|
|
|
def actor_type
|
|
|
|
if @json['type'].is_a?(Array)
|
|
|
|
@json['type'].find { |type| ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES.include?(type) }
|
|
|
|
else
|
|
|
|
@json['type']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-09 05:52:15 +10:00
|
|
|
def image_url(key)
|
|
|
|
value = first_of_value(@json[key])
|
|
|
|
|
|
|
|
return if value.nil?
|
|
|
|
|
2024-02-23 08:27:24 +11:00
|
|
|
if value.is_a?(String)
|
|
|
|
value = fetch_resource_without_id_validation(value)
|
|
|
|
return if value.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
value = first_of_value(value['url']) if value.is_a?(Hash) && value['type'] == 'Image'
|
|
|
|
value = value['href'] if value.is_a?(Hash)
|
|
|
|
value if value.is_a?(String)
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def public_key
|
|
|
|
value = first_of_value(@json['publicKey'])
|
|
|
|
|
|
|
|
return if value.nil?
|
|
|
|
return value['publicKeyPem'] if value.is_a?(Hash)
|
|
|
|
|
2017-10-04 10:13:48 +11:00
|
|
|
key = fetch_resource_without_id_validation(value)
|
2017-08-09 05:52:15 +10:00
|
|
|
key['publicKeyPem'] if key
|
|
|
|
end
|
|
|
|
|
2017-09-05 02:26:33 +10:00
|
|
|
def url
|
|
|
|
return if @json['url'].blank?
|
2018-01-08 15:00:23 +11:00
|
|
|
|
|
|
|
url_candidate = url_to_href(@json['url'], 'text/html')
|
|
|
|
|
|
|
|
if unsupported_uri_scheme?(url_candidate) || mismatching_origin?(url_candidate)
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
url_candidate
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-14 20:41:08 +10:00
|
|
|
def property_values
|
|
|
|
return unless @json['attachment'].is_a?(Array)
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2019-03-30 12:12:06 +11:00
|
|
|
as_array(@json['attachment']).select { |attachment| attachment['type'] == 'PropertyValue' }.map { |attachment| attachment.slice('name', 'value') }
|
2018-04-14 20:41:08 +10:00
|
|
|
end
|
|
|
|
|
2018-01-08 15:00:23 +11:00
|
|
|
def mismatching_origin?(url)
|
|
|
|
needle = Addressable::URI.parse(url).host
|
|
|
|
haystack = Addressable::URI.parse(@uri).host
|
|
|
|
|
|
|
|
!haystack.casecmp(needle).zero?
|
2017-09-05 02:26:33 +10:00
|
|
|
end
|
|
|
|
|
2017-09-08 20:00:17 +10:00
|
|
|
def outbox_total_items
|
2020-03-09 10:10:29 +11:00
|
|
|
collection_info('outbox').first
|
2017-09-08 20:00:17 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def following_total_items
|
2020-03-09 10:10:29 +11:00
|
|
|
collection_info('following').first
|
2017-09-08 20:00:17 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def followers_total_items
|
2020-03-09 10:10:29 +11:00
|
|
|
collection_info('followers').first
|
2017-09-08 20:00:17 +10:00
|
|
|
end
|
|
|
|
|
2020-03-09 10:10:29 +11:00
|
|
|
def following_private?
|
|
|
|
!collection_info('following').last
|
|
|
|
end
|
|
|
|
|
|
|
|
def followers_private?
|
|
|
|
!collection_info('followers').last
|
|
|
|
end
|
|
|
|
|
|
|
|
def collection_info(type)
|
|
|
|
return [nil, nil] if @json[type].blank?
|
2017-09-08 20:00:17 +10:00
|
|
|
return @collections[type] if @collections.key?(type)
|
|
|
|
|
2017-10-04 10:13:48 +11:00
|
|
|
collection = fetch_resource_without_id_validation(@json[type])
|
2017-09-08 20:00:17 +10:00
|
|
|
|
2020-03-09 10:10:29 +11:00
|
|
|
total_items = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
|
|
|
|
has_first_page = collection.is_a?(Hash) && collection['first'].present?
|
|
|
|
@collections[type] = [total_items, has_first_page]
|
2020-10-23 23:21:31 +11:00
|
|
|
rescue HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::LengthValidationError
|
2020-03-09 10:10:29 +11:00
|
|
|
@collections[type] = [nil, nil]
|
2017-09-08 20:00:17 +10:00
|
|
|
end
|
|
|
|
|
2017-11-19 05:39:02 +11:00
|
|
|
def moved_account
|
|
|
|
account = ActivityPub::TagManager.instance.uri_to_resource(@json['movedTo'], Account)
|
2024-02-02 01:56:46 +11:00
|
|
|
account ||= ActivityPub::FetchRemoteAccountService.new.call(@json['movedTo'], break_on_redirect: true, request_id: @options[:request_id])
|
2017-11-19 05:39:02 +11:00
|
|
|
account
|
|
|
|
end
|
|
|
|
|
2017-09-09 21:41:45 +10:00
|
|
|
def skip_download?
|
|
|
|
@account.suspended? || domain_block&.reject_media?
|
|
|
|
end
|
|
|
|
|
2017-08-09 05:52:15 +10:00
|
|
|
def auto_suspend?
|
2017-09-19 14:53:16 +10:00
|
|
|
domain_block&.suspend?
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def auto_silence?
|
2017-09-19 14:53:16 +10:00
|
|
|
domain_block&.silence?
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def domain_block
|
|
|
|
return @domain_block if defined?(@domain_block)
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2019-06-22 08:13:10 +10:00
|
|
|
@domain_block = DomainBlock.rule_for(@domain)
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|
2017-09-19 14:53:16 +10:00
|
|
|
|
|
|
|
def key_changed?
|
|
|
|
!@old_public_key.nil? && @old_public_key != @account.public_key
|
|
|
|
end
|
|
|
|
|
2020-11-08 10:28:39 +11:00
|
|
|
def suspension_changed?
|
|
|
|
@suspension_changed
|
|
|
|
end
|
|
|
|
|
2019-01-19 01:56:55 +11:00
|
|
|
def clear_tombstones!
|
2019-02-06 12:50:52 +11:00
|
|
|
Tombstone.where(account_id: @account.id).delete_all
|
2019-01-19 01:56:55 +11:00
|
|
|
end
|
|
|
|
|
2017-09-19 14:53:16 +10:00
|
|
|
def protocol_changed?
|
|
|
|
!@old_protocol.nil? && @old_protocol != @account.protocol
|
|
|
|
end
|
|
|
|
|
2018-04-13 09:27:22 +10:00
|
|
|
def process_tags
|
2018-04-02 07:55:42 +10:00
|
|
|
return if @json['tag'].blank?
|
2018-04-13 09:27:22 +10:00
|
|
|
|
2018-04-02 07:55:42 +10:00
|
|
|
as_array(@json['tag']).each do |tag|
|
2018-05-02 20:40:24 +10:00
|
|
|
process_emoji tag if equals_or_includes?(tag['type'], 'Emoji')
|
2018-04-02 07:55:42 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-13 09:27:22 +10:00
|
|
|
def process_emoji(tag)
|
2018-04-02 07:55:42 +10:00
|
|
|
return if skip_download?
|
|
|
|
return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
|
|
|
|
|
|
|
|
shortcode = tag['name'].delete(':')
|
|
|
|
image_url = tag['icon']['url']
|
|
|
|
uri = tag['id']
|
|
|
|
updated = tag['updated']
|
|
|
|
emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
|
|
|
|
|
2018-11-11 09:59:51 +11:00
|
|
|
return unless emoji.nil? || image_url != emoji.image_remote_url || (updated && updated >= emoji.updated_at)
|
2018-04-02 07:55:42 +10:00
|
|
|
|
|
|
|
emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
|
|
|
|
emoji.image_remote_url = image_url
|
|
|
|
emoji.save
|
|
|
|
end
|
2017-08-09 05:52:15 +10:00
|
|
|
end
|