Browse Source

Add tombstones for remote statuses (#9830)

* Add Tombstone model to remember object deletion

* Do not recreate a status if it has been deleted

* Record Tombstone for remote deleted items

Also, only record deleted items from same-host actors

* Clear an user's tombstones when their key change
ThibG 1 month ago
parent
commit
75b1488cf4

+ 1
- 0
app/lib/activitypub/activity/create.rb View File

@@ -6,6 +6,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
6 6
 
7 7
   def perform
8 8
     return if unsupported_object_type? || invalid_origin?(@object['id'])
9
+    return if Tombstone.exists?(uri: @object['id'])
9 10
 
10 11
     RedisLock.acquire(lock_options) do |lock|
11 12
       if lock.acquired?

+ 12
- 2
app/lib/activitypub/activity/delete.rb View File

@@ -21,8 +21,9 @@ class ActivityPub::Activity::Delete < ActivityPub::Activity
21 21
   def delete_note
22 22
     return if object_uri.nil?
23 23
 
24
-    RedisLock.acquire(lock_options) do |_lock|
25
-      delete_later!(object_uri)
24
+    unless invalid_origin?(object_uri)
25
+      RedisLock.acquire(lock_options) { |_lock| delete_later!(object_uri) }
26
+      Tombstone.find_or_create_by(uri: object_uri, account: @account)
26 27
     end
27 28
 
28 29
     @status   = Status.find_by(uri: object_uri, account: @account)
@@ -74,4 +75,13 @@ class ActivityPub::Activity::Delete < ActivityPub::Activity
74 75
   def lock_options
75 76
     { redis: Redis.current, key: "create:#{object_uri}" }
76 77
   end
78
+
79
+  def invalid_origin?(url)
80
+    return true if unsupported_uri_scheme?(url)
81
+
82
+    needle   = Addressable::URI.parse(url).host
83
+    haystack = Addressable::URI.parse(@account.uri).host
84
+
85
+    !haystack.casecmp(needle).zero?
86
+  end
77 87
 end

+ 15
- 0
app/models/tombstone.rb View File

@@ -0,0 +1,15 @@
1
+# frozen_string_literal: true
2
+
3
+# == Schema Information
4
+#
5
+# Table name: tombstones
6
+#
7
+#  id         :bigint(8)        not null, primary key
8
+#  account_id :bigint(8)
9
+#  uri        :string           not null
10
+#  created_at :datetime         not null
11
+#  updated_at :datetime         not null
12
+#
13
+
14
+class Tombstone < ApplicationRecord
15
+end

+ 6
- 0
app/services/activitypub/process_account_service.rb View File

@@ -33,6 +33,8 @@ class ActivityPub::ProcessAccountService < BaseService
33 33
 
34 34
     after_protocol_change! if protocol_changed?
35 35
     after_key_change! if key_changed? && !@options[:signed_with_known_key]
36
+    clear_tombstones! if key_changed?
37
+
36 38
     unless @options[:only_key]
37 39
       check_featured_collection! if @account.featured_collection_url.present?
38 40
       check_links! unless @account.fields.empty?
@@ -209,6 +211,10 @@ class ActivityPub::ProcessAccountService < BaseService
209 211
     !@old_public_key.nil? && @old_public_key != @account.public_key
210 212
   end
211 213
 
214
+  def clear_tombstones!
215
+    Tombstone.delete_all(account_id: @account.id)
216
+  end
217
+
212 218
   def protocol_changed?
213 219
     !@old_protocol.nil? && @old_protocol != @account.protocol
214 220
   end

+ 12
- 0
db/migrate/20190117114553_create_tombstones.rb View File

@@ -0,0 +1,12 @@
1
+class CreateTombstones < ActiveRecord::Migration[5.2]
2
+  def change
3
+    create_table :tombstones do |t|
4
+      t.belongs_to :account, foreign_key: { on_delete: :cascade }
5
+      t.string :uri, null: false
6
+
7
+      t.timestamps
8
+    end
9
+
10
+    add_index :tombstones, :uri
11
+  end
12
+end

+ 11
- 1
db/schema.rb View File

@@ -10,7 +10,7 @@
10 10
 #
11 11
 # It's strongly recommended that you check this file into your version control system.
12 12
 
13
-ActiveRecord::Schema.define(version: 2019_01_03_124754) do
13
+ActiveRecord::Schema.define(version: 2019_01_17_114553) do
14 14
 
15 15
   # These are extensions that must be enabled in order to support this database
16 16
   enable_extension "plpgsql"
@@ -615,6 +615,15 @@ ActiveRecord::Schema.define(version: 2019_01_03_124754) do
615 615
     t.index ["name"], name: "index_tags_on_name", unique: true
616 616
   end
617 617
 
618
+  create_table "tombstones", force: :cascade do |t|
619
+    t.bigint "account_id"
620
+    t.string "uri", null: false
621
+    t.datetime "created_at", null: false
622
+    t.datetime "updated_at", null: false
623
+    t.index ["account_id"], name: "index_tombstones_on_account_id"
624
+    t.index ["uri"], name: "index_tombstones_on_uri"
625
+  end
626
+
618 627
   create_table "users", force: :cascade do |t|
619 628
     t.string "email", default: "", null: false
620 629
     t.datetime "created_at", null: false
@@ -743,6 +752,7 @@ ActiveRecord::Schema.define(version: 2019_01_03_124754) do
743 752
   add_foreign_key "statuses_tags", "tags", name: "fk_3081861e21", on_delete: :cascade
744 753
   add_foreign_key "stream_entries", "accounts", name: "fk_5659b17554", on_delete: :cascade
745 754
   add_foreign_key "subscriptions", "accounts", name: "fk_9847d1cbb5", on_delete: :cascade
755
+  add_foreign_key "tombstones", "accounts", on_delete: :cascade
746 756
   add_foreign_key "users", "accounts", name: "fk_50500f500d", on_delete: :cascade
747 757
   add_foreign_key "users", "invites", on_delete: :nullify
748 758
   add_foreign_key "users", "oauth_applications", column: "created_by_application_id", on_delete: :nullify

Loading…
Cancel
Save