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.
This commit is contained in:
Sam Saffron 2019-04-03 16:41:52 +11:00
parent b2dc65f953
commit 1c57ae6657
2 changed files with 31 additions and 1 deletions

View File

@ -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

View File

@ -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