From 7f3797b6352456b2f70b28d71c6d5cdd404f9d5e Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Thu, 11 Sep 2014 12:36:37 +1000
Subject: [PATCH] FEATURE: Nice Topic, Good Topic and Great Topic badges

Note we will revoke all old badges post badges that went to post #1
and instead get topic badges
---
 app/models/badge.rb                 |  7 +++++--
 config/locales/client.en.yml        |  9 +++++++++
 db/fixtures/006_badges.rb           | 14 +++++++++-----
 spec/services/badge_granter_spec.rb | 21 ++++++++++++---------
 4 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/app/models/badge.rb b/app/models/badge.rb
index ad776da3f..285355403 100644
--- a/app/models/badge.rb
+++ b/app/models/badge.rb
@@ -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
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index d697cfd8d..5cdf125ac 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -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
diff --git a/db/fixtures/006_badges.rb b/db/fixtures/006_badges.rb
index 690fefb57..4dd292ce6 100644
--- a/db/fixtures/006_badges.rb
+++ b/db/fixtures/006_badges.rb
@@ -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
diff --git a/spec/services/badge_granter_spec.rb b/spec/services/badge_granter_spec.rb
index 38304d468..cd85d3f6b 100644
--- a/spec/services/badge_granter_spec.rb
+++ b/spec/services/badge_granter_spec.rb
@@ -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