mirror of https://github.com/mastodon/mastodon
Add ability to email announcements to all users
parent
3a60b53e9a
commit
442fab1094
@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Admin::Announcements::DistributionsController < Admin::BaseController
|
||||
before_action :set_announcement
|
||||
|
||||
def create
|
||||
authorize @announcement, :distribute?
|
||||
@terms_of_service.touch(:notification_sent_at)
|
||||
Admin::DistributeAnnouncementNotificationWorker.perform_async(@announcement.id)
|
||||
redirect_to admin_announcements_index_path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_announcement
|
||||
@announcement = Announcement.find(params[:announcement_id])
|
||||
end
|
||||
end
|
@ -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
|
@ -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
|
@ -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') }
|
@ -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)
|
@ -0,0 +1,7 @@
|
||||
<%= t('user_mailer.announcement_published.title') %>
|
||||
|
||||
===
|
||||
|
||||
<%= t('user_mailer.announcement_published.description', domain: site_hostname) %>
|
||||
|
||||
<%= @announcement.text %>
|
@ -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
|
@ -0,0 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AddNotificationSentAtToAnnouncements < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_column :announcements, :notification_sent_at, :datetime
|
||||
end
|
||||
end
|
@ -0,0 +1,32 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Admin::DistributeAnnouncementNotificationWorker do
|
||||
let(:worker) { described_class.new }
|
||||
|
||||
describe '#perform' do
|
||||
context 'with missing record' do
|
||||
it 'runs without error' do
|
||||
expect { worker.perform(nil) }.to_not raise_error
|
||||
end
|
||||
end
|
||||
|
||||
context 'with valid announcement' do
|
||||
let(:announcement) { Fabricate(:announcement) }
|
||||
let!(:user) { Fabricate :user, confirmed_at: 3.days.ago }
|
||||
|
||||
it 'sends the announcement via email', :inline_jobs do
|
||||
emails = capture_emails { worker.perform(announcement.id) }
|
||||
|
||||
expect(emails.size)
|
||||
.to eq(1)
|
||||
expect(emails.first)
|
||||
.to have_attributes(
|
||||
to: [user.email],
|
||||
subject: I18n.t('user_mailer.announcement_published.subject')
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue