Skip to content

Commit 9198919

Browse files
ClearlyClairehiyuki2578
authored andcommitted
Set and store report URIs (mastodon#10303)
Fixes mastodon#10271
1 parent 22fa1bd commit 9198919

8 files changed

Lines changed: 52 additions & 6 deletions

File tree

app/lib/activitypub/activity/flag.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ def perform
1414
@account,
1515
target_account,
1616
status_ids: target_statuses.nil? ? [] : target_statuses.map(&:id),
17-
comment: @json['content'] || ''
17+
comment: @json['content'] || '',
18+
uri: report_uri
1819
)
1920
end
2021
end
@@ -28,4 +29,8 @@ def skip_reports?
2829
def object_uris
2930
@object_uris ||= Array(@object.is_a?(Array) ? @object.map { |item| value_or_id(item) } : value_or_id(@object))
3031
end
32+
33+
def report_uri
34+
@json['id'] unless @json['id'].nil? || invalid_origin?(@json['id'])
35+
end
3136
end

app/models/report.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# action_taken_by_account_id :bigint(8)
1414
# target_account_id :bigint(8) not null
1515
# assigned_account_id :bigint(8)
16+
# uri :string
1617
#
1718

1819
class Report < ApplicationRecord
@@ -28,6 +29,12 @@ class Report < ApplicationRecord
2829

2930
validates :comment, length: { maximum: 1000 }
3031

32+
def local?
33+
false # Force uri_for to use uri attribute
34+
end
35+
36+
before_validation :set_uri, only: :create
37+
3138
def object_type
3239
:flag
3340
end
@@ -89,4 +96,8 @@ def history
8996

9097
Admin::ActionLog.from("(#{sql}) AS admin_action_logs")
9198
end
99+
100+
def set_uri
101+
self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
102+
end
92103
end

app/serializers/activitypub/flag_serializer.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class ActivityPub::FlagSerializer < ActiveModel::Serializer
55
attribute :virtual_object, key: :object
66

77
def id
8-
# This is nil for now
98
ActivityPub::TagManager.instance.uri_for(object)
109
end
1110

app/services/report_service.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def create_report!
2121
@report = @source_account.reports.create!(
2222
target_account: @target_account,
2323
status_ids: @status_ids,
24-
comment: @comment
24+
comment: @comment,
25+
uri: @options[:uri]
2526
)
2627
end
2728

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddUriToReports < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :reports, :uri, :string
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_03_14_181829) do
13+
ActiveRecord::Schema.define(version: 2019_03_17_135723) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -526,6 +526,7 @@
526526
t.bigint "action_taken_by_account_id"
527527
t.bigint "target_account_id", null: false
528528
t.bigint "assigned_account_id"
529+
t.string "uri"
529530
t.index ["account_id"], name: "index_reports_on_account_id"
530531
t.index ["target_account_id"], name: "index_reports_on_target_account_id"
531532
end

spec/lib/activitypub/activity/flag_spec.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
require 'rails_helper'
22

33
RSpec.describe ActivityPub::Activity::Flag do
4-
let(:sender) { Fabricate(:account, domain: 'example.com') }
4+
let(:sender) { Fabricate(:account, domain: 'example.com', uri: 'http://example.com/account') }
55
let(:flagged) { Fabricate(:account) }
66
let(:status) { Fabricate(:status, account: flagged, uri: 'foobar') }
7+
let(:flag_id) { nil }
78

89
let(:json) do
910
{
1011
'@context': 'https://www.w3.org/ns/activitystreams',
11-
id: nil,
12+
id: flag_id,
1213
type: 'Flag',
1314
content: 'Boo!!',
1415
actor: ActivityPub::TagManager.instance.uri_for(sender),
@@ -34,4 +35,22 @@
3435
expect(report.status_ids).to eq [status.id]
3536
end
3637
end
38+
39+
describe '#perform with a defined uri' do
40+
subject { described_class.new(json, sender) }
41+
let (:flag_id) { 'http://example.com/reports/1' }
42+
43+
before do
44+
subject.perform
45+
end
46+
47+
it 'creates a report' do
48+
report = Report.find_by(account: sender, target_account: flagged)
49+
50+
expect(report).to_not be_nil
51+
expect(report.comment).to eq 'Boo!!'
52+
expect(report.status_ids).to eq [status.id]
53+
expect(report.uri).to eq flag_id
54+
end
55+
end
3756
end

spec/services/report_service_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
subject.call(source_account, remote_account, forward: false)
2222
expect(a_request(:post, 'http://example.com/inbox')).to_not have_been_made
2323
end
24+
25+
it 'has an uri' do
26+
report = subject.call(source_account, remote_account, forward: true)
27+
expect(report.uri).to_not be_nil
28+
end
2429
end
2530

2631
context 'when other reports already exist for the same target' do

0 commit comments

Comments
 (0)