2017-08-09 04:52:15 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::ProcessAccountService < BaseService
|
|
|
|
include JsonLdHelper
|
|
|
|
|
|
|
|
# Should be called with confirmed valid JSON
|
|
|
|
# and WebFinger-resolved username and domain
|
|
|
|
def call(username, domain, json)
|
2018-01-08 13:00:23 +09:00
|
|
|
return if json['inbox'].blank? || unsupported_uri_scheme?(json['id'])
|
2017-08-23 01:30:15 +09:00
|
|
|
|
2017-09-08 19:00:17 +09:00
|
|
|
@json = json
|
|
|
|
@uri = @json['id']
|
|
|
|
@username = username
|
|
|
|
@domain = domain
|
|
|
|
@collections = {}
|
2017-08-09 04:52:15 +09:00
|
|
|
|
2017-09-19 13:53:16 +09:00
|
|
|
RedisLock.acquire(lock_options) do |lock|
|
|
|
|
if lock.acquired?
|
2018-03-20 22:57:46 +09:00
|
|
|
@account = Account.find_remote(@username, @domain)
|
2017-09-19 13:53:16 +09:00
|
|
|
@old_public_key = @account&.public_key
|
|
|
|
@old_protocol = @account&.protocol
|
|
|
|
|
|
|
|
create_account if @account.nil?
|
|
|
|
update_account
|
2018-04-13 08:27:22 +09:00
|
|
|
process_tags
|
2017-09-19 13:53:16 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-13 08:27:22 +09:00
|
|
|
return if @account.nil?
|
|
|
|
|
2017-09-19 13:53:16 +09:00
|
|
|
after_protocol_change! if protocol_changed?
|
|
|
|
after_key_change! if key_changed?
|
2018-04-13 08:27:22 +09:00
|
|
|
check_featured_collection! if @account.featured_collection_url.present?
|
2017-08-09 04:52:15 +09:00
|
|
|
|
|
|
|
@account
|
|
|
|
rescue Oj::ParseError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_account
|
|
|
|
@account = Account.new
|
2017-08-21 08:14:40 +09:00
|
|
|
@account.protocol = :activitypub
|
2017-08-09 04:52:15 +09:00
|
|
|
@account.username = @username
|
|
|
|
@account.domain = @domain
|
|
|
|
@account.suspended = true if auto_suspend?
|
|
|
|
@account.silenced = true if auto_silence?
|
|
|
|
@account.private_key = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_account
|
|
|
|
@account.last_webfingered_at = Time.now.utc
|
|
|
|
@account.protocol = :activitypub
|
2017-09-19 13:53:16 +09:00
|
|
|
|
|
|
|
set_immediate_attributes!
|
|
|
|
set_fetchable_attributes!
|
|
|
|
|
2017-09-09 20:41:45 +09:00
|
|
|
@account.save_with_optional_media!
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
2017-09-19 13:53:16 +09:00
|
|
|
def set_immediate_attributes!
|
2018-03-04 17:19:11 +09: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'] || ''
|
|
|
|
@account.url = url || @uri
|
2018-05-15 05:56:45 +09:00
|
|
|
@account.uri = @uri
|
2018-03-04 17:19:11 +09:00
|
|
|
@account.display_name = @json['name'] || ''
|
|
|
|
@account.note = @json['summary'] || ''
|
|
|
|
@account.locked = @json['manuallyApprovesFollowers'] || false
|
2018-04-14 19:41:08 +09:00
|
|
|
@account.fields = property_values || {}
|
2018-05-08 20:30:04 +09:00
|
|
|
@account.actor_type = actor_type
|
2017-09-19 13:53:16 +09: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?
|
2018-01-03 08:38:20 +09:00
|
|
|
@account.moved_to_account = @json['movedTo'].present? ? moved_account : nil
|
2017-09-19 13:53:16 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def after_protocol_change!
|
2017-08-21 08:14:40 +09:00
|
|
|
ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
|
|
|
|
end
|
|
|
|
|
2017-09-19 13:53:16 +09:00
|
|
|
def after_key_change!
|
|
|
|
RefollowWorker.perform_async(@account.id)
|
|
|
|
end
|
|
|
|
|
2018-03-04 17:19:11 +09:00
|
|
|
def check_featured_collection!
|
|
|
|
ActivityPub::SynchronizeFeaturedCollectionWorker.perform_async(@account.id)
|
|
|
|
end
|
|
|
|
|
2018-05-08 20:30:04 +09: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 04:52:15 +09:00
|
|
|
def image_url(key)
|
|
|
|
value = first_of_value(@json[key])
|
|
|
|
|
|
|
|
return if value.nil?
|
2017-09-05 01:26:33 +09:00
|
|
|
return value['url'] if value.is_a?(Hash)
|
2017-08-09 04:52:15 +09:00
|
|
|
|
2017-10-04 08:13:48 +09:00
|
|
|
image = fetch_resource_without_id_validation(value)
|
2017-08-09 04:52:15 +09: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-04 08:13:48 +09:00
|
|
|
key = fetch_resource_without_id_validation(value)
|
2017-08-09 04:52:15 +09:00
|
|
|
key['publicKeyPem'] if key
|
|
|
|
end
|
|
|
|
|
2017-09-05 01:26:33 +09:00
|
|
|
def url
|
|
|
|
return if @json['url'].blank?
|
2018-01-08 13:00:23 +09: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 19:41:08 +09:00
|
|
|
def property_values
|
|
|
|
return unless @json['attachment'].is_a?(Array)
|
|
|
|
@json['attachment'].select { |attachment| attachment['type'] == 'PropertyValue' }.map { |attachment| attachment.slice('name', 'value') }
|
|
|
|
end
|
|
|
|
|
2018-01-08 13:00:23 +09:00
|
|
|
def mismatching_origin?(url)
|
|
|
|
needle = Addressable::URI.parse(url).host
|
|
|
|
haystack = Addressable::URI.parse(@uri).host
|
|
|
|
|
|
|
|
!haystack.casecmp(needle).zero?
|
2017-09-05 01:26:33 +09:00
|
|
|
end
|
|
|
|
|
2017-09-08 19:00:17 +09:00
|
|
|
def outbox_total_items
|
|
|
|
collection_total_items('outbox')
|
|
|
|
end
|
|
|
|
|
|
|
|
def following_total_items
|
|
|
|
collection_total_items('following')
|
|
|
|
end
|
|
|
|
|
|
|
|
def followers_total_items
|
|
|
|
collection_total_items('followers')
|
|
|
|
end
|
|
|
|
|
|
|
|
def collection_total_items(type)
|
|
|
|
return if @json[type].blank?
|
|
|
|
return @collections[type] if @collections.key?(type)
|
|
|
|
|
2017-10-04 08:13:48 +09:00
|
|
|
collection = fetch_resource_without_id_validation(@json[type])
|
2017-09-08 19:00:17 +09:00
|
|
|
|
|
|
|
@collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
|
|
|
|
rescue HTTP::Error, OpenSSL::SSL::SSLError
|
|
|
|
@collections[type] = nil
|
|
|
|
end
|
|
|
|
|
2017-11-19 03:39:02 +09:00
|
|
|
def moved_account
|
|
|
|
account = ActivityPub::TagManager.instance.uri_to_resource(@json['movedTo'], Account)
|
|
|
|
account ||= ActivityPub::FetchRemoteAccountService.new.call(@json['movedTo'], id: true)
|
|
|
|
account
|
|
|
|
end
|
|
|
|
|
2017-09-09 20:41:45 +09:00
|
|
|
def skip_download?
|
|
|
|
@account.suspended? || domain_block&.reject_media?
|
|
|
|
end
|
|
|
|
|
2017-08-09 04:52:15 +09:00
|
|
|
def auto_suspend?
|
2017-09-19 13:53:16 +09:00
|
|
|
domain_block&.suspend?
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def auto_silence?
|
2017-09-19 13:53:16 +09:00
|
|
|
domain_block&.silence?
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def domain_block
|
|
|
|
return @domain_block if defined?(@domain_block)
|
|
|
|
@domain_block = DomainBlock.find_by(domain: @domain)
|
|
|
|
end
|
2017-09-19 13:53:16 +09:00
|
|
|
|
|
|
|
def key_changed?
|
|
|
|
!@old_public_key.nil? && @old_public_key != @account.public_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def protocol_changed?
|
|
|
|
!@old_protocol.nil? && @old_protocol != @account.protocol
|
|
|
|
end
|
|
|
|
|
|
|
|
def lock_options
|
|
|
|
{ redis: Redis.current, key: "process_account:#{@uri}" }
|
|
|
|
end
|
2018-04-02 06:55:42 +09:00
|
|
|
|
2018-04-13 08:27:22 +09:00
|
|
|
def process_tags
|
2018-04-02 06:55:42 +09:00
|
|
|
return if @json['tag'].blank?
|
2018-04-13 08:27:22 +09:00
|
|
|
|
2018-04-02 06:55:42 +09:00
|
|
|
as_array(@json['tag']).each do |tag|
|
2018-05-02 19:40:24 +09:00
|
|
|
process_emoji tag if equals_or_includes?(tag['type'], 'Emoji')
|
2018-04-02 06:55:42 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-13 08:27:22 +09:00
|
|
|
def process_emoji(tag)
|
2018-04-02 06:55:42 +09: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)
|
|
|
|
|
|
|
|
return unless emoji.nil? || emoji.updated_at >= updated
|
|
|
|
|
|
|
|
emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
|
|
|
|
emoji.image_remote_url = image_url
|
|
|
|
emoji.save
|
|
|
|
end
|
2017-08-09 04:52:15 +09:00
|
|
|
end
|