Add welcome thread welcome notification (#6221) [deploy]

* Add Welcome Thread Broadcast to generator.rb

* Add welcome trait to broadcasts.rb

* Add additional tests around welcome_broadcasts:
- ensure that the correct Broadcast is sent
- ensure that a User only receives a single Notification
- ensure that only certain Users receieve the Notification

* Refactor and remove unncessary code from generator.rb and generator_spec.rb

* Refactor generator_spec and eagerly load welcome_thread_comment to get spec passing

* Initialize user in place of receiver_id in generator.rb

* Add before action to make generator_spec more readable

* Add latest_published_thread method to generator.rb

  * Update generator.rb to be reusable with latest_published_thread
  * Update generator_spec to use welcome tags for Article obj
  * Create mascot_account using let to reduce User creation in spec

* Adjust expectation for a User who should not receive a notification
This commit is contained in:
Julianna Tetreault 2020-03-19 11:57:32 -06:00 committed by GitHub
parent 24c3090842
commit e8183189a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 22 deletions

View file

@ -3,7 +3,7 @@ module Broadcasts
module WelcomeNotification
class Generator
def initialize(receiver_id)
@receiver_id = receiver_id
@user = User.find(receiver_id)
end
def self.call(*args)
@ -11,18 +11,33 @@ module Broadcasts
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 = ...`
return if commented_on_welcome_thread? || received_notification?
# 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)`
Notification.send_welcome_notification(user.id, welcome_broadcast.id)
end
def received_notification?
Notification.exists?(notifiable: welcome_broadcast, user: user)
end
def commented_on_welcome_thread?
welcome_thread = latest_published_thread("welcome")
Comment.where(commentable: welcome_thread, user: user).any?
end
private
attr_reader :receiver_id
def welcome_broadcast
@welcome_broadcast ||= Broadcast.find_by(title: "Welcome Notification: welcome_thread")
end
def latest_published_thread(tag_name)
Article.published.
order("published_at ASC").
cached_tagged_with(tag_name).last
end
attr_reader :user
end
end
end

View file

@ -1,12 +1,19 @@
FactoryBot.define do
factory :broadcast do
active { false }
end
trait :onboarding do
title { "Welcome Notification" }
type_of { "Onboarding" }
processed_html { "Welcome! Introduce yourself in our <a href='/welcome'>welcome thread!</a>" }
factory :welcome_broadcast do
title { "Welcome Notification: welcome_thread" }
type_of { "Welcome" }
processed_html { "Sloan here again! 👋 DEV is a friendly community. Why not introduce yourself by leaving a comment in <a href='/welcome'>the welcome thread</a>!" }
end
# TODO: [@thepracticaldev/delightful] Remove onboarding factory once welcome notifications are live.
factory :onboarding_broadcast do
title { "Welcome Notification" }
type_of { "Onboarding" }
processed_html { "Welcome! Introduce yourself in our <a href='/welcome'>welcome thread!</a>" }
end
end
trait :active do

View file

@ -593,8 +593,8 @@ RSpec.describe "NotificationsIndex", type: :request do
context "when a user has a new welcome notification" do
# TODO: [@thepracticaldev/delightful] Only test against type_of Welcome once Onbarding notifications have been removed.
let(:active_broadcast) { create(:broadcast, :onboarding, :active) }
let(:inactive_broadcast) { create(:broadcast, :onboarding) }
let(:active_broadcast) { create(:onboarding_broadcast, :active) }
let(:inactive_broadcast) { create(:onboarding_broadcast) }
before { sign_in user }

View file

@ -2,6 +2,15 @@ require "rails_helper"
RSpec.describe Broadcasts::WelcomeNotification::Generator, type: :service do
describe "::call" do
let(:receiving_user) { create(:user) }
let(:user) { create(:user) }
let(:mascot_account) { create(:user) }
let!(:welcome_broadcast) { create(:welcome_broadcast, :active) }
before do
allow(User).to receive(:mascot_account).and_return(mascot_account)
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"
@ -10,10 +19,32 @@ RSpec.describe Broadcasts::WelcomeNotification::Generator, type: :service do
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"
before do
welcome_thread_article = create(:article, title: "Welcome Thread - v0", published: true, tags: "welcome")
create(:comment, commentable: welcome_thread_article, commentable_type: "Article", user: user)
end
it "generates the correct broadcast type and sends the notification to the user", :aggregate_failures do
expect(receiving_user.notifications.count).to eq(0)
sidekiq_perform_enqueued_jobs { described_class.call(receiving_user.id) }
expect(receiving_user.notifications.count).to eq(1)
expect(receiving_user.notifications.first.notifiable).to eq(welcome_broadcast)
end
it "does not send a notification to a user who has commented in a welcome thread", :aggregate_failures do
expect(user.notifications.count).to eq(0)
sidekiq_perform_enqueued_jobs { described_class.call(user.id) }
expect(user.notifications.count).to eq(0)
end
it "does not send a duplicate notification" do
2.times do
sidekiq_perform_enqueued_jobs { described_class.call(receiving_user.id) }
end
expect(receiving_user.notifications.count).to eq(1)
end
end
context "when sending a twitter_connect notification" do

View file

@ -7,7 +7,7 @@ RSpec.describe Notifications::WelcomeNotification::Send, type: :service do
end
it "creates a new welcome notification", :aggregate_failures do
welcome_broadcast = create(:broadcast, :onboarding)
welcome_broadcast = create(:onboarding_broadcast)
welcome_notification = described_class.call(create(:user).id, welcome_broadcast)
expect(welcome_notification).to be_kind_of(Notification)

View file

@ -1,8 +1,8 @@
require "rails_helper"
RSpec.describe Notifications::WelcomeNotificationWorker, type: :worker do
describe "#perform" do
let(:broadcast) { create(:broadcast, :onboarding, :active) }
let(:inactive_broadcast) { create(:broadcast, :onboarding) }
let(:broadcast) { create(:onboarding_broadcast, :active) }
let(:inactive_broadcast) { create(:onboarding_broadcast) }
let(:user) { create(:user) }
let(:service) { Notifications::WelcomeNotification::Send }
let(:worker) { subject }