Skip to content

Commit

Permalink
PERF: static id for like post action type (#112)
Browse files Browse the repository at this point in the history
Before flags were in the database, PostActionType.types were stored in memory and getting PostActionType.types[:like] was a very cheap operation.

Now, PostActionType is cached in Redis and we should try to avoid sending too many requests, especially that id for like is always 2.
  • Loading branch information
lis2 authored Sep 11, 2024
1 parent 8f327ee commit eeec0a8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
7 changes: 6 additions & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,12 @@
post = activity.object.stored.model

if user && post
PostActionCreator.new(user, post, PostActionType.types[:like], reason: :activity_pub).perform
PostActionCreator.new(
user,
post,
PostActionType::LIKE_POST_ACTION_ID,
reason: :activity_pub,
).perform
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/discourse_activity_pub/ap/activity/like_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
PostAction.exists?(
post_id: post.id,
user_id: @user.id,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
),
).to be(true)
end
Expand Down Expand Up @@ -84,7 +84,7 @@
PostAction.exists?(
post_id: post.id,
user_id: @user.id,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
),
).to be(true)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/discourse_activity_pub/ap/activity/undo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
:post_action,
user: user,
post: post,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
end
let!(:person) { Fabricate(:discourse_activity_pub_actor_person, model: user) }
Expand All @@ -64,7 +64,7 @@
PostAction.exists?(
post_id: post.id,
user_id: user.id,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
),
).to be(false)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/post_action_destroyer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
:post_action,
user: user2,
post: post,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
end
let!(:post_action2) do
Fabricate(
:post_action,
user: user3,
post: post,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
end
let!(:like1) { Fabricate(:discourse_activity_pub_activity_like, actor: actor2, object: note) }
Expand Down
2 changes: 1 addition & 1 deletion spec/models/post_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
:post_action,
user: user2,
post: post,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
end

Expand Down

0 comments on commit eeec0a8

Please sign in to comment.