[deploy] Prompt new users to customize their experience (#6959)

* Rename find_broadcast to find_auth_broadcast

* Fix broken div rendering on /notifications tab

* Allow customize notification to be enqueued

* Add specs for customize welcome notification

Also clean up existing specs to match!

* Rename customization notification method for clarity

* Remove some redundant assertions in generator spec
This commit is contained in:
Vaidehi Joshi 2020-03-30 16:17:52 -07:00 committed by GitHub
parent b6fd02b70f
commit 28956d90a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 28 deletions

View file

@ -16,6 +16,7 @@ module Broadcasts
send_welcome_notification unless notification_enqueued
send_authentication_notification unless notification_enqueued
send_ux_customization_notification unless notification_enqueued
end
private
@ -36,6 +37,13 @@ module Broadcasts
@notification_enqueued = true
end
def send_ux_customization_notification
return if received_notification?(customize_broadcast) || user.created_at > 5.days.ago
Notification.send_welcome_notification(user.id, customize_broadcast.id)
@notification_enqueued = true
end
def received_notification?(broadcast)
Notification.exists?(notifiable: broadcast, user: user)
end
@ -53,15 +61,19 @@ module Broadcasts
@welcome_broadcast ||= Broadcast.find_by(title: "Welcome Notification: welcome_thread")
end
def customize_broadcast
@customize_broadcast ||= Broadcast.find_by(title: "Welcome Notification: customize_experience")
end
def identities
@identities ||= user.identities.where(provider: SiteConfig.authentication_providers)
end
def authentication_broadcast
@authentication_broadcast ||= find_broadcast
@authentication_broadcast ||= find_auth_broadcast
end
def find_broadcast
def find_auth_broadcast
missing_identities = SiteConfig.authentication_providers.map do |provider|
identities.exists?(provider: provider) ? nil : "#{provider}_connect"
end.compact

View file

@ -10,7 +10,7 @@
<%= json_data["broadcast"]["processed_html"].html_safe %>
<% if notification.json_data['broadcast']['type_of'] == "Welcome" %>
<div class="opt-out"><a href="<%= user_settings_path(tab: :notifications) %>">Go to your settings to manage your notification settings.</a></<div>
<div class="opt-out"><a href="<%= user_settings_path(tab: :notifications) %>">Go to your settings to manage your notification settings.</a></div>
<% end %>
</div>
<% end %>

View file

@ -249,7 +249,8 @@ broadcast_messages = {
set_up_profile: "Welcome to DEV! 👋 I'm Sloan, the community mascot and I'm here to help get you started. Let's begin by <a href='/settings'>setting up your profile</a>!",
welcome_thread: "Sloan here again! 👋 DEV is a friendly community. Why not introduce yourself by leaving a comment in <a href='/welcome'>the welcome thread</a>!",
twitter_connect: "You're on a roll! 🎉 Let's connect your <a href='/settings'> Twitter account</a> to complete your identity so that we don't think you're a robot. 🤖",
github_connect: "You're on a roll! 🎉 Let's connect your <a href='/settings'> GitHub account</a> to complete your identity so that we don't think you're a robot. 🤖"
github_connect: "You're on a roll! 🎉 Let's connect your <a href='/settings'> GitHub account</a> to complete your identity so that we don't think you're a robot. 🤖",
customize_experience: "Sloan here! 👋 Did you know that that you can customize your DEV experience? Try changing <a href='settings/ux'>your font and theme</a> and find the best style for you!"
}
broadcast_messages.each do |type, message|

View file

@ -20,6 +20,12 @@ FactoryBot.define do
processed_html { "You're on a roll! 🎉 Let's connect your <a href='/settings'> GitHub account</a> to complete your identity so that we don't think you're a robot. 🤖" }
end
factory :customize_broadcast do
title { "Welcome Notification: customize_experience" }
type_of { "Welcome" }
processed_html { "Sloan here! 👋 Did you know that that you can customize your DEV experience? Try changing <a href='settings/ux'>your font and theme</a> and find the best style for you!" }
end
# TODO: [@thepracticaldev/delightful] Remove onboarding factory once welcome notifications are live.
factory :onboarding_broadcast do
title { "Welcome Notification" }

View file

@ -6,8 +6,10 @@ RSpec.describe Broadcasts::WelcomeNotification::Generator, type: :service do
let!(:welcome_broadcast) { create(:welcome_broadcast) }
let!(:twitter_connect_broadcast) { create(:twitter_connect_broadcast) }
let!(:github_connect_broadcast) { create(:github_connect_broadcast) }
let!(:customize_broadcast) { create(:customize_broadcast) }
before do
allow(Notification).to receive(:send_welcome_notification).and_call_original
allow(User).to receive(:mascot_account).and_return(mascot_account)
SiteConfig.staff_user_id = mascot_account.id
end
@ -58,20 +60,13 @@ RSpec.describe Broadcasts::WelcomeNotification::Generator, type: :service do
end.not_to change(user.notifications, :count)
end
it "does not send a duplicate notifications" do
2.times do
sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_welcome_notification) }
end
it "does not send duplicate notifications" do
2.times { sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_welcome_notification) } }
expect(user.notifications.count).to eq(1)
end
end
describe "#send_authentication_notification" do
before do
allow(Notification).to receive(:send_welcome_notification)
end
it "does not send notification if user is created less than a day ago" do
user = create(:user, :with_identity, identities: ["github"])
sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_authentication_notification) }
@ -81,13 +76,13 @@ RSpec.describe Broadcasts::WelcomeNotification::Generator, type: :service do
it "generates and sends the appropriate broadcast (twitter)" do
user = create(:user, :with_identity, identities: ["github"], created_at: 1.day.ago)
sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_authentication_notification) }
expect(Notification).to have_received(:send_welcome_notification).with(user.id, twitter_connect_broadcast.id)
expect(user.notifications.first.notifiable).to eq(twitter_connect_broadcast)
end
it "generates and sends the appropriate broadcast (github)" do
user = create(:user, :with_identity, identities: ["twitter"], created_at: 1.day.ago)
sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_authentication_notification) }
expect(Notification).to have_received(:send_welcome_notification).with(user.id, github_connect_broadcast.id)
expect(user.notifications.first.notifiable).to eq(github_connect_broadcast)
end
it "does not send notification if user is authenticated with both services" do
@ -96,31 +91,49 @@ RSpec.describe Broadcasts::WelcomeNotification::Generator, type: :service do
expect(Notification).not_to have_received(:send_welcome_notification).with(user.id, github_connect_broadcast.id)
end
it "does not send a duplicate notifications (github)" do
allow(Notification).to receive(:send_welcome_notification).and_call_original
it "does not send duplicate notifications (github)" do
user = create(:user, :with_identity, identities: ["twitter"], created_at: 1.day.ago)
2.times do
sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_authentication_notification) }
end
expect(Notification).to have_received(:send_welcome_notification).with(user.id, github_connect_broadcast.id).exactly(:once)
expect(user.notifications.count).to eq(1)
end
it "does not send a duplicate notifications (twitter)" do
allow(Notification).to receive(:send_welcome_notification).and_call_original
it "does not send duplicate notifications (twitter)" do
user = create(:user, :with_identity, identities: ["github"], created_at: 1.day.ago)
2.times do
sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_authentication_notification) }
end
expect(Notification).to have_received(:send_welcome_notification).with(user.id, twitter_connect_broadcast.id).exactly(:once)
expect(user.notifications.count).to eq(1)
end
end
context "when sending a set_up_profile notification" do
xit "generates the appropriate broadcast to be sent to a user"
xit "it sends a welcome notification for that broadcast"
xit "it does not send duplicate welcome notification for that broadcast"
xit "does not send a notification to a user who has set up their profile"
describe "#send_ux_customization_notification" do
# Ensure that the user is already authenticated with all providers and has commented on the
# welcome thread so that we can actually trigger just the customize notification only.
let(:user) { create(:user, :with_identity, identities: %w[twitter github], created_at: 5.days.ago) }
before do
create(:comment, commentable: welcome_thread, commentable_type: "Article", user: user)
end
it "does not send a notification to a newly-created user" do
user.update!(created_at: Time.zone.now)
sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_ux_customization_notification) }
expect(Notification).not_to have_received(:send_welcome_notification)
end
it "generates the correct broadcast type and sends the notification to the user" do
sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_ux_customization_notification) }
expect(user.notifications.count).to eq(1)
expect(user.notifications.first.notifiable).to eq(customize_broadcast)
end
it "does not send duplicate notifications" do
2.times do
sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_ux_customization_notification) }
end
expect(user.notifications.count).to eq(1)
end
end
end