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)
|
2017-09-05 01:26:33 +09:00
|
|
|
return if json['inbox'].blank?
|
2017-08-23 01:30:15 +09:00
|
|
|
|
2017-08-09 04:52:15 +09:00
|
|
|
@json = json
|
|
|
|
@uri = @json['id']
|
|
|
|
@username = username
|
|
|
|
@domain = domain
|
|
|
|
@account = Account.find_by(uri: @uri)
|
|
|
|
|
2017-08-21 08:14:40 +09:00
|
|
|
create_account if @account.nil?
|
|
|
|
upgrade_account if @account.ostatus?
|
2017-08-09 04:52:15 +09:00
|
|
|
update_account
|
|
|
|
|
|
|
|
@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.uri = @uri
|
|
|
|
@account.suspended = true if auto_suspend?
|
|
|
|
@account.silenced = true if auto_silence?
|
|
|
|
@account.private_key = nil
|
|
|
|
@account.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_account
|
|
|
|
@account.last_webfingered_at = Time.now.utc
|
|
|
|
@account.protocol = :activitypub
|
|
|
|
@account.inbox_url = @json['inbox'] || ''
|
|
|
|
@account.outbox_url = @json['outbox'] || ''
|
2017-09-05 01:26:33 +09:00
|
|
|
@account.shared_inbox_url = (@json['endpoints'].is_a?(Hash) ? @json['endpoints']['sharedInbox'] : @json['sharedInbox']) || ''
|
2017-08-09 04:52:15 +09:00
|
|
|
@account.followers_url = @json['followers'] || ''
|
2017-09-05 01:26:33 +09:00
|
|
|
@account.url = url || @uri
|
2017-08-09 04:52:15 +09:00
|
|
|
@account.display_name = @json['name'] || ''
|
|
|
|
@account.note = @json['summary'] || ''
|
|
|
|
@account.avatar_remote_url = image_url('icon')
|
|
|
|
@account.header_remote_url = image_url('image')
|
|
|
|
@account.public_key = public_key || ''
|
2017-09-03 06:13:35 +09:00
|
|
|
@account.locked = @json['manuallyApprovesFollowers'] || false
|
2017-08-09 04:52:15 +09:00
|
|
|
@account.save!
|
|
|
|
end
|
|
|
|
|
2017-08-21 08:14:40 +09:00
|
|
|
def upgrade_account
|
|
|
|
ActivityPub::PostUpgradeWorker.perform_async(@account.domain)
|
|
|
|
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
|
|
|
|
|
|
|
image = fetch_resource(value)
|
|
|
|
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)
|
|
|
|
|
|
|
|
key = fetch_resource(value)
|
|
|
|
key['publicKeyPem'] if key
|
|
|
|
end
|
|
|
|
|
2017-09-05 01:26:33 +09:00
|
|
|
def url
|
|
|
|
return if @json['url'].blank?
|
|
|
|
|
|
|
|
value = first_of_value(@json['url'])
|
|
|
|
|
|
|
|
return value if value.is_a?(String)
|
|
|
|
|
|
|
|
value['href']
|
|
|
|
end
|
|
|
|
|
2017-08-09 04:52:15 +09:00
|
|
|
def auto_suspend?
|
|
|
|
domain_block && domain_block.suspend?
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_silence?
|
|
|
|
domain_block && domain_block.silence?
|
|
|
|
end
|
|
|
|
|
|
|
|
def domain_block
|
|
|
|
return @domain_block if defined?(@domain_block)
|
|
|
|
@domain_block = DomainBlock.find_by(domain: @domain)
|
|
|
|
end
|
|
|
|
end
|