Skip to content

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

FEATURE: Use translated content in notification and digest emails (#44240)

Natalie Tay3af8bcdc98a3
diff --git a/app/helpers/user_notifications_helper.rb b/app/helpers/user_notifications_helper.rb
index f4c1286f8ae..c385860d309 100644
--- a/app/helpers/user_notifications_helper.rb
+++ b/app/helpers/user_notifications_helper.rb
@@ -76,11 +76,31 @@ module UserNotificationsHelper
normalize_name(post.user.name) != normalize_name(post.user.username)
end
- def format_for_email(post, use_excerpt)
- html = use_excerpt ? post.excerpt : post.cooked
+ def format_for_email(post, use_excerpt, localization: nil)
+ cooked = localization&.cooked.presence || post.cooked
+ html = use_excerpt ? Post.excerpt(cooked, nil, post: post) : cooked
PrettyText.format_for_email(html, post).html_safe
end
+ def digest_topic_title(topic)
+ @localized_topic_titles[topic.id] || topic.title
+ end
+
+ def digest_post_cooked(post)
+ @localized_posts[post.id]&.cooked.presence || post.cooked
+ end
+
+ def digest_post_excerpt(post)
+ Post.excerpt(
+ digest_post_cooked(post),
+ 1000,
+ strip_links: true,
+ text_entities: true,
+ markdown_images: true,
+ post: post,
+ )
+ end
+
def digest_custom_html(position_key)
digest_custom "user_notifications.digest.custom.html.#{position_key}"
end
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index c33d7e0a201..3b6efba8562 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -258,140 +258,144 @@ class UserNotifications < ActionMailer::Base
end
def digest(user, opts = {})
- build_summary_for(user)
- if !opts[:skip_unsubscribe_links]
- @unsubscribe_key = UnsubscribeKey.create_key_for(@user, UnsubscribeKey::DIGEST_TYPE)
- end
+ I18n.with_locale(user_locale(user)) do
+ build_summary_for(user)
+ if !opts[:skip_unsubscribe_links]
+ @unsubscribe_key = UnsubscribeKey.create_key_for(@user, UnsubscribeKey::DIGEST_TYPE)
+ end
- @since = opts[:since].presence
- @since ||= [user.last_seen_at, user.user_stat&.digest_attempted_at, 1.month.ago].compact.max
+ @since = opts[:since].presence
+ @since ||= [user.last_seen_at, user.user_stat&.digest_attempted_at, 1.month.ago].compact.max
- # Fetch some topics and posts to show
- digest_opts = {
- limit: SiteSetting.digest_topics + SiteSetting.digest_other_topics,
- top_order: true,
- }
- topics_for_digest = Topic.for_digest(user, @since, digest_opts)
- if topics_for_digest.empty? && !user.user_option.try(:include_tl0_in_digests)
- # Find some topics from new users that are at least 24 hours old
- topics_for_digest =
- Topic.for_digest(user, @since, digest_opts.merge(include_tl0: true)).where(
- "topics.created_at < ?",
- 24.hours.ago,
- )
- end
+ # Fetch some topics and posts to show
+ digest_opts = {
+ limit: SiteSetting.digest_topics + SiteSetting.digest_other_topics,
+ top_order: true,
+ }
+ topics_for_digest = Topic.for_digest(user, @since, digest_opts)
+ if topics_for_digest.empty? && !user.user_option.try(:include_tl0_in_digests)
+ # Find some topics from new users that are at least 24 hours old
+ topics_for_digest =
+ Topic.for_digest(user, @since, digest_opts.merge(include_tl0: true)).where(
+ "topics.created_at < ?",
+ 24.hours.ago,
+ )
+ end
+
+ @popular_topics = topics_for_digest[0, SiteSetting.digest_topics]
- @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
+ )
- if @popular_topics.present?
- @other_new_for_you =
- (
- if topics_for_digest.size > SiteSetting.digest_topics
- topics_for_digest[SiteSetting.digest_topics..-1]
+ @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
- )
-
- @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
- @excerpts = {}
+ @digest_best_posts = @popular_topics.to_h { |topic| [topic.id, topic.best_post] }
+ prepare_digest_localizations(user, topics_for_digest.to_a)
+ @excerpts = {}
- @popular_topics.each do |t|
- next if t.first_post.blank?
- @excerpts[t.first_post.id] = email_excerpt(t.first_post.cooked, t.first_post)
- end
-
- # Try to find 3 interesting stats for the top of the digest
- new_topics_count = Topic.for_digest(user, @since).count
- # We used topics from new users instead, so count should match
- new_topics_count = topics_for_digest.size if new_topics_count == 0
-
- @counts = [
- {
- id: "new_topics",
- label_key: "user_notifications.digest.new_topics",
- value: new_topics_count,
- href: "#{Discourse.base_url}/new",
- },
- ]
-
- # totalling unread notifications (which are low-priority only) and unread
- # PMs and bookmark reminder notifications, so the total is both unread low
- # and high priority PMs
- value = user.unread_notifications + user.unread_high_priority_notifications
- if value > 0
- @counts << {
- id: "unread_notifications",
- label_key: "user_notifications.digest.unread_notifications",
- value: value,
- href: "#{Discourse.base_url}/my/notifications",
- }
- end
+ @popular_topics.each do |t|
+ next if t.first_post.blank?
+ @excerpts[t.first_post.id] = email_excerpt(digest_post_cooked(t.first_post), t.first_post)
+ end
- if @counts.size < 3
- value = user.unread_notifications_of_type(Notification.types[:liked], since: @since)
+ # Try to find 3 interesting stats for the top of the digest
+ new_topics_count = Topic.for_digest(user, @since).count
+ # We used topics from new users instead, so count should match
+ new_topics_count = topics_for_digest.size if new_topics_count == 0
+
+ @counts = [
+ {
+ id: "new_topics",
+ label_key: "user_notifications.digest.new_topics",
+ value: new_topics_count,
+ href: "#{Discourse.base_url}/new",
+ },
+ ]
+
+ # totalling unread notifications (which are low-priority only) and unread
+ # PMs and bookmark reminder notifications, so the total is both unread low
+ # and high priority PMs
+ value = user.unread_notifications + user.unread_high_priority_notifications
if value > 0
@counts << {
- id: "likes_received",
- label_key: "user_notifications.digest.liked_received",
+ id: "unread_notifications",
+ label_key: "user_notifications.digest.unread_notifications",
value: value,
href: "#{Discourse.base_url}/my/notifications",
}
end
- end
- if @counts.size < 3 && user.user_option.digest_after_minutes.to_i >= 1440
- value = summary_new_users_count(@since)
- if value > 0
- @counts << {
- id: "new_users",
- label_key: "user_notifications.digest.new_users",
- value: value,
- href: "#{Discourse.base_url}/about",
- }
+ if @counts.size < 3
+ value = user.unread_notifications_of_type(Notification.types[:liked], since: @since)
+ if value > 0
+ @counts << {
+ id: "likes_received",
+ label_key: "user_notifications.digest.liked_received",
+ value: value,
+ href: "#{Discourse.base_url}/my/notifications",
+ }
+ end
end
- end
- @preheader_text = I18n.t("user_notifications.digest.preheader", since: @since)
+ if @counts.size < 3 && user.user_option.digest_after_minutes.to_i >= 1440
+ value = summary_new_users_count(@since)
+ if value > 0
+ @counts << {
+ id: "new_users",
+ label_key: "user_notifications.digest.new_users",
+ value: value,
+ href: "#{Discourse.base_url}/about",
+ }
+ end
+ end
- subject_key = "user_notifications.digest.subject_template"
+ @preheader_text = I18n.t("user_notifications.digest.preheader", since: @since)
- if SiteSetting.simple_email_subject && I18n.exists?("#{subject_key}_improved")
- subject_key += "_improved"
- end
+ subject_key = "user_notifications.digest.subject_template"
- opts = {
- from_alias: I18n.t("user_notifications.digest.from", site_name: Email.site_title),
- 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"),
- }
+ if SiteSetting.simple_email_subject && I18n.exists?("#{subject_key}_improved")
+ subject_key += "_improved"
+ end
- opts[:recipient_user] = user
+ opts = {
+ from_alias: I18n.t("user_notifications.digest.from", site_name: Email.site_title),
+ 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"),
+ }
+
+ opts[:recipient_user] = user
- build_email(user.email, opts)
+ build_email(user.email, opts)
+ end
end
end
@@ -496,12 +500,46 @@ class UserNotifications < ActionMailer::Base
protected
+ def localized_email_posts(posts, user)
+ return {} if !SiteSetting.content_localization_enabled || SiteSetting.private_email?
+
+ ActiveRecord::Associations::Preloader.new(records: posts, associations: :localizations).call
+ posts.each_with_object({}) do |post, localizations|
+ next unless ContentLocalization.show_translated_post?(post, user.guardian)
+ # Unsaved content changes must take precedence over persisted translations.
+ next if post.will_save_change_to_raw? || post.will_save_change_to_cooked?
+
+ localization = post.get_localization
+ if localization && localization.post_version == post.version
+ localizations[post.id] = localization
+ end
+ end
+ end
+
+ def prepare_digest_localizations(user, topics)
+ posts = [
+ *@popular_topics.map(&:first_post),
+ *@digest_best_posts.values,
+ *@popular_posts,
+ ].compact
+ @localized_posts = localized_email_posts(posts, user)
+ @localized_topic_titles = {}
+ return if !SiteSetting.content_localization_enabled || SiteSetting.private_email?
+
+ topics = [*topics, *@popular_posts.map(&:topic)].uniq(&:id)
+ ActiveRecord::Associations::Preloader.new(records: topics, associations: :localizations).call
+ topics.each do |topic|
+ title = ContentLocalization.translated_topic_title(topic, user.guardian)
+ @localized_topic_titles[topic.id] = title if title
+ end
+ end
+
def user_locale(user)
user.effective_locale
end
- def email_post_markdown(post, add_posted_by = false)
- result = +"#{post.raw}\n\n"
+ def email_post_markdown(post, add_posted_by = false, localization: nil)
+ result = +"#{localization&.raw.presence || post.raw}\n\n"
if add_posted_by
result << "#{I18n.t("user_notifications.posted_by", username: post.username, post_date: post.created_at.strftime("%m/%d/%Y"))}\n\n"
end
@@ -577,6 +615,13 @@ class UserNotifications < ActionMailer::Base
use_topic_title_subject = false
end
+ if !SiteSetting.private_email? && !original_subject && topic_title == post.topic.title
+ I18n.with_locale(user_locale(user)) do
+ topic_title =
+ ContentLocalization.translated_topic_title(post.topic, user.guardian) || topic_title
+ end
+ end
+
email_options = {
title: topic_title,
post: post,
@@ -601,7 +646,7 @@ class UserNotifications < ActionMailer::Base
email_options[:group_name] = Group.find_by(id: group_id)&.name
end
- send_notification_email(email_options)
+ I18n.with_locale(user_locale(user)) { send_notification_email(email_options) }
end
def send_notification_email(opts)
@@ -692,10 +737,18 @@ class UserNotifications < ActionMailer::Base
# make .present? cheaper
context_posts = context_posts.to_a
+ in_reply_to_post = post.reply_to_post if user.user_option.email_in_reply_to
+ localized_posts = localized_email_posts([post, in_reply_to_post, *context_posts].compact, user)
if context_posts.present?
context << +"-- \n*#{I18n.t("user_notifications.previous_discussion")}*\n"
- context_posts.each { |cp| context << email_post_markdown(cp, true) }
+ context_posts.each do |context_post|
+ context << email_post_markdown(
+ context_post,
+ true,
+ localization: localized_posts[context_post.id],
+ )
+ end
end
translation_override_exists =
@@ -714,7 +767,10 @@ class UserNotifications < ActionMailer::Base
"_to_topic_body"
end
- topic_excerpt = post.excerpt.tr("\n", " ") if post.is_first_post? && post.excerpt
+ if post.is_first_post?
+ cooked = localized_posts[post.id]&.cooked.presence || post.cooked
+ topic_excerpt = Post.excerpt(cooked, nil, post: post)&.tr("\n", " ")
+ end
topic_url = post.topic&.url
if SiteSetting.private_email?
@@ -741,13 +797,11 @@ class UserNotifications < ActionMailer::Base
EmailLog.where(user_id: user.id).where("created_at > ?", 1.day.ago).count >=
(SiteSetting.max_emails_per_day_per_user - 1)
- in_reply_to_post = post.reply_to_post if user.user_option.email_in_reply_to
-
if SiteSetting.private_email?
message = I18n.t("system_messages.contents_hidden")
else
message =
- email_post_markdown(post) +
+ email_post_markdown(post, localization: localized_posts[post.id]) +
(
if reached_limit
"\n\n#{I18n.t "user_notifications.reached_limit", count: SiteSetting.max_emails_per_day_per_user}"
@@ -767,6 +821,7 @@ class UserNotifications < ActionMailer::Base
format: :html,
locals: {
context_posts: context_posts,
+ localized_posts: localized_posts,
reached_limit: reached_limit,
post: post,
in_reply_to_post: in_reply_to_post,
diff --git a/app/views/email/_post.html.erb b/app/views/email/_post.html.erb
index 6567549bb6c..c5a88444d4b 100644
--- a/app/views/email/_post.html.erb
+++ b/app/views/email/_post.html.erb
@@ -24,5 +24,5 @@
</td>
</tr>
</table>
- <div class='body'><%= format_for_email(post, use_excerpt) %></div>
+ <div class='body'><%= format_for_email(post, use_excerpt, localization: local_assigns[:localization]) %></div>
</div>
diff --git a/app/views/email/notification.html.erb b/app/views/email/notification.html.erb
index 75ac7354c0b..12e57747eab 100644
--- a/app/views/email/notification.html.erb
+++ b/app/views/email/notification.html.erb
@@ -1,3 +1,4 @@
+<% localized_posts = local_assigns.fetch(:localized_posts, {}) %>
<div id='main' class=<%= classes %>>
<%- if reply_above_line.present? %>
<div class="reply-above-line">
@@ -12,7 +13,7 @@
<%- if SiteSetting.private_email? %>
<p><%= t('system_messages.contents_hidden') %></p>
<% else %>
- <%= render partial: 'email/post', locals: { post: post, use_excerpt: SiteSetting.post_excerpts_in_emails } %>
+ <%= render partial: 'email/post', locals: { post: post, localization: localized_posts[post.id], use_excerpt: SiteSetting.post_excerpts_in_emails } %>
<% if in_reply_to_post.present? || context_posts.present? %>
<div class='footer'>%{respond_instructions}</div>
@@ -21,13 +22,13 @@
<% if in_reply_to_post.present? %>
<h4 class='previous-discussion'><%= t "user_notifications.in_reply_to" %></h4>
- <%= render partial: 'email/post', locals: { post: in_reply_to_post, use_excerpt: true} %>
+ <%= render partial: 'email/post', locals: { post: in_reply_to_post, localization: localized_posts[in_reply_to_post.id], use_excerpt: true} %>
<% end %>
<% if context_posts.present? %>
<h4 class='previous-discussion'><%= t "user_notifications.previous_discussion" %></h4>
<% context_posts.each do |p| %>
- <%= render partial: 'email/post', locals: { post: p, use_excerpt: false } %>
+ <%= render partial: 'email/post', locals: { post: p, localization: localized_posts[p.id], use_excerpt: false } %>
<% end %>
<% end %>
diff --git a/app/views/user_notifications/digest/_new_topic.html.erb b/app/views/user_notifications/digest/_new_topic.html.erb
index a8948cac973..ba425f31110 100644
--- a/app/views/user_notifications/digest/_new_topic.html.erb
+++ b/app/views/user_notifications/digest/_new_topic.html.erb
@@ -1,7 +1,7 @@
<tr style="vertical-align:top;">
<td class="digest-new-topic-title with-dir" style="padding:8px;">
<a href="<%= Discourse.base_url_no_prefix + topic.relative_url %>" style="font-weight:400;line-height:1.3;margin:0;padding:0;text-decoration:none;">
- <strong><%= gsub_emoji_to_unicode(topic.title.truncate(100, separator: /\s/)) -%></strong>
+ <strong><%= gsub_emoji_to_unicode(digest_topic_title(topic).truncate(100, separator: /\s/)) -%></strong>
</a>
<%- if SiteSetting.show_topic_featured_link_in_digest && topic.featured_link %>
<a class='topic-featured-link' href='<%= topic.featured_link %>'><%= raw topic_featured_link_domain(topic.featured_link) %></a>
diff --git a/app/views/user_notifications/digest/_other_new_topics.text.erb b/app/views/user_notifications/digest/_other_new_topics.text.erb
index bb1c598f840..f1224d1c4c2 100644
--- a/app/views/user_notifications/digest/_other_new_topics.text.erb
+++ b/app/views/user_notifications/digest/_other_new_topics.text.erb
@@ -2,6 +2,6 @@
**<%=t 'user_notifications.digest.more_new' %>**
<%- @other_new_for_you.each do |t| %>
-* <%= raw(@markdown_linker.create(t.title, t.url)) %> - <%= t.posts_count %> - <%- if t.category %>[<%= t.category.name %>]<%- end %>
+* <%= raw(@markdown_linker.create(digest_topic_title(t), t.url)) %> - <%= t.posts_count %> - <%- if t.category %>[<%= t.category.name %>]<%- end %>
<%- end -%>
<%- end %>
\ No newline at end of file
diff --git a/app/views/user_notifications/digest/_popular_post.html.erb b/app/views/user_notifications/digest/_popular_post.html.erb
index 7a9c1859a2f..498b0592ad6 100644
--- a/app/views/user_notifications/digest/_popular_post.html.erb
+++ b/app/views/user_notifications/digest/_popular_post.html.erb
@@ -2,7 +2,7 @@
<tbody>
<tr>
<td class="post-excerpt with-dir" style="color:#0a0a0a;font-size:14px;font-weight:normal;padding:16px;width:100%;">
- <%= email_excerpt(post.cooked, post) %>
+ <%= email_excerpt(digest_post_cooked(post), post) %>
</td>
</tr>
</tbody>
@@ -34,7 +34,7 @@
</td>
<td style="color:#0a0a0a;line-height:1.3;padding:0 8px 8px 8px;text-align:<%= rtl? ? 'left' : 'right' %>;">
<p style="color:#8f8f8f;line-height:1.3;margin:0 0 10px 0;padding:0;">
- <a href="<%= post.full_url -%>" style="font-weight:400;line-height:1.3;margin:0;padding:0;text-decoration:none"><%= gsub_emoji_to_unicode(post.topic.title.truncate(100, separator: /\s/)) -%></a>
+ <a href="<%= post.full_url -%>" style="font-weight:400;line-height:1.3;margin:0;padding:0;text-decoration:none"><%= gsub_emoji_to_unicode(digest_topic_title(post.topic).truncate(100, separator: /\s/)) -%></a>
</p>
<a href="<%= post.full_url -%>" class="digest-button with-accent-colors" style="width:100%;text-decoration:none;padding:8px 16px;white-space: nowrap;">
<%=t 'user_notifications.digest.join_the_discussion' %>
diff --git a/app/views/user_notifications/digest/_popular_posts.text.erb b/app/views/user_notifications/digest/_popular_posts.text.erb
index 7e98d91bf71..8bb419e665a 100644
--- a/app/views/user_notifications/digest/_popular_posts.text.erb
+++ b/app/views/user_notifications/digest/_popular_posts.text.erb
@@ -2,8 +2,8 @@
### <%=t 'user_notifications.digest.popular_posts' %>
<%- @popular_posts.each_with_index do |post,i| %>
- <%= post.user.username -%> - <%= raw(@markdown_linker.create(post.topic.title, post.topic.url)) %>
+ <%= post.user.username -%> - <%= raw(@markdown_linker.create(digest_topic_title(post.topic), post.topic.url)) %>
- <%= raw(post.excerpt(1000, strip_links: true, text_entities: true, markdown_images: true)) %>
+ <%= raw(digest_post_excerpt(post)) %>
<%- end %>
<%- end %>
\ No newline at end of file
diff --git a/app/views/user_notifications/digest/_popular_topic.html.erb b/app/views/user_notifications/digest/_popular_topic.html.erb
index 7d4d0cf3ea5..8bd86f7d30a 100644
--- a/app/views/user_notifications/digest/_popular_topic.html.erb
+++ b/app/views/user_notifications/digest/_popular_topic.html.erb
@@ -20,7 +20,7 @@
<td class="with-dir" style="padding:<%= rtl? ? '0 16px 8px 8px' : '0 8px 8px 16px' %>;width:100%;">
<h2 class="digest-topic-title" style="font-size:18px;font-weight:400;line-height:1.3;margin:0;padding:0;word-wrap:normal">
<a href="<%= Discourse.base_url_no_prefix + topic.relative_url %>" style="font-weight:400;line-height:1.3;margin:0;padding:0;text-decoration:none">
- <strong><%= gsub_emoji_to_unicode(topic.title.truncate(100, separator: /\s/)) -%></strong>
+ <strong><%= gsub_emoji_to_unicode(digest_topic_title(topic).truncate(100, separator: /\s/)) -%></strong>
</a>
</h2>
<%- if SiteSetting.show_topic_featured_link_in_digest && topic.featured_link %>
diff --git a/app/views/user_notifications/digest/_popular_topics.text.erb b/app/views/user_notifications/digest/_popular_topics.text.erb
index 73a44de82b9..c359fb83ced 100644
--- a/app/views/user_notifications/digest/_popular_topics.text.erb
+++ b/app/views/user_notifications/digest/_popular_topics.text.erb
@@ -3,10 +3,10 @@
### <%=t 'user_notifications.digest.popular_topics' %>
<%- @popular_topics.each_with_index do |t,i| %>
- <%= raw(@markdown_linker.create(t.title, t.url)) %>
+ <%= raw(@markdown_linker.create(digest_topic_title(t), t.url)) %>
- <%- if t.best_post.present? %>
- <%= raw(t.best_post.excerpt(1000, strip_links: true, text_entities: true, markdown_images: true)) %>
+ <%- if @digest_best_posts[t.id].present? %>
+ <%= raw(digest_post_excerpt(@digest_best_posts[t.id])) %>
<%- end %>
<%= digest_custom_text("below_post_#{i+1}") %>
diff --git a/spec/jobs/user_email_spec.rb b/spec/jobs/user_email_spec.rb
index c013b9d000d..f0b411041b0 100644
--- a/spec/jobs/user_email_spec.rb
+++ b/spec/jobs/user_email_spec.rb
@@ -37,6 +37,46 @@ 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 "delivers translated digests according to each recipient's language preferences" do
+ SiteSetting.allow_user_locale = true
+ SiteSetting.content_localization_enabled = true
+ popular_topic.update!(locale: "en")
+ post = Fabricate(:post, topic: popular_topic, locale: "en", raw: "Original digest post")
+ post_translation = Fabricate(:post_localization, post: post)
+ topic_translation = Fabricate(:topic_localization, topic: popular_topic)
+ french_post_translation =
+ Fabricate(:post_localization, post: post, locale: "fr", raw: "Contenu traduit")
+ french_topic_translation =
+ Fabricate(:topic_localization, topic: popular_topic, locale: "fr", title: "Sujet traduit")
+ user.update!(locale: "ja")
+ french_user =
+ Fabricate(:user, locale: "fr", last_seen_at: 8.days.ago, last_emailed_at: 8.days.ago)
+ english_reader =
+ Fabricate(:user, locale: "ja", last_seen_at: 8.days.ago, last_emailed_at: 8.days.ago)
+ english_reader.user_option.update!(understood_languages: ["en"])
+ english_user =
+ Fabricate(:user, locale: "en", last_seen_at: 8.days.ago, last_emailed_at: 8.days.ago)
+
+ recipients = [
+ [user, topic_translation.title, post_translation.raw],
+ [french_user, french_topic_translation.title, french_post_translation.raw],
+ [english_reader, popular_topic.title, post.raw],
+ [english_user, popular_topic.title, post.raw],
+ ]
+ content = recipients.flat_map { |recipient, title, raw| [title, raw] }.uniq
+ recipients.each do |recipient, title, raw|
+ Jobs::UserEmail.new.execute(type: :digest, user_id: recipient.id)
+ mail = ActionMailer::Base.deliveries.last
+ expect(mail.to).to eq([recipient.email])
+ renderer = Email::Renderer.new(mail)
+ [renderer.html, renderer.text].each do |body|
+ expect(body).to include(title, raw)
+ expect(body).not_to include(*(content - [title, raw]))
+ end
+ end
+ expect(ActionMailer::Base.deliveries.size).to eq(4)
+ end
+
it "doesn't call the mailer when the user is missing" do
Jobs::UserEmail.new.execute(type: :digest, user_id: User.last.id + 10_000)
expect(ActionMailer::Base.deliveries).to eq([])
diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb
index 6cd532fcff4..01e9aa53b04 100644
--- a/spec/mailers/user_notifications_spec.rb
+++ b/spec/mailers/user_notifications_spec.rb
@@ -529,7 +529,7 @@ RSpec.describe UserNotifications do
it "applies lang/xml:lang html attributes" do
SiteSetting.default_locale = "pl_PL"
- html = email.html_part.to_s
+ html = email.html_part.body.to_s
expect(html).to match(' lang="pl-PL"')
expect(html).to match(' xml:lang="pl-PL"')
@@ -560,6 +560,474 @@ RSpec.describe UserNotifications do
end
end
+ describe "translated digest content" do
+ fab!(:user) { Fabricate(:user, locale: "ja") }
+ fab!(:author) { Fabricate(:user, trust_level: TrustLevel[2]) }
+ fab!(:topic) { Fabricate(:topic, user: author, locale: "en", created_at: 1.hour.ago) }
+ fab!(:other_topic) { Fabricate(:topic, user: author, locale: "en", created_at: 2.hours.ago) }
+ fab!(:post) do
+ Fabricate(:post, user: author, topic: topic, locale: "en", raw: "Original first post")
+ end
+ fab!(:reply) do
+ Fabricate(
+ :post,
+ user: author,
+ topic: topic,
+ locale: "en",
+ raw: "Original popular reply",
+ score: 100,
+ created_at: 1.hour.ago,
+ )
+ end
+ fab!(:post_translation) { Fabricate(:post_localization, post: post) }
+ fab!(:reply_translation) { Fabricate(:post_localization, post: reply) }
+ fab!(:topic_translation) { Fabricate(:topic_localization, topic: topic) }
+ fab!(:other_topic_translation) { Fabricate(:topic_localization, topic: other_topic) }
+
+ before do
+ SiteSetting.allow_user_locale = true
+ SiteSetting.content_localization_enabled = true
+ SiteSetting.digest_topics = 1
+ SiteSetting.digest_other_topics = 1
+ SiteSetting.digest_posts = 1
+ topic.update!(bumped_at: 1.hour.ago)
+ other_topic.update!(bumped_at: 2.hours.ago)
+ end
+
+ def digest_mail(recipient = user)
+ UserNotifications.digest(recipient, since: 1.day.ago)
+ end
+
+ it "translates popular topics, popular posts, and other new topics in both parts" do
+ mail = digest_mail
+ html = mail.html_part.body.to_s
+ text = mail.text_part.body.to_s
+ [html, text].each do |body|
+ expect(body).to include(
+ topic_translation.title,
+ other_topic_translation.title,
+ reply_translation.raw,
+ )
+ expect(body).not_to include(topic.title, other_topic.title, reply.raw)
+ end
+ expect(html).to include(post_translation.raw)
+ expect(text).not_to include(post.raw)
+ expect(html).to include(I18n.t("user_notifications.digest.popular_topics", locale: "ja"))
+ expect(I18n.locale).to eq(:en)
+ end
+
+ it "keeps content in the recipient's selected language and understood languages" do
+ topic.update!(locale: "ja")
+ post.update!(locale: "ja")
+ reply.update!(locale: "en")
+ other_topic.update!(locale: "fr")
+ user.user_option.update!(understood_languages: ["en"])
+ mail = digest_mail
+ [mail.html_part.body.to_s, mail.text_part.body.to_s].each do |body|
+ expect(body).to include(topic.title, reply.raw, other_topic_translation.title)
+ expect(body).not_to include(
+ topic_translation.title,
+ reply_translation.raw,
+ other_topic.title,
+ )
+ end
+ expect(mail.html_part.body.to_s).to include(post.raw)
+ end
+
+ it "falls back for missing and stale post translations" do
+ post_translation.destroy!
+ topic_translation.destroy!
+ reply_translation.update!(post_version: reply.version - 1)
+ mail = digest_mail
+ expect(mail.html_part.body.to_s).to include(topic.title, post.raw, reply.raw)
+ expect(mail.text_part.body.to_s).to include(topic.title, reply.raw)
+ expect(mail.html_part.body.to_s).not_to include(reply_translation.raw)
+ end
+
+ it "respects the automatic translation preference" do
+ user.user_option.update!(automatically_translate: false)
+ mail = digest_mail
+ [mail.html_part.body.to_s, mail.text_part.body.to_s].each do |body|
+ expect(body).to include(topic.title, other_topic.title, reply.raw)
+ expect(body).not_to include(topic_translation.title, reply_translation.raw)
+ end
+ end
+
+ it "keeps original content when localization is disabled" do
+ SiteSetting.content_localization_enabled = false
+ mail = digest_mail
+ [mail.html_part.body.to_s, mail.text_part.body.to_s].each do |body|
+ expect(body).to include(topic.title, reply.raw)
+ expect(body).not_to include(topic_translation.title, reply_translation.raw)
+ end
+ end
+
+ it "uses the effective locale when user locales are disabled" do
+ SiteSetting.allow_user_locale = false
+ mail = digest_mail
+ [mail.html_part.body.to_s, mail.text_part.body.to_s].each do |body|
+ expect(body).to include(topic.title, reply.raw)
+ expect(body).not_to include(topic_translation.title, reply_translation.raw)
+ end
+ end
+
+ it "uses normalized language matches and skips sources whose language is unknown" do
+ user.update!(locale: "pt_BR")
+ topic_translation.update!(locale: "pt")
+ reply_translation.update!(locale: "pt")
+ post.update!(locale: nil)
+ mail = digest_mail
+ [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.raw)
+ expect(mail.html_part.body.to_s).not_to include(post_translation.raw)
+ end
+
+ it "escapes translated titles and sanitizes translated post content" do
+ title = %(<img src="x" onerror="alert(1)"> Translated meeting)
+ topic_translation.update!(title: title)
+ PostLocalizationUpdater.update(
+ post: reply,
+ locale: "ja",
+ user: author,
+ raw:
+ %(Safe localized reply\n\n<img src="x" onerror="alert(1)">\n\n<script>alert(1)</script>),
+ )
+ html = Nokogiri::HTML5.fragment(Email::Renderer.new(digest_mail).html)
+ expect(html.text).to include(title, "Safe localized reply")
+ expect(html.css("script, [onerror], [onload]")).to be_empty
+ end
+
+ it "formats translated links and redacts secure images from digest excerpts" do
+ setup_s3
+ SiteSetting.secure_uploads = true
+ SiteSetting.login_required = true
+ secure_url = "/secure-uploads/original/1X/translated.png"
+ [post_translation, reply_translation].each do |translation|
+ translation.update!(
+ cooked: "<p>#{translation.raw}<a href='/u/admin'>Admin</a><img src='#{secure_url}'></p>",
+ )
+ end
+ mail = digest_mail
+ renderer = Email::Renderer.new(mail)
+ html = Nokogiri::HTML5.fragment(renderer.html)
+ expect(html.css("a").map { |link| link["href"] }).to include("#{Discourse.base_url}/u/admin")
+ expect(html.at_css("[data-stripped-secure-upload]")).to be_present
+ expect(html.css("img").map { |image| image["src"] }).not_to include(
+ secure_url,
+ "#{Discourse.base_url}#{secure_url}",
+ )
+ end
+
+ it "loads translations in a bounded number of queries as the digest grows" do
+ original_queries = track_sql_queries { digest_mail.html_part.body.to_s }
+ additional_topic = Fabricate(:topic, user: author, locale: "en", created_at: 1.hour.ago)
+ additional_post = Fabricate(:post, user: author, topic: additional_topic, locale: "en")
+ Fabricate(:topic_localization, topic: additional_topic)
+ Fabricate(:post_localization, post: additional_post)
+ SiteSetting.digest_topics = 2
+ expanded_queries = track_sql_queries { digest_mail.html_part.body.to_s }
+ %w[post_localizations topic_localizations].each do |table|
+ expect(expanded_queries.count { |query| query.include?(%(FROM "#{table}")) }).to eq(
+ original_queries.count { |query| query.include?(%(FROM "#{table}")) },
+ )
+ end
+ end
+ end
+
+ describe "translated notification content" do
+ fab!(:user) { Fabricate(:user, locale: "ja") }
+ fab!(:topic) { Fabricate(:topic, locale: "en") }
+ fab!(:post) { Fabricate(:post, topic: topic, locale: "en") }
+ fab!(:reply) { Fabricate(:post, topic: topic, locale: "en", reply_to_post_number: 1) }
+ fab!(:post_translation) { Fabricate(:post_localization, post: post) }
+ fab!(:reply_translation) { Fabricate(:post_localization, post: reply) }
+ fab!(:topic_translation) { Fabricate(:topic_localization, topic: topic) }
+
+ before do
+ SiteSetting.allow_user_locale = true
+ SiteSetting.content_localization_enabled = true
+ user.user_option.update!(
+ email_previous_replies: UserOption.previous_replies_type[:always],
+ email_in_reply_to: true,
+ )
+ end
+
+ def notification_mail(recipient = user)
+ UserNotifications.user_replied(
+ recipient,
+ post: reply,
+ notification_type: "replied",
+ notification_data_hash: {
+ original_username: reply.username,
+ topic_title: topic.title,
+ },
+ )
+ end
+
+ it "uses the recipient's translations for the subject, post, and previous replies" do
+ original_post_attributes = reply.attributes.slice("raw", "cooked")
+ original_topic_title = topic.title
+ mail = notification_mail
+ expect(mail.subject).to include(topic_translation.title)
+ [mail.html_part.body.to_s, mail.text_part.body.to_s].each do |body|
+ expect(body).to include(reply_translation.raw, post_translation.raw)
+ expect(body).not_to include(reply.raw, post.raw)
+ end
+ expect(reply.reload.attributes.slice("raw", "cooked")).to eq(original_post_attributes)
+ expect(topic.reload.title).to eq(original_topic_title)
+ expect(I18n.locale).to eq(:en)
+ end
+
+ it "uses translations for mailing list notifications" do
+ mail = UserNotifications.mailing_list_notify(user, reply)
+ expect(mail.subject).to include(topic_translation.title)
+ expect(mail.html_part.body.to_s).to include(reply_translation.raw)
+ expect(mail.text_part.body.to_s).to include(reply_translation.raw)
+ end
+
+ it "preserves the original PM subject when the topic has been renamed" do
+ topic.update!(archetype: Archetype.private_message, category_id: nil)
+ original_title = "Original PM title"
+ mail =
+ UserNotifications.user_private_message(
+ user,
+ post: reply,
+ notification_type: "private_message",
+ notification_data_hash: {
+ original_username: reply.username,
+ topic_title: original_title,
+ },
+ )
+ expect(mail.subject).to include(original_title)
+ expect(mail.html_part.body.to_s).to include(reply_translation.raw)
+ expect(mail.text_part.body.to_s).to include(reply_translation.raw)
+ end
+
+ it "preserves plugin title and body overrides when translations are available" do
+ plugin = Plugin::Instance.new
+ custom_title = "Plugin supplied title"
+ custom_raw = "Plugin supplied plain text"
+ custom_cooked = "<p>Plugin supplied HTML</p>"
+ modifier =
+ proc do |options|
+ options[:title] = custom_title
+ options[:post].raw = custom_raw
+ options[:post].cooked = custom_cooked
+ options
+ end
+ DiscoursePluginRegistry.register_modifier(plugin, :user_notification_email_options, &modifier)
+
+ mail = notification_mail
+ aggregate_failures do
+ expect(mail.subject).to include(custom_title)
+ expect(mail.html_part.body.to_s).to include(custom_cooked)
+ expect(mail.text_part.body.to_s).to include(custom_raw)
+ expect(mail.html_part.body.to_s).to include(post_translation.raw)
+ expect(mail.text_part.body.to_s).to include(post_translation.raw)
+ end
+ ensure
+ DiscoursePluginRegistry.unregister_modifier(
+ plugin,
+ :user_notification_email_options,
+ &modifier
+ )
+ end
+
+ it "uses translated excerpts" do
+ SiteSetting.post_excerpts_in_emails = true
+ expect(notification_mail.html_part.body.to_s).to include(reply_translation.raw)
+ end
+
+ it "falls back to the original when translations are missing" do
+ reply_translation.destroy!
+ topic_translation.destroy!
+ mail = notification_mail
+ expect(mail.subject).to include(topic.title)
+ expect(mail.html_part.body.to_s).to include(reply.raw)
+ expect(mail.text_part.body.to_s).to include(reply.raw)
+ end
+
+ it "respects the automatic translation preference" do
+ user.user_option.update!(automatically_translate: false)
+ mail = notification_mail
+ expect(mail.subject).to include(topic.title)
+ expect(mail.html_part.body.to_s).to include(reply.raw)
+ expect(mail.text_part.body.to_s).to include(reply.raw)
+ end
+
+ it "keeps content in languages the recipient understands" do
+ user.user_option.update!(understood_languages: ["en"])
+ mail = notification_mail
+ expect(mail.subject).to include(topic.title)
+ expect(mail.html_part.body.to_s).to include(reply.raw)
+ expect(mail.text_part.body.to_s).to include(reply.raw)
+ end
+
+ it "keeps selected and understood languages while translating other context posts" do
+ topic.update!(locale: "ja")
+ reply.update!(locale: "ja", post_number: 4)
+ french_post =
+ Fabricate(:post, topic: topic, locale: "fr", post_number: 3, raw: "French context")
+ french_translation = Fabricate(:post_localization, post: french_post)
+ user.user_option.update!(understood_languages: ["en_GB"])
+ mail = notification_mail
+ expect(mail.subject).to include(topic.title)
+ [mail.html_part.body.to_s, mail.text_part.body.to_s].each do |body|
+ expect(body).to include(reply.raw, post.raw, french_translation.raw)
+ expect(body).not_to include(
+ topic_translation.title,
+ reply_translation.raw,
+ post_translation.raw,
+ french_post.raw,
+ )
+ end
+ end
+
+ it "keeps original content when localization is disabled" do
+ SiteSetting.content_localization_enabled = false
+ mail = notification_mail
+ expect(mail.subject).to include(topic.title)
+ expect(mail.html_part.body.to_s).to include(reply.raw)
+ expect(mail.text_part.body.to_s).to include(reply.raw)
+ end
+
+ it "keeps original content when the source language is unknown" do
+ topic.update!(locale: nil)
+ reply.update!(locale: nil)
+ mail = notification_mail
+ expect(mail.subject).to include(topic.title)
+ expect(mail.html_part.body.to_s).to include(reply.raw)
+ expect(mail.text_part.body.to_s).to include(reply.raw)
+ end
+
+ it "redacts translated content in private emails" do
+ SiteSetting.private_email = true
+ mail = notification_mail
+ expect(mail.subject).not_to include(topic_translation.title, topic.title)
+ [mail.html_part.body.to_s, mail.text_part.body.to_s].each do |body|
+ expect(body).not_to include(
+ reply_translation.raw,
+ post_translation.raw,
+ reply.raw,
+ post.raw,
+ )
+ end
+ end
+
+ it "falls back to original content for stale translations" do
+ reply_translation.update!(post_version: reply.version - 1)
+ mail = notification_mail
+ expect(mail.html_part.body.to_s).to include(reply.raw)
+ expect(mail.text_part.body.to_s).to include(reply.raw)
+ end
+
+ it "uses normalized locale matches" do
+ user.update!(locale: "pt_BR")
+ reply_translation.update!(locale: "pt")
+ mail = notification_mail
+ expect(mail.html_part.body.to_s).to include(reply_translation.raw)
+ expect(mail.text_part.body.to_s).to include(reply_translation.raw)
+ end
+
+ it "uses the effective locale when user locales are disabled" do
+ SiteSetting.allow_user_locale = false
+ mail = notification_mail
+ expect(mail.subject).to include(topic.title)
+ expect(mail.html_part.body.to_s).to include(reply.raw)
+ expect(mail.text_part.body.to_s).to include(reply.raw)
+ end
+
+ it "uses translated content with customized email templates" do
+ [SiteSetting.default_locale, user.locale].each do |locale|
+ TranslationOverride.upsert!(
+ locale,
+ "user_notifications.user_replied.text_body_template",
+ "%{topic_title}\n\n%{message}\n\n%{context}",
+ )
+ end
+ mail = notification_mail
+ renderer = Email::Renderer.new(mail)
+ [renderer.html, renderer.text].each do |body|
+ expect(body).to include(
+ topic_translation.title,
+ reply_translation.raw,
+ post_translation.raw,
+ )
+ end
+ end
+
+ it "uses the translated title and excerpt when inviting a user to a topic" do
+ mail =
+ UserNotifications.user_invited_to_topic(
+ user,
+ post: post,
+ notification_type: "invited_to_topic",
+ notification_data_hash: {
+ original_username: post.username,
+ topic_title: topic.title,
+ },
+ )
+ [mail.html_part.body.to_s, mail.text_part.body.to_s].each do |body|
+ expect(body).to include(topic_translation.title, post_translation.raw)
+ end
+ end
+
+ it "keeps original incoming email subjects for staged recipients" do
+ user.update!(staged: true)
+ incoming_email = Fabricate(:incoming_email, post: post, subject: "Original incoming subject")
+ expect(notification_mail.subject).to include(incoming_email.subject)
+ end
+
+ it "formats translated links and redacts secure images" do
+ setup_s3
+ SiteSetting.secure_uploads = true
+ SiteSetting.login_required = true
+ secure_url = "/secure-uploads/original/1X/translated.png"
+ reply_translation.update!(
+ cooked:
+ "<p>#{reply_translation.raw}<a href='/u/admin'>Admin</a><img src='#{secure_url}'></p>",
+ )
+ html = Nokogiri::HTML5.fragment(notification_mail.html_part.body.to_s)
+ expect(html.css("a").map { |link| link["href"] }).to include("#{Discourse.base_url}/u/admin")
+ expect(html.css("img").map { |image| image["src"] }).not_to include(
+ secure_url,
+ "#{Discourse.base_url}#{secure_url}",
+ )
+ expect(html.at_css("[data-stripped-secure-upload]")).to be_present
+ end
+
+ it "loads post translations in a bounded number of queries as context grows" do
+ original_queries = track_sql_queries { notification_mail.html_part.body.to_s }
+ 3.times do
+ context_post = Fabricate(:post, topic: topic, locale: "en")
+ Fabricate(:post_localization, post: context_post)
+ end
+ latest_post = Fabricate(:post, topic: topic, locale: "en")
+ latest_translation = Fabricate(:post_localization, post: latest_post)
+ mail = nil
+ expanded_queries =
+ track_sql_queries do
+ mail = UserNotifications.mailing_list_notify(user, latest_post)
+ mail.html_part.body.to_s
+ end
+ expect(mail.html_part.body.to_s).to include(latest_translation.raw, reply_translation.raw)
+ expect(expanded_queries.count { |query| query.include?('FROM "post_localizations"') }).to eq(
+ original_queries.count { |query| query.include?('FROM "post_localizations"') },
+ )
+ end
+
+ it "selects content independently for each recipient" do
+ notification_mail.html_part.body.to_s
+ other_user = Fabricate(:user, locale: "en")
+ mail = notification_mail(other_user)
+ expect(mail.subject).to include(topic.title)
+ expect(mail.html_part.body.to_s).to include(reply.raw)
+ expect(mail.text_part.body.to_s).to include(reply.raw)
+ end
+ end
+
describe ".user_replied" do
let(:response_by_user) { Fabricate(:user, name: "John Doe") }
let(:category) { Fabricate(:category, name: "India") }