From 11fe293e1b318a12b75f0c5d1bb208fdbb46417e Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 27 Mar 2019 15:55:23 +0100 Subject: Remove unused ActivityPub `@context` values depending on response (#10378) Fix #8078 --- app/lib/activitypub/adapter.rb | 75 +++++++++++++++++++++++++-------------- app/lib/activitypub/serializer.rb | 30 ++++++++++++++++ 2 files changed, 78 insertions(+), 27 deletions(-) create mode 100644 app/lib/activitypub/serializer.rb (limited to 'app/lib/activitypub') diff --git a/app/lib/activitypub/adapter.rb b/app/lib/activitypub/adapter.rb index 99f4d9305..7e0b16c25 100644 --- a/app/lib/activitypub/adapter.rb +++ b/app/lib/activitypub/adapter.rb @@ -1,30 +1,23 @@ # frozen_string_literal: true class ActivityPub::Adapter < ActiveModelSerializers::Adapter::Base - CONTEXT = { - '@context': [ - 'https://www.w3.org/ns/activitystreams', - 'https://w3id.org/security/v1', - - { - 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', - 'sensitive' => 'as:sensitive', - 'movedTo' => { '@id' => 'as:movedTo', '@type' => '@id' }, - 'alsoKnownAs' => { '@id' => 'as:alsoKnownAs', '@type' => '@id' }, - 'Hashtag' => 'as:Hashtag', - 'ostatus' => 'http://ostatus.org#', - 'atomUri' => 'ostatus:atomUri', - 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri', - 'conversation' => 'ostatus:conversation', - 'toot' => 'http://joinmastodon.org/ns#', - 'Emoji' => 'toot:Emoji', - 'focalPoint' => { '@container' => '@list', '@id' => 'toot:focalPoint' }, - 'featured' => { '@id' => 'toot:featured', '@type' => '@id' }, - 'schema' => 'http://schema.org#', - 'PropertyValue' => 'schema:PropertyValue', - 'value' => 'schema:value', - }, - ], + NAMED_CONTEXT_MAP = { + activitystreams: 'https://www.w3.org/ns/activitystreams', + security: 'https://w3id.org/security/v1', + }.freeze + + CONTEXT_EXTENSION_MAP = { + manually_approves_followers: { 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers' }, + sensitive: { 'sensitive' => 'as:sensitive' }, + hashtag: { 'Hashtag' => 'as:Hashtag' }, + moved_to: { 'movedTo' => { '@id' => 'as:movedTo', '@type' => '@id' } }, + also_known_as: { 'alsoKnownAs' => { '@id' => 'as:alsoKnownAs', '@type' => '@id' } }, + emoji: { 'toot' => 'http://joinmastodon.org/ns#', 'Emoji' => 'toot:Emoji' }, + featured: { 'toot' => 'http://joinmastodon.org/ns#', 'featured' => { '@id' => 'toot:featured', '@type' => '@id' } }, + property_value: { 'schema' => 'http://schema.org#', 'PropertyValue' => 'schema:PropertyValue', 'value' => 'schema:value' }, + atom_uri: { 'ostatus' => 'http://ostatus.org#', 'atomUri' => 'ostatus:atomUri' }, + conversation: { 'ostatus' => 'http://ostatus.org#', 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri', 'conversation' => 'ostatus:conversation' }, + focal_point: { 'toot' => 'http://joinmastodon.org/ns#', 'focalPoint' => { '@container' => '@list', '@id' => 'toot:focalPoint' } }, }.freeze def self.default_key_transform @@ -36,8 +29,36 @@ class ActivityPub::Adapter < ActiveModelSerializers::Adapter::Base end def serializable_hash(options = nil) - options = serialization_options(options) - serialized_hash = ActiveModelSerializers::Adapter::Attributes.new(serializer, instance_options).serializable_hash(options) - CONTEXT.merge(self.class.transform_key_casing!(serialized_hash, instance_options)) + options = serialization_options(options) + serialized_hash = serializer.serializable_hash(options) + serialized_hash = self.class.transform_key_casing!(serialized_hash, instance_options) + + { '@context' => serialized_context }.merge(serialized_hash) + end + + private + + def serialized_context + context_array = [] + + serializer_options = serializer.send(:instance_options) || {} + named_contexts = [:activitystreams] + serializer._named_contexts.keys + serializer_options.fetch(:named_contexts, {}).keys + context_extensions = serializer._context_extensions.keys + serializer_options.fetch(:context_extensions, {}).keys + + named_contexts.each do |key| + context_array << NAMED_CONTEXT_MAP[key] + end + + extensions = context_extensions.each_with_object({}) do |key, h| + h.merge!(CONTEXT_EXTENSION_MAP[key]) + end + + context_array << extensions unless extensions.empty? + + if context_array.size == 1 + context_array.first + else + context_array + end end end diff --git a/app/lib/activitypub/serializer.rb b/app/lib/activitypub/serializer.rb new file mode 100644 index 000000000..07bd8c494 --- /dev/null +++ b/app/lib/activitypub/serializer.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class ActivityPub::Serializer < ActiveModel::Serializer + with_options instance_writer: false, instance_reader: true do |serializer| + serializer.class_attribute :_named_contexts + serializer.class_attribute :_context_extensions + + self._named_contexts ||= {} + self._context_extensions ||= {} + end + + def self.inherited(base) + super + + base._named_contexts = _named_contexts.dup + base._context_extensions = _context_extensions.dup + end + + def self.context(*named_contexts) + named_contexts.each do |context| + _named_contexts[context] = true + end + end + + def self.context_extensions(*extension_names) + extension_names.each do |extension_name| + _context_extensions[extension_name] = true + end + end +end -- cgit From e86663b1da9c31b57baf244effb94c063e10a424 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 27 Mar 2019 19:58:24 +0100 Subject: Fix alternative relay support regression (#10398) Fix #10324 --- app/lib/activitypub/activity/announce.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'app/lib/activitypub') diff --git a/app/lib/activitypub/activity/announce.rb b/app/lib/activitypub/activity/announce.rb index 9f8ffd9fb..1aa6ee9ec 100644 --- a/app/lib/activitypub/activity/announce.rb +++ b/app/lib/activitypub/activity/announce.rb @@ -47,6 +47,10 @@ class ActivityPub::Activity::Announce < ActivityPub::Activity followed_by_local_accounts? || requested_through_relay? || reblog_of_local_status? end + def requested_through_relay? + super || Relay.find_by(inbox_url: @account.inbox_url)&.enabled? + end + def reblog_of_local_status? status_from_uri(object_uri)&.account&.local? end -- cgit From f1bc90ab508cbdebc646324f87db48a9e80036f4 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 28 Mar 2019 04:44:59 +0100 Subject: Rename :poll to :preloadable_poll and :owned_poll to :poll on Status (#10401) Also, fix some n+1 queries Resolve #10365 --- app/helpers/stream_entries_helper.rb | 4 ++-- app/lib/activitypub/activity/create.rb | 10 +++++----- app/lib/activitypub/activity/update.rb | 4 ++-- app/lib/formatter.rb | 4 ++-- app/models/notification.rb | 2 +- app/models/status.rb | 19 ++++++++++--------- app/serializers/activitypub/note_serializer.rb | 14 +++++++------- app/serializers/activitypub/update_poll_serializer.rb | 2 +- app/serializers/rest/status_serializer.rb | 2 +- app/services/post_status_service.rb | 2 +- app/views/stream_entries/_detailed_status.html.haml | 6 +++--- app/views/stream_entries/_simple_status.html.haml | 6 +++--- .../activitypub/distribute_poll_update_worker.rb | 4 ++-- config/locales/activerecord.en.yml | 5 +++-- spec/lib/activitypub/activity/create_spec.rb | 4 ++-- 15 files changed, 45 insertions(+), 43 deletions(-) (limited to 'app/lib/activitypub') diff --git a/app/helpers/stream_entries_helper.rb b/app/helpers/stream_entries_helper.rb index 8392afa73..4734e32a4 100644 --- a/app/helpers/stream_entries_helper.rb +++ b/app/helpers/stream_entries_helper.rb @@ -105,8 +105,8 @@ module StreamEntriesHelper end def poll_summary(status) - return unless status.poll - status.poll.options.map { |o| "[ ] #{o}" }.join("\n") + return unless status.preloadable_poll + status.preloadable_poll.options.map { |o| "[ ] #{o}" }.join("\n") end def status_description(status) diff --git a/app/lib/activitypub/activity/create.rb b/app/lib/activitypub/activity/create.rb index 8fe7b9138..dabdcbcf7 100644 --- a/app/lib/activitypub/activity/create.rb +++ b/app/lib/activitypub/activity/create.rb @@ -68,7 +68,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity thread: replied_to_status, conversation: conversation_from_uri(@object['conversation']), media_attachment_ids: process_attachments.take(4).map(&:id), - owned_poll: process_poll, + poll: process_poll, } end end @@ -240,11 +240,11 @@ class ActivityPub::Activity::Create < ActivityPub::Activity end def poll_vote? - return false if replied_to_status.nil? || replied_to_status.poll.nil? || !replied_to_status.local? || !replied_to_status.poll.options.include?(@object['name']) + return false if replied_to_status.nil? || replied_to_status.preloadable_poll.nil? || !replied_to_status.local? || !replied_to_status.preloadable_poll.options.include?(@object['name']) - unless replied_to_status.poll.expired? - replied_to_status.poll.votes.create!(account: @account, choice: replied_to_status.poll.options.index(@object['name']), uri: @object['id']) - ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, replied_to_status.id) unless replied_to_status.poll.hide_totals? + unless replied_to_status.preloadable_poll.expired? + replied_to_status.preloadable_poll.votes.create!(account: @account, choice: replied_to_status.preloadable_poll.options.index(@object['name']), uri: @object['id']) + ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, replied_to_status.id) unless replied_to_status.preloadable_poll.hide_totals? end true diff --git a/app/lib/activitypub/activity/update.rb b/app/lib/activitypub/activity/update.rb index bc9a63f98..70035325b 100644 --- a/app/lib/activitypub/activity/update.rb +++ b/app/lib/activitypub/activity/update.rb @@ -23,8 +23,8 @@ class ActivityPub::Activity::Update < ActivityPub::Activity return reject_payload! if invalid_origin?(@object['id']) status = Status.find_by(uri: object_uri, account_id: @account.id) - return if status.nil? || status.poll.nil? + return if status.nil? || status.preloadable_poll.nil? - ActivityPub::ProcessPollService.new.call(status.poll, @object) + ActivityPub::ProcessPollService.new.call(status.preloadable_poll, @object) end end diff --git a/app/lib/formatter.rb b/app/lib/formatter.rb index aadf03b2a..59dfc9004 100644 --- a/app/lib/formatter.rb +++ b/app/lib/formatter.rb @@ -19,8 +19,8 @@ class Formatter raw_content = status.text - if options[:inline_poll_options] && status.poll - raw_content = raw_content + "\n\n" + status.poll.options.map { |title| "[ ] #{title}" }.join("\n") + if options[:inline_poll_options] && status.preloadable_poll + raw_content = raw_content + "\n\n" + status.preloadable_poll.options.map { |title| "[ ] #{title}" }.join("\n") end return '' if raw_content.blank? diff --git a/app/models/notification.rb b/app/models/notification.rb index 982136c05..300269e24 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -25,7 +25,7 @@ class Notification < ApplicationRecord poll: 'Poll', }.freeze - STATUS_INCLUDES = [:account, :application, :media_attachments, :tags, active_mentions: :account, reblog: [:account, :application, :media_attachments, :tags, active_mentions: :account]].freeze + STATUS_INCLUDES = [:account, :application, :preloadable_poll, :media_attachments, :tags, active_mentions: :account, reblog: [:account, :application, :preloadable_poll, :media_attachments, :tags, active_mentions: :account]].freeze belongs_to :account, optional: true belongs_to :from_account, class_name: 'Account', optional: true diff --git a/app/models/status.rb b/app/models/status.rb index d3fb83cca..8d31fd382 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -45,7 +45,7 @@ class Status < ApplicationRecord belongs_to :account, inverse_of: :statuses belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account', optional: true belongs_to :conversation, optional: true - belongs_to :poll, optional: true + belongs_to :preloadable_poll, class_name: 'Poll', foreign_key: 'poll_id', optional: true belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies, optional: true belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, optional: true @@ -63,7 +63,7 @@ class Status < ApplicationRecord has_one :notification, as: :activity, dependent: :destroy has_one :stream_entry, as: :activity, inverse_of: :status has_one :status_stat, inverse_of: :status - has_one :owned_poll, class_name: 'Poll', inverse_of: :status, dependent: :destroy + has_one :poll, inverse_of: :status, dependent: :destroy validates :uri, uniqueness: true, presence: true, unless: :local? validates :text, presence: true, unless: -> { with_media? || reblog? } @@ -72,7 +72,7 @@ class Status < ApplicationRecord validates :reblog, uniqueness: { scope: :account }, if: :reblog? validates :visibility, exclusion: { in: %w(direct limited) }, if: :reblog? - accepts_nested_attributes_for :owned_poll + accepts_nested_attributes_for :poll default_scope { recent } @@ -107,7 +107,7 @@ class Status < ApplicationRecord :tags, :preview_cards, :stream_entry, - :poll, + :preloadable_poll, account: :account_stat, active_mentions: { account: :account_stat }, reblog: [ @@ -118,7 +118,7 @@ class Status < ApplicationRecord :media_attachments, :conversation, :status_stat, - :poll, + :preloadable_poll, account: :account_stat, active_mentions: { account: :account_stat }, ], @@ -214,10 +214,11 @@ class Status < ApplicationRecord def emojis return @emojis if defined?(@emojis) - fields = [spoiler_text, text] - fields += owned_poll.options unless owned_poll.nil? + + fields = [spoiler_text, text] + fields += preloadable_poll.options unless preloadable_poll.nil? + @emojis = CustomEmoji.from_text(fields.join(' '), account.domain) - @emojis end def mark_for_mass_destruction! @@ -453,7 +454,7 @@ class Status < ApplicationRecord end def set_poll_id - update_column(:poll_id, owned_poll.id) unless owned_poll.nil? + update_column(:poll_id, poll.id) unless poll.nil? end def set_visibility diff --git a/app/serializers/activitypub/note_serializer.rb b/app/serializers/activitypub/note_serializer.rb index 0666bea5a..d11cfa59a 100644 --- a/app/serializers/activitypub/note_serializer.rb +++ b/app/serializers/activitypub/note_serializer.rb @@ -29,7 +29,7 @@ class ActivityPub::NoteSerializer < ActivityPub::Serializer end def type - object.poll ? 'Question' : 'Note' + object.preloadable_poll ? 'Question' : 'Note' end def summary @@ -125,29 +125,29 @@ class ActivityPub::NoteSerializer < ActivityPub::Serializer end def poll_options - object.poll.loaded_options + object.preloadable_poll.loaded_options end def poll_and_multiple? - object.poll&.multiple? + object.preloadable_poll&.multiple? end def poll_and_not_multiple? - object.poll && !object.poll.multiple? + object.preloadable_poll && !object.preloadable_poll.multiple? end def closed - object.poll.expires_at.iso8601 + object.preloadable_poll.expires_at.iso8601 end alias end_time closed def poll_and_expires? - object.poll&.expires_at&.present? + object.preloadable_poll&.expires_at&.present? end def poll_and_expired? - object.poll&.expired? + object.preloadable_poll&.expired? end class MediaAttachmentSerializer < ActivityPub::Serializer diff --git a/app/serializers/activitypub/update_poll_serializer.rb b/app/serializers/activitypub/update_poll_serializer.rb index a9a09747f..b894f309f 100644 --- a/app/serializers/activitypub/update_poll_serializer.rb +++ b/app/serializers/activitypub/update_poll_serializer.rb @@ -6,7 +6,7 @@ class ActivityPub::UpdatePollSerializer < ActivityPub::Serializer has_one :object, serializer: ActivityPub::NoteSerializer def id - [ActivityPub::TagManager.instance.uri_for(object), '#updates/', object.poll.updated_at.to_i].join + [ActivityPub::TagManager.instance.uri_for(object), '#updates/', object.preloadable_poll.updated_at.to_i].join end def type diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb index 30edf397b..106777b6e 100644 --- a/app/serializers/rest/status_serializer.rb +++ b/app/serializers/rest/status_serializer.rb @@ -21,7 +21,7 @@ class REST::StatusSerializer < ActiveModel::Serializer has_many :emojis, serializer: REST::CustomEmojiSerializer has_one :preview_card, key: :card, serializer: REST::PreviewCardSerializer - has_one :poll, serializer: REST::PollSerializer + has_one :preloadable_poll, key: :poll, serializer: REST::PollSerializer def id object.id.to_s diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb index 3f392a6e6..e7366c7e8 100644 --- a/app/services/post_status_service.rb +++ b/app/services/post_status_service.rb @@ -155,7 +155,7 @@ class PostStatusService < BaseService text: @text, media_attachments: @media || [], thread: @in_reply_to, - owned_poll_attributes: poll_attributes, + poll_attributes: poll_attributes, sensitive: (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?, spoiler_text: @options[:spoiler_text] || '', visibility: @visibility, diff --git a/app/views/stream_entries/_detailed_status.html.haml b/app/views/stream_entries/_detailed_status.html.haml index d18ecd37a..4459581d9 100644 --- a/app/views/stream_entries/_detailed_status.html.haml +++ b/app/views/stream_entries/_detailed_status.html.haml @@ -22,9 +22,9 @@ %a.status__content__spoiler-link{ href: '#' }= t('statuses.show_more') .e-content{ lang: status.language, style: "display: #{!current_account&.user&.setting_expand_spoilers && status.spoiler_text? ? 'none' : 'block'}; direction: #{rtl_status?(status) ? 'rtl' : 'ltr'}" }= Formatter.instance.format(status, custom_emojify: true, autoplay: autoplay) - - if status.poll - = react_component :poll, disabled: true, poll: ActiveModelSerializers::SerializableResource.new(status.poll, serializer: REST::PollSerializer, scope: current_user, scope_name: :current_user).as_json do - = render partial: 'stream_entries/poll', locals: { status: status, poll: status.poll, autoplay: autoplay } + - if status.preloadable_poll + = react_component :poll, disabled: true, poll: ActiveModelSerializers::SerializableResource.new(status.preloadable_poll, serializer: REST::PollSerializer, scope: current_user, scope_name: :current_user).as_json do + = render partial: 'stream_entries/poll', locals: { status: status, poll: status.preloadable_poll, autoplay: autoplay } - elsif !status.media_attachments.empty? - if status.media_attachments.first.video? - video = status.media_attachments.first diff --git a/app/views/stream_entries/_simple_status.html.haml b/app/views/stream_entries/_simple_status.html.haml index a499a8634..ba22c5340 100644 --- a/app/views/stream_entries/_simple_status.html.haml +++ b/app/views/stream_entries/_simple_status.html.haml @@ -26,9 +26,9 @@ %a.status__content__spoiler-link{ href: '#' }= t('statuses.show_more') .e-content{ lang: status.language, style: "display: #{!current_account&.user&.setting_expand_spoilers && status.spoiler_text? ? 'none' : 'block'}; direction: #{rtl_status?(status) ? 'rtl' : 'ltr'}" }= Formatter.instance.format(status, custom_emojify: true, autoplay: autoplay) - - if status.poll - = react_component :poll, disabled: true, poll: ActiveModelSerializers::SerializableResource.new(status.poll, serializer: REST::PollSerializer, scope: current_user, scope_name: :current_user).as_json do - = render partial: 'stream_entries/poll', locals: { status: status, poll: status.poll, autoplay: autoplay } + - if status.preloadable_poll + = react_component :poll, disabled: true, poll: ActiveModelSerializers::SerializableResource.new(status.preloadable_poll, serializer: REST::PollSerializer, scope: current_user, scope_name: :current_user).as_json do + = render partial: 'stream_entries/poll', locals: { status: status, poll: status.preloadable_poll, autoplay: autoplay } - elsif !status.media_attachments.empty? - if status.media_attachments.first.video? - video = status.media_attachments.first diff --git a/app/workers/activitypub/distribute_poll_update_worker.rb b/app/workers/activitypub/distribute_poll_update_worker.rb index d60fde557..5eaca6fda 100644 --- a/app/workers/activitypub/distribute_poll_update_worker.rb +++ b/app/workers/activitypub/distribute_poll_update_worker.rb @@ -9,7 +9,7 @@ class ActivityPub::DistributePollUpdateWorker @status = Status.find(status_id) @account = @status.account - return unless @status.poll + return unless @status.preloadable_poll ActivityPub::DeliveryWorker.push_bulk(inboxes) do |inbox_url| [payload, @account.id, inbox_url] @@ -29,7 +29,7 @@ class ActivityPub::DistributePollUpdateWorker def inboxes return @inboxes if defined?(@inboxes) - @inboxes = [@status.mentions, @status.reblogs, @status.poll.votes].flat_map do |relation| + @inboxes = [@status.mentions, @status.reblogs, @status.preloadable_poll.votes].flat_map do |relation| relation.includes(:account).map do |record| record.account.preferred_inbox_url if !record.account.local? && record.account.activitypub? end diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 561ce68b8..8533418cc 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -2,8 +2,9 @@ en: activerecord: attributes: - status: - owned_poll: Poll + poll: + expires_at: Deadline + options: Choices errors: models: account: diff --git a/spec/lib/activitypub/activity/create_spec.rb b/spec/lib/activitypub/activity/create_spec.rb index 3a1463d95..412609de4 100644 --- a/spec/lib/activitypub/activity/create_spec.rb +++ b/spec/lib/activitypub/activity/create_spec.rb @@ -464,7 +464,7 @@ RSpec.describe ActivityPub::Activity::Create do context 'when a vote to a local poll' do let(:poll) { Fabricate(:poll, options: %w(Yellow Blue)) } - let!(:local_status) { Fabricate(:status, owned_poll: poll) } + let!(:local_status) { Fabricate(:status, poll: poll) } let(:object_json) do { @@ -489,7 +489,7 @@ RSpec.describe ActivityPub::Activity::Create do poll.save(validate: false) poll end - let!(:local_status) { Fabricate(:status, owned_poll: poll) } + let!(:local_status) { Fabricate(:status, poll: poll) } let(:object_json) do { -- cgit From 1714ea597866556ef9dd21d5d382f1d9181e0924 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 30 Mar 2019 02:12:06 +0100 Subject: Add ActivityPub representation for identity proofs (#10414) * Add ActivityPub representation for identity proofs * Add tests --- app/lib/activitypub/adapter.rb | 1 + app/lib/proof_provider/keybase.rb | 3 +- app/serializers/activitypub/actor_serializer.rb | 24 +++++++++++-- .../activitypub/process_account_service.rb | 28 ++++++++++++++- .../activitypub/process_account_service_spec.rb | 41 ++++++++++++++++++++++ 5 files changed, 93 insertions(+), 4 deletions(-) (limited to 'app/lib/activitypub') diff --git a/app/lib/activitypub/adapter.rb b/app/lib/activitypub/adapter.rb index 7e0b16c25..94eb2899c 100644 --- a/app/lib/activitypub/adapter.rb +++ b/app/lib/activitypub/adapter.rb @@ -18,6 +18,7 @@ class ActivityPub::Adapter < ActiveModelSerializers::Adapter::Base atom_uri: { 'ostatus' => 'http://ostatus.org#', 'atomUri' => 'ostatus:atomUri' }, conversation: { 'ostatus' => 'http://ostatus.org#', 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri', 'conversation' => 'ostatus:conversation' }, focal_point: { 'toot' => 'http://joinmastodon.org/ns#', 'focalPoint' => { '@container' => '@list', '@id' => 'toot:focalPoint' } }, + identity_proof: { 'toot' => 'http://joinmastodon.org/ns#', 'IdentityProof' => 'toot:IdentityProof' }, }.freeze def self.default_key_transform diff --git a/app/lib/proof_provider/keybase.rb b/app/lib/proof_provider/keybase.rb index 672e1cb4b..628972e9d 100644 --- a/app/lib/proof_provider/keybase.rb +++ b/app/lib/proof_provider/keybase.rb @@ -28,7 +28,8 @@ class ProofProvider::Keybase return end - return if @proof.provider_username.blank? + # Do not perform synchronous validation for remote accounts + return if @proof.provider_username.blank? || !@proof.account.local? if verifier.valid? @proof.verified = true diff --git a/app/serializers/activitypub/actor_serializer.rb b/app/serializers/activitypub/actor_serializer.rb index 4b982b955..0644219fb 100644 --- a/app/serializers/activitypub/actor_serializer.rb +++ b/app/serializers/activitypub/actor_serializer.rb @@ -6,7 +6,7 @@ class ActivityPub::ActorSerializer < ActivityPub::Serializer context :security context_extensions :manually_approves_followers, :featured, :also_known_as, - :moved_to, :property_value, :hashtag, :emoji + :moved_to, :property_value, :hashtag, :emoji, :identity_proof attributes :id, :type, :following, :followers, :inbox, :outbox, :featured, @@ -115,7 +115,7 @@ class ActivityPub::ActorSerializer < ActivityPub::Serializer end def virtual_attachments - object.fields + object.fields + object.identity_proofs.active end def moved_to @@ -158,4 +158,24 @@ class ActivityPub::ActorSerializer < ActivityPub::Serializer Formatter.instance.format_field(object.account, object.value) end end + + class AccountIdentityProofSerializer < ActivityPub::Serializer + attributes :type, :name, :signature_algorithm, :signature_value + + def type + 'IdentityProof' + end + + def name + object.provider_username + end + + def signature_algorithm + object.provider + end + + def signature_value + object.token + end + end end diff --git a/app/services/activitypub/process_account_service.rb b/app/services/activitypub/process_account_service.rb index 5e3308428..6d0609ca0 100644 --- a/app/services/activitypub/process_account_service.rb +++ b/app/services/activitypub/process_account_service.rb @@ -24,6 +24,7 @@ class ActivityPub::ProcessAccountService < BaseService create_account if @account.nil? update_account process_tags + process_attachments else raise Mastodon::RaceConditionError end @@ -151,7 +152,7 @@ class ActivityPub::ProcessAccountService < BaseService def property_values return unless @json['attachment'].is_a?(Array) - @json['attachment'].select { |attachment| attachment['type'] == 'PropertyValue' }.map { |attachment| attachment.slice('name', 'value') } + as_array(@json['attachment']).select { |attachment| attachment['type'] == 'PropertyValue' }.map { |attachment| attachment.slice('name', 'value') } end def mismatching_origin?(url) @@ -231,6 +232,23 @@ class ActivityPub::ProcessAccountService < BaseService end end + 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 + def process_emoji(tag) return if skip_download? return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank? @@ -247,4 +265,12 @@ class ActivityPub::ProcessAccountService < BaseService emoji.image_remote_url = image_url emoji.save end + + 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 end diff --git a/spec/services/activitypub/process_account_service_spec.rb b/spec/services/activitypub/process_account_service_spec.rb index d3318b2ed..ab8fb5cc3 100644 --- a/spec/services/activitypub/process_account_service_spec.rb +++ b/spec/services/activitypub/process_account_service_spec.rb @@ -28,4 +28,45 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do expect(account.fields[1].value).to eq 'Unit test' end end + + context 'identity proofs' do + let(:payload) do + { + id: 'https://foo.test', + type: 'Actor', + inbox: 'https://foo.test/inbox', + attachment: [ + { type: 'IdentityProof', name: 'Alice', signatureAlgorithm: 'keybase', signatureValue: 'a' * 66 }, + ], + }.with_indifferent_access + end + + it 'parses out of attachment' do + account = subject.call('alice', 'example.com', payload) + + expect(account.identity_proofs.count).to eq 1 + + proof = account.identity_proofs.first + + expect(proof.provider).to eq 'keybase' + expect(proof.provider_username).to eq 'Alice' + expect(proof.token).to eq 'a' * 66 + end + + it 'removes no longer present proofs' do + account = Fabricate(:account, username: 'alice', domain: 'example.com') + old_proof = Fabricate(:account_identity_proof, account: account, provider: 'keybase', provider_username: 'Bob', token: 'b' * 66) + + subject.call('alice', 'example.com', payload) + + expect(account.identity_proofs.count).to eq 1 + expect(account.identity_proofs.find_by(id: old_proof.id)).to be_nil + end + + it 'queues a validity check on the proof' do + allow(ProofProvider::Keybase::Worker).to receive(:perform_async) + account = subject.call('alice', 'example.com', payload) + expect(ProofProvider::Keybase::Worker).to have_received(:perform_async) + end + end end -- cgit