From 76fe99b76a7c69321899c188afb34868b09ed1cd Mon Sep 17 00:00:00 2001 From: Vaidehi Joshi Date: Fri, 14 Feb 2020 12:30:25 -0800 Subject: [PATCH] Add Broadcast seeds + generator service to set up for welcome notifications (#6080) * Add more Broadcasts to seeds for welcome notification flow * Remove unnecessary welcoming_user_id from development config * Add basic service class for generating appropriate broadcast per user Eventually, this should be called from a rake task that will rely on this service to determine which broadcast, if any, is the appropriate one to send to newly signed-up users, and will enqueue a worker to create/send a welcome notification when appropriate. * Add a few TODOs, plus some general cleanup * Add some skipped tests for WelcomeNotification::Generator --- app/models/broadcast.rb | 1 + .../welcome_notification/generator.rb | 28 ++++++++++++++++ .../welcome_notification/send.rb | 2 +- config/environments/development.rb | 2 -- db/seeds.rb | 17 ++++++++++ .../welcome_notification/generator_spec.rb | 33 +++++++++++++++++++ .../welcome_notification/send_spec.rb | 2 +- 7 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 app/services/broadcasts/welcome_notification/generator.rb create mode 100644 spec/services/broadcasts/welcome_notification/generator_spec.rb diff --git a/app/models/broadcast.rb b/app/models/broadcast.rb index 84581abe5..54acaa6b1 100644 --- a/app/models/broadcast.rb +++ b/app/models/broadcast.rb @@ -4,6 +4,7 @@ class Broadcast < ApplicationRecord has_many :notifications, as: :notifiable, inverse_of: :notifiable validates :title, :type_of, :processed_html, presence: true + # TODO: [@thepracticaldev/delightful] Remove Onboarding type once we have launched welcome notifications. validates :type_of, inclusion: { in: %w[Announcement Onboarding Welcome] } def get_inner_body(content) diff --git a/app/services/broadcasts/welcome_notification/generator.rb b/app/services/broadcasts/welcome_notification/generator.rb new file mode 100644 index 000000000..5e8c58803 --- /dev/null +++ b/app/services/broadcasts/welcome_notification/generator.rb @@ -0,0 +1,28 @@ +# Generates a broadcast to be delivered as a notification. +module Broadcasts + module WelcomeNotification + class Generator + def initialize(receiver_id) + @receiver_id = receiver_id + end + + def self.call(*args) + new(*args).call + end + + def call + # This method should find the user based on the `receiver_id`. + # It should then determine the appropriate Broadcast for a user, + # based on the `created_at` and the different conditions for sending a notification. + # `welcome_broadcast = ...` + + # Once it has the appropriate Broadcast to be sent, it should send a notification for it: + # `Notification.send_welcome_notification(receiver_id, welcome_broadcast.id)` + end + + private + + attr_reader :receiver_id + end + end +end diff --git a/app/services/notifications/welcome_notification/send.rb b/app/services/notifications/welcome_notification/send.rb index 1d46aa91b..775e05a49 100644 --- a/app/services/notifications/welcome_notification/send.rb +++ b/app/services/notifications/welcome_notification/send.rb @@ -1,4 +1,4 @@ -# send welcome notification +# Creates and sends a specific welcome notification. module Notifications module WelcomeNotification class Send diff --git a/config/environments/development.rb b/config/environments/development.rb index b7a1a46e4..0d45dbba3 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -96,8 +96,6 @@ Rails.application.configure do # Debug is the default log_level, but can be changed per environment. config.log_level = :debug - config.welcoming_user_id = ENV["WELCOMING_USER_ID"] - # See for other config options config.after_initialize do Bullet.enable = true diff --git a/db/seeds.rb b/db/seeds.rb index 4d03c2301..de7da2c5d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -201,6 +201,7 @@ end Rails.logger.info "7. Creating Broadcasts" +# TODO: [@thepracticaldev/delightful] Remove this once we have launched welcome notifications. Broadcast.create!( title: "Welcome Notification", processed_html: "Welcome to dev.to! Start by introducing yourself in the welcome thread.", @@ -208,6 +209,22 @@ Broadcast.create!( sent: true, ) +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. 🤖" +} + +broadcast_messages.each do |type, message| + Broadcast.create!( + title: "Welcome Notification: #{type}", + processed_html: message, + type_of: "Welcome", + sent: true, + ) +end + ############################################################################## Rails.logger.info "8. Creating Chat Channels and Messages" diff --git a/spec/services/broadcasts/welcome_notification/generator_spec.rb b/spec/services/broadcasts/welcome_notification/generator_spec.rb new file mode 100644 index 000000000..db7fd6917 --- /dev/null +++ b/spec/services/broadcasts/welcome_notification/generator_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Broadcasts::WelcomeNotification::Generator, type: :service do + describe "::call" do + 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 + + context "when sending a welcome_thread 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 commented in a welcome thread" + end + + context "when sending a twitter_connect 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 is connected via twitter" + end + + context "when sending a github_connect 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 is connected via github" + end + end +end diff --git a/spec/services/notifications/welcome_notification/send_spec.rb b/spec/services/notifications/welcome_notification/send_spec.rb index c0ec810d6..a51a4fd69 100644 --- a/spec/services/notifications/welcome_notification/send_spec.rb +++ b/spec/services/notifications/welcome_notification/send_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Notifications::WelcomeNotification::Send, type: :service do allow(User).to receive(:welcoming_account).and_return(create(:user)) end - it "checks a newly created welcome notification", :aggregate_failures do + it "creates a new welcome notification", :aggregate_failures do welcome_broadcast = create(:broadcast, :onboarding) welcome_notification = described_class.call(create(:user).id, welcome_broadcast)