FIX: grant first quote at the date post was created

FIX: grant first quote at the date post was created

Previously due to #b2dc65f9534ea date on the quoted_posts table could not be trusted.

This changes it so we use the date on the actual post as the grant date.

Note: there is an edge case where you create a post and only add a quote a week later. In this case the badge will not be awarded at the correct time (it will display it was granted a week ago). That said this is far more rare than the current situation.

diff --git a/lib/badge_queries.rb b/lib/badge_queries.rb
index dec8a45..1a8faf5 100644
--- a/lib/badge_queries.rb
+++ b/lib/badge_queries.rb
@@ -23,7 +23,7 @@ SQL
 SQL
 
   FirstQuote = <<SQL
-  SELECT ids.user_id, q.post_id, q.created_at granted_at
+  SELECT ids.user_id, q.post_id, p3.created_at granted_at
   FROM
   (
     SELECT p1.user_id, MIN(q1.id) id
@@ -34,6 +34,7 @@ SQL
     GROUP BY p1.user_id
   ) ids
   JOIN quoted_posts q ON q.id = ids.id
+  JOIN badge_posts p3 ON q.post_id = p3.id
 SQL
 
   FirstLink = <<SQL
diff --git a/spec/models/badge_spec.rb b/spec/models/badge_spec.rb
index 47f2ec7..e704c9c 100644
--- a/spec/models/badge_spec.rb
+++ b/spec/models/badge_spec.rb
@@ -98,6 +98,35 @@ describe Badge do
     end
   end
 
+  context "First Quote" do
+    let(:quoted_post_badge) do
+      Badge.find(Badge::FirstQuote)
+    end
+
+    it "Awards at the correct award date" do
+      freeze_time
+      post1 = create_post
+
+      raw = <<~RAW
+        [quote="#{post1.user.username}, post:#{post1.post_number}, topic:#{post1.topic_id}"]
+        lorem
+        [/quote]
+      RAW
+
+      post2 = create_post(raw: raw)
+
+      quoted_post = QuotedPost.find_by(post_id: post2.id)
+      freeze_time 1.year.from_now
+      quoted_post.update!(created_at: Time.now)
+
+      BadgeGranter.backfill(quoted_post_badge)
+      user_badge = post2.user.user_badges.find_by(badge_id: quoted_post_badge.id)
+
+      expect(user_badge.granted_at).to eq_time(post2.created_at)
+
+    end
+  end
+
   context "PopularLink badge" do
 
     let(:popular_link_badge) do

GitHub sha: 1c57ae66

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

This commit has been mentioned on Discourse Meta. There might be relevant details there: