about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2019-03-17 15:34:56 +0100
committerEugen Rochko <eugen@zeonfederated.com>2019-03-17 15:34:56 +0100
commita20354a20b9dffada0e8d6170ebc2ff13c79baea (patch)
tree74d1a47399b0cbd24187562b1a864ae5e984979c
parent5e38ef87a7b8bac59ffa0d98464086ab8a60a2e1 (diff)
Set and store report URIs (#10303)
Fixes #10271
-rw-r--r--app/lib/activitypub/activity/flag.rb7
-rw-r--r--app/models/report.rb11
-rw-r--r--app/serializers/activitypub/flag_serializer.rb1
-rw-r--r--app/services/report_service.rb3
-rw-r--r--db/migrate/20190317135723_add_uri_to_reports.rb5
-rw-r--r--db/schema.rb3
-rw-r--r--spec/lib/activitypub/activity/flag_spec.rb23
-rw-r--r--spec/services/report_service_spec.rb5
8 files changed, 52 insertions, 6 deletions
diff --git a/app/lib/activitypub/activity/flag.rb b/app/lib/activitypub/activity/flag.rb
index 0d10d6c3c..f73b93058 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 2804020f5..86c303798 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 53e8f726d..1e7a46dd9 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 1bcc1c0d5..73bd6694f 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 000000000..47c0f2a21
--- /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 23ec08238..790e347c3 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 3f082a813..ec7359f2f 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 e8b094c89..454e4d896 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