2017-08-08 15:52:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::ProcessAccountService < BaseService
|
|
|
|
include JsonLdHelper
|
2019-07-08 21:27:35 -04:00
|
|
|
include DomainControlHelper
|
2017-08-08 15:52:15 -04:00
|
|
|
|
|
|
|
# Should be called with confirmed valid JSON
|
|
|
|
# and WebFinger-resolved username and domain
|
2018-08-26 14:21:03 -04:00
|
|
|
def call(username, domain, json, options = {})
|
2019-07-08 21:27:35 -04:00
|
|
|
return if json['inbox'].blank? || unsupported_uri_scheme?(json['id']) || domain_not_allowed?(domain)
|
2017-08-22 12:30:15 -04:00
|
|
|
|
2018-08-26 14:21:03 -04:00
|
|
|
@options = options
|
2017-09-08 06:00:17 -04:00
|
|
|
@json = json
|
|
|
|
@uri = @json['id']
|
|
|
|
@username = username
|
|
|
|
@domain = domain
|
|
|
|
@collections = {}
|
2017-08-08 15:52:15 -04:00
|
|
|
|
2017-09-19 00:53:16 -04:00
|
|
|
RedisLock.acquire(lock_options) do |lock|
|
|
|
|
if lock.acquired?
|
2020-02-24 15:15:53 -05: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
|
2017-09-19 00:53:16 -04:00
|
|
|
|
|
|
|
create_account if @account.nil?
|
|
|
|
update_account
|
2018-04-12 19:27:22 -04:00
|
|
|
process_tags
|
2019-03-29 21:12:06 -04:00
|
|
|
process_attachments
|
2018-05-16 06:29:45 -04:00
|
|
|
else
|
|
|
|
raise Mastodon::RaceConditionError
|
2017-09-19 00:53:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-12 19:27:22 -04:00
|
|
|
return if @account.nil?
|
|
|
|
|
2017-09-19 00:53:16 -04:00
|
|
|
after_protocol_change! if protocol_changed?
|
2018-08-26 14:21:03 -04:00
|
|
|
after_key_change! if key_changed? && !@options[:signed_with_known_key]
|
2019-01-18 09:56:55 -05:00
|
|
|
clear_tombstones! if key_changed?
|
|
|
|
|
2019-01-07 15:45:13 -05:00
|
|
|
unless @options[:only_key]
|
|
|
|
check_featured_collection! if @account.featured_collection_url.present?
|
|
|
|
check_links! unless @account.fields.empty?
|
|
|
|
end
|
2017-08-08 15:52:15 -04:00
|
|
|
|
|
|
|
@account
|
|
|
|
rescue Oj::ParseError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_account
|
|
|
|
@account = Account.new
|
2019-05-14 13:05:02 -04:00
|
|
|
@account.protocol = :activitypub
|
|
|
|
@account.username = @username
|
|
|
|
@account.domain = @domain
|
|
|
|
@account.private_key = nil
|
|
|
|
@account.suspended_at = domain_block.created_at if auto_suspend?
|
2019-07-01 13:13:30 -04:00
|
|
|
@account.silenced_at = domain_block.created_at if auto_silence?
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_account
|
2019-01-07 15:45:13 -05:00
|
|
|
@account.last_webfingered_at = Time.now.utc unless @options[:only_key]
|
2017-08-08 15:52:15 -04:00
|
|
|
@account.protocol = :activitypub
|
2017-09-19 00:53:16 -04:00
|
|
|
|
|
|
|
set_immediate_attributes!
|
2019-01-07 15:45:13 -05:00
|
|
|
set_fetchable_attributes! unless @options[:only_keys]
|
2017-09-19 00:53:16 -04:00
|
|
|
|
2017-09-09 07:41:45 -04:00
|
|
|
@account.save_with_optional_media!
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
|
|
|
|
2017-09-19 00:53:16 -04:00
|
|
|
def set_immediate_attributes!
|
2018-03-04 03:19:11 -05: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.featured_collection_url = @json['featured'] || ''
|
2020-06-02 13:24:53 -04:00
|
|
|
@account.devices_url = @json['devices'] || ''
|
2018-03-04 03:19:11 -05:00
|
|
|
@account.url = url || @uri
|
2018-05-14 16:56:45 -04:00
|
|
|
@account.uri = @uri
|
2018-03-04 03:19:11 -05:00
|
|
|
@account.display_name = @json['name'] || ''
|
|
|
|
@account.note = @json['summary'] || ''
|
|
|
|
@account.locked = @json['manuallyApprovesFollowers'] || false
|
2018-04-14 06:41:08 -04:00
|
|
|
@account.fields = property_values || {}
|
2018-12-28 20:24:36 -05:00
|
|
|
@account.also_known_as = as_array(@json['alsoKnownAs'] || []).map { |item| value_or_id(item) }
|
2018-05-08 07:30:04 -04:00
|
|
|
@account.actor_type = actor_type
|
2019-08-29 18:14:36 -04:00
|
|
|
@account.discoverable = @json['discoverable'] || false
|
2017-09-19 00:53:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_fetchable_attributes!
|
|
|
|
@account.avatar_remote_url = image_url('icon') unless skip_download?
|
|
|
|
@account.header_remote_url = image_url('image') unless skip_download?
|
|
|
|
@account.public_key = public_key || ''
|
|
|
|
@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-08 19:10:29 -04:00
|
|
|
@account.hide_collections = following_private? || followers_private?
|
2018-01-02 18:38:20 -05:00
|
|
|
@account.moved_to_account = @json['movedTo'].present? ? moved_account : nil
|
2017-09-19 00:53:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def after_protocol_change!
|
2017-08-20 19:14:40 -04:00
|
|
|
ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
|
|
|
|
end
|
|
|
|
|
2017-09-19 00:53:16 -04:00
|
|
|
def after_key_change!
|
|
|
|
RefollowWorker.perform_async(@account.id)
|
|
|
|
end
|
|
|
|
|
2018-03-04 03:19:11 -05:00
|
|
|
def check_featured_collection!
|
|
|
|
ActivityPub::SynchronizeFeaturedCollectionWorker.perform_async(@account.id)
|
|
|
|
end
|
|
|
|
|
2018-09-18 10:45:58 -04:00
|
|
|
def check_links!
|
|
|
|
VerifyAccountLinksWorker.perform_async(@account.id)
|
|
|
|
end
|
|
|
|
|
2018-05-08 07:30:04 -04: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-08 15:52:15 -04:00
|
|
|
def image_url(key)
|
|
|
|
value = first_of_value(@json[key])
|
|
|
|
|
|
|
|
return if value.nil?
|
2017-09-04 12:26:33 -04:00
|
|
|
return value['url'] if value.is_a?(Hash)
|
2017-08-08 15:52:15 -04:00
|
|
|
|
2017-10-03 19:13:48 -04:00
|
|
|
image = fetch_resource_without_id_validation(value)
|
2017-08-08 15:52:15 -04:00
|
|
|
image['url'] if image
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_key
|
|
|
|
value = first_of_value(@json['publicKey'])
|
|
|
|
|
|
|
|
return if value.nil?
|
|
|
|
return value['publicKeyPem'] if value.is_a?(Hash)
|
|
|
|
|
2017-10-03 19:13:48 -04:00
|
|
|
key = fetch_resource_without_id_validation(value)
|
2017-08-08 15:52:15 -04:00
|
|
|
key['publicKeyPem'] if key
|
|
|
|
end
|
|
|
|
|
2017-09-04 12:26:33 -04:00
|
|
|
def url
|
|
|
|
return if @json['url'].blank?
|
2018-01-07 23:00:23 -05: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 06:41:08 -04:00
|
|
|
def property_values
|
|
|
|
return unless @json['attachment'].is_a?(Array)
|
2019-03-29 21:12:06 -04:00
|
|
|
as_array(@json['attachment']).select { |attachment| attachment['type'] == 'PropertyValue' }.map { |attachment| attachment.slice('name', 'value') }
|
2018-04-14 06:41:08 -04:00
|
|
|
end
|
|
|
|
|
2018-01-07 23:00:23 -05:00
|
|
|
def mismatching_origin?(url)
|
|
|
|
needle = Addressable::URI.parse(url).host
|
|
|
|
haystack = Addressable::URI.parse(@uri).host
|
|
|
|
|
|
|
|
!haystack.casecmp(needle).zero?
|
2017-09-04 12:26:33 -04:00
|
|
|
end
|
|
|
|
|
2017-09-08 06:00:17 -04:00
|
|
|
def outbox_total_items
|
2020-03-08 19:10:29 -04:00
|
|
|
collection_info('outbox').first
|
2017-09-08 06:00:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def following_total_items
|
2020-03-08 19:10:29 -04:00
|
|
|
collection_info('following').first
|
2017-09-08 06:00:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def followers_total_items
|
2020-03-08 19:10:29 -04:00
|
|
|
collection_info('followers').first
|
2017-09-08 06:00:17 -04:00
|
|
|
end
|
|
|
|
|
2020-03-08 19:10:29 -04: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 06:00:17 -04:00
|
|
|
return @collections[type] if @collections.key?(type)
|
|
|
|
|
2017-10-03 19:13:48 -04:00
|
|
|
collection = fetch_resource_without_id_validation(@json[type])
|
2017-09-08 06:00:17 -04:00
|
|
|
|
2020-03-08 19:10:29 -04: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]
|
2017-09-08 06:00:17 -04:00
|
|
|
rescue HTTP::Error, OpenSSL::SSL::SSLError
|
2020-03-08 19:10:29 -04:00
|
|
|
@collections[type] = [nil, nil]
|
2017-09-08 06:00:17 -04:00
|
|
|
end
|
|
|
|
|
2017-11-18 13:39:02 -05:00
|
|
|
def moved_account
|
|
|
|
account = ActivityPub::TagManager.instance.uri_to_resource(@json['movedTo'], Account)
|
2018-07-28 17:14:55 -04:00
|
|
|
account ||= ActivityPub::FetchRemoteAccountService.new.call(@json['movedTo'], id: true, break_on_redirect: true)
|
2017-11-18 13:39:02 -05:00
|
|
|
account
|
|
|
|
end
|
|
|
|
|
2017-09-09 07:41:45 -04:00
|
|
|
def skip_download?
|
|
|
|
@account.suspended? || domain_block&.reject_media?
|
|
|
|
end
|
|
|
|
|
2017-08-08 15:52:15 -04:00
|
|
|
def auto_suspend?
|
2017-09-19 00:53:16 -04:00
|
|
|
domain_block&.suspend?
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def auto_silence?
|
2017-09-19 00:53:16 -04:00
|
|
|
domain_block&.silence?
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def domain_block
|
|
|
|
return @domain_block if defined?(@domain_block)
|
2019-06-21 18:13:10 -04:00
|
|
|
@domain_block = DomainBlock.rule_for(@domain)
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|
2017-09-19 00:53:16 -04:00
|
|
|
|
|
|
|
def key_changed?
|
|
|
|
!@old_public_key.nil? && @old_public_key != @account.public_key
|
|
|
|
end
|
|
|
|
|
2019-01-18 09:56:55 -05:00
|
|
|
def clear_tombstones!
|
2019-02-05 20:50:52 -05:00
|
|
|
Tombstone.where(account_id: @account.id).delete_all
|
2019-01-18 09:56:55 -05:00
|
|
|
end
|
|
|
|
|
2017-09-19 00:53:16 -04:00
|
|
|
def protocol_changed?
|
|
|
|
!@old_protocol.nil? && @old_protocol != @account.protocol
|
|
|
|
end
|
|
|
|
|
|
|
|
def lock_options
|
|
|
|
{ redis: Redis.current, key: "process_account:#{@uri}" }
|
|
|
|
end
|
2018-04-01 17:55:42 -04:00
|
|
|
|
2018-04-12 19:27:22 -04:00
|
|
|
def process_tags
|
2018-04-01 17:55:42 -04:00
|
|
|
return if @json['tag'].blank?
|
2018-04-12 19:27:22 -04:00
|
|
|
|
2018-04-01 17:55:42 -04:00
|
|
|
as_array(@json['tag']).each do |tag|
|
2018-05-02 06:40:24 -04:00
|
|
|
process_emoji tag if equals_or_includes?(tag['type'], 'Emoji')
|
2018-04-01 17:55:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-29 21:12:06 -04:00
|
|
|
def process_attachments
|
|
|
|
return if @json['attachment'].blank?
|
|
|
|
|
|
|
|
previous_proofs = @account.identity_proofs.to_a
|
|
|
|
current_proofs = []
|
|
|
|
|
|
|
|
as_array(@json['attachment']).each do |attachment|
|
|
|
|
next unless equals_or_includes?(attachment['type'], 'IdentityProof')
|
|
|
|
current_proofs << process_identity_proof(attachment)
|
|
|
|
end
|
|
|
|
|
|
|
|
previous_proofs.each do |previous_proof|
|
|
|
|
next if current_proofs.any? { |current_proof| current_proof.id == previous_proof.id }
|
|
|
|
previous_proof.delete
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-12 19:27:22 -04:00
|
|
|
def process_emoji(tag)
|
2018-04-01 17:55:42 -04: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-10 17:59:51 -05:00
|
|
|
return unless emoji.nil? || image_url != emoji.image_remote_url || (updated && updated >= emoji.updated_at)
|
2018-04-01 17:55:42 -04:00
|
|
|
|
|
|
|
emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
|
|
|
|
emoji.image_remote_url = image_url
|
|
|
|
emoji.save
|
|
|
|
end
|
2019-03-29 21:12:06 -04:00
|
|
|
|
|
|
|
def process_identity_proof(attachment)
|
|
|
|
provider = attachment['signatureAlgorithm']
|
|
|
|
provider_username = attachment['name']
|
|
|
|
token = attachment['signatureValue']
|
|
|
|
|
|
|
|
@account.identity_proofs.where(provider: provider, provider_username: provider_username).find_or_create_by(provider: provider, provider_username: provider_username, token: token)
|
|
|
|
end
|
2017-08-08 15:52:15 -04:00
|
|
|
end
|