From fc728af0cd39ad51c94bae1b2438aa16abeef048 Mon Sep 17 00:00:00 2001 From: Mac Siri Date: Tue, 31 Mar 2020 17:32:11 -0400 Subject: [PATCH] Prompt new users to customize their feed (#6979) --- .../welcome_notification/generator.rb | 23 +++++++++-- db/seeds.rb | 1 + spec/factories/broadcasts.rb | 8 +++- .../welcome_notification/generator_spec.rb | 41 ++++++++++++++++++- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/app/services/broadcasts/welcome_notification/generator.rb b/app/services/broadcasts/welcome_notification/generator.rb index 4c1d4756e..4223b182e 100644 --- a/app/services/broadcasts/welcome_notification/generator.rb +++ b/app/services/broadcasts/welcome_notification/generator.rb @@ -17,6 +17,7 @@ module Broadcasts send_welcome_notification unless notification_enqueued send_authentication_notification unless notification_enqueued send_ux_customization_notification unless notification_enqueued + send_feed_customization_notification unless notification_enqueued end private @@ -38,12 +39,18 @@ module Broadcasts end def send_ux_customization_notification - return if received_notification?(customize_broadcast) || user.created_at > 5.days.ago + return if received_notification?(customize_ux_broadcast) || user.created_at > 5.days.ago - Notification.send_welcome_notification(user.id, customize_broadcast.id) + Notification.send_welcome_notification(user.id, customize_ux_broadcast.id) @notification_enqueued = true end + def send_feed_customization_notification + return if user_is_following_tags? || received_notification?(customize_feed_broadcast) || user.created_at > 3.days.ago + + Notification.send_welcome_notification(user.id, customize_feed_broadcast.id) + end + def received_notification?(broadcast) Notification.exists?(notifiable: broadcast, user: user) end @@ -57,12 +64,20 @@ module Broadcasts identities.count == SiteConfig.authentication_providers.count end + def user_is_following_tags? + user.cached_followed_tag_names.count > 1 + end + def welcome_broadcast @welcome_broadcast ||= Broadcast.find_by(title: "Welcome Notification: welcome_thread") end - def customize_broadcast - @customize_broadcast ||= Broadcast.find_by(title: "Welcome Notification: customize_experience") + def customize_ux_broadcast + @customize_ux_broadcast ||= Broadcast.find_by(title: "Welcome Notification: customize_experience") + end + + def customize_feed_broadcast + @customize_feed_broadcast ||= Broadcast.find_by(title: "Welcome Notification: customize_feed") end def identities diff --git a/db/seeds.rb b/db/seeds.rb index 06af6c56b..5b87a757c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -250,6 +250,7 @@ broadcast_messages = { 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. 🤖", + customize_feed: "Hi, it's me again! 👋 Now that you're a part of the DEV community, let's focus on personalizing your content. You can start by following some tags to help customize your feed! 🎉", 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!" } diff --git a/spec/factories/broadcasts.rb b/spec/factories/broadcasts.rb index 7bc8089e8..7bb4557b0 100644 --- a/spec/factories/broadcasts.rb +++ b/spec/factories/broadcasts.rb @@ -20,12 +20,18 @@ 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 + factory :customize_ux_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 + factory :customize_feed_broadcast do + title { "Welcome Notification: customize_feed" } + type_of { "Welcome" } + processed_html { "Hi, it's me again! 👋 Now that you're a part of the DEV community, let's focus on personalizing your content. You can start by following some tags to help customize your feed! 🎉" } + 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 a427730cc..af8529ab3 100644 --- a/spec/services/broadcasts/welcome_notification/generator_spec.rb +++ b/spec/services/broadcasts/welcome_notification/generator_spec.rb @@ -6,7 +6,8 @@ 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) } + let!(:customize_ux_broadcast) { create(:customize_ux_broadcast) } + let!(:customize_feed_broadcast) { create(:customize_feed_broadcast) } before do allow(Notification).to receive(:send_welcome_notification).and_call_original @@ -126,7 +127,7 @@ RSpec.describe Broadcasts::WelcomeNotification::Generator, type: :service do 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) + expect(user.notifications.first.notifiable).to eq(customize_ux_broadcast) end it "does not send duplicate notifications" do @@ -136,4 +137,40 @@ RSpec.describe Broadcasts::WelcomeNotification::Generator, type: :service do expect(user.notifications.count).to eq(1) end end + + describe "#send_feed_customization_notification" do + let!(:user) { create(:user, :with_identity, identities: %w[twitter github], created_at: 3.days.ago) } + + 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_feed_customization_notification) } + expect(Notification).not_to have_received(:send_welcome_notification) + end + + it "does not send a notification to a user that is following 2 tags" do + 2.times { user.follow(create(:tag)) } + sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_feed_customization_notification) } + expect(Notification).not_to have_received(:send_welcome_notification) + end + + it "sends a notification to a user with 0 tag follows" do + sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_feed_customization_notification) } + expect(user.notifications.count).to eq(1) + expect(user.notifications.first.notifiable).to eq(customize_feed_broadcast) + end + + it "does not send duplicate notifications" do + 2.times do + sidekiq_perform_enqueued_jobs { described_class.new(user.id).send(:send_feed_customization_notification) } + end + 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" + end end