Skip to content

Commit 861e2d8

Browse files
authored
DEV: Improve performance on actor lookup (#266)
Two fixes here: - eager load the associated model when looking up an actor by ap_key - add a unique index on ap_key Should improve performance on `post "actor/:key/inbox"` requests
1 parent da4c543 commit 861e2d8

File tree

5 files changed

+29
-22
lines changed

5 files changed

+29
-22
lines changed

app/controllers/discourse_activity_pub/a_p/actors_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def show
1919
protected
2020

2121
def ensure_actor_exists
22-
unless @actor = DiscourseActivityPubActor.find_by(ap_key: params[:key])
22+
unless @actor = DiscourseActivityPubActor.includes(:model).find_by(ap_key: params[:key])
2323
render_activity_pub_error("not_found", 404)
2424
end
2525
end

app/models/discourse_activity_pub_actor.rb

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -390,33 +390,34 @@ def local_username_uniqueness
390390
# Table name: discourse_activity_pub_actors
391391
#
392392
# id :bigint not null, primary key
393-
# ap_id :string not null
393+
# ap_former_type :string
394394
# ap_key :string
395395
# ap_type :string not null
396-
# domain :string
397-
# local :boolean
398396
# available :boolean default(TRUE)
397+
# default_visibility :string
398+
# deleted_at :datetime
399+
# domain :string
400+
# enabled :boolean
401+
# icon_url :string
399402
# inbox :string
400-
# outbox :string
401-
# username :string
402-
# name :string
403-
# model_id :integer
403+
# local :boolean
404404
# model_type :string
405+
# name :string
406+
# outbox :string
407+
# post_object_type :string
405408
# private_key :text
406409
# public_key :text
410+
# publication_type :string
411+
# shared_inbox :string
412+
# username :string
407413
# created_at :datetime not null
408414
# updated_at :datetime not null
409-
# icon_url :string
410-
# shared_inbox :string
411-
# enabled :boolean
412-
# default_visibility :string
413-
# publication_type :string
414-
# post_object_type :string
415-
# deleted_at :datetime
416-
# ap_former_type :string
415+
# ap_id :string not null
416+
# model_id :integer
417417
#
418418
# Indexes
419419
#
420-
# index_discourse_activity_pub_actors_on_ap_id (ap_id) UNIQUE
421-
# unique_activity_pub_actor_models (model_type,model_id) UNIQUE
420+
# index_discourse_activity_pub_actors_on_ap_id (ap_id) UNIQUE
421+
# index_discourse_activity_pub_actors_on_ap_key (ap_key) UNIQUE
422+
# unique_activity_pub_actor_models (model_type,model_id) UNIQUE
422423
#

app/models/discourse_activity_pub_authorization.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ class DiscourseActivityPubAuthorization < ActiveRecord::Base
1313
# Table name: discourse_activity_pub_authorizations
1414
#
1515
# id :bigint not null, primary key
16-
# user_id :integer not null
17-
# actor_id :bigint
18-
# client_id :bigint
1916
# token :string(1000)
2017
# created_at :datetime not null
2118
# updated_at :datetime not null
19+
# actor_id :bigint
20+
# client_id :bigint
21+
# user_id :integer not null
2222
#
2323
# Indexes
2424
#

app/models/discourse_activity_pub_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def verify_credentials
9292
#
9393
# id :bigint not null, primary key
9494
# auth_type :integer not null
95-
# domain :string(1000) not null
9695
# credentials :json not null
96+
# domain :string(1000) not null
9797
# created_at :datetime not null
9898
# updated_at :datetime not null
9999
#
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
class AddIndexToDiscourseActivityPubActorsApKey < ActiveRecord::Migration[7.2]
3+
def change
4+
add_index :discourse_activity_pub_actors, :ap_key, unique: true
5+
end
6+
end

0 commit comments

Comments
 (0)