diff --git a/app/services/broadcasts/welcome_notification/generator.rb b/app/services/broadcasts/welcome_notification/generator.rb index 143ef48fe..4c1d4756e 100644 --- a/app/services/broadcasts/welcome_notification/generator.rb +++ b/app/services/broadcasts/welcome_notification/generator.rb @@ -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 diff --git a/app/views/notifications/_broadcast.html.erb b/app/views/notifications/_broadcast.html.erb index 13c75e5e2..eb8994fc6 100644 --- a/app/views/notifications/_broadcast.html.erb +++ b/app/views/notifications/_broadcast.html.erb @@ -10,7 +10,7 @@ <%= json_data["broadcast"]["processed_html"].html_safe %> <% if notification.json_data['broadcast']['type_of'] == "Welcome" %> -
Go to your settings to manage your notification settings. +
Go to your settings to manage your notification settings.
<% end %>
<% end %> diff --git a/db/seeds.rb b/db/seeds.rb index 5e3c903ec..06af6c56b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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 setting up your profile!", welcome_thread: "Sloan here again! 👋 DEV is a friendly community. Why not introduce yourself by leaving a comment in the welcome thread!", twitter_connect: "You're on a roll! 🎉 Let's connect your Twitter account 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 GitHub account 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 GitHub account 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 your font and theme and find the best style for you!" } broadcast_messages.each do |type, message| diff --git a/spec/factories/broadcasts.rb b/spec/factories/broadcasts.rb index 3547eb6cf..7bc8089e8 100644 --- a/spec/factories/broadcasts.rb +++ b/spec/factories/broadcasts.rb @@ -20,6 +20,12 @@ FactoryBot.define do processed_html { "You're on a roll! 🎉 Let's connect your GitHub account 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 your font and theme 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" } diff --git a/spec/services/broadcasts/welcome_notification/generator_spec.rb b/spec/services/broadcasts/welcome_notification/generator_spec.rb index 7bf68b495..a427730cc 100644 --- a/spec/services/broadcasts/welcome_notification/generator_spec.rb +++ b/spec/services/broadcasts/welcome_notification/generator_spec.rb @@ -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