Skip to content

Commit

Permalink
Merge pull request #7615 from fjordllc/feature/article-publication-no…
Browse files Browse the repository at this point in the history
…tifications

ブログ(articles)が公開されたら、管理者、メンター、現役一般受講生、現役研修生に通知する
  • Loading branch information
komagata authored Apr 24, 2024
2 parents 1affec9 + ff33b8f commit 090993e
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def create
@article.user = current_user if @article.user.nil?
set_wip
if @article.save
Newspaper.publish(:create_article, { article: @article })
redirect_to redirect_url(@article), notice: notice_message(@article)
else
render :new
Expand All @@ -41,6 +42,7 @@ def create
def update
set_wip
if @article.update(article_params)
Newspaper.publish(:create_article, { article: @article })
redirect_to redirect_url(@article), notice: notice_message(@article)
else
render :edit
Expand All @@ -49,6 +51,7 @@ def update

def destroy
@article.destroy
Newspaper.publish(:destroy_article, { article: @article })
redirect_to articles_url, notice: '記事を削除しました'
end

Expand Down
17 changes: 17 additions & 0 deletions app/mailers/activity_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,21 @@ def product_update(args = {})

message
end

# required params: article, receiver
def create_article(args = {})
@article = params&.key?(:article) ? params[:article] : args[:article]
@receiver ||= args[:receiver]

@user = @receiver
@link_url = notification_redirector_url(
link: "/articles/#{@article.id}",
kind: Notification.kinds[:create_article]
)
subject = "新しいブログ「#{@article.title}」を#{@article.user.login_name}さんが投稿しました!"
message = mail(to: @user.email, subject:)
message.perform_deliveries = @user.mail_notification? && !@user.retired?

message
end
end
1 change: 1 addition & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Article < ApplicationRecord
}

belongs_to :user
alias sender user
include ActionView::Helpers::AssetUrlHelper

THUMBNAIL_SIZE = [1200, 630].freeze
Expand Down
8 changes: 8 additions & 0 deletions app/models/article_notification_destroyer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class ArticleNotificationDestroyer
def call(payload)
article = payload[:article]
Notification.where(link: "/articles/#{article.id}").destroy_all
end
end
19 changes: 19 additions & 0 deletions app/models/article_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class ArticleNotifier
def call(payload)
article = payload[:article]
return if article.wip?

receivers = User.students_trainees_mentors_and_admins.reject { |receiver| receiver == article.user }
send_notification(article:, receivers:)
end

private

def send_notification(article:, receivers:)
receivers.each do |receiver|
ActivityDelivery.with(article:, receiver:).notify(:create_article)
end
end
end
3 changes: 2 additions & 1 deletion app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class Notification < ApplicationRecord
signed_up: 20,
regular_event_updated: 21,
no_correct_answer: 22,
comebacked: 23
comebacked: 23,
create_article: 24
}

scope :unreads, -> { where(read: false) }
Expand Down
15 changes: 15 additions & 0 deletions app/notifiers/activity_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,19 @@ def moved_up_event_waiting_user(params = {})
read: false
)
end

def create_article(params = {})
params.merge!(@params)
article = params[:article]
receiver = params[:receiver]

notification(
body: "#{article.user.login_name}さんがブログに「#{article.title}」を投稿しました。",
kind: :create_article,
receiver:,
sender: article.user,
link: Rails.application.routes.url_helpers.polymorphic_path(article),
read: false
)
end
end
4 changes: 4 additions & 0 deletions app/views/activity_mailer/create_article.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= render '/notification_mailer/notification_mailer_template',
title: "#{@article.user.login_name}さんがブログに「#{@article.title}」を投稿しました。",
link_url: @link_url, link_text: 'ブログへ' do
= md2html(@article.body)
3 changes: 3 additions & 0 deletions config/initializers/newspaper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@
Newspaper.subscribe(:product_update, ProductUpdateNotifierForChecker.new)
Newspaper.subscribe(:came_comment, CommentNotifier.new)
Newspaper.subscribe(:came_comment_in_talk, CommentNotifierForAdmin.new)

Newspaper.subscribe(:create_article, ArticleNotifier.new)
Newspaper.subscribe(:destroy_article, ArticleNotificationDestroyer.new)
end
23 changes: 23 additions & 0 deletions test/deliveries/activity_delivery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,27 @@ class ActivityDeliveryTest < ActiveSupport::TestCase
ActivityDelivery.with(**params).notify(:came_comment)
end
end

test '.notify(:create_article)' do
params = {
article: articles(:article1),
receiver: users(:kimura)
}

assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do
ActivityDelivery.notify!(:create_article, **params)
end

assert_difference -> { AbstractNotifier::Testing::Driver.enqueued_deliveries.count }, 1 do
ActivityDelivery.notify(:create_article, **params)
end

assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do
ActivityDelivery.with(**params).notify!(:create_article)
end

assert_difference -> { AbstractNotifier::Testing::Driver.enqueued_deliveries.count }, 1 do
ActivityDelivery.with(**params).notify(:create_article)
end
end
end
62 changes: 62 additions & 0 deletions test/mailers/activity_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1135,4 +1135,66 @@ class ActivityMailerTest < ActionMailer::TestCase

assert_empty ActionMailer::Base.deliveries
end

test 'create_article notifies students, trainees, mentors, and admins' do
target_users = %i[hatsuno kensyu mentormentaro machida]

target_users.each do |target_user|
article = articles(:article1)
receiver = users(target_user)

ActivityMailer.create_article(
article:,
receiver:
).deliver_now

assert_not ActionMailer::Base.deliveries.empty?
email = ActionMailer::Base.deliveries.last
query = CGI.escapeHTML({ kind: 24, link: "/articles/#{article.id}" }.to_param)
assert_equal ['[email protected]'], email.from
assert_equal [receiver.email], email.to
assert_equal "新しいブログ「#{article.title}」を#{article.user.login_name}さんが投稿しました!", email.subject
assert_match(%r{<a .+ href="http://localhost:3000/notification/redirector\?#{query}">ブログへ</a>}, email.body.to_s)
end
end

test 'create_article notifies students, trainees, mentors, and admins with params' do
target_users = %i[hatsuno kensyu mentormentaro machida]

target_users.each do |target_user|
article = articles(:article1)
receiver = users(target_user)

mailer = ActivityMailer.with(
article:,
receiver:
).create_article

perform_enqueued_jobs do
mailer.deliver_later
end

assert_not ActionMailer::Base.deliveries.empty?
email = ActionMailer::Base.deliveries.last
query = CGI.escapeHTML({ kind: 24, link: "/articles/#{article.id}" }.to_param)
assert_equal ['[email protected]'], email.from
assert_equal [receiver.email], email.to
assert_equal "新しいブログ「#{article.title}」を#{article.user.login_name}さんが投稿しました!", email.subject
assert_match(%r{<a .+ href="http://localhost:3000/notification/redirector\?#{query}">ブログへ</a>}, email.body.to_s)
end
end

test 'create_article notifies students, trainees, mentors, and admins with mail_notification off' do
article = articles(:article1)
receiver = users(:mentormentaro)
receiver.update(mail_notification: false)

ActivityMailer.create_article(
sender: article.user,
receiver:,
article:
).deliver_now

assert_empty ActionMailer::Base.deliveries
end
end
8 changes: 8 additions & 0 deletions test/mailers/previews/activity_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,12 @@ def no_correct_answer

ActivityMailer.with(question:, receiver:).no_correct_answer
end

def create_article
article = Article.find(ActiveRecord::FixtureSet.identify(:article1))
receiver = User.find(ActiveRecord::FixtureSet.identify(:kimura))
user = User.find(article.user_id)

ActivityMailer.with(article:, receiver:, sender: user).create_article
end
end

0 comments on commit 090993e

Please sign in to comment.