Skip to content

A platform for community discussion. Free, open, simple.

FIX: Stop the reactions like sync from re-trashing likes every hour

Régis Hanol7fce4641b372
diff --git a/plugins/discourse-reactions/app/jobs/regular/discourse_reactions/like_synchronizer.rb b/plugins/discourse-reactions/app/jobs/regular/discourse_reactions/like_synchronizer.rb
index 8c158741e32..ac2582ebd5c 100644
--- a/plugins/discourse-reactions/app/jobs/regular/discourse_reactions/like_synchronizer.rb
+++ b/plugins/discourse-reactions/app/jobs/regular/discourse_reactions/like_synchronizer.rb
@@ -3,6 +3,8 @@
module Jobs
module DiscourseReactions
class LikeSynchronizer < ::Jobs::Base
+ cluster_concurrency 1
+
def execute(args = {})
if !SiteSetting.discourse_reactions_enabled ||
!SiteSetting.discourse_reactions_like_sync_enabled
diff --git a/plugins/discourse-reactions/app/jobs/scheduled/discourse_reactions/scheduled_like_synchronizer.rb b/plugins/discourse-reactions/app/jobs/scheduled/discourse_reactions/scheduled_like_synchronizer.rb
index e173e20a6e7..ccaa85111df 100644
--- a/plugins/discourse-reactions/app/jobs/scheduled/discourse_reactions/scheduled_like_synchronizer.rb
+++ b/plugins/discourse-reactions/app/jobs/scheduled/discourse_reactions/scheduled_like_synchronizer.rb
@@ -3,15 +3,10 @@
module Jobs
module DiscourseReactions
class ScheduledLikeSynchronizer < ::Jobs::Scheduled
- every 1.hour
+ every 1.day
def execute(args = {})
- if !SiteSetting.discourse_reactions_enabled ||
- !SiteSetting.discourse_reactions_like_sync_enabled
- return
- end
-
- ::DiscourseReactions::ReactionLikeSynchronizer.sync!
+ ::Jobs.enqueue(Jobs::DiscourseReactions::LikeSynchronizer)
end
end
end
diff --git a/plugins/discourse-reactions/app/services/discourse_reactions/reaction_like_synchronizer.rb b/plugins/discourse-reactions/app/services/discourse_reactions/reaction_like_synchronizer.rb
index e23e4510906..64a9ed7bc27 100644
--- a/plugins/discourse-reactions/app/services/discourse_reactions/reaction_like_synchronizer.rb
+++ b/plugins/discourse-reactions/app/services/discourse_reactions/reaction_like_synchronizer.rb
@@ -2,6 +2,8 @@
module DiscourseReactions
class ReactionLikeSynchronizer
+ BATCH_SIZE = 2_000
+
def self.sync!
new.sync!
end
@@ -13,107 +15,98 @@ module DiscourseReactions
def sync!
return if !SiteSetting.discourse_reactions_like_sync_enabled
- pre_run_report = DiscourseReactions::MigrationReport.run(print_report: false)
+ inserted = recovered = trashed = 0
+ min_id, max_id = DiscourseReactions::ReactionUser.pick("MIN(id)", "MAX(id)")
+
+ (min_id.to_i..max_id.to_i).step(BATCH_SIZE) do |batch_min_id|
+ @reaction_user_ids = batch_min_id..(batch_min_id + BATCH_SIZE - 1)
+ batch_inserted, batch_recovered, batch_trashed = sync_batch
+ inserted += batch_inserted
+ recovered += batch_recovered
+ trashed += batch_trashed
+ end
+
Rails.logger.info(
- "[ReactionLikeSynchronizer] Starting sync.... Pre-run report:\n\n#{DiscourseReactions::MigrationReport.humanized_report_data(pre_run_report)}",
+ "[ReactionLikeSynchronizer] Inserted #{inserted}, recovered #{recovered} and trashed #{trashed} post action likes.",
)
+ end
+
+ private
- # We want this to be all-or-nothing because the scope of each update/insert/delete
- # batch is so dependent on the `all_affected_post_action_ids` or previously affected
- # IDs. If we don't do this in a transaction, we could end up with a partial update
- # on error and have no easy way of correcting data.
+ def sync_batch
ActiveRecord::Base.transaction do
inserted_post_action_ids = create_missing_post_actions
recovered_post_action_ids = recover_trashed_post_actions
-
- created_or_recovered_post_action_ids =
- (recovered_post_action_ids + inserted_post_action_ids).uniq
-
- Rails.logger.info(
- "[ReactionLikeSynchronizer] Inserted #{inserted_post_action_ids.length} post action likes, recovered #{recovered_post_action_ids.length} trashed post action likes. (#{created_or_recovered_post_action_ids.length} total)",
- )
-
+ created_or_recovered_post_action_ids = inserted_post_action_ids + recovered_post_action_ids
create_missing_user_actions(created_or_recovered_post_action_ids)
trashed_post_action_ids = trash_excluded_post_actions
- Rails.logger.info(
- "[ReactionLikeSynchronizer] Trashed #{trashed_post_action_ids.length} post action likes.",
- )
delete_excluded_user_actions(trashed_post_action_ids)
- all_affected_post_action_ids =
- (created_or_recovered_post_action_ids + trashed_post_action_ids).uniq
-
- update_post_like_counts(all_affected_post_action_ids)
- update_topic_like_counts(all_affected_post_action_ids)
- update_user_stats(all_affected_post_action_ids)
- upsert_given_daily_likes(all_affected_post_action_ids)
+ recalculate_counters(created_or_recovered_post_action_ids + trashed_post_action_ids)
- TopicUser.update_post_action_cache(
- post_id:
- PostAction.with_deleted.where(id: all_affected_post_action_ids).pluck(:post_id).uniq,
- )
+ [
+ inserted_post_action_ids.length,
+ recovered_post_action_ids.length,
+ trashed_post_action_ids.length,
+ ]
end
+ end
- post_run_report =
- DiscourseReactions::MigrationReport.run(
- print_report: false,
- previous_report_data: pre_run_report,
- )
-
- Rails.logger.info(
- "[ReactionLikeSynchronizer] Sync completed! Post-run report:\n\n#{DiscourseReactions::MigrationReport.humanized_report_data(post_run_report, pre_run_report)}",
- )
+ def recalculate_counters(post_action_ids)
+ return if post_action_ids.empty?
- { pre_run_report:, post_run_report: }
+ rows = DB.query(<<~SQL, post_action_ids:)
+ SELECT post_actions.post_id, posts.topic_id, post_actions.user_id AS liker_id, posts.user_id AS author_id
+ FROM post_actions
+ INNER JOIN posts ON posts.id = post_actions.post_id
+ WHERE post_actions.id IN (:post_action_ids)
+ SQL
+ post_ids = rows.map(&:post_id).uniq
+ liker_ids = rows.map(&:liker_id).uniq
+
+ Post.recalculate_like_counts!(post_ids)
+ Topic.recalculate_like_counts!(rows.map(&:topic_id).uniq)
+ UserStat.recalculate_likes!((liker_ids + rows.map(&:author_id)).compact.uniq)
+ GivenDailyLike.recalculate!(liker_ids)
+ TopicUser.update_post_action_cache(post_id: post_ids)
end
- # Find all ReactionUser records that do not have a corresponding
- # PostAction like record, for any reactions that are not in
- # excluded_from_like, and create a PostAction record for each.
def create_missing_post_actions
- sql_query = <<~SQL
- INSERT INTO post_actions(
- post_id, user_id, post_action_type_id, created_at, updated_at
- )
- SELECT ru.post_id, ru.user_id, :pa_like, ru.created_at, ru.updated_at
- FROM discourse_reactions_reaction_users ru
- INNER JOIN discourse_reactions_reactions
- ON discourse_reactions_reactions.id = ru.reaction_id
+ DB.query_single(<<~SQL, sql_params)
+ WITH eligible AS (#{eligible_reaction_users_sql})
+ INSERT INTO post_actions (post_id, user_id, post_action_type_id, created_at, updated_at)
+ SELECT eligible.post_id, eligible.user_id, :pa_like, eligible.created_at, eligible.updated_at
+ FROM eligible
LEFT JOIN post_actions
- ON post_actions.user_id = ru.user_id
- AND post_actions.post_id = ru.post_id
+ ON post_actions.user_id = eligible.user_id
+ AND post_actions.post_id = eligible.post_id
+ AND post_actions.post_action_type_id = :pa_like
WHERE post_actions.id IS NULL
- #{@excluded_from_like.any? ? " AND discourse_reactions_reactions.reaction_value NOT IN (:excluded_from_like)" : ""}
+ ON CONFLICT DO NOTHING
RETURNING post_actions.id
SQL
-
- DB.query_single(
- sql_query,
- pa_like: PostActionType::LIKE_POST_ACTION_ID,
- excluded_from_like: @excluded_from_like,
- )
end
- # Find all trashed PostAction records matching ReactionUser records,
- # which are not in excluded_from_like, and untrash them.
def recover_trashed_post_actions
- sql_query = <<~SQL
+ DB.query_single(<<~SQL, sql_params)
+ WITH eligible AS (#{eligible_reaction_users_sql}),
+ recoverable AS (
+ SELECT MAX(post_actions.id) AS id
+ FROM post_actions
+ INNER JOIN eligible
+ ON eligible.user_id = post_actions.user_id
+ AND eligible.post_id = post_actions.post_id
+ WHERE post_actions.post_action_type_id = :pa_like
+ GROUP BY post_actions.user_id, post_actions.post_id
+ HAVING BOOL_AND(post_actions.deleted_at IS NOT NULL)
+ )
UPDATE post_actions
SET deleted_at = NULL, deleted_by_id = NULL, updated_at = NOW()
- FROM discourse_reactions_reaction_users ru
- INNER JOIN discourse_reactions_reactions
- ON discourse_reactions_reactions.id = ru.reaction_id
- WHERE post_actions.deleted_at IS NOT NULL AND post_actions.user_id = ru.user_id
- AND post_actions.post_id = ru.post_id AND post_actions.post_action_type_id = :pa_like
- #{@excluded_from_like.any? ? " AND discourse_reactions_reactions.reaction_value NOT IN (:excluded_from_like)" : ""}
+ FROM recoverable
+ WHERE post_actions.id = recoverable.id
+ RETURNING post_actions.id
SQL
-
- DB.query_single(
- sql_query,
- pa_like: PostActionType::LIKE_POST_ACTION_ID,
- excluded_from_like: @excluded_from_like,
- )
end
# Create the corresponding UserAction records for the PostAction records. In
@@ -122,39 +115,15 @@ module DiscourseReactions
# The only difference between LIKE and WAS LIKED is the user;
# * LIKE is the post action user because they are the one who liked the post
# * WAS LIKED is done by the post user, because they are the like-ee
- #
- # No need to do any UserAction inserts if there wasn't any PostAction changes.
def create_missing_user_actions(post_action_ids)
- return if post_action_ids.none?
-
- sql_query = <<~SQL
- INSERT INTO user_actions (
- action_type, user_id, acting_user_id, target_post_id, target_topic_id, created_at, updated_at
- )
- SELECT :ua_like,
- post_actions.user_id,
- post_actions.user_id,
- post_actions.post_id,
- posts.topic_id,
- post_actions.created_at,
- post_actions.created_at
- FROM post_actions
- INNER JOIN posts ON posts.id = post_actions.post_id
- WHERE post_actions.id IN (:post_action_ids) AND posts.user_id IS NOT NULL
- ON CONFLICT DO NOTHING
- SQL
- inserted_user_action_count =
- DB.exec(sql_query, ua_like: UserAction::LIKE, post_action_ids: post_action_ids)
- Rails.logger.info(
- "[ReactionsLikeSynchronizer] Inserted #{inserted_user_action_count} like UserActions",
- )
+ return if post_action_ids.empty?
- sql_query = <<~SQL
+ DB.exec(<<~SQL, **sql_params, post_action_ids:)
INSERT INTO user_actions (
action_type, user_id, acting_user_id, target_post_id, target_topic_id, created_at, updated_at
)
- SELECT :ua_was_liked,
- posts.user_id,
+ SELECT types.action_type,
+ CASE types.action_type WHEN :ua_like THEN post_actions.user_id ELSE posts.user_id END,
post_actions.user_id,
post_actions.post_id,
posts.topic_id,
@@ -162,255 +131,65 @@ module DiscourseReactions
post_actions.created_at
FROM post_actions
INNER JOIN posts ON posts.id = post_actions.post_id
+ CROSS JOIN (VALUES (:ua_like), (:ua_was_liked)) AS types(action_type)
WHERE post_actions.id IN (:post_action_ids) AND posts.user_id IS NOT NULL
ON CONFLICT DO NOTHING
SQL
- inserted_user_action_count =
- DB.exec(sql_query, ua_was_liked: UserAction::WAS_LIKED, post_action_ids: post_action_ids)
- Rails.logger.info(
- "[ReactionsLikeSynchronizer] Inserted #{inserted_user_action_count} was_liked UserActions",
- )
end
- # Delete any UserAction records for LIKE or WAS_LIKED that match up with
- # PostAction records that got trashed.
def delete_excluded_user_actions(trashed_post_action_ids)
return if trashed_post_action_ids.empty?
- sql_query = <<~SQL
+ DB.exec(<<~SQL, **sql_params, trashed_post_action_ids:)
DELETE FROM user_actions
- WHERE id IN (
- -- Select IDs for LIKED actions
- SELECT user_actions.id
- FROM user_actions
- INNER JOIN post_actions ON user_actions.target_post_id = post_actions.post_id
- AND user_actions.acting_user_id = post_actions.user_id
- WHERE post_actions.id IN (:trashed_post_action_ids)
- AND user_actions.action_type = :ua_like
-
- UNION
-
- -- Select IDs for WAS_LIKED actions
- SELECT user_actions.id
- FROM user_actions user_actions
- INNER JOIN post_actions ON user_actions.target_post_id = post_actions.post_id
- INNER JOIN posts ON posts.id = post_actions.post_id
- WHERE post_actions.id IN (:trashed_post_action_ids)
- AND user_actions.action_type = :ua_was_liked
- AND user_actions.user_id = posts.user_id
+ USING post_actions
+ WHERE post_actions.id IN (:trashed_post_action_ids)
+ AND user_actions.target_post_id = post_actions.post_id
AND user_actions.acting_user_id = post_actions.user_id
- )
+ AND user_actions.action_type IN (:ua_like, :ua_was_liked)
SQL
- DB.exec(
- sql_query,
- ua_like: UserAction::LIKE,
- ua_was_liked: UserAction::WAS_LIKED,
- trashed_post_action_ids: trashed_post_action_ids,
- )
end
- # Find all PostAction records that have a ReactionUser record that
- # uses a reaction in the excluded_from_like list and trash them.
def trash_excluded_post_actions
- return [] if @excluded_from_like.none?
+ return [] if @excluded_from_like.empty?
- sql_query = <<~SQL
+ DB.query_single(<<~SQL, sql_params)
UPDATE post_actions
SET deleted_at = NOW()
FROM discourse_reactions_reaction_users ru
INNER JOIN discourse_reactions_reactions ON discourse_reactions_reactions.id = ru.reaction_id
- WHERE post_actions.user_id = ru.user_id
+ WHERE post_actions.deleted_at IS NULL
+ AND post_actions.user_id = ru.user_id
AND post_actions.post_id = ru.post_id
- AND post_actions.post_action_type_id = :like
+ AND post_actions.post_action_type_id = :pa_like
AND discourse_reactions_reactions.reaction_value IN (:excluded_from_like)
+ AND ru.id BETWEEN :min_reaction_user_id AND :max_reaction_user_id
RETURNING post_actions.id
SQL
-
- DB.query_single(
- sql_query,
- like: PostActionType::LIKE_POST_ACTION_ID,
- excluded_from_like: @excluded_from_like,
- ua_like: UserAction::LIKE,
- ua_was_liked: UserAction::WAS_LIKED,
- )
- end
-
- # Update the like_count counter cache on all Post records
- # affected by created/recovered/trashed post actions.
- def update_post_like_counts(all_affected_post_action_ids)
- post_ids = DB.query_single(<<~SQL, post_action_ids: all_affected_post_action_ids)
- SELECT DISTINCT post_id
- FROM post_actions
- WHERE ID IN (:post_action_ids)
- SQL
-
- sql_query = <<~SQL
- UPDATE posts
- SET like_count = (
- SELECT COUNT(*)
- FROM post_actions
- WHERE post_actions.post_id = posts.id
- AND post_action_type_id = 2
- AND post_actions.deleted_at IS NULL
- )
- WHERE posts.id IN (:post_ids)
- SQL
- DB.exec(sql_query, post_ids: post_ids)
- end
-
- # Update the like_count counter cache on all Topic records
- # affected by created/recovered/trashed post actions.
- def update_topic_like_counts(all_affected_post_action_ids)
- topic_ids = DB.query_single(<<~SQL, post_action_ids: all_affected_post_action_ids)
- SELECT DISTINCT topic_id
- FROM posts
- INNER JOIN post_actions ON post_actions.post_id = posts.id
- WHERE post_actions.id IN (:post_action_ids)
- SQL
-
- sql_query = <<~SQL
- UPDATE topics
- SET like_count = (
- SELECT SUM(like_count)
- FROM posts
- WHERE posts.topic_id = topics.id
- )
- WHERE topics.id IN (:topic_ids)
- SQL
-
- DB.exec(sql_query, topic_ids: topic_ids)
end
- # Update the likes_given and likes_received counter caches on the
- # UserStat table based on posts matching up with created/restored/trashed
- # post actions.
- #
- # The UserAction records (which are created/deleted before this) are an
- # easier way to calculate this rather than going via PostAction again.
- def update_user_stats(all_affected_post_action_ids)
- return if all_affected_post_action_ids.empty?
-
- users_needing_likes_received_recalc = DB.query_single(<<~SQL, all_affected_post_action_ids)
- SELECT DISTINCT posts.user_id
- FROM posts
- INNER JOIN post_actions ON post_actions.post_id = posts.id
- WHERE post_actions.id IN (?)
- SQL
-
- if users_needing_likes_received_recalc.any?
- # NOTE: UserAction created as a result of a PostAction like
- # will have acting_user_id, target_post_id, and target_topic_id
- # filled but NOT target_user_id, see UserActionManager.post_action_rows
- sql_query = <<~SQL
- WITH likes_received_cte AS (
- SELECT posts.user_id AS user_id, COUNT(user_actions.id) AS new_likes_received
- FROM user_actions
- INNER JOIN posts ON user_actions.target_post_id = posts.id
- WHERE user_actions.action_type = :ua_was_liked
- AND posts.user_id IN (:affected_user_ids)
- GROUP BY posts.user_id
- )
- UPDATE user_stats
- SET likes_received = lrc.new_likes_received
- FROM likes_received_cte lrc
- WHERE user_stats.user_id = lrc.user_id
- RETURNING user_stats.user_id
- SQL
-
- changed_user_ids =
- DB.query_single(
- sql_query,
- affected_user_ids: users_needing_likes_received_recalc,
- ua_was_liked: UserAction::WAS_LIKED,
- )
- UserStat.where(user_id: users_needing_likes_received_recalc - changed_user_ids).update_all(
- likes_received: 0,
- )
- end
-
- users_needing_likes_given_recalc = DB.query_single(<<~SQL, all_affected_post_action_ids)
- SELECT DISTINCT user_id
- FROM post_actions
- WHERE post_actions.id IN (?)
+ def eligible_reaction_users_sql
+ <<~SQL
+ SELECT ru.user_id, ru.post_id, ru.created_at, ru.updated_at
+ FROM discourse_reactions_reaction_users ru
+ INNER JOIN discourse_reactions_reactions ON discourse_reactions_reactions.id = ru.reaction_id
+ INNER JOIN posts ON posts.id = ru.post_id
+ WHERE ru.id BETWEEN :min_reaction_user_id AND :max_reaction_user_id
+ AND posts.deleted_at IS NULL
+ AND posts.user_id <> ru.user_id
+ #{"AND discourse_reactions_reactions.reaction_value NOT IN (:excluded_from_like)" if @excluded_from_like.any?}
SQL
-
- if users_needing_likes_given_recalc.any?
- sql_query = <<~SQL
- WITH likes_given_cte AS (
- SELECT user_actions.acting_user_id AS user_id, COUNT(user_actions.id) AS new_likes_given
- FROM user_actions
- WHERE user_actions.action_type = :ua_like
- AND user_actions.acting_user_id IN (:affected_user_ids)
- GROUP BY user_actions.acting_user_id
- )
- UPDATE user_stats
- SET likes_given = lgc.new_likes_given
- FROM likes_given_cte lgc
- WHERE user_stats.user_id = lgc.user_id
- RETURNING user_stats.user_id
- SQL
-
- changed_user_ids =
- DB.query_single(
- sql_query,
- affected_user_ids: users_needing_likes_given_recalc,
- ua_like: UserAction::LIKE,
- )
- UserStat.where(user_id: users_needing_likes_given_recalc - changed_user_ids).update_all(
- likes_given: 0,
- )
- end
end
- # Upsert any existing GivenDailyLike records for the users who created
- # or were affected by the created/restored/trashed post actions. There
- # is a count per day per user that needs to be recalculated.
- #
- # We delete any GivenDailyLike records that would equate to a count of 0
- # for that day and that user, which is based on UserAction records (which
- # are created or destroyed before this).
- def upsert_given_daily_likes(all_affected_post_action_ids)
- return if all_affected_post_action_ids.blank?
-
- sql_query = <<~SQL
- SELECT user_id
- FROM post_actions
- WHERE post_actions.id = ANY(ARRAY[:all_affected_post_action_ids])
- UNION
- SELECT posts.user_id
- FROM posts
- INNER JOIN post_actions ON post_actions.post_id = posts.id
- WHERE post_actions.id = ANY(ARRAY[:all_affected_post_action_ids])
- SQL
- user_ids =
- DB.query_single(sql_query, all_affected_post_action_ids: all_affected_post_action_ids)
-
- return if user_ids.blank?
-
- sql_query = <<~SQL
- INSERT INTO given_daily_likes (user_id, given_date, likes_given)
- SELECT user_actions.acting_user_id, DATE(user_actions.created_at) AS given_date, COUNT(*) AS likes_given
- FROM user_actions
- WHERE user_actions.action_type = :ua_like
- AND user_actions.acting_user_id IN (:user_ids)
- GROUP BY user_actions.acting_user_id, DATE(user_actions.created_at)
- ON CONFLICT (user_id, given_date)
- DO UPDATE SET likes_given = EXCLUDED.likes_given
- SQL
- DB.exec(sql_query, ua_like: UserAction::LIKE, user_ids: user_ids)
-
- sql_query = <<~SQL
- DELETE FROM given_daily_likes gdl
- WHERE NOT EXISTS (
- SELECT 1
- FROM user_actions
- WHERE
- user_actions.acting_user_id = gdl.user_id
- AND user_actions.action_type = :ua_like
- AND DATE(user_actions.created_at) = gdl.given_date
- ) AND gdl.user_id IN (:user_ids)
- SQL
- DB.exec(sql_query, ua_like: UserAction::LIKE, user_ids: user_ids)
+ def sql_params
+ {
+ pa_like: PostActionType::LIKE_POST_ACTION_ID,
+ ua_like: UserAction::LIKE,
+ ua_was_liked: UserAction::WAS_LIKED,
+ excluded_from_like: @excluded_from_like,
+ min_reaction_user_id: @reaction_user_ids.first,
+ max_reaction_user_id: @reaction_user_ids.last,
+ }
end
end
end
diff --git a/plugins/discourse-reactions/config/locales/server.en.yml b/plugins/discourse-reactions/config/locales/server.en.yml
index 0a66d16f946..3d753ad858e 100644
--- a/plugins/discourse-reactions/config/locales/server.en.yml
+++ b/plugins/discourse-reactions/config/locales/server.en.yml
@@ -13,7 +13,7 @@ en:
discourse_reactions_enabled_reactions: "Defines a list of enabled reactions, any emoji is allowed here."
discourse_reactions_desaturated_reaction_panel: "Reduces visual noise of reactions by displaying them desaturated until hover."
discourse_reactions_excluded_from_like: "Reactions that do not count as a Like. Any reactions that are not on this list will count as a Like for badges, reporting, and other purposes."
- discourse_reactions_like_sync_enabled: "If this is enabled, historical reactions will have their matching Like records created, except those reactions defined in {{setting:discourse_reactions_excluded_from_like}}. This sync will happen on a regular basis in the background, and also when you change {{setting:discourse_reactions_excluded_from_like}}."
+ discourse_reactions_like_sync_enabled: "If this is enabled, historical reactions will have their matching Like records created, except those reactions defined in {{setting:discourse_reactions_excluded_from_like}}. This sync will happen daily in the background, and also when you change {{setting:discourse_reactions_excluded_from_like}} or enable this setting."
discourse_reactions_allow_any_emoji: "If this is enabled, will add a button allowing users to select any emoji in the reactions picker. Members will be allowed to choose any emoji, including custom emoji, for reactions. To limit available reactions, use the {{setting:emoji_deny_list}} site setting."
enable_discourse_reactions_by_default: "Changes the default value for the {{setting:discourse_reactions_enabled}} setting from disabled to enabled when this upcoming change is enabled."
errors:
diff --git a/plugins/discourse-reactions/lib/discourse_reactions/migration_report.rb b/plugins/discourse-reactions/lib/discourse_reactions/migration_report.rb
deleted file mode 100644
index c73882d1eee..00000000000
--- a/plugins/discourse-reactions/lib/discourse_reactions/migration_report.rb
+++ /dev/null
@@ -1,88 +0,0 @@
-# frozen_string_literal: true
-
-module DiscourseReactions
- # TODO (martin) Remove this once we have rolled out reactions like sync more widely.
- class MigrationReport
- def self.run(previous_report_data: {}, print_report: true)
- report_data = {
- 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::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),
- user_stat_likes_received_total: UserStat.sum(:likes_received),
- given_daily_like_total: GivenDailyLike.sum(:likes_given),
- badge_count: UserBadge.count,
- reactions_total: DiscourseReactions::ReactionUser.count,
- }
-
- puts humanized_report_data(report_data, previous_report_data) if print_report
-
- report_data
- end
-
- def self.generate_diff(report_data, previous_report_data)
- report_data_diff_indicators = {}
-
- previous_report_data.each do |key, value|
- diff_indicator =
- if report_data[key] < value
- " (-#{value - report_data[key]})"
- elsif report_data[key] > value
- " (+#{report_data[key] - value})"
- else
- " (no change)"
- end
-
- report_data_diff_indicators[key] = diff_indicator
- end
-
- report_data_diff_indicators
- end
-
- def self.humanized_report_data(report_data, previous_report_data = {})
- report_data_diff_indicators = generate_diff(report_data, previous_report_data)
-
- report_data_reactions_breakdown = {}
- SiteSetting
- .discourse_reactions_enabled_reactions
- .split("|")
- .each do |reaction|
- report_data_reactions_breakdown[reaction] = DiscourseReactions::Reaction.where(
- reaction_value: reaction,
- ).sum(:reaction_users_count)
- end
-
- <<~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]}
- UserStat.likes_given: #{report_data[:user_stat_likes_given_total]}#{report_data_diff_indicators[:user_stat_likes_given_total]}
- UserStat.likes_received: #{report_data[:user_stat_likes_received_total]}#{report_data_diff_indicators[:user_stat_likes_received_total]}
- Post.like_count: #{report_data[:post_like_count_total]}#{report_data_diff_indicators[:post_like_count_total]}
- Topic.like_count: #{report_data[:topic_like_count_total]}#{report_data_diff_indicators[:topic_like_count_total]}
- TopicUser.liked: #{report_data[:topic_user_liked]}#{report_data_diff_indicators[:topic_user_liked]}
- Badge count: #{report_data[:badge_count]}#{report_data_diff_indicators[:badge_count]}
- Given daily like total: #{report_data[:given_daily_like_total]}#{report_data_diff_indicators[:given_daily_like_total]}
- Reactions: #{report_data[:reactions_total]}#{report_data_diff_indicators[:reactions_total]}
- #{
- report_data_reactions_breakdown
- .map { |reaction, count| " -> #{reaction}: #{count}" }
- .join("\n")
- }
-
- REPORT
- end
- end
-end
diff --git a/plugins/discourse-reactions/plugin.rb b/plugins/discourse-reactions/plugin.rb
index 71f91aecea5..c1c497059d7 100644
--- a/plugins/discourse-reactions/plugin.rb
+++ b/plugins/discourse-reactions/plugin.rb
@@ -56,7 +56,6 @@ after_initialize do
lib/discourse_reactions/posts_reaction_loader.rb
lib/discourse_reactions/topic_view_serializer_extension.rb
lib/discourse_reactions/topic_view_posts_serializer_extension.rb
- lib/discourse_reactions/migration_report.rb
lib/discourse_reactions/post_reactions_query.rb
app/jobs/regular/discourse_reactions/like_synchronizer.rb
app/jobs/scheduled/discourse_reactions/scheduled_like_synchronizer.rb
@@ -435,10 +434,10 @@ after_initialize do
end
end
- on(:site_setting_changed) do |name, old_value, new_value|
- if name == :discourse_reactions_excluded_from_like &&
+ on(:site_setting_changed) do |name|
+ if name.in?(%i[discourse_reactions_excluded_from_like discourse_reactions_like_sync_enabled]) &&
SiteSetting.discourse_reactions_like_sync_enabled
- ::Jobs.cancel_scheduled_job(Jobs::DiscourseReactions::LikeSynchronizer)
+ ::Jobs.cancel_scheduled_job("DiscourseReactions::LikeSynchronizer")
::Jobs.enqueue_at(5.minutes.from_now, Jobs::DiscourseReactions::LikeSynchronizer)
end
end
diff --git a/plugins/discourse-reactions/spec/plugin_spec.rb b/plugins/discourse-reactions/spec/plugin_spec.rb
index adcca014e14..387f23f068f 100644
--- a/plugins/discourse-reactions/spec/plugin_spec.rb
+++ b/plugins/discourse-reactions/spec/plugin_spec.rb
@@ -24,12 +24,30 @@ describe DiscourseReactions do
expect(clap_reaction.data).to include(user_2.name)
end
- describe "on_setting_change(discourse_reactions_excluded_from_like)" do
- it "kicks off the background job to sync post actions when site setting changes" do
+ describe "on site_setting_changed" do
+ it "kicks off the background job to sync post actions when the excluded reactions change" do
expect_enqueued_with(job: Jobs::DiscourseReactions::LikeSynchronizer) do
SiteSetting.discourse_reactions_excluded_from_like = "confetti_ball|-1"
end
end
+
+ it "cancels any sync already scheduled" do
+ Jobs
+ .expects(:cancel_scheduled_job)
+ .with do |job_name|
+ "Jobs::#{job_name.to_s.camelcase}" == Jobs::DiscourseReactions::LikeSynchronizer.name
+ end
+
+ SiteSetting.discourse_reactions_excluded_from_like = "confetti_ball|-1"
+ end
+
+ it "kicks off the background job to sync post actions when the sync gets enabled" do
+ SiteSetting.discourse_reactions_like_sync_enabled = false
+
+ expect_enqueued_with(job: Jobs::DiscourseReactions::LikeSynchronizer) do
+ SiteSetting.discourse_reactions_like_sync_enabled = true
+ end
+ end
end
describe "user_action_stream_builder modifier for UserAction.stream" do
diff --git a/plugins/discourse-reactions/spec/services/reaction_like_synchronizer_spec.rb b/plugins/discourse-reactions/spec/services/reaction_like_synchronizer_spec.rb
index 1eb38b4953c..1a3570f0d95 100644
--- a/plugins/discourse-reactions/spec/services/reaction_like_synchronizer_spec.rb
+++ b/plugins/discourse-reactions/spec/services/reaction_like_synchronizer_spec.rb
@@ -2,8 +2,8 @@
RSpec.describe DiscourseReactions::ReactionLikeSynchronizer do
let!(:user) { Fabricate(:user) }
- let!(:post) { Fabricate(:post, like_count: 1) }
- let!(:post_2) { Fabricate(:post, like_count: 0) }
+ let!(:post) { Fabricate(:post) }
+ let!(:post_2) { Fabricate(:post) }
before do
SiteSetting.discourse_reactions_like_sync_enabled = true
@@ -20,8 +20,7 @@ RSpec.describe DiscourseReactions::ReactionLikeSynchronizer do
# records are created, rather than building this all manually.
let!(:reaction_user) do
DiscourseReactions::ReactionManager.new(reaction_value: "+1", user: user, post: post).toggle!
- @reaction_plus_one = DiscourseReactions::Reaction.find_by(reaction_value: "+1", post: post)
- DiscourseReactions::ReactionUser.find_by(user: user, post: post, reaction: @reaction_plus_one)
+ DiscourseReactions::ReactionUser.find_by(user: user, post: post)
end
let!(:reaction_user_2) do
@@ -30,13 +29,13 @@ RSpec.describe DiscourseReactions::ReactionLikeSynchronizer do
user: user,
post: post_2,
).toggle!
- @reaction_clap = DiscourseReactions::Reaction.find_by(reaction_value: "clap", post: post_2)
- DiscourseReactions::ReactionUser.find_by(user: user, post: post_2, reaction: @reaction_clap)
+ DiscourseReactions::ReactionUser.find_by(user: user, post: post_2)
end
it "does nothing if discourse_reactions_like_sync_enabled is false" do
- DB.expects(:exec).never
SiteSetting.discourse_reactions_like_sync_enabled = false
+ SiteSetting.discourse_reactions_excluded_from_like = ""
+
expect { described_class.sync! }.not_to change { PostAction.count }
end
@@ -45,164 +44,160 @@ RSpec.describe DiscourseReactions::ReactionLikeSynchronizer do
SiteSetting.discourse_reactions_excluded_from_like += "|+1" # +1 added
end
- it "trashes PostAction records" do
- post_action_id = reaction_user.post_action_like.id
- expect(reaction_user.post_action_like).to be_present
- expect { described_class.sync! }.to change { PostAction.count }.by(-1)
- expect(reaction_user.reload.post_action_like).to be_nil
- expect(PostAction.with_deleted.find_by(id: post_action_id).deleted_at).to be_present
- end
+ it "trashes the likes and recalculates derived data" do
+ post_action = reaction_user.post_action_like
- it "removes UserAction records for LIKED and WAS_LIKED" do
expect { described_class.sync! }.to change { UserAction.count }.by(-2)
- end
- it "updates the like_count on the associated Post records" do
- expect(post.reload.like_count).to eq(1)
- expect(post_2.reload.like_count).to eq(0)
- described_class.sync!
- expect(post.reload.like_count).to eq(0)
- expect(post_2.reload.like_count).to eq(0)
- end
-
- it "updates the like_count on the associated Topic records" do
- expect(post.topic.reload.like_count).to eq(1)
- expect(post_2.topic.reload.like_count).to eq(0)
- described_class.sync!
+ expect(post_action.reload.trashed?).to eq(true)
+ expect(post.reload).to have_attributes(like_count: 0, like_score: 0)
expect(post.topic.reload.like_count).to eq(0)
- expect(post_2.topic.reload.like_count).to eq(0)
- end
-
- it "updates the liked column on TopicUser for associated topic and users" do
- expect(post.topic.topic_users.find_by(user: user).liked).to eq(true)
- expect(post_2.topic.topic_users.find_by(user: user).liked).to eq(false)
- described_class.sync!
- expect(post.topic.topic_users.find_by(user: user).liked).to eq(false)
- expect(post_2.topic.topic_users.find_by(user: user).liked).to eq(false)
- end
-
- it "updates the UserStat likes_given and likes_received columns" do
- expect(user.user_stat.reload.likes_given).to eq(1)
- expect(post.user.user_stat.reload.likes_received).to eq(1)
- described_class.sync!
+ expect(topic_user.reload.liked).to eq(false)
expect(user.user_stat.reload.likes_given).to eq(0)
expect(post.user.user_stat.reload.likes_received).to eq(0)
+ expect(GivenDailyLike.exists?(user:)).to eq(false)
end
- it "updates/recalculates the GivenDailyLike table likes_given on all given_date days and deletes records where likes_given would be 0" do
- expect(
- GivenDailyLike.exists?(user: user, given_date: Time.now.to_date, likes_given: 1),
- ).to eq(true)
+ it "does not re-trash already trashed likes" do
+ post_action = reaction_user.post_action_like
described_class.sync!
- expect(GivenDailyLike.exists?(user: user, given_date: Time.now.to_date)).to eq(false)
+ post_action.update_columns(deleted_at: 1.day.ago)
+
+ expect { described_class.sync! }.not_to change { post_action.reload.deleted_at }
end
end
describe "when reactions are removed from the exclusion list" do
- it "creates PostAction records" do
+ before do
SiteSetting.discourse_reactions_excluded_from_like = "-1" # clap removed
- expect(reaction_user_2.post_action_like).to be_nil
- expect { described_class.sync! }.to change { PostAction.count }.by(1)
- expect(reaction_user_2.reload.post_action_like).to be_present
end
- it "updates existing trashed PostUpdate records to recover them" do
- trashed_post_action =
- Fabricate(
- :post_action,
- post: reaction_user_2.post,
- user: reaction_user_2.user,
- post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
- )
- trashed_post_action.trash!(Fabricate(:user))
- SiteSetting.discourse_reactions_excluded_from_like = "-1" # clap removed
- expect { described_class.sync! }.to change { PostAction.count }.by(1)
- expect(trashed_post_action.reload.trashed?).to eq(false)
- end
+ it "creates the likes and recalculates derived data" do
+ SiteSetting.max_likes_per_day = 2
- it "creates UserAction records for LIKED and WAS_LIKED" do
- SiteSetting.discourse_reactions_excluded_from_like = "-1" # clap removed
- expect { described_class.sync! }.to change { UserAction.count }.by(2)
- expect(
- UserAction.exists?(
- action_type: UserAction::LIKE,
- user_id: reaction_user_2.post_action_like.user_id,
- acting_user_id: reaction_user_2.post_action_like.user_id,
- target_post_id: reaction_user_2.post_action_like.post_id,
- target_topic_id: reaction_user_2.post.topic_id,
- ),
- ).to eq(true)
+ stub_const(described_class, :BATCH_SIZE, 1) { described_class.sync! }
+
+ expect(reaction_user_2.reload.post_action_like).to be_present
expect(
- UserAction.exists?(
- action_type: UserAction::WAS_LIKED,
- user_id: reaction_user_2.post.user_id,
- acting_user_id: reaction_user_2.post_action_like.user_id,
- target_post_id: reaction_user_2.post_action_like.post_id,
- target_topic_id: reaction_user_2.post.topic_id,
+ UserAction.where(target_post_id: post_2.id, target_topic_id: post_2.topic_id).pluck(
+ :action_type,
+ :user_id,
+ :acting_user_id,
),
- ).to eq(true)
+ ).to contain_exactly(
+ [UserAction::LIKE, user.id, user.id],
+ [UserAction::WAS_LIKED, post_2.user_id, user.id],
+ )
+ expect(post_2.reload).to have_attributes(like_count: 1, like_score: 1)
+ expect(post_2.topic.reload.like_count).to eq(1)
+ expect(topic_user_2.reload.liked).to eq(true)
+ expect(user.user_stat.reload.likes_given).to eq(2)
+ expect(post_2.user.user_stat.reload.likes_received).to eq(1)
+ expect(GivenDailyLike.find_by(user:, given_date: Time.zone.today)).to have_attributes(
+ likes_given: 2,
+ limit_reached: true,
+ )
end
- it "skips UserAction records where the post has a null user" do
- reaction_user_2.post.update_columns(user_id: nil)
- SiteSetting.discourse_reactions_excluded_from_like = "-1" # clap removed
- expect { described_class.sync! }.not_to change { UserAction.count }
- end
+ it "creates the likes when the user has another post action on the post" do
+ Fabricate(
+ :post_action,
+ user:,
+ post: post_2,
+ post_action_type_id: PostActionType.types[:off_topic],
+ )
+
+ described_class.sync!
- it "if no reactions are excluded from like it adds post actions for ones previously excluded" do
- SiteSetting.discourse_reactions_excluded_from_like = ""
- expect(reaction_user_2.post_action_like).to be_nil
- expect { described_class.sync! }.to change { PostAction.count }.by(1)
expect(reaction_user_2.reload.post_action_like).to be_present
end
- it "updates the like_count on the associated Post records" do
+ it "recovers likes it trashed and recalculates derived data" do
+ SiteSetting.discourse_reactions_excluded_from_like = "-1|+1"
+ described_class.sync!
+ SiteSetting.discourse_reactions_excluded_from_like = "-1"
+
+ expect { described_class.sync! }.to change { UserAction.count }.by(2)
+
+ expect(reaction_user.reload.post_action_like).to be_present
expect(post.reload.like_count).to eq(1)
- expect(post_2.reload.like_count).to eq(0)
+ end
+
+ it "recovers likes the user trashed by switching to an excluded reaction" do
+ DiscourseReactions::ReactionManager.new(reaction_value: "-1", user:, post:).toggle!
+ SiteSetting.discourse_reactions_excluded_from_like = ""
- SiteSetting.discourse_reactions_excluded_from_like = "-1" # clap removed
described_class.sync!
- expect(post.reload.like_count).to eq(1)
- expect(post_2.reload.like_count).to eq(1)
+
+ expect(PostAction.where(user:, post:).count).to eq(1)
end
- it "updates the like_count on the associated Topic records" do
- expect(post.topic.reload.like_count).to eq(1)
- expect(post_2.topic.reload.like_count).to eq(0)
+ it "ignores deleted posts" do
+ admin = Fabricate(:admin)
+ PostDestroyer.new(admin, post).destroy
+ PostDestroyer.new(admin, post_2).destroy
- SiteSetting.discourse_reactions_excluded_from_like = "-1" # clap removed
described_class.sync!
- expect(post.topic.reload.like_count).to eq(1)
- expect(post_2.topic.reload.like_count).to eq(1)
+
+ expect(
+ PostAction.where(user:, post_action_type_id: PostActionType::LIKE_POST_ACTION_ID),
+ ).to be_empty
+ end
+
+ it "never violates the uniqueness of likes" do
+ [post, post_2, post_2].each do |liked_post|
+ PostAction.new(
+ user:,
+ post: liked_post,
+ post_action_type_id: PostActionType::LIKE_POST_ACTION_ID,
+ deleted_at: 1.day.ago,
+ ).save!(validate: false)
+ end
+
+ expect { described_class.sync! }.not_to raise_error
+ expect(PostAction.where(user:, post: [post, post_2]).group(:post_id).count).to eq(
+ post.id => 1,
+ post_2.id => 1,
+ )
end
- it "updates the liked column on TopicUser for associated topic and users" do
- expect(post.topic.topic_users.find_by(user: user).liked).to eq(true)
- expect(post_2.topic.topic_users.find_by(user: user).liked).to eq(false)
+ it "never creates or recovers self-likes" do
+ SiteSetting.discourse_reactions_excluded_from_like = "-1|+1|clap"
+ described_class.sync!
+ Post.where(id: [post.id, post_2.id]).update_all(user_id: user.id)
+ SiteSetting.discourse_reactions_excluded_from_like = "-1"
- SiteSetting.discourse_reactions_excluded_from_like = "-1" # clap removed
described_class.sync!
- expect(post.topic.topic_users.find_by(user: user).liked).to eq(true)
- expect(post_2.topic.topic_users.find_by(user: user).liked).to eq(true)
+
+ expect(
+ PostAction.where(user:, post_action_type_id: PostActionType::LIKE_POST_ACTION_ID),
+ ).to be_empty
end
- it "updates the UserStat likes_given and likes_received columns" do
- expect(user.user_stat.reload.likes_given).to eq(1)
- expect(post.user.user_stat.reload.likes_received).to eq(1)
- expect(post_2.user.user_stat.reload.likes_received).to eq(0)
+ it "ignores private message likes in user stats" do
+ pm_post = Fabricate(:private_message_post, user: post_2.user, recipient: user)
+ PostActionCreator.like(user, pm_post)
- SiteSetting.discourse_reactions_excluded_from_like = "-1" # clap removed
described_class.sync!
+
expect(user.user_stat.reload.likes_given).to eq(2)
- expect(post.user.user_stat.reload.likes_received).to eq(1)
expect(post_2.user.user_stat.reload.likes_received).to eq(1)
end
- it "updates/recalculates the GivenDailyLike table likes_given on all given_date days and deletes records where likes_given would be 0" do
- SiteSetting.discourse_reactions_excluded_from_like = "-1" # clap removed
- expect { described_class.sync! }.to change {
- GivenDailyLike.find_by(user: user, given_date: Time.now.to_date).likes_given
- }.to 2
+ it "excludes whispers and deleted posts from topic like counts" do
+ post_2.update_columns(post_type: Post.types[:whisper])
+ Fabricate(:post, topic: post_2.topic, like_count: 1, deleted_at: Time.zone.now)
+
+ described_class.sync!
+
+ expect(post_2.topic.reload.like_count).to eq(0)
+ end
+
+ it "ignores posts without an author" do
+ post_2.update_columns(user_id: nil)
+
+ expect { described_class.sync! }.not_to change { UserAction.count }
+ expect(reaction_user_2.reload.post_action_like).to be_nil
end
end
end