mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 23:58:31 -05:00
FIX: ensure consistency on topic featured users
This commit is contained in:
parent
f7d4cb6745
commit
5193c9fd16
3 changed files with 76 additions and 0 deletions
|
@ -8,6 +8,7 @@ module Jobs
|
||||||
Group.refresh_automatic_groups!
|
Group.refresh_automatic_groups!
|
||||||
Notification.ensure_consistency!
|
Notification.ensure_consistency!
|
||||||
UserAction.ensure_consistency!
|
UserAction.ensure_consistency!
|
||||||
|
TopicFeaturedUsers.ensure_consistency!
|
||||||
UserStat.update_view_counts(13.hours.ago)
|
UserStat.update_view_counts(13.hours.ago)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -24,6 +24,50 @@ class TopicFeaturedUsers
|
||||||
topic.featured_user4_id].uniq.compact
|
topic.featured_user4_id].uniq.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.ensure_consistency!
|
||||||
|
|
||||||
|
sql = <<SQL
|
||||||
|
|
||||||
|
WITH cte as (
|
||||||
|
SELECT
|
||||||
|
t.id, p.user_id,
|
||||||
|
ROW_NUMBER() OVER(PARTITION BY t.id ORDER BY COUNT(*) DESC) as rank
|
||||||
|
FROM topics t
|
||||||
|
JOIN posts p ON p.topic_id = t.id
|
||||||
|
WHERE p.deleted_at IS NULL AND NOT p.hidden AND p.user_id <> t.user_id AND
|
||||||
|
p.user_id <> t.last_post_user_id
|
||||||
|
GROUP BY t.id, p.user_id
|
||||||
|
)
|
||||||
|
|
||||||
|
UPDATE topics tt
|
||||||
|
SET
|
||||||
|
featured_user1_id = featured_user1,
|
||||||
|
featured_user2_id = featured_user2,
|
||||||
|
featured_user3_id = featured_user3,
|
||||||
|
featured_user4_id = featured_user4
|
||||||
|
FROM (
|
||||||
|
SELECT
|
||||||
|
c.id,
|
||||||
|
MAX(case when c.rank = 1 then c.user_id end) featured_user1,
|
||||||
|
MAX(case when c.rank = 2 then c.user_id end) featured_user2,
|
||||||
|
MAX(case when c.rank = 3 then c.user_id end) featured_user3,
|
||||||
|
MAX(case when c.rank = 4 then c.user_id end) featured_user4
|
||||||
|
FROM cte as c
|
||||||
|
WHERE c.rank <= 4
|
||||||
|
GROUP BY c.id
|
||||||
|
) x
|
||||||
|
WHERE x.id = tt.id AND
|
||||||
|
(
|
||||||
|
COALESCE(featured_user1_id,-99) <> COALESCE(featured_user1,-99) OR
|
||||||
|
COALESCE(featured_user2_id,-99) <> COALESCE(featured_user2,-99) OR
|
||||||
|
COALESCE(featured_user3_id,-99) <> COALESCE(featured_user3,-99) OR
|
||||||
|
COALESCE(featured_user4_id,-99) <> COALESCE(featured_user4,-99)
|
||||||
|
)
|
||||||
|
SQL
|
||||||
|
|
||||||
|
Topic.exec_sql(sql)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def keys(args)
|
def keys(args)
|
||||||
|
|
31
spec/models/topic_featured_users_spec.rb
Normal file
31
spec/models/topic_featured_users_spec.rb
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe TopicFeaturedUsers do
|
||||||
|
it 'ensures consistenct' do
|
||||||
|
|
||||||
|
t = Fabricate(:topic)
|
||||||
|
Fabricate(:post, topic_id: t.id, user_id: t.user_id)
|
||||||
|
p2 = Fabricate(:post, topic_id: t.id)
|
||||||
|
Fabricate(:post, topic_id: t.id, user_id: p2.user_id)
|
||||||
|
p4 = Fabricate(:post, topic_id: t.id)
|
||||||
|
p5 = Fabricate(:post, topic_id: t.id)
|
||||||
|
|
||||||
|
t.update_columns(featured_user1_id: 66,
|
||||||
|
featured_user2_id: 70,
|
||||||
|
featured_user3_id: 12,
|
||||||
|
featured_user4_id: 7,
|
||||||
|
last_post_user_id: p5.user_id
|
||||||
|
)
|
||||||
|
|
||||||
|
TopicFeaturedUsers.ensure_consistency!
|
||||||
|
|
||||||
|
t.reload
|
||||||
|
|
||||||
|
t.featured_user1_id.should == p2.user_id
|
||||||
|
t.featured_user2_id.should == p4.user_id
|
||||||
|
t.featured_user3_id.should == nil
|
||||||
|
t.featured_user4_id.should == nil
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue