FEATURE: Nice Topic, Good Topic and Great Topic badges

Note we will revoke all old badges post badges that went to post 
and instead get topic badges
This commit is contained in:
Sam 2014-09-11 12:36:37 +10:00
parent 61bcde6284
commit 7f3797b635
4 changed files with 35 additions and 16 deletions
app/models
config/locales
db/fixtures
spec/services

View file

@ -13,6 +13,9 @@ class Badge < ActiveRecord::Base
FirstQuote = 15
ReadGuidelines = 16
Reader = 17
NiceTopic = 18
GoodTopic = 19
GreatTopic = 20
# other consts
AutobiographerMinBioLength = 10
@ -165,12 +168,12 @@ SQL
(:backfill OR u.id IN (:user_ids) )
SQL
def self.like_badge(count)
def self.like_badge(count, is_topic)
# we can do better with dates, but its hard work
"
SELECT p.user_id, p.id post_id, p.updated_at granted_at
FROM badge_posts p
WHERE p.like_count >= #{count.to_i} AND
WHERE #{is_topic ? "p.post_number = 1" : "p.post_number > 1" } AND p.like_count >= #{count.to_i} AND
(:backfill OR p.id IN (:post_ids) )
"
end

View file

@ -2181,6 +2181,15 @@ en:
great_post:
name: Great Post
description: Received 50 likes on a post. This badge can be granted multiple times
nice_topic:
name: Nice Topic
description: Received 10 likes on a topic. This badge can be granted multiple times
good_post:
name: Good Topic
description: Received 25 likes on a topic. This badge can be granted multiple times
great_post:
name: Great Topic
description: Received 50 likes on a topic. This badge can be granted multiple times
first_like:
name: First Like
description: Liked a post

View file

@ -191,20 +191,24 @@ end
#
# Like system badges.
like_badges = [
{id: 6, name: "Nice Post", type: BadgeType::Bronze, multiple: true},
{id: 7, name: "Good Post", type: BadgeType::Silver, multiple: true},
{id: 8, name: "Great Post", type: BadgeType::Gold, multiple: true}
{id: Badge::NicePost, name: "Nice Post", type: BadgeType::Bronze},
{id: Badge::GoodPost, name: "Good Post", type: BadgeType::Silver},
{id: Badge::GreatPost, name: "Great Post", type: BadgeType::Gold},
{id: Badge::NiceTopic, name: "Nice Topic", type: BadgeType::Bronze, topic: true},
{id: Badge::GoodTopic, name: "Good Topic", type: BadgeType::Silver, topic: true},
{id: Badge::GreatTopic, name: "Great Topic", type: BadgeType::Gold, topic: true}
]
like_badges.each do |spec|
Badge.seed do |b|
b.id = spec[:id]
b.default_name = spec[:name]
b.badge_type_id = spec[:type]
b.multiple_grant = spec[:multiple]
b.multiple_grant = true
b.target_posts = true
b.show_posts = true
b.query = Badge::Queries.like_badge(Badge.like_badge_counts[spec[:id]])
b.query = Badge::Queries.like_badge(Badge.like_badge_counts[spec[:id]], spec[:topic])
b.default_badge_grouping_id = BadgeGrouping::Posting
b.trigger = Badge::Trigger::PostAction
b.system = true

View file

@ -42,17 +42,17 @@ describe BadgeGranter do
it 'should grant missing badges' do
post = Fabricate(:post, like_count: 30)
2.times {
BadgeGranter.backfill(Badge.find(Badge::NicePost), post_ids: [post.id])
BadgeGranter.backfill(Badge.find(Badge::GoodPost))
BadgeGranter.backfill(Badge.find(Badge::NiceTopic), post_ids: [post.id])
BadgeGranter.backfill(Badge.find(Badge::GoodTopic))
}
# TODO add welcome
post.user.user_badges.pluck(:badge_id).sort.should == [Badge::NicePost,Badge::GoodPost]
post.user.user_badges.pluck(:badge_id).sort.should == [Badge::NiceTopic,Badge::GoodTopic]
post.user.notifications.count.should == 2
Badge.find(Badge::NicePost).grant_count.should == 1
Badge.find(Badge::GoodPost).grant_count.should == 1
Badge.find(Badge::NiceTopic).grant_count.should == 1
Badge.find(Badge::GoodTopic).grant_count.should == 1
end
end
@ -187,26 +187,29 @@ describe BadgeGranter do
BadgeGranter.process_queue!
UserBadge.find_by(user_id: user.id, badge_id: 5).should_not be_nil
post = create_post(topic: post.topic, user: user)
action = PostAction.act(liker, post, PostActionType.types[:like])
# Nice post badge
post.update_attributes like_count: 10
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: action)
BadgeGranter.process_queue!
UserBadge.find_by(user_id: user.id, badge_id: 6).should_not be_nil
UserBadge.where(user_id: user.id, badge_id: 6).count.should == 1
UserBadge.find_by(user_id: user.id, badge_id: Badge::NicePost).should_not be_nil
UserBadge.where(user_id: user.id, badge_id: Badge::NicePost).count.should == 1
# Good post badge
post.update_attributes like_count: 25
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: action)
BadgeGranter.process_queue!
UserBadge.find_by(user_id: user.id, badge_id: 7).should_not be_nil
UserBadge.find_by(user_id: user.id, badge_id: Badge::GoodPost).should_not be_nil
# Great post badge
post.update_attributes like_count: 50
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: action)
BadgeGranter.process_queue!
UserBadge.find_by(user_id: user.id, badge_id: 8).should_not be_nil
UserBadge.find_by(user_id: user.id, badge_id: Badge::GreatPost).should_not be_nil
# Revoke badges on unlike
post.update_attributes like_count: 49