Work in progress backfill for like badges

This commit is contained in:
Sam 2014-07-01 22:00:31 +10:00
parent a60a1c0181
commit bc44bfcdf2
6 changed files with 80 additions and 3 deletions

View File

@ -27,6 +27,7 @@
</div> </div>
</div> </div>
{{/link-to}} {{/link-to}}
<!-- <a href="{{unbound topic.url}}/{{unbound post_number}}"><span class="post-number">#{{post_number}}</span> {{topic.title}}</a> -->
{{/each}} {{/each}}
</div> </div>
{{#if canLoadMore}} {{#if canLoadMore}}

View File

@ -28,7 +28,7 @@ module Jobs
# Grant "Welcome" badge to the user if they do not already have it. # Grant "Welcome" badge to the user if they do not already have it.
BadgeGranter.grant(Badge.find(5), user) BadgeGranter.grant(Badge.find(5), user)
[{id: 6, count: 10}, {id: 7, count: 25}, {id: 8, count: 100}].each do |b| Badge.like_badge_info.each do |b|
if post.like_count >= b[:count] if post.like_count >= b[:count]
BadgeGranter.grant(Badge.find(b[:id]), user, post_id: post.id) BadgeGranter.grant(Badge.find(b[:id]), user, post_id: post.id)
else else

View File

@ -7,6 +7,11 @@ class Badge < ActiveRecord::Base
validates :allow_title, inclusion: [true, false] validates :allow_title, inclusion: [true, false]
validates :multiple_grant, inclusion: [true, false] validates :multiple_grant, inclusion: [true, false]
Welcome = 5
NicePost = 6
GoodPost = 7
GreatPost = 8
def self.trust_level_badge_ids def self.trust_level_badge_ids
(1..4).to_a (1..4).to_a
end end
@ -20,6 +25,14 @@ class Badge < ActiveRecord::Base
!self.multiple_grant? !self.multiple_grant?
end end
def self.like_badge_info
[
{id: NicePost, count: 10},
{id: GoodPost, count: 25},
{id: GreatPost, count: 100}
]
end
end end
# == Schema Information # == Schema Information

View File

@ -1,5 +1,5 @@
class UserBadgeSerializer < ApplicationSerializer class UserBadgeSerializer < ApplicationSerializer
attributes :id, :granted_at, :count, :post_id attributes :id, :granted_at, :count, :post_id, :post_number
has_one :badge has_one :badge
has_one :user, serializer: BasicUserSerializer, root: :users has_one :user, serializer: BasicUserSerializer, root: :users
@ -11,10 +11,15 @@ class UserBadgeSerializer < ApplicationSerializer
end end
def include_post_id? def include_post_id?
!object.post_id.nil? object.post_id && object.post
end end
alias :include_topic? :include_post_id? alias :include_topic? :include_post_id?
def post_number
object.post && object.post.post_number
end
def topic def topic
object.post.topic object.post.topic
end end

View File

@ -55,4 +55,49 @@ class BadgeGranter
Jobs.enqueue(:update_badges, args) Jobs.enqueue(:update_badges, args)
end end
def self.backfill_like_badges
Badge.like_badge_info.each do |info|
sql = "
DELETE FROM user_badges
WHERE badge_id = :id AND
NOT EXISTS (SELECT 1 FROM posts p
JOIN topics t on p.topic_id = t.id
WHERE p.deleted_at IS NULL AND
t.deleted_at IS NULL AND
t.visible AND
post_id = p.id AND
p.like_count >= :count
)
"
Badge.exec_sql(sql, info)
sql = "
INSERT INTO user_badges(badge_id, user_id, granted_at, granted_by_id, post_id)
SELECT :id, p.user_id, :now, -1, p.id
FROM posts p
JOIN topics t on p.topic_id = t.id
WHERE p.deleted_at IS NULL AND
t.deleted_at IS NULL AND
t.visible AND
p.like_count >= :count AND
NOT EXISTS (SELECT 1 FROM user_badges ub
WHERE ub.post_id = p.id AND
ub.badge_id = :id AND
ub.user_id = p.user_id)
"
Badge.exec_sql(sql, info.merge(now: Time.now))
sql = "
UPDATE badges b
SET grant_count = (SELECT COUNT(*) FROM user_badges WHERE badge_id = :id)
WHERE b.id = :id
"
Badge.exec_sql(sql, info)
end
end
end end

View File

@ -9,6 +9,19 @@ describe BadgeGranter do
SiteSetting.enable_badges = true SiteSetting.enable_badges = true
end end
describe 'backfill like badges' do
it 'should grant missing badges' do
post = Fabricate(:post, like_count: 30)
BadgeGranter.backfill_like_badges
# TODO add welcome
post.user.user_badges.pluck(:badge_id).sort.should == [Badge::NicePost,Badge::GoodPost]
Badge.find(Badge::NicePost).grant_count.should == 1
Badge.find(Badge::GoodPost).grant_count.should == 1
end
end
describe 'grant' do describe 'grant' do
it 'grants a badge' do it 'grants a badge' do