Add ability to email announcements to all users (#33928)

This commit is contained in:
Claire 2025-03-06 15:05:27 +01:00 committed by GitHub
parent d2ce9a6064
commit 5a100bf38f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 275 additions and 11 deletions

View file

@ -0,0 +1,18 @@
# frozen_string_literal: true
class Admin::Announcements::DistributionsController < Admin::BaseController
before_action :set_announcement
def create
authorize @announcement, :distribute?
@announcement.touch(:notification_sent_at)
Admin::DistributeAnnouncementNotificationWorker.perform_async(@announcement.id)
redirect_to admin_announcements_path
end
private
def set_announcement
@announcement = Announcement.find(params[:announcement_id])
end
end

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
class Admin::Announcements::PreviewsController < Admin::BaseController
before_action :set_announcement
def show
authorize @announcement, :distribute?
@user_count = @announcement.scope_for_notification.count
end
private
def set_announcement
@announcement = Announcement.find(params[:announcement_id])
end
end

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
class Admin::Announcements::TestsController < Admin::BaseController
before_action :set_announcement
def create
authorize @announcement, :distribute?
UserMailer.announcement_published(current_user, @announcement).deliver_later!
redirect_to admin_announcements_path
end
private
def set_announcement
@announcement = Announcement.find(params[:announcement_id])
end
end

View file

@ -219,6 +219,15 @@ class UserMailer < Devise::Mailer
end
end
def announcement_published(user, announcement)
@resource = user
@announcement = announcement
I18n.with_locale(locale) do
mail subject: default_i18n_subject
end
end
private
def default_devise_subject

View file

@ -4,17 +4,18 @@
#
# Table name: announcements
#
# id :bigint(8) not null, primary key
# text :text default(""), not null
# published :boolean default(FALSE), not null
# all_day :boolean default(FALSE), not null
# scheduled_at :datetime
# starts_at :datetime
# ends_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
# published_at :datetime
# status_ids :bigint(8) is an Array
# id :bigint(8) not null, primary key
# all_day :boolean default(FALSE), not null
# ends_at :datetime
# notification_sent_at :datetime
# published :boolean default(FALSE), not null
# published_at :datetime
# scheduled_at :datetime
# starts_at :datetime
# status_ids :bigint(8) is an Array
# text :text default(""), not null
# created_at :datetime not null
# updated_at :datetime not null
#
class Announcement < ApplicationRecord
@ -54,6 +55,10 @@ class Announcement < ApplicationRecord
update!(published: false, scheduled_at: nil)
end
def notification_sent?
notification_sent_at.present?
end
def mentions
@mentions ||= Account.from_text(text)
end
@ -86,6 +91,10 @@ class Announcement < ApplicationRecord
end
end
def scope_for_notification
User.confirmed.joins(:account).merge(Account.without_suspended)
end
private
def grouped_ordered_announcement_reactions

View file

@ -16,4 +16,8 @@ class AnnouncementPolicy < ApplicationPolicy
def destroy?
role.can?(:manage_announcements)
end
def distribute?
record.published? && !record.notification_sent? && role.can?(:manage_settings)
end
end

View file

@ -10,6 +10,8 @@
= l(announcement.created_at)
%div
- if can?(:distribute, announcement)
= table_link_to 'mail', t('admin.terms_of_service.notify_users'), admin_announcement_preview_path(announcement)
- if can?(:update, announcement)
- if announcement.published?
= table_link_to 'toggle_off', t('admin.announcements.unpublish'), unpublish_admin_announcement_path(announcement), method: :post, data: { confirm: t('admin.accounts.are_you_sure') }

View file

@ -0,0 +1,20 @@
- content_for :page_title do
= t('admin.announcements.preview.title')
- content_for :heading_actions do
.back-link
= link_to admin_announcements_path do
= material_symbol 'chevron_left'
= t('admin.announcements.back')
%p.lead
= t('admin.announcements.preview.explanation_html', count: @user_count, display_count: number_with_delimiter(@user_count))
.prose
= linkify(@announcement.text)
%hr.spacer/
.content__heading__actions
= link_to t('admin.terms_of_service.preview.send_preview', email: current_user.email), admin_announcement_test_path(@announcement), method: :post, class: 'button button-secondary'
= link_to t('admin.terms_of_service.preview.send_to_all', count: @user_count, display_count: number_with_delimiter(@user_count)), admin_announcement_distribution_path(@announcement), method: :post, class: 'button', data: { confirm: t('admin.reports.are_you_sure') }

View file

@ -0,0 +1,12 @@
= content_for :heading do
= render 'application/mailer/heading',
image_url: frontend_asset_url('images/mailer-new/heading/user.png'),
title: t('user_mailer.announcement_published.title', domain: site_hostname)
%table.email-w-full{ cellspacing: 0, cellpadding: 0, border: 0, role: 'presentation' }
%tr
%td.email-body-padding-td
%table.email-inner-card-table{ cellspacing: 0, cellpadding: 0, border: 0, role: 'presentation' }
%tr
%td.email-inner-card-td.email-prose
%p= t('user_mailer.announcement_published.description', domain: site_hostname)
= linkify(@announcement.text)

View file

@ -0,0 +1,7 @@
<%= t('user_mailer.announcement_published.title') %>
===
<%= t('user_mailer.announcement_published.description', domain: site_hostname) %>
<%= @announcement.text %>

View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
class Admin::DistributeAnnouncementNotificationWorker
include Sidekiq::Worker
def perform(announcement_id)
announcement = Announcement.find(announcement_id)
announcement.scope_for_notification.find_each do |user|
UserMailer.announcement_published(user, announcement).deliver_later!
end
rescue ActiveRecord::RecordNotFound
true
end
end