From 8a9ea06dee2d3a7f94e4f4c7094ec3594c83442a Mon Sep 17 00:00:00 2001 From: "Pia B." Date: Thu, 28 May 2026 22:14:31 +0200 Subject: [PATCH] fixes bug Admin Mailer trends mail not displayed correctly (#39122) --- app/mailers/admin_mailer.rb | 10 ++++-- config/sidekiq.yml | 2 +- spec/mailers/admin_mailer_spec.rb | 55 +++++++++++++++++++++++++++---- 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/app/mailers/admin_mailer.rb b/app/mailers/admin_mailer.rb index fe2325b6f3..dcf0ef5c7f 100644 --- a/app/mailers/admin_mailer.rb +++ b/app/mailers/admin_mailer.rb @@ -34,9 +34,13 @@ class AdminMailer < ApplicationMailer end def new_trends(links, tags, statuses) - @links = links - @tags = tags - @statuses = statuses + ActiveRecord::Associations::Preloader.new(records: [*links, *tags, *statuses], associations: [:trend]).call + + @links = links.filter { |link| link.trend.present? } + @tags = tags.filter { |tag| tag.trend.present? } + @statuses = statuses.filter { |status| status.trend.present? } + + return unless @links.any? || @tags.any? || @statuses.any? mail subject: default_i18n_subject(instance: @instance) end diff --git a/config/sidekiq.yml b/config/sidekiq.yml index 5beb95a3f8..ae30a5c011 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -17,7 +17,7 @@ class: Scheduler::ScheduledStatusesScheduler queue: scheduler trends_refresh_scheduler: - every: '5m' + every: ['5m', first_in: '4m'] class: Scheduler::Trends::RefreshScheduler queue: scheduler trends_review_notifications_scheduler: diff --git a/spec/mailers/admin_mailer_spec.rb b/spec/mailers/admin_mailer_spec.rb index e71a8308bf..34ed2d4dfa 100644 --- a/spec/mailers/admin_mailer_spec.rb +++ b/spec/mailers/admin_mailer_spec.rb @@ -71,16 +71,22 @@ RSpec.describe AdminMailer do describe '.new_trends' do let(:recipient) { Fabricate(:account, username: 'Snurf') } - let(:link) { Fabricate(:preview_card, trendable: true, language: 'en') } - let(:status) { Fabricate(:status) } - let(:tag) { Fabricate(:tag) } - let(:mail) { described_class.with(recipient: recipient).new_trends([link], [tag], [status]) } + let!(:link) { Fabricate(:preview_card, trendable: true, language: 'en') } + let!(:status) { Fabricate(:status) } + let!(:tag) { Fabricate(:tag, display_name: 'Test Tag') } + let!(:other_tag) { Fabricate(:tag, display_name: 'Test Tag') } + let!(:another_tag) { Fabricate(:tag, display_name: 'Test Tag') } + let(:mail) { described_class.with(recipient: recipient).new_trends([link], [tag, other_tag, another_tag], [status]) } + let(:status_trend) { Fabricate(:status_trend, status: status, account: Fabricate(:account)) } + let(:tag_trend) { Fabricate(:tag_trend, tag: tag) } + let(:other_tag_trend) { Fabricate(:tag_trend, tag: other_tag) } + let(:preview_card_trend) { Fabricate(:preview_card_trend, preview_card: link) } before do - PreviewCardTrend.create!(preview_card: link) - StatusTrend.create!(status: status, account: Fabricate(:account)) - TagTrend.create!(tag: tag) recipient.user.update(locale: :en) + status_trend + tag_trend + preview_card_trend end it 'renders the email' do @@ -96,6 +102,41 @@ RSpec.describe AdminMailer do .and match(link.title) .and match(tag.display_name) end + + context 'when between queueing and sending trends gets deleted' do + let(:queue_mail) { described_class.with(recipient: recipient).new_trends([link], [tag, other_tag], [status]).deliver_later! } + + before do + recipient.user.update(locale: :en) + end + + it 'sends the email when all but one trends were deleted without the respective tag or status or link' do + other_tag_trend + expect(queue_mail.successfully_enqueued?).to be(true) + + TagTrend.delete_all + StatusTrend.delete_all + expect { queue_mail.perform_now }.to send_email( + to: recipient.user_email, + from: 'notifications@localhost', + subject: I18n.t('admin_mailer.new_trends.subject', instance: Rails.configuration.x.local_domain) + ) + expect(mail.body).to have_text(/The following items need a review before they can be displayed publicly/) + .and match(link.title) + expect(mail.body).to_not match(ActivityPub::TagManager.instance.url_for(status)) + expect(mail.body).to_not match(tag.display_name) + end + + it 'returns nil when no trends are present' do + expect(queue_mail.successfully_enqueued?).to be(true) + + TagTrend.delete_all + StatusTrend.delete_all + PreviewCardTrend.delete_all + + expect { queue_mail.perform_now }.to_not send_email + end + end end describe '.new_software_updates' do