Skip to content

Commit df8caf8

Browse files
ClearlyClairehiyuki2578
authored andcommitted
Add tombstones for remote statuses (mastodon#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
1 parent 51e4208 commit df8caf8

6 files changed

Lines changed: 57 additions & 3 deletions

File tree

app/lib/activitypub/activity/create.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
66

77
def perform
88
return if unsupported_object_type? || invalid_origin?(@object['id'])
9+
return if Tombstone.exists?(uri: @object['id'])
910

1011
RedisLock.acquire(lock_options) do |lock|
1112
if lock.acquired?

app/lib/activitypub/activity/delete.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ def delete_person
2121
def delete_note
2222
return if object_uri.nil?
2323

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)
2627
end
2728

2829
@status = Status.find_by(uri: object_uri, account: @account)
@@ -74,4 +75,13 @@ def payload
7475
def lock_options
7576
{ redis: Redis.current, key: "create:#{object_uri}" }
7677
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
7787
end

app/models/tombstone.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

app/services/activitypub/process_account_service.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def call(username, domain, json, options = {})
3333

3434
after_protocol_change! if protocol_changed?
3535
after_key_change! if key_changed? && !@options[:signed_with_known_key]
36+
clear_tombstones! if key_changed?
37+
3638
unless @options[:only_key]
3739
check_featured_collection! if @account.featured_collection_url.present?
3840
check_links! unless @account.fields.empty?
@@ -209,6 +211,10 @@ def key_changed?
209211
!@old_public_key.nil? && @old_public_key != @account.public_key
210212
end
211213

214+
def clear_tombstones!
215+
Tombstone.delete_all(account_id: @account.id)
216+
end
217+
212218
def protocol_changed?
213219
!@old_protocol.nil? && @old_protocol != @account.protocol
214220
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

db/schema.rb

Lines changed: 11 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_01_03_124754) do
13+
ActiveRecord::Schema.define(version: 2019_01_17_114553) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -616,6 +616,15 @@
616616
t.index ["name"], name: "index_tags_on_name", unique: true
617617
end
618618

619+
create_table "tombstones", force: :cascade do |t|
620+
t.bigint "account_id"
621+
t.string "uri", null: false
622+
t.datetime "created_at", null: false
623+
t.datetime "updated_at", null: false
624+
t.index ["account_id"], name: "index_tombstones_on_account_id"
625+
t.index ["uri"], name: "index_tombstones_on_uri"
626+
end
627+
619628
create_table "users", force: :cascade do |t|
620629
t.string "email", default: "", null: false
621630
t.datetime "created_at", null: false
@@ -744,6 +753,7 @@
744753
add_foreign_key "statuses_tags", "tags", name: "fk_3081861e21", on_delete: :cascade
745754
add_foreign_key "stream_entries", "accounts", name: "fk_5659b17554", on_delete: :cascade
746755
add_foreign_key "subscriptions", "accounts", name: "fk_9847d1cbb5", on_delete: :cascade
756+
add_foreign_key "tombstones", "accounts", on_delete: :cascade
747757
add_foreign_key "users", "accounts", name: "fk_50500f500d", on_delete: :cascade
748758
add_foreign_key "users", "invites", on_delete: :nullify
749759
add_foreign_key "users", "oauth_applications", column: "created_by_application_id", on_delete: :nullify

0 commit comments

Comments
 (0)