Skip to content

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

FIX: Recalculate like counters with one shared rule

Régis Hanol5ac46a970dc5
diff --git a/app/models/directory_item.rb b/app/models/directory_item.rb
index 01f1d43577a..f01cd99d4eb 100644
--- a/app/models/directory_item.rb
+++ b/app/models/directory_item.rb
@@ -196,10 +196,8 @@ class DirectoryItem < ActiveRecord::Base
visible_posts AS (
SELECT p.id
FROM posts p
- JOIN visible_topics vt ON vt.id = p.topic_id
- WHERE p.deleted_at IS NULL
- AND p.post_type = :regular_post_type
- AND NOT p.hidden
+ JOIN topics t ON t.id = p.topic_id
+ WHERE #{UserStat::COUNTABLE_POST_CONDITIONS}
)
SELECT
ua.user_id,
diff --git a/app/models/given_daily_like.rb b/app/models/given_daily_like.rb
index 165ab8c78f8..0f043284975 100644
--- a/app/models/given_daily_like.rb
+++ b/app/models/given_daily_like.rb
@@ -7,6 +7,27 @@ class GivenDailyLike < ActiveRecord::Base
where(user_id: user_id, given_date: date)
end
+ def self.recalculate!(user_ids)
+ transaction do
+ where(user_id: user_ids).delete_all
+
+ DB.exec(
+ <<~SQL,
+ INSERT INTO given_daily_likes (user_id, given_date, likes_given, limit_reached)
+ SELECT user_id, created_at::DATE, COUNT(*), COUNT(*) >= :max_likes_per_day
+ FROM post_actions
+ WHERE user_id IN (:user_ids)
+ AND post_action_type_id = :like
+ AND deleted_at IS NULL
+ GROUP BY user_id, created_at::DATE
+ SQL
+ user_ids:,
+ like: PostActionType::LIKE_POST_ACTION_ID,
+ max_likes_per_day: SiteSetting.max_likes_per_day,
+ )
+ end
+ end
+
def self.increment_for(user_id)
return unless user_id
given_date = Date.today
diff --git a/app/models/post.rb b/app/models/post.rb
index 7d982dd6391..9dd804c9ae0 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -191,6 +191,33 @@ class Post < ActiveRecord::Base
@notices ||= Enum.new(custom: "custom", new_user: "new_user", returning_user: "returning_user")
end
+ def self.recalculate_like_counts!(post_ids)
+ DB.exec(
+ <<~SQL,
+ UPDATE posts
+ SET like_count = (
+ SELECT COUNT(*)
+ FROM post_actions
+ WHERE post_actions.post_id = posts.id
+ AND post_actions.post_action_type_id = :like
+ AND post_actions.deleted_at IS NULL
+ ),
+ like_score = (
+ SELECT COALESCE(SUM(CASE WHEN users.moderator OR users.admin THEN :staff_like_weight ELSE 1 END), 0)
+ FROM post_actions
+ INNER JOIN users ON users.id = post_actions.user_id
+ WHERE post_actions.post_id = posts.id
+ AND post_actions.post_action_type_id = :like
+ AND post_actions.deleted_at IS NULL
+ )
+ WHERE posts.id IN (:post_ids)
+ SQL
+ post_ids:,
+ like: PostActionType::LIKE_POST_ACTION_ID,
+ staff_like_weight: SiteSetting.staff_like_weight,
+ )
+ end
+
def self.find_by_detail(key, value)
includes(:post_details).find_by(post_details: { key: key, value: value })
end
diff --git a/app/models/post_action.rb b/app/models/post_action.rb
index 0b4e90b6a38..6bee9a1d9b1 100644
--- a/app/models/post_action.rb
+++ b/app/models/post_action.rb
@@ -226,26 +226,14 @@ class PostAction < ActiveRecord::Base
def update_counters
# Update denormalized counts
column = "#{post_action_type_key}_count"
- count = PostAction.where(post_id: post_id).where(post_action_type_id: post_action_type_id).count
# We probably want to refactor this method to something cleaner.
case post_action_type_key
when :like
- # 'like_score' is weighted higher for staff accounts
- score =
- PostAction
- .joins(:user)
- .where(post_id: post_id)
- .sum(
- "CASE WHEN users.moderator OR users.admin THEN #{SiteSetting.staff_like_weight} ELSE 1 END",
- )
- Post.where(id: post_id).update_all [
- "like_count = :count, like_score = :score",
- count: count,
- score: score,
- ]
+ Post.recalculate_like_counts!([post_id])
else
if ActiveRecord::Base.connection.column_exists?(:posts, column)
+ count = PostAction.where(post_id:, post_action_type_id:).count
Post.where(id: post_id).update_all ["#{column} = ?", count]
end
end
@@ -261,7 +249,7 @@ class PostAction < ActiveRecord::Base
)
end
- Topic.find_by(id: topic_id)&.update_action_counts if column == "like_count"
+ Topic.recalculate_like_counts!([topic_id]) if column == "like_count"
end
end
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 86886e9bb2c..1fca8296903 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -495,6 +495,37 @@ class Topic < ActiveRecord::Base
)
end
+ def self.recalculate_like_counts!(topic_ids)
+ counts = DB.query(<<~SQL, topic_ids:, whisper: Post.types[:whisper])
+ WITH previous AS (SELECT id, like_count FROM topics WHERE id IN (:topic_ids))
+ UPDATE topics
+ SET like_count = (
+ SELECT COALESCE(SUM(posts.like_count), 0)
+ FROM posts
+ WHERE posts.topic_id = topics.id
+ AND posts.post_type <> :whisper
+ AND posts.deleted_at IS NULL
+ )
+ FROM previous
+ WHERE topics.id = previous.id
+ RETURNING topics.id, previous.like_count AS previous_count, topics.like_count AS current_count
+ SQL
+
+ crossed =
+ counts.select do |count|
+ og_image_counter_regeneration_needed?(count.previous_count, count.current_count)
+ end
+ topics = where(id: crossed.map(&:id)).index_by(&:id)
+ crossed.each do |count|
+ topics[count.id]&.enqueue_og_image_regeneration_after_counter_change(
+ previous_count: count.previous_count,
+ current_count: count.current_count,
+ )
+ end
+
+ counts
+ end
+
def self.og_image_counter_regeneration_needed?(previous_count, current_count)
previous_count = previous_count.to_i
current_count = current_count.to_i
@@ -1459,15 +1490,9 @@ class Topic < ActiveRecord::Base
end
def update_action_counts
- old_like_count = like_count
- new_like_count =
- Post.where.not(post_type: Post.types[:whisper]).where(topic_id: id).sum(:like_count)
-
- update_column(:like_count, new_like_count)
- enqueue_og_image_regeneration_after_counter_change(
- previous_count: old_like_count,
- current_count: new_like_count,
- )
+ count = Topic.recalculate_like_counts!([id]).first
+ self.like_count = count.current_count
+ clear_attribute_changes(%i[like_count])
end
def posters_summary(options = {}) # avatar lookup in options
diff --git a/app/models/user_stat.rb b/app/models/user_stat.rb
index d2a8af57f0e..fa625edc892 100644
--- a/app/models/user_stat.rb
+++ b/app/models/user_stat.rb
@@ -14,6 +14,43 @@ class UserStat < ActiveRecord::Base
UPDATE_UNREAD_MINUTES_AGO = 10
UPDATE_UNREAD_USERS_LIMIT = 10_000
+ COUNTABLE_POST_CONDITIONS = <<~SQL
+ p.deleted_at IS NULL
+ AND p.post_type = :regular_post_type
+ AND NOT p.hidden
+ AND t.deleted_at IS NULL
+ AND t.visible
+ AND t.archetype = 'regular'
+ SQL
+
+ def self.recalculate_likes!(user_ids)
+ DB.exec(
+ <<~SQL,
+ UPDATE user_stats
+ SET likes_given = (#{countable_user_actions_count_sql(:like_type)}),
+ likes_received = (#{countable_user_actions_count_sql(:was_liked_type)})
+ WHERE user_id IN (:user_ids)
+ SQL
+ user_ids:,
+ like_type: UserAction::LIKE,
+ was_liked_type: UserAction::WAS_LIKED,
+ regular_post_type: Post.types[:regular],
+ )
+ end
+
+ def self.countable_user_actions_count_sql(action_type)
+ <<~SQL
+ SELECT COUNT(*)
+ FROM user_actions ua
+ INNER JOIN posts p ON p.id = ua.target_post_id
+ INNER JOIN topics t ON t.id = p.topic_id
+ WHERE ua.user_id = user_stats.user_id
+ AND ua.action_type = :#{action_type}
+ AND #{COUNTABLE_POST_CONDITIONS}
+ SQL
+ end
+ private_class_method :countable_user_actions_count_sql
+
def self.update_first_unread_pm(last_seen, limit: UPDATE_UNREAD_USERS_LIMIT)
DB.exec(
<<~SQL,
diff --git a/app/services/user_merger.rb b/app/services/user_merger.rb
index c7d78e17cf0..2a007fb2d44 100644
--- a/app/services/user_merger.rb
+++ b/app/services/user_merger.rb
@@ -97,35 +97,7 @@ class UserMerger
user_ids: [@acting_user.id]
end
- sql = <<~SQL
- INSERT INTO given_daily_likes AS g (user_id, likes_given, given_date, limit_reached)
- SELECT
- :target_user_id AS user_id,
- COUNT(1) AS likes_given,
- a.created_at::DATE AS given_date,
- COUNT(1) >= :max_likes_per_day AS limit_reached
- FROM post_actions AS a
- WHERE a.user_id = :target_user_id
- AND a.deleted_at IS NULL
- AND EXISTS(
- SELECT 1
- FROM given_daily_likes AS g
- WHERE g.user_id = :source_user_id AND a.created_at::DATE = g.given_date
- )
- GROUP BY given_date
- ON CONFLICT (user_id, given_date)
- DO UPDATE
- SET likes_given = EXCLUDED.likes_given,
- limit_reached = EXCLUDED.limit_reached
- SQL
-
- DB.exec(
- sql,
- source_user_id: @source_user.id,
- target_user_id: @target_user.id,
- max_likes_per_day: SiteSetting.max_likes_per_day,
- action_type_id: PostActionType.types[:like],
- )
+ GivenDailyLike.recalculate!([@target_user.id])
end
def merge_post_timings
@@ -235,12 +207,12 @@ class UserMerger
WHERE user_id = :target_user_id
SQL
- # likes_given, likes_received, new_since, read_faq, first_post_created_at
+ UserStat.recalculate_likes!([@target_user.id])
+
+ # new_since, read_faq, first_post_created_at
DB.exec(<<~SQL, source_user_id: @source_user.id, target_user_id: @target_user.id)
UPDATE user_stats AS t
- SET likes_given = t.likes_given + s.likes_given,
- likes_received = t.likes_received + s.likes_received,
- new_since = LEAST(t.new_since, s.new_since),
+ SET new_since = LEAST(t.new_since, s.new_since),
read_faq = LEAST(t.read_faq, s.read_faq),
first_post_created_at = LEAST(t.first_post_created_at, s.first_post_created_at)
FROM user_stats AS s
diff --git a/spec/models/given_daily_like_spec.rb b/spec/models/given_daily_like_spec.rb
index 34a3cc26f4a..c97fe07f812 100644
--- a/spec/models/given_daily_like_spec.rb
+++ b/spec/models/given_daily_like_spec.rb
@@ -6,6 +6,30 @@ RSpec.describe GivenDailyLike do
expect { GivenDailyLike.decrement_for(nil) }.not_to raise_error
end
+ describe ".recalculate!" do
+ fab!(:user)
+
+ it "recounts live likes per day and deletes days without any" do
+ SiteSetting.max_likes_per_day = 2
+ freeze_time
+ Fabricate(:like, user:, created_at: 1.day.ago)
+ Fabricate(:like, user:)
+ Fabricate(:like, user:)
+ Fabricate(:like, user:, deleted_at: Time.zone.now)
+ Fabricate(:post_action, user:, post_action_type_id: PostActionType.types[:off_topic])
+ GivenDailyLike.create!(user:, given_date: 2.days.ago.to_date, likes_given: 5)
+
+ GivenDailyLike.recalculate!([user.id])
+
+ expect(
+ GivenDailyLike
+ .where(user:)
+ .order(:given_date)
+ .pluck(:given_date, :likes_given, :limit_reached),
+ ).to eq([[1.day.ago.to_date, 1, false], [Date.current, 2, true]])
+ end
+ end
+
context "with a user" do
fab!(:user)
diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb
index e64db73a816..40eac0566c2 100644
--- a/spec/models/post_action_spec.rb
+++ b/spec/models/post_action_spec.rb
@@ -437,6 +437,13 @@ RSpec.describe PostAction do
expect(post.topic.like_count).to eq(0)
end
+ it "only counts likes in like_score" do
+ PostActionCreator.off_topic(moderator, post)
+ PostActionCreator.like(codinghorror, post)
+
+ expect(post.reload.like_score).to eq(1)
+ end
+
it "changes given_likes only when likes are added or removed" do
freeze_time
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index 0897b942c61..537dfbe8a2f 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -4034,6 +4034,20 @@ RSpec.describe Topic do
end
end
+ describe ".recalculate_like_counts!" do
+ it "sums the like counts of each topic's live non-whisper posts" do
+ topic_1 = Fabricate(:post, like_count: 2).topic
+ Fabricate(:post, topic: topic_1, like_count: 3, post_type: Post.types[:whisper])
+ Fabricate(:post, topic: topic_1, like_count: 4, deleted_at: Time.zone.now)
+ topic_2 = Fabricate(:post, like_count: 5).topic
+
+ Topic.recalculate_like_counts!([topic_1.id, topic_2.id])
+
+ expect(topic_1.reload.like_count).to eq(2)
+ expect(topic_2.reload.like_count).to eq(5)
+ end
+ end
+
describe "#update_action_counts" do
let(:topic) { Fabricate(:topic) }
diff --git a/spec/models/user_stat_spec.rb b/spec/models/user_stat_spec.rb
index cf017da5aa4..783b53cc5b7 100644
--- a/spec/models/user_stat_spec.rb
+++ b/spec/models/user_stat_spec.rb
@@ -9,6 +9,58 @@ RSpec.describe UserStat do
expect(user.user_stat.new_since).to be_present
end
+ describe ".recalculate_likes!" do
+ fab!(:liker, :user)
+ fab!(:author, :user)
+
+ def like!(post)
+ UserAction.create!(
+ action_type: UserAction::LIKE,
+ user_id: liker.id,
+ acting_user_id: liker.id,
+ target_post_id: post.id,
+ target_topic_id: post.topic_id,
+ )
+ UserAction.create!(
+ action_type: UserAction::WAS_LIKED,
+ user_id: author.id,
+ acting_user_id: liker.id,
+ target_post_id: post.id,
+ target_topic_id: post.topic_id,
+ )
+ end
+
+ it "only counts likes on visible regular posts in visible public topics" do
+ like!(Fabricate(:post, user: author))
+ like!(Fabricate(:private_message_post, user: author))
+ like!(Fabricate(:post, user: author, post_type: Post.types[:whisper]))
+ like!(Fabricate(:post, user: author, hidden: true))
+ like!(Fabricate(:post, user: author, deleted_at: Time.zone.now))
+ like!(Fabricate(:post, user: author, topic: Fabricate(:topic, visible: false)))
+ liker.user_stat.update!(likes_given: 42)
+
+ UserStat.recalculate_likes!([liker.id, author.id])
+
+ expect(liker.user_stat.reload).to have_attributes(likes_given: 1, likes_received: 0)
+ expect(author.user_stat.reload).to have_attributes(likes_given: 0, likes_received: 1)
+ end
+
+ it "matches the directory refresh" do
+ like!(Fabricate(:post, user: author))
+ like!(Fabricate(:post, user: author, hidden: true))
+ DirectoryItem.refresh_period!(:all, force: true)
+ directory_counts =
+ UserStat.where(user_id: [liker.id, author.id]).pluck(:likes_given, :likes_received)
+ UserStat.where(user_id: [liker.id, author.id]).update_all(likes_given: 9, likes_received: 9)
+
+ UserStat.recalculate_likes!([liker.id, author.id])
+
+ expect(
+ UserStat.where(user_id: [liker.id, author.id]).pluck(:likes_given, :likes_received),
+ ).to eq(directory_counts)
+ end
+ end
+
describe "#update_view_counts" do
let(:user) { Fabricate(:user) }
let(:stat) { user.user_stat }
diff --git a/spec/services/user_merger_spec.rb b/spec/services/user_merger_spec.rb
index 814e5734d69..3a12a96ad29 100644
--- a/spec/services/user_merger_spec.rb
+++ b/spec/services/user_merger_spec.rb
@@ -168,6 +168,7 @@ RSpec.describe UserMerger do
end
it "merges likes" do
+ UserActionManager.enable
now = Time.zone.now
freeze_time(now - 1.day)
@@ -193,6 +194,7 @@ RSpec.describe UserMerger do
expect(given_daily_like_count_for(target_user, Date.yesterday)).to eq(3)
expect(given_daily_like_count_for(source_user, Date.today)).to eq(0)
expect(given_daily_like_count_for(target_user, Date.today)).to eq(2)
+ expect(target_user.user_stat.reload.likes_given).to eq(5)
end
end