Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add a supported digest content modifier (#44370)

Mark VanLandinghamfb21c762e0af
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index 3b6efba8562..15006974bf3 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -282,40 +282,56 @@ class UserNotifications < ActionMailer::Base
)
end
- @popular_topics = topics_for_digest[0, SiteSetting.digest_topics]
-
- if @popular_topics.present?
- @other_new_for_you =
- (
- if topics_for_digest.size > SiteSetting.digest_topics
- topics_for_digest[SiteSetting.digest_topics..-1]
- else
- []
- end
- )
-
- @popular_posts =
- if SiteSetting.digest_posts > 0
- Post
- .order("posts.score DESC")
- .for_mailing_list(user, @since)
- .where("posts.post_type = ?", Post.types[:regular])
- .where(
- "posts.deleted_at IS NULL AND posts.hidden = false AND posts.user_deleted = false",
- )
- .where(
- "posts.post_number > ? AND posts.score > ?",
- 1,
- ScoreCalculator.default_score_weights[:like_score] * 5.0,
- )
- .where("posts.created_at < ?", (SiteSetting.editing_grace_period || 0).seconds.ago)
- .limit(SiteSetting.digest_posts)
- else
- []
- end
+ posts_for_digest =
+ if SiteSetting.digest_topics > 0 && topics_for_digest.present? &&
+ SiteSetting.digest_posts > 0
+ Post
+ .order("posts.score DESC")
+ .for_mailing_list(user, @since)
+ .where("posts.post_type = ?", Post.types[:regular])
+ .where(
+ "posts.deleted_at IS NULL AND posts.hidden = false AND posts.user_deleted = false",
+ )
+ .where(
+ "posts.post_number > ? AND posts.score > ?",
+ 1,
+ ScoreCalculator.default_score_weights[:like_score] * 5.0,
+ )
+ .where("posts.created_at < ?", (SiteSetting.editing_grace_period || 0).seconds.ago)
+ .limit(SiteSetting.digest_posts)
+ else
+ []
+ end
+ # Replacement topics/posts must be authorized and bounded before Core prepares them.
+ # Custom templates receive template_locals in both HTML and text formats.
+ content =
+ DiscoursePluginRegistry.apply_modifier(
+ :user_digest_content,
+ {
+ topics: topics_for_digest,
+ posts: posts_for_digest,
+ template: nil,
+ template_locals: {
+ },
+ subject_key: nil,
+ has_custom_content: false,
+ },
+ user,
+ @since,
+ )
+ topics_for_digest = content[:topics].to_a
+ ActiveRecord::Associations::Preloader.new(
+ records: topics_for_digest,
+ associations: :first_post,
+ ).call
+ @popular_topics = topics_for_digest.first(SiteSetting.digest_topics)
+ @other_new_for_you = topics_for_digest.drop(SiteSetting.digest_topics)
+ @popular_posts = content[:posts].to_a
+
+ if @popular_topics.present? || @popular_posts.present? || content[:has_custom_content]
@digest_best_posts = @popular_topics.to_h { |topic| [topic.id, topic.best_post] }
- prepare_digest_localizations(user, topics_for_digest.to_a)
+ prepare_digest_localizations(user, topics_for_digest)
@excerpts = {}
@popular_topics.each do |t|
@@ -376,7 +392,7 @@ class UserNotifications < ActionMailer::Base
@preheader_text = I18n.t("user_notifications.digest.preheader", since: @since)
- subject_key = "user_notifications.digest.subject_template"
+ subject_key = content[:subject_key] || "user_notifications.digest.subject_template"
if SiteSetting.simple_email_subject && I18n.exists?("#{subject_key}_improved")
subject_key += "_improved"
@@ -387,13 +403,22 @@ class UserNotifications < ActionMailer::Base
subject: I18n.t(subject_key, email_prefix: @email_prefix, date: short_date(Time.now)),
add_unsubscribe_link: !opts[:skip_unsubscribe_links],
unsubscribe_url: "#{Discourse.base_url}/email/unsubscribe/#{@unsubscribe_key}",
- topic_ids: topics_for_digest.pluck(:id),
- post_ids:
- topics_for_digest.joins(:posts).where(posts: { post_number: 1 }).pluck("posts.id"),
+ topic_ids: topics_for_digest.map(&:id),
+ post_ids: topics_for_digest.filter_map { |topic| topic.first_post&.id },
}
opts[:recipient_user] = user
+ if content[:template]
+ render_options = {
+ template: content[:template],
+ locals: content[:template_locals],
+ layout: false,
+ }
+ opts[:html_override] = render_to_string(**render_options, formats: [:html])
+ opts[:body] = render_to_string(**render_options, formats: [:text])
+ end
+
build_email(user.email, opts)
end
end
diff --git a/spec/jobs/user_email_spec.rb b/spec/jobs/user_email_spec.rb
index f0b411041b0..a21e3e35ed2 100644
--- a/spec/jobs/user_email_spec.rb
+++ b/spec/jobs/user_email_spec.rb
@@ -37,6 +37,22 @@ RSpec.describe Jobs::UserEmail do
fab!(:user) { Fabricate(:user, last_seen_at: 8.days.ago, last_emailed_at: 8.days.ago) }
fab!(:popular_topic) { Fabricate(:topic, user: Fabricate(:admin), created_at: 1.hour.ago) }
+ it "records a digest attempt after delivering content selected by a plugin" do
+ freeze_time
+ reply = Fabricate(:post, topic: popular_topic, post_number: 2)
+ plugin = Plugin::Instance.new
+ modifier = proc { |content| content.merge(topics: [], posts: [reply]) }
+ DiscoursePluginRegistry.register_modifier(plugin, :user_digest_content, &modifier)
+
+ Jobs::UserEmail.new.execute(type: :digest, user_id: user.id)
+
+ expect(ActionMailer::Base.deliveries.last.to).to eq([user.email])
+ expect(ActionMailer::Base.deliveries.last.text_part.body.to_s).to include(reply.raw)
+ expect(user.user_stat.reload.digest_attempted_at).to eq_time(Time.zone.now)
+ ensure
+ DiscoursePluginRegistry.unregister_modifier(plugin, :user_digest_content, &modifier)
+ end
+
it "delivers translated digests according to each recipient's language preferences" do
SiteSetting.allow_user_locale = true
SiteSetting.content_localization_enabled = true
diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb
index 01e9aa53b04..7e562399f47 100644
--- a/spec/mailers/user_notifications_spec.rb
+++ b/spec/mailers/user_notifications_spec.rb
@@ -231,6 +231,100 @@ RSpec.describe UserNotifications do
end
end
+ context "with a content modifier" do
+ let(:plugin) { Plugin::Instance.new }
+
+ it "skips sending when a modifier removes all content" do
+ Fabricate(:topic, user: Fabricate(:admin))
+ modifier = proc { |content| content.merge(topics: [], posts: []) }
+ DiscoursePluginRegistry.register_modifier(plugin, :user_digest_content, &modifier)
+
+ expect(email.to).to be_blank
+ ensure
+ DiscoursePluginRegistry.unregister_modifier(plugin, :user_digest_content, &modifier)
+ end
+
+ it "renders selected topics and replies with the standard digest preparation" do
+ SiteSetting.content_localization_enabled = true
+ SiteSetting.allow_user_locale = true
+ user.update!(locale: "ja")
+ topic = Fabricate(:topic, locale: "en")
+ first_post = Fabricate(:post, topic: topic, locale: "en")
+ reply = Fabricate(:post, topic: topic, locale: "en", post_number: 2)
+ topic_translation = Fabricate(:topic_localization, topic: topic)
+ post_translation = Fabricate(:post_localization, post: first_post)
+ reply_translation = Fabricate(:post_localization, post: reply)
+ modifier =
+ proc { |content| content.merge(topics: [topic], posts: Post.where(id: reply.id)) }
+ DiscoursePluginRegistry.register_modifier(plugin, :user_digest_content, &modifier)
+
+ mail = UserNotifications.digest(user)
+
+ [mail.html_part.body.to_s, mail.text_part.body.to_s].each do |body|
+ expect(body).to include(topic_translation.title, reply_translation.raw)
+ end
+ expect(mail.html_part.body.to_s).to include(post_translation.raw)
+ expect(mail.header["X-Discourse-Topic-Ids"].to_s).to eq(topic.id.to_s)
+ expect(mail.header["X-Discourse-Post-Ids"].to_s).to eq(first_post.id.to_s)
+ ensure
+ DiscoursePluginRegistry.unregister_modifier(plugin, :user_digest_content, &modifier)
+ end
+
+ it "renders replies without new topics" do
+ reply = Fabricate(:post, post_number: 2, raw: "A reply for the digest")
+ modifier = proc { |content| content.merge(topics: Topic.none, posts: [reply]) }
+ DiscoursePluginRegistry.register_modifier(plugin, :user_digest_content, &modifier)
+
+ mail = UserNotifications.digest(user)
+
+ expect(mail.to).to eq([user.email])
+ expect(mail.html_part.body.to_s).to include(reply.raw)
+ expect(mail.text_part.body.to_s).to include(reply.raw)
+ ensure
+ DiscoursePluginRegistry.unregister_modifier(plugin, :user_digest_content, &modifier)
+ end
+
+ it "renders custom content in both formats with the digest envelope" do
+ require "action_view/testing/resolvers"
+ original_view_paths = UserNotifications.view_paths
+ UserNotifications.prepend_view_path(
+ ActionView::FixtureResolver.new(
+ "custom_digest.html.erb" => "<p><%= message %></p>",
+ "custom_digest.text.erb" => "<%= message %>",
+ ),
+ )
+ message = "Additional digest content"
+ modifier =
+ proc do |content, recipient, since|
+ content.merge(
+ topics: [],
+ posts: [],
+ template: "custom_digest",
+ template_locals: {
+ message: "#{message} for #{recipient.username} since #{since.to_date}",
+ },
+ subject_key: "user_notifications.digest.new_topics",
+ has_custom_content: true,
+ )
+ end
+ DiscoursePluginRegistry.register_modifier(plugin, :user_digest_content, &modifier)
+
+ mail = UserNotifications.digest(user, since: 1.day.ago)
+
+ expect(mail.to).to eq([user.email])
+ expect(mail.subject).to eq(I18n.t("user_notifications.digest.new_topics"))
+ [mail.html_part.body.to_s, mail.text_part.body.to_s].each do |body|
+ expect(body).to include("#{message} for #{user.username} since #{1.day.ago.to_date}")
+ end
+ expect(mail.header["List-Unsubscribe"].to_s).to match(%r{/email/unsubscribe/\h{64}})
+ expect(mail.header["List-Unsubscribe-Post"].to_s).to eq("List-Unsubscribe=One-Click")
+ expect(mail.header["X-Discourse-Topic-Ids"]).to be_nil
+ ensure
+ UserNotifications.view_paths = original_view_paths
+ DiscoursePluginRegistry.unregister_modifier(plugin, :user_digest_content, &modifier)
+ end
+ end
+
context "with topics only from new users" do
let!(:new_today) do
Fabricate(