Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERF: static id for like post action type #305

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def reactions_received
.where(
post_id: post_ids,
deleted_at: nil,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
.joins(<<~SQL)
LEFT JOIN discourse_reactions_reaction_users ON
Expand Down Expand Up @@ -166,7 +166,7 @@ def post_reactions_users
likes =
post.post_actions.where(
DiscourseReactions::PostActionExtension.filter_reaction_likes_sql,
like: PostActionType.types[:like],
like: PostActionType::LIKE_POST_ACTION_ID,
valid_reactions: DiscourseReactions::Reaction.valid_reactions.to_a,
)

Expand Down
2 changes: 1 addition & 1 deletion app/models/discourse_reactions/reaction_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def post_action_like
PostAction.find_by(
user_id: self.user_id,
post_id: self.post_id,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def create_missing_post_actions

DB.query_single(
sql_query,
pa_like: PostActionType.types[:like],
pa_like: PostActionType::LIKE_POST_ACTION_ID,
excluded_from_like: @excluded_from_like,
)
end
Expand All @@ -111,7 +111,7 @@ def recover_trashed_post_actions

DB.query_single(
sql_query,
pa_like: PostActionType.types[:like],
pa_like: PostActionType::LIKE_POST_ACTION_ID,
excluded_from_like: @excluded_from_like,
)
end
Expand Down Expand Up @@ -228,7 +228,7 @@ def trash_excluded_post_actions

DB.query_single(
sql_query,
like: PostActionType.types[:like],
like: PostActionType::LIKE_POST_ACTION_ID,
excluded_from_like: @excluded_from_like,
ua_like: UserAction::LIKE,
ua_was_liked: UserAction::WAS_LIKED,
Expand Down
7 changes: 5 additions & 2 deletions app/services/discourse_reactions/reaction_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ def initialize(reaction_value:, user:, post:)
@user = user
@post = post
@like =
@post.post_actions.find_by(user: @user, post_action_type_id: PostActionType.types[:like])
@post.post_actions.find_by(
user: @user,
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
@previous_reaction_value =
if @like && !reaction_user
DiscourseReactions::Reaction.main_reaction_id
Expand Down Expand Up @@ -97,7 +100,7 @@ def add_shadow_like(notify: true)
end

def remove_shadow_like
PostActionDestroyer.new(@user, @post, PostActionType.types[:like]).perform
PostActionDestroyer.new(@user, @post, PostActionType::LIKE_POST_ACTION_ID).perform
delete_like_reaction
remove_reaction_notification
end
Expand Down
7 changes: 4 additions & 3 deletions lib/discourse_reactions/migration_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def self.run(previous_report_data: {}, print_report: true)
topic_user_liked: TopicUser.where(liked: true).count,
user_actions_liked: UserAction.where(action_type: UserAction::LIKE).count,
user_actions_was_liked: UserAction.where(action_type: UserAction::WAS_LIKED).count,
post_action_likes: PostAction.where(post_action_type_id: PostActionType.types[:like]).count,
post_action_likes:
PostAction.where(post_action_type_id: PostActionType::LIKE_POST_ACTION_ID).count,
post_like_count_total: Post.sum(:like_count),
topic_like_count_total: Topic.sum(:like_count),
user_stat_likes_given_total: UserStat.sum(:likes_given),
Expand Down Expand Up @@ -58,12 +59,12 @@ def self.humanized_report_data(report_data, previous_report_data = {})
<<~REPORT
Reaction migration report:
------------------------------------------------------------

main_reaction_id: #{DiscourseReactions::Reaction.main_reaction_id}
discourse_reactions_like_sync_enabled: #{SiteSetting.discourse_reactions_like_sync_enabled}
discourse_reactions_enabled_reactions: #{SiteSetting.discourse_reactions_enabled_reactions}
discourse_reactions_excluded_from_like: #{SiteSetting.discourse_reactions_excluded_from_like}

PostAction likes: #{report_data[:post_action_likes]}#{report_data_diff_indicators[:post_action_likes]}
UserActions.liked: #{report_data[:user_actions_liked]}#{report_data_diff_indicators[:user_actions_liked]}
UserActions.was_liked: #{report_data[:user_actions_was_liked]}#{report_data_diff_indicators[:user_actions_was_liked]}
Expand Down
8 changes: 6 additions & 2 deletions lib/discourse_reactions/topic_view_serializer_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.load_post_action_reaction_users_for_posts(post_ids)
)
.where(post_id: post_ids)
.where("post_actions.deleted_at IS NULL")
.where(post_action_type_id: PostActionType.types[:like])
.where(post_action_type_id: PostActionType::LIKE_POST_ACTION_ID)
.where(
"post_actions.post_id IN (#{DiscourseReactions::PostActionExtension.post_action_with_reaction_user_sql})",
valid_reactions: DiscourseReactions::Reaction.reactions_counting_as_like,
Expand Down Expand Up @@ -42,7 +42,8 @@ def posts
def self.prepended(base)
def base.posts_reaction_users_count(post_ids)
posts_reaction_users_count_query =
DB.query(<<~SQL, post_ids: Array.wrap(post_ids), like_id: PostActionType.types[:like])
DB.query(
<<~SQL,
SELECT union_subquery.post_id, COUNT(DISTINCT(union_subquery.user_id)) FROM (
SELECT user_id, post_id FROM post_actions
WHERE post_id IN (:post_ids)
Expand All @@ -55,6 +56,9 @@ def base.posts_reaction_users_count(post_ids)
WHERE posts.id IN (:post_ids)
) AS union_subquery WHERE union_subquery.post_ID IS NOT NULL GROUP BY union_subquery.post_id
SQL
post_ids: Array.wrap(post_ids),
like_id: PostActionType::LIKE_POST_ACTION_ID,
)

posts_reaction_users_count_query.each_with_object({}) do |row, hash|
hash[row.post_id] = row.count
Expand Down
8 changes: 4 additions & 4 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ class Engine < ::Rails::Engine
# will count as the main_reaction_id.
like =
object.post_actions.find do |post_action|
post_action.post_action_type_id == PostActionType.types[:like] && !post_action.trashed? &&
post_action.user_id == scope.user.id
post_action.post_action_type_id == PostActionType::LIKE_POST_ACTION_ID &&
!post_action.trashed? && post_action.user_id == scope.user.id
end

return nil if like.blank?
Expand All @@ -188,7 +188,7 @@ class Engine < ::Rails::Engine

like_post_action =
object.post_actions.find do |post_action|
post_action.post_action_type_id == PostActionType.types[:like] &&
post_action.post_action_type_id == PostActionType::LIKE_POST_ACTION_ID &&
post_action.user_id == scope.user.id && !post_action.trashed?
end

Expand Down Expand Up @@ -260,7 +260,7 @@ class Engine < ::Rails::Engine
SQL
start_date: report.start_date.to_date,
end_date: report.end_date.to_date,
like: PostActionType.types[:like],
like: PostActionType::LIKE_POST_ACTION_ID,
valid_reactions: DiscourseReactions::Reaction.valid_reactions.to_a,
)

Expand Down
2 changes: 1 addition & 1 deletion spec/fabricators/reaction_user_fabricator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
:post_action,
user: reaction_user.user,
post: reaction_user.post,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
created_at: reaction_user.created_at,
)
end
Expand Down
8 changes: 4 additions & 4 deletions spec/reports/reactions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
:post_action,
post: post_1,
user: user_1,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
created_at: 1.day.ago,
)
Fabricate(
Expand All @@ -32,7 +32,7 @@
:post_action,
post: post_2,
user: user_2,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
created_at: 1.day.ago,
)

Expand Down Expand Up @@ -73,14 +73,14 @@
:post_action,
post: post_1,
user: user_1,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
created_at: 1.day.ago,
)
Fabricate(
:post_action,
post: post_2,
user: user_2,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
created_at: 1.day.ago,
deleted_at: 1.day.ago,
)
Expand Down
10 changes: 5 additions & 5 deletions spec/requests/custom_reactions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
:post_action,
post: post_2,
user: user_5,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
end
fab!(:reaction_user_1) do
Expand Down Expand Up @@ -411,7 +411,7 @@
:post_action,
post: post_1,
user: user_5,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
sign_in(user_1)

Expand All @@ -438,7 +438,7 @@
:post_action,
post: post_1,
user: user_4,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
sign_in(user_1)

Expand Down Expand Up @@ -537,7 +537,7 @@
:post_action,
post: post_for_enabled_reactions,
user: user_4,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)

get "/discourse-reactions/posts/#{post_for_enabled_reactions.id}/reactions-users.json"
Expand Down Expand Up @@ -575,7 +575,7 @@
put "/discourse-reactions/posts/#{post_1.id}/custom-reactions/heart/toggle.json"
end.to change { Notification.count }.by(1).and change { PostAction.count }.by(1)

expect(PostAction.last.post_action_type_id).to eq(PostActionType.types[:like])
expect(PostAction.last.post_action_type_id).to eq(PostActionType::LIKE_POST_ACTION_ID)

expect do
put "/discourse-reactions/posts/#{post_1.id}/custom-reactions/heart/toggle.json"
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/post_action_users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
get "/post_action_users.json",
params: {
id: post.id,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
}
expect(response.status).to eq(200)
expect(response.parsed_body["post_action_users"].map { |u| u["id"] }).to match_array(
Expand Down
2 changes: 1 addition & 1 deletion spec/serializers/post_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
:post_action,
post: post_1,
user: user_4,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
end

Expand Down
6 changes: 3 additions & 3 deletions spec/serializers/topic_view_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
:post_action,
post: post_1,
user: user_1,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
end
fab!(:like_2) do
Fabricate(
:post_action,
post: post_1,
user: user_2,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
end
let(:topic) { post_1.topic }
Expand Down Expand Up @@ -94,7 +94,7 @@
:post_action,
post: post_1,
user: user_1,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
end
let(:topic) { post_1.topic }
Expand Down
2 changes: 1 addition & 1 deletion spec/services/reaction_like_synchronizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
:post_action,
post: reaction_user_2.post,
user: reaction_user_2.user,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
trashed_post_action.trash!(Fabricate(:user))
SiteSetting.discourse_reactions_excluded_from_like = "-1" # clap removed
Expand Down
6 changes: 3 additions & 3 deletions spec/services/reaction_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def reaction_manager(reaction_value)
PostAction.find_by(
post: post,
user: user,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
),
).to be_nil
end
Expand All @@ -104,7 +104,7 @@ def reaction_manager(reaction_value)
PostAction.find_by(
post: post,
user: user,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
),
).to be_present
end
Expand Down Expand Up @@ -158,7 +158,7 @@ def reaction_manager(reaction_value)
:post_action,
post: post,
user: user,
post_action_type_id: PostActionType.types[:like],
post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
)
end

Expand Down