From a20354a20b9dffada0e8d6170ebc2ff13c79baea Mon Sep 17 00:00:00 2001 From: ThibG Date: Sun, 17 Mar 2019 15:34:56 +0100 Subject: [PATCH] Set and store report URIs (#10303) Fixes #10271 --- app/lib/activitypub/activity/flag.rb | 7 +++++- app/models/report.rb | 11 +++++++++ .../activitypub/flag_serializer.rb | 1 - app/services/report_service.rb | 3 ++- .../20190317135723_add_uri_to_reports.rb | 5 ++++ db/schema.rb | 3 ++- spec/lib/activitypub/activity/flag_spec.rb | 23 +++++++++++++++++-- spec/services/report_service_spec.rb | 5 ++++ 8 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20190317135723_add_uri_to_reports.rb diff --git a/app/lib/activitypub/activity/flag.rb b/app/lib/activitypub/activity/flag.rb index 0d10d6c3c9..f73b93058f 100644 --- a/app/lib/activitypub/activity/flag.rb +++ b/app/lib/activitypub/activity/flag.rb @@ -14,7 +14,8 @@ class ActivityPub::Activity::Flag < ActivityPub::Activity @account, target_account, status_ids: target_statuses.nil? ? [] : target_statuses.map(&:id), - comment: @json['content'] || '' + comment: @json['content'] || '', + uri: report_uri ) end end @@ -28,4 +29,8 @@ class ActivityPub::Activity::Flag < ActivityPub::Activity def object_uris @object_uris ||= Array(@object.is_a?(Array) ? @object.map { |item| value_or_id(item) } : value_or_id(@object)) end + + def report_uri + @json['id'] unless @json['id'].nil? || invalid_origin?(@json['id']) + end end diff --git a/app/models/report.rb b/app/models/report.rb index 2804020f52..86c303798e 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -13,6 +13,7 @@ # action_taken_by_account_id :bigint(8) # target_account_id :bigint(8) not null # assigned_account_id :bigint(8) +# uri :string # class Report < ApplicationRecord @@ -28,6 +29,12 @@ class Report < ApplicationRecord validates :comment, length: { maximum: 1000 } + def local? + false # Force uri_for to use uri attribute + end + + before_validation :set_uri, only: :create + def object_type :flag end @@ -89,4 +96,8 @@ class Report < ApplicationRecord Admin::ActionLog.from("(#{sql}) AS admin_action_logs") end + + def set_uri + self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local? + end end diff --git a/app/serializers/activitypub/flag_serializer.rb b/app/serializers/activitypub/flag_serializer.rb index 53e8f726db..1e7a46dd9e 100644 --- a/app/serializers/activitypub/flag_serializer.rb +++ b/app/serializers/activitypub/flag_serializer.rb @@ -5,7 +5,6 @@ class ActivityPub::FlagSerializer < ActiveModel::Serializer attribute :virtual_object, key: :object def id - # This is nil for now ActivityPub::TagManager.instance.uri_for(object) end diff --git a/app/services/report_service.rb b/app/services/report_service.rb index 1bcc1c0d56..73bd6694f2 100644 --- a/app/services/report_service.rb +++ b/app/services/report_service.rb @@ -21,7 +21,8 @@ class ReportService < BaseService @report = @source_account.reports.create!( target_account: @target_account, status_ids: @status_ids, - comment: @comment + comment: @comment, + uri: @options[:uri] ) end diff --git a/db/migrate/20190317135723_add_uri_to_reports.rb b/db/migrate/20190317135723_add_uri_to_reports.rb new file mode 100644 index 0000000000..47c0f2a21d --- /dev/null +++ b/db/migrate/20190317135723_add_uri_to_reports.rb @@ -0,0 +1,5 @@ +class AddUriToReports < ActiveRecord::Migration[5.2] + def change + add_column :reports, :uri, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 23ec08238e..790e347c3d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_03_14_181829) do +ActiveRecord::Schema.define(version: 2019_03_17_135723) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -525,6 +525,7 @@ ActiveRecord::Schema.define(version: 2019_03_14_181829) do t.bigint "action_taken_by_account_id" t.bigint "target_account_id", null: false t.bigint "assigned_account_id" + t.string "uri" t.index ["account_id"], name: "index_reports_on_account_id" t.index ["target_account_id"], name: "index_reports_on_target_account_id" end diff --git a/spec/lib/activitypub/activity/flag_spec.rb b/spec/lib/activitypub/activity/flag_spec.rb index 3f082a8137..ec7359f2fe 100644 --- a/spec/lib/activitypub/activity/flag_spec.rb +++ b/spec/lib/activitypub/activity/flag_spec.rb @@ -1,14 +1,15 @@ require 'rails_helper' RSpec.describe ActivityPub::Activity::Flag do - let(:sender) { Fabricate(:account, domain: 'example.com') } + let(:sender) { Fabricate(:account, domain: 'example.com', uri: 'http://example.com/account') } let(:flagged) { Fabricate(:account) } let(:status) { Fabricate(:status, account: flagged, uri: 'foobar') } + let(:flag_id) { nil } let(:json) do { '@context': 'https://www.w3.org/ns/activitystreams', - id: nil, + id: flag_id, type: 'Flag', content: 'Boo!!', actor: ActivityPub::TagManager.instance.uri_for(sender), @@ -34,4 +35,22 @@ RSpec.describe ActivityPub::Activity::Flag do expect(report.status_ids).to eq [status.id] end end + + describe '#perform with a defined uri' do + subject { described_class.new(json, sender) } + let (:flag_id) { 'http://example.com/reports/1' } + + before do + subject.perform + end + + it 'creates a report' do + report = Report.find_by(account: sender, target_account: flagged) + + expect(report).to_not be_nil + expect(report.comment).to eq 'Boo!!' + expect(report.status_ids).to eq [status.id] + expect(report.uri).to eq flag_id + end + end end diff --git a/spec/services/report_service_spec.rb b/spec/services/report_service_spec.rb index e8b094c89b..454e4d896f 100644 --- a/spec/services/report_service_spec.rb +++ b/spec/services/report_service_spec.rb @@ -21,6 +21,11 @@ RSpec.describe ReportService, type: :service do subject.call(source_account, remote_account, forward: false) expect(a_request(:post, 'http://example.com/inbox')).to_not have_been_made end + + it 'has an uri' do + report = subject.call(source_account, remote_account, forward: true) + expect(report.uri).to_not be_nil + end end context 'when other reports already exist for the same target' do