[deploy] Strengthen badge achievement code and fix badge email (#7000)

* Add badges and badge achievements to seeds

* Strengthen BadgeAchievement code and fix mailer bug

* Add indentation
This commit is contained in:
rhymes 2020-04-02 15:14:10 +02:00 committed by GitHub
parent 34dd9a1dd4
commit 2926f6be12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 123 additions and 37 deletions

View file

@ -1,5 +1,6 @@
class ApplicationMailer < ActionMailer::Base
layout "mailer"
helper ApplicationHelper
default(
from: -> { "DEV Community <#{SiteConfig.default_site_email}>" },

View file

@ -1,4 +1,7 @@
class BadgeAchievement < ApplicationRecord
CONTEXT_MESSAGE_ALLOWED_TAGS = %w[strong em i b u a code].freeze
CONTEXT_MESSAGE_ALLOWED_ATTRIBUTES = %w[href name].freeze
belongs_to :user
belongs_to :badge
belongs_to :rewarder, class_name: "User", optional: true
@ -7,30 +10,34 @@ class BadgeAchievement < ApplicationRecord
validates :badge_id, uniqueness: { scope: :user_id }
after_create :award_credits
after_create_commit :notify_recipient
after_create_commit :send_email_notification
after_create :award_credits
before_validation :render_rewarding_context_message_html
private
def render_rewarding_context_message_html
return if rewarding_context_message_markdown.blank?
return unless rewarding_context_message_markdown
parsed_markdown = MarkdownParser.new(rewarding_context_message_markdown)
html = parsed_markdown.finalize
final_html = ActionController::Base.helpers.sanitize html,
tags: %w[strong em i b u a code],
attributes: %w[href name]
final_html = ActionController::Base.helpers.sanitize(
html,
tags: CONTEXT_MESSAGE_ALLOWED_TAGS,
attributes: CONTEXT_MESSAGE_ALLOWED_ATTRIBUTES,
)
self.rewarding_context_message = final_html
end
private
def notify_recipient
Notification.send_new_badge_achievement_notification(self)
end
def send_email_notification
return unless user.class.name == "User" && user.email.present? && user.email_badge_notifications
return unless user.is_a?(User)
return unless user.email && user.email_badge_notifications
BadgeAchievements::SendEmailNotificationWorker.perform_async(id)
end

View file

@ -8,23 +8,29 @@
<p>
<%= @badge.description %>
</p>
<% if @badge_achievement.rewarding_context_message %>
<p>
<em>
<%= @badge_achievement.rewarding_context_message.html_safe %>
</em>
</p>
<br />
<% end %>
<p>
<em>
<%= @badge_achievement.rewarding_context_message.html_safe %>
</em>
</p>
<br />
<p>
<a href="<%= "#{ApplicationConfig["APP_DOMAIN"]}/#{@user.username}" %>" style="font-weight:bold;color:white;background:#4e57ef;padding: 5px 10px;border-radius:3px;text-decoration:none">
<a
href="<%= user_url(@user) %>"
style="font-weight:bold;color:white;background:#4e57ef;padding: 5px 10px;border-radius:3px;text-decoration:none">
Check out your profile
</a>
</p>
<hr style="opacity:0.4;width: 150px;margin: 30px auto;" />
<p>
You also get <strong><a href="<%= credits_url %>">5 new credits</a></strong> to use for <strong><a href="<%= root_url %>/listings">community listings</a></strong><br />
You also get <strong><a href="<%= credits_url %>">5 new credits</a></strong> to use for <strong><a href="<%= classified_listings_url %>">community listings</a></strong><br />
if you have anything you'd like to promote. 🎉
</p>
<p>
<strong><a href="<%= root_url %>/about-listings">More information about listings<a></strong>
<strong><a href="<%= app_url("/about-listings") %>">More information about listings<a></strong>
</p>
</center>

View file

@ -4,10 +4,10 @@ Congratulations, <%= @user.name %>! You got the <%= @badge.title %> badge! Be su
<%= strip_tags @badge_achievement.rewarding_context_message %>
https://dev.to/<%= @user.username %>
<%= user_url(@user) %>
You also get 5 new credits to use for community listings if you have anything you'd like to promote: https://dev.to/listings
You also get 5 new credits to use for community listings if you have anything you'd like to promote: <%= classified_listings_url %>
To manage your credits: visit: https://dev.to/credits
To manage your credits: visit: <%= credits_url %>
For more information about listings, visit: https://dev.to/about-listings
For more information about listings, visit: <%= app_url("/about-listings") %>

View file

@ -93,6 +93,8 @@ Organization.find_each do |organization|
end
end
users_in_random_order = User.order(Arel.sql("RANDOM()"))
##############################################################################
counter += 1
@ -324,11 +326,21 @@ HtmlVariant.create!(
counter += 1
Rails.logger.info "#{counter}. Creating Badges"
Badge.create!(
title: Faker::Lorem.word,
description: Faker::Lorem.sentence,
badge_image: File.open(Rails.root.join("app/assets/images/#{rand(1..40)}.png")),
)
5.times do
Badge.create!(
title: "#{Faker::Lorem.word} #{rand(100)}",
description: Faker::Lorem.sentence,
badge_image: File.open(Rails.root.join("app/assets/images/#{rand(1..40)}.png")),
)
end
users_in_random_order.limit(10).each do |user|
user.badge_achievements.create!(
badge: Badge.order(Arel.sql("RANDOM()")).limit(1).take,
rewarding_context_message_markdown: Faker::Markdown.random,
)
end
##############################################################################
@ -375,8 +387,8 @@ end
counter += 1
Rails.logger.info "#{counter}. Creating Classified Listings"
users = User.order(Arel.sql("RANDOM()")).to_a
users.each { |user| Credit.add_to(user, rand(100)) }
users_in_random_order.each { |user| Credit.add_to(user, rand(100)) }
users = users_in_random_order.to_a
listings_categories = ClassifiedListing.categories_available.keys
listings_categories.each_with_index do |category, index|

View file

@ -152,9 +152,9 @@ RSpec.describe NotifyMailer, type: :mailer do
def create_badge_achievement(user, badge, rewarder)
BadgeAchievement.create(
user_id: user.id,
badge_id: badge.id,
rewarder_id: rewarder.id,
user: user,
badge: badge,
rewarder: rewarder,
rewarding_context_message_markdown: "Hello [Yoho](/hey)",
)
end
@ -172,14 +172,74 @@ RSpec.describe NotifyMailer, type: :mailer do
expect(email.to).to eq([user.email])
end
it "includes the tracking pixel" do
expect(email.html_part.body).to include("open.gif")
context "when rendering the HTML email" do
it "includes the tracking pixel" do
expect(email.html_part.body).to include("open.gif")
end
it "includes UTM params" do
expect(email.html_part.body).to include(CGI.escape("utm_medium=email"))
expect(email.html_part.body).to include(CGI.escape("utm_source=notify_mailer"))
expect(email.html_part.body).to include(CGI.escape("utm_campaign=new_badge_email"))
end
it "includes the user URL" do
expect(email.html_part.body).to include(CGI.escape(URL.user(user)))
end
it "includes the listings URL" do
expect(email.html_part.body).to include(
CGI.escape(
Rails.application.routes.url_helpers.classified_listings_url,
),
)
end
it "includes the about listings URL" do
expect(email.html_part.body).to include(
CGI.escape(URL.url("/about-listings")),
)
end
it "includes the rewarding_context_message in the email" do
expect(email.html_part.body).to include("Hello <a")
expect(email.html_part.body).to include(CGI.escape(URL.url("/hey")))
end
it "does not include the rewarding_context_message in the email" do
allow(badge_achievement).to receive(:rewarding_context_message).and_return(nil)
expect(email.html_part.body).not_to include("Hello <a")
expect(email.html_part.body).not_to include(CGI.escape(URL.url("/hey")))
end
end
it "includes UTM params" do
expect(email.html_part.body).to include(CGI.escape("utm_medium=email"))
expect(email.html_part.body).to include(CGI.escape("utm_source=notify_mailer"))
expect(email.html_part.body).to include(CGI.escape("utm_campaign=new_badge_email"))
context "when rendering the text email" do
it "includes the user URL" do
expect(email.text_part.body).to include(URL.user(user))
end
it "includes the listings URL" do
expect(email.text_part.body).to include(
Rails.application.routes.url_helpers.classified_listings_url,
)
end
it "includes the about listings URL" do
expect(email.text_part.body).to include(URL.url("/about-listings"))
end
it "includes the rewarding_context_message in the email" do
expect(email.text_part.body).to include("Hello Yoho")
expect(email.text_part.body).not_to include(URL.url("/hey"))
end
it "does not include the rewarding_context_message in the email" do
allow(badge_achievement).to receive(:rewarding_context_message).and_return(nil)
expect(email.text_part.body).not_to include("Hello Yoho")
expect(email.text_part.body).not_to include(URL.url("/hey"))
end
end
end