Skip to content

Commit

Permalink
Set and store report URIs (#10303)
Browse files Browse the repository at this point in the history
Fixes #10271
  • Loading branch information
ClearlyClaire authored and Gargron committed Mar 17, 2019
1 parent 5e38ef8 commit a20354a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 6 deletions.
7 changes: 6 additions & 1 deletion app/lib/activitypub/activity/flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def perform
@account,
target_account,
status_ids: target_statuses.nil? ? [] : target_statuses.map(&:id),
comment: @json['content'] || ''
comment: @json['content'] || '',
uri: report_uri
)
end
end
Expand All @@ -28,4 +29,8 @@ def skip_reports?
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
11 changes: 11 additions & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -89,4 +96,8 @@ def history

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
1 change: 0 additions & 1 deletion app/serializers/activitypub/flag_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion app/services/report_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def create_report!
@report = @source_account.reports.create!(
target_account: @target_account,
status_ids: @status_ids,
comment: @comment
comment: @comment,
uri: @options[:uri]
)
end

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20190317135723_add_uri_to_reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUriToReports < ActiveRecord::Migration[5.2]
def change
add_column :reports, :uri, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -525,6 +525,7 @@
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
Expand Down
23 changes: 21 additions & 2 deletions spec/lib/activitypub/activity/flag_spec.rb
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -34,4 +35,22 @@
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
5 changes: 5 additions & 0 deletions spec/services/report_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
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
Expand Down

0 comments on commit a20354a

Please sign in to comment.