A platform for community discussion. Free, open, simple.
FIX: Hold push notifications for new posts until their translation is stored (#44767)
Rafael dos Santos Silva80fc7d877c82
diff --git a/app/jobs/regular/deliver_push_notification.rb b/app/jobs/regular/deliver_push_notification.rbindex d75a4a2eb18..18b20a0f394 100644--- a/app/jobs/regular/deliver_push_notification.rb+++ b/app/jobs/regular/deliver_push_notification.rb@@ -3,6 +3,22 @@module Jobsclass DeliverPushNotification < ::Jobs::Basedef execute(args)+ payload = args[:payload].with_indifferent_access+ token = args[:localization_wait_token]+ return if token && !PushNotificationLocalizationWait.claim(payload[:post_id], token)++ begin+ deliver(args, payload, held: token.present?)+ rescue StandardError+ # Lets the retry or the wait limit's delivery send it instead.+ PushNotificationLocalizationWait.unclaim(payload[:post_id], token) if token+ raise+ end+ end++ private++ def deliver(args, payload, held:)user = User.find_by(id: args[:user_id])push_window = SiteSetting.push_notification_time_window_minsif !user ||@@ -13,43 +29,66 @@ module Jobsreturnend- payload = args[:payload].with_indifferent_access-if SiteSetting.content_localization_enabled- I18n.with_locale(user.effective_locale) { localize_content!(payload) }+ post = Post.includes(:localizations).find_by(id: payload[:post_id]) if payload[:post_id]+ topic = post&.topic || (Topic.find_by(id: payload[:topic_id]) if payload[:topic_id])++ return if !held && localization_pending?(post, user) && held?(post, user, args)++ translated =+ I18n.with_locale(user.effective_locale) do+ ContentLocalization.translated_notification_fields(+ topic: topic,+ post: post,+ scope: user.guardian,+ )+ end+ translated.delete(:excerpt) if args[:keep_excerpt]+ payload.merge!(translated)endPushNotificationPusher.push(user, payload.deep_dup) if user.push_subscriptions.exists?HubPushNotificationPusher.push(user, payload.deep_dup)end- private+ # Translations are usually stored seconds after the post, so a push sent right+ # away would carry the original text. Plugins producing translations decide+ # whether one is still on its way for this recipient.+ def localization_pending?(post, user)+ return false if post.nil?+ return false if SiteSetting.push_notification_localization_max_wait_mins <= 0+ return false if !ContentLocalization.automatically_translate?(user.guardian)+ return false if ContentLocalization.understands?(post.locale, user.guardian)+ return false if PushNotificationLocalizationWait.done?(post.id)- def localize_content!(payload)- locale = I18n.locale.to_s.sub("-", "_")+ DiscoursePluginRegistry.apply_modifier(+ :deliver_push_notification_localization_pending,+ false,+ post,+ user,+ )+ end- if (topic_id = payload[:topic_id])- topic_localization =- TopicLocalization.find_by(topic_id: topic_id, locale: locale) ||- TopicLocalization.matching_locale(locale).find_by(topic_id: topic_id)- payload[:topic_title] = topic_localization.title if topic_localization- end+ def held?(post, user, args)+ token =+ PushNotificationLocalizationWait.hold(+ post_id: post.id,+ locale: user.effective_locale,+ job_args: args,+ )- if (post_id = payload[:post_id])- post_localization =- PostLocalization.find_by(post_id: post_id, locale: locale) ||- PostLocalization.matching_locale(locale).find_by(post_id: post_id)- if post_localization- payload[:excerpt] = Post.excerpt(- post_localization.cooked,- 400,- text_entities: true,- strip_links: true,- remap_emoji: true,- plain_hashtags: true,- )- end+ # A translation stored between the check and the hold has already fired its+ # release, so look again rather than wait for the limit.+ begin+ post.reload+ still_pending = localization_pending?(post, user)+ rescue StandardError+ # Otherwise the held push would be sent alongside this job's retry.+ raise if PushNotificationLocalizationWait.claim(post.id, token)+ return trueend++ still_pending || !PushNotificationLocalizationWait.claim(post.id, token)endendenddiff --git a/app/models/post_localization.rb b/app/models/post_localization.rbindex 561cdbcc5b8..702f2d0f70f 100644--- a/app/models/post_localization.rb+++ b/app/models/post_localization.rb@@ -15,8 +15,14 @@ class PostLocalization < ActiveRecord::Basevalidates :localizer_user_id, presence: truevalidates :locale, uniqueness: { scope: :post_id }+ after_commit :trigger_saved_event, on: %i[create update]+private+ def trigger_saved_event+ DiscourseEvent.trigger(:post_localization_saved, self)+ end+def access_control_post_id_for_uploadpost_idenddiff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rbindex 6e9bf06108e..eaccf6759b1 100644--- a/app/services/post_alerter.rb+++ b/app/services/post_alerter.rb@@ -59,18 +59,34 @@ class PostAlerterpayload,user,)- MessageBus.publish("/notification-alert/#{user.id}", payload, user_ids: [user.id])++ live_payload = payload+ if SiteSetting.content_localization_enabled+ translated =+ I18n.with_locale(user.effective_locale) do+ ContentLocalization.translated_notification_fields(+ topic: post.topic,+ post: post,+ scope: user.guardian,+ )+ end+ translated.delete(:excerpt) if excerpt+ live_payload = payload.merge(translated)+ end++ MessageBus.publish("/notification-alert/#{user.id}", live_payload, user_ids: [user.id])endend- push_notification(user, payload)+ # A caller-provided excerpt isn't the post's text, so its translation doesn't apply.+ push_notification(user, payload, keep_excerpt: excerpt.present?)# deprecated. use push_notification insteadDiscourseEvent.trigger(:post_notification_alert, user, payload)endend- def self.push_notification(user, payload)+ def self.push_notification(user, payload, keep_excerpt: false)return if user.do_not_disturb?# This DiscourseEvent needs to be independent of the push_notification_filters for some use cases.@@ -91,10 +107,13 @@ class PostAlerterdelay = (push_window - (Time.now - user.last_seen_at) / 60)end+ job_args = { user_id: user.id, payload: payload }+ job_args[:keep_excerpt] = true if keep_excerpt+if delay.present?- Jobs.enqueue_in(delay.minutes, :deliver_push_notification, user_id: user.id, payload: payload)+ Jobs.enqueue_in(delay.minutes, :deliver_push_notification, **job_args)else- Jobs.enqueue(:deliver_push_notification, user_id: user.id, payload: payload)+ Jobs.enqueue(:deliver_push_notification, **job_args)endend@@ -744,8 +763,8 @@ class PostAlerter)end- def push_notification(user, payload)- self.class.push_notification(user, payload)+ def push_notification(user, payload, keep_excerpt: false)+ self.class.push_notification(user, payload, keep_excerpt:)enddef expand_group_mentions(groups, post)diff --git a/config/initializers/301-push-notification-localization-wait.rb b/config/initializers/301-push-notification-localization-wait.rbnew file mode 100644index 00000000000..e72f2f8e24e--- /dev/null+++ b/config/initializers/301-push-notification-localization-wait.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++DiscourseEvent.on(:post_localization_saved) do |localization|+ PushNotificationLocalizationWait.release(localization.post_id, locale: localization.locale)+end++# Triggered by translators once they are done with a post, whether or not they+# stored a translation for every locale.+DiscourseEvent.on(:post_localizations_finished) do |post_id|+ PushNotificationLocalizationWait.finish(post_id)+enddiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 9d84793fa69..d8484004c2c 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -3128,6 +3128,7 @@ en:push_notifications_icon: "The badge icon that appears in the notification corner. A 96×96 monochromatic PNG with transparency is recommended."enable_desktop_push_notifications: "Activate push notifications for the desktop interface. This feature enables real-time alerts from the site directly to your desktop, enhancing engagement and ensuring users are always updated. However, the effectiveness of this feature relies on browser support for push notifications."push_notification_time_window_mins: "Wait (n) minutes before sending push notification. Helps prevent push notifications from being sent to an active online user."+ push_notification_localization_max_wait_mins: "Longest time (in minutes) to hold a push notification for a new post while its translation into the recipient's language is produced. Set to 0 to send without waiting."base_font: "Base font to use for most text on the site. Themes can override via the `--font-family` CSS custom property."heading_font: "Font to use for headings on the site. Themes can override via the `--heading-font-family` CSS custom property."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex d8ce6114b7f..3be744dbe68 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -493,6 +493,10 @@ basic:default: 1min: 0area: "notifications"+ push_notification_localization_max_wait_mins:+ default: 3+ min: 0+ hidden: trueshort_title:default: ""max: 12diff --git a/lib/content_localization.rb b/lib/content_localization.rbindex 57c3971a2b4..f4c175c4cf4 100644--- a/lib/content_localization.rb+++ b/lib/content_localization.rb@@ -66,6 +66,36 @@ class ContentLocalizationtopic.get_localization&.fancy_title.presenceend+ # Expects I18n.locale to be the recipient's locale.+ # @param topic [Topic, nil]+ # @param post [Post, nil]+ # @param scope [Guardian] The recipient's guardian+ # @return [Hash] translated `:topic_title` and `:excerpt`, for whichever are available+ def self.translated_notification_fields(topic:, post:, scope:)+ fields = {}++ if (title = translated_topic_title(topic, scope))+ fields[:topic_title] = title+ end++ if show_translated_post?(post, scope)+ localization = post.get_localization+ # Stale translations would describe an earlier revision of the post.+ if localization&.post_version == post.version && localization.cooked.present?+ fields[:excerpt] = Post.excerpt(+ localization.cooked,+ 400,+ text_entities: true,+ strip_links: true,+ remap_emoji: true,+ plain_hashtags: true,+ )+ end+ end++ fields+ end+# This method returns true when we should try to show the translated category.# @param category [Category] The category record# @param scope [Object] The serializer scope from which the method is calleddiff --git a/lib/push_notification_localization_wait.rb b/lib/push_notification_localization_wait.rbnew file mode 100644index 00000000000..f74180dcf34--- /dev/null+++ b/lib/push_notification_localization_wait.rb@@ -0,0 +1,85 @@+# frozen_string_literal: true++# Holds push notifications for a new post until its translation into the+# recipient's locale is stored, so they don't go out in the original language.+#+# A held push is released when that translation is stored, when the translator+# reports it is done with the post, or when the wait limit passes. All of them may+# fire for the same push, so delivery claims the push first and only one sends it.+class PushNotificationLocalizationWait+ DONE_FIELD = "done"+ HELD_PREFIX = "held:"+ CLAIMED_PREFIX = "claimed:"+ # Outlives the wait limit by far, so deliveries delayed by a backed-up queue can+ # still claim their push.+ TTL = 1.day++ # @param post_id [Integer]+ # @param locale [String] The recipient's locale+ # @param job_args [Hash] Arguments of the push delivery job being held+ # @return [String] Token identifying the held push, passed to {.claim}+ def self.hold(post_id:, locale:, job_args:)+ token = SecureRandom.hex(8)+ args = job_args.except(:current_site_id).merge(localization_wait_token: token)+ limit = SiteSetting.push_notification_localization_max_wait_mins.minutes++ Discourse.redis.multi do |transaction|+ transaction.hset(key(post_id), "#{HELD_PREFIX}#{token}", { locale:, args: }.to_json)+ transaction.expire(key(post_id), TTL)+ end+ Jobs.enqueue_in(limit, :deliver_push_notification, args)++ token+ end++ # Records claims rather than removing held pushes, so a push whose state has+ # expired is still delivered.+ # @return [Boolean] Whether the caller is the one that must deliver the push+ def self.claim(post_id, token)+ claimed, _ =+ Discourse.redis.multi do |transaction|+ transaction.hsetnx(key(post_id), "#{CLAIMED_PREFIX}#{token}", "1")+ transaction.hdel(key(post_id), "#{HELD_PREFIX}#{token}")+ transaction.expire(key(post_id), TTL)+ end+ claimed+ end++ # For a delivery that failed after claiming, so another one can send the push.+ def self.unclaim(post_id, token)+ Discourse.redis.hdel(key(post_id), "#{CLAIMED_PREFIX}#{token}")+ end++ # @return [Boolean] Whether the translator is done with the post+ def self.done?(post_id)+ Discourse.redis.hexists(key(post_id), DONE_FIELD)+ end++ # Delivers pushes held for recipients reading `locale`, or all of them.+ def self.release(post_id, locale: nil)+ Discourse+ .redis+ .hgetall(key(post_id))+ .each do |field, value|+ next if !field.start_with?(HELD_PREFIX)++ held = JSON.parse(value)+ next if locale && !LocaleNormalizer.is_same?(held["locale"], locale)++ Jobs.enqueue(:deliver_push_notification, held["args"])+ end+ end++ def self.finish(post_id)+ Discourse.redis.multi do |transaction|+ transaction.hset(key(post_id), DONE_FIELD, "1")+ transaction.expire(key(post_id), TTL)+ end+ release(post_id)+ end++ def self.key(post_id)+ "push_notification_localization_wait:#{post_id}"+ end+ private_class_method :key+enddiff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rbindex 499a3e53679..95fcb4bd425 100644--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb@@ -20,25 +20,8 @@ module Jobsreturn if post.blank? || post.raw.blank? || post.deleted_at.present?force = args[:force] || false- return if post.user_id <= 0 && !force && !SiteSetting.ai_translation_include_bot_content-- topic = post.topic- return if topic.blank?-- if force- # no restrictions- elsif topic.archetype == Archetype.private_message- case SiteSetting.ai_translation_personal_messages- when "all"- # allow- when "group"- return unless TopicAllowedGroup.exists?(topic_id: topic.id)- else- return- end- else- return if !DiscourseAi::Translation.category_allowed?(topic.category)- end+ return if post.topic.blank?+ return if !force && !DiscourseAi::Translation.post_translatable?(post)# the user may fill locale in manuallyif (detected_locale = post.locale).blank?@@ -78,6 +61,12 @@ module Jobs{ type: :localized, id: post.id },post.topic.secure_audience_publish_messages,)+ ensure+ # Lets anything waiting on this post's translations stop waiting, even when+ # some of them weren't produced.+ if args[:post_id].present?+ DiscourseEvent.trigger(:post_localizations_finished, args[:post_id].to_i)+ endendprivatediff --git a/plugins/discourse-ai/lib/translation.rb b/plugins/discourse-ai/lib/translation.rbindex 596239cdb82..f5c06269c04 100644--- a/plugins/discourse-ai/lib/translation.rb+++ b/plugins/discourse-ai/lib/translation.rb@@ -2,6 +2,8 @@module DiscourseAimodule Translation+ PENDING_LOCALIZATION_WINDOW = 5.minutes+def self.enabled?SiteSetting.discourse_ai_enabled && SiteSetting.ai_translation_enabled && has_llm_model? &&SiteSetting.content_localization_supported_locales.present?@@ -146,6 +148,42 @@ module DiscourseAiendend+ # Whether new posts like this one get translated as soon as they are created.+ def self.post_translatable?(post)+ return false if post.user_id <= 0 && !SiteSetting.ai_translation_include_bot_content++ topic = post.topic+ return false if topic.blank?++ if topic.archetype == Archetype.private_message+ case SiteSetting.ai_translation_personal_messages+ when "all"+ true+ when "group"+ TopicAllowedGroup.exists?(topic_id: topic.id)+ else+ false+ end+ else+ category_allowed?(topic.category)+ end+ end++ # Whether a translation of a just-created post into `locale` is still being produced.+ def self.post_localization_pending?(post, locale)+ return false if !enabled?+ return false if post.created_at < PENDING_LOCALIZATION_WINDOW.ago+ return false if locales.none? { |l| LocaleNormalizer.is_same?(l, locale) }+ return false if !post_translatable?(post)+ return false if !credits_available_for_post_detection?++ # Detection hasn't run yet, so a translation may still be needed.+ return true if post.locale.blank?+ return false if LocaleNormalizer.is_same?(post.locale, locale)++ post.localizations.none? { |l| LocaleNormalizer.is_same?(l.locale, locale) }+ end+def self.llm_model_for_agent(agent_id)return nil if agent_id.blank?diff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rbindex f37503412d1..d02daacb30a 100644--- a/plugins/discourse-ai/plugin.rb+++ b/plugins/discourse-ai/plugin.rb@@ -244,6 +244,10 @@ after_initialize doDiscourseAi::PostImageCaptions.process_cooked(doc, post, locale: post_localization.locale)end+ register_modifier(:deliver_push_notification_localization_pending) do |pending, post, user|+ pending || DiscourseAi::Translation.post_localization_pending?(post, user.effective_locale)+ end+register_modifier(:post_search_index_text) do |text, post_id, cooked, locale|DiscourseAi::PostImageCaptions.append_to_search_text(text, post_id, cooked, locale: locale)enddiff --git a/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb b/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rbindex 53d0357d5d3..00ee76ced9e 100644--- a/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb@@ -46,6 +46,31 @@ describe Jobs::DetectTranslatePost dojob.execute({ post_id: post.id })end+ describe "finishing with a post" do+ it "reports it is done after translating the post" do+ post.update!(locale: "en")+ DiscourseAi::Translation::PostLocalizer.stubs(:localize)++ events =+ DiscourseEvent.track_events(:post_localizations_finished) do+ job.execute({ post_id: post.id })+ end++ expect(events.map { |e| e[:params] }).to eq([[post.id]])+ end++ it "reports it is done when it skips the post" do+ SiteSetting.discourse_ai_enabled = false++ events =+ DiscourseEvent.track_events(:post_localizations_finished) do+ job.execute({ post_id: post.id })+ end++ expect(events.map { |e| e[:params] }).to eq([[post.id]])+ end+ end+it "detects locale" doallow(DiscourseAi::Translation::PostLocaleDetector).to receive(:detect_locale).with(post,diff --git a/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb b/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rbindex 6664283c42c..a894d3f666e 100644--- a/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb@@ -252,4 +252,32 @@ describe DiscourseAi::Translation::EntryPoint doexpect(job_enqueued?(job: :localize_tags)).to eq(false)endend++ describe "push notifications for new posts" do+ fab!(:user) { Fabricate(:user, locale: "ja", last_seen_at: 1.hour.ago) }+ fab!(:post)++ before do+ SiteSetting.content_localization_enabled = true+ SiteSetting.allow_user_locale = true+ SiteSetting.content_localization_supported_locales = "en|ja"+ Fabricate(:push_subscription, user: user)+ end++ it "holds the push until the translation is stored" do+ PushNotificationPusher.expects(:push).never++ Jobs::DeliverPushNotification.new.execute(+ user_id: user.id,+ payload: {+ post_id: post.id,+ topic_id: post.topic_id,+ excerpt: post.excerpt,+ },+ )++ held = Jobs::DeliverPushNotification.jobs.last["args"].first+ expect(held["localization_wait_token"]).to be_present+ end+ endenddiff --git a/plugins/discourse-ai/spec/lib/translation/translation_spec.rb b/plugins/discourse-ai/spec/lib/translation/translation_spec.rbindex 7fa76b04b8a..5cf988b82de 100644--- a/plugins/discourse-ai/spec/lib/translation/translation_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/translation_spec.rb@@ -115,4 +115,50 @@ describe DiscourseAi::Translation doexpect(described_class.category_allowed?(private_category.id)).to eq(false)endend++ describe ".post_localization_pending?" do+ fab!(:post)++ before do+ SiteSetting.content_localization_supported_locales = "en|ja"+ freeze_time+ end++ it "is true while the post's locale hasn't been detected" do+ expect(described_class.post_localization_pending?(post, "ja")).to eq(true)+ end++ it "is true until the translation into the locale is stored" do+ post.update_columns(locale: "en")+ expect(described_class.post_localization_pending?(post, "ja")).to eq(true)++ Fabricate(:post_localization, post: post, locale: "ja")+ expect(described_class.post_localization_pending?(post.reload, "ja")).to eq(false)+ end++ it "is false when the post is already in the locale" do+ post.update_columns(locale: "ja")+ expect(described_class.post_localization_pending?(post, "ja")).to eq(false)+ end++ it "is false for locales that aren't translated into" do+ expect(described_class.post_localization_pending?(post, "fr")).to eq(false)+ end++ it "is false for older posts" do+ post.update_columns(created_at: (described_class::PENDING_LOCALIZATION_WINDOW + 1.minute).ago)+ expect(described_class.post_localization_pending?(post, "ja")).to eq(false)+ end++ it "is false for posts that aren't translated automatically" do+ SiteSetting.ai_translation_category_scope = "exclude_strict"+ SiteSetting.ai_translation_categories = post.topic.category_id.to_s+ expect(described_class.post_localization_pending?(post, "ja")).to eq(false)+ end++ it "is false when translation is disabled" do+ SiteSetting.ai_translation_enabled = false+ expect(described_class.post_localization_pending?(post, "ja")).to eq(false)+ end+ endenddiff --git a/spec/jobs/deliver_push_notification_spec.rb b/spec/jobs/deliver_push_notification_spec.rbindex 73804a69911..16f5dc59381 100644--- a/spec/jobs/deliver_push_notification_spec.rb+++ b/spec/jobs/deliver_push_notification_spec.rb@@ -90,8 +90,10 @@ RSpec.describe Jobs::DeliverPushNotification doenddescribe "content localization" do- fab!(:topic) { Fabricate(:topic, title: "Original topic title") }- fab!(:localized_post) { Fabricate(:post, topic: topic, raw: "Original post content") }+ fab!(:topic) { Fabricate(:topic, title: "Original topic title", locale: "en") }+ fab!(:localized_post) do+ Fabricate(:post, topic: topic, raw: "Original post content", locale: "en")+ endlet(:localizable_payload) do{@@ -186,5 +188,211 @@ RSpec.describe Jobs::DeliverPushNotification doJobs::DeliverPushNotification.new.execute(user_id: user.id, payload: localizable_payload)end++ it "keeps the original excerpt when the translation is for an older version of the post" do+ Fabricate(:push_subscription, user: user)+ Fabricate(+ :post_localization,+ post: localized_post,+ locale: "ja",+ post_version: localized_post.version - 1,+ cooked: "<p>古い翻訳</p>",+ )++ PushNotificationPusher.expects(:push).with { |_, p| p[:excerpt] == "Original post content" }++ Jobs::DeliverPushNotification.new.execute(user_id: user.id, payload: localizable_payload)+ end++ it "keeps the original content when the user turned off automatic translation" do+ user.user_option.update!(automatically_translate: false)+ Fabricate(:push_subscription, user: user)+ Fabricate(:post_localization, post: localized_post, locale: "ja", cooked: "<p>翻訳</p>")++ PushNotificationPusher.expects(:push).with { |_, p| p[:excerpt] == "Original post content" }++ Jobs::DeliverPushNotification.new.execute(user_id: user.id, payload: localizable_payload)+ end++ it "keeps a caller-provided excerpt" do+ Fabricate(:push_subscription, user: user)+ Fabricate(:topic_localization, topic: topic, locale: "ja", title: "ローカライズされたトピック")+ Fabricate(:post_localization, post: localized_post, locale: "ja", cooked: "<p>翻訳</p>")++ PushNotificationPusher+ .expects(:push)+ .with do |_, p|+ p[:topic_title] == "ローカライズされたトピック" && p[:excerpt] == "Original post content"+ end++ Jobs::DeliverPushNotification.new.execute(+ user_id: user.id,+ payload: localizable_payload,+ keep_excerpt: true,+ )+ end++ describe "when a translation is still pending" do+ let(:plugin) { Plugin::Instance.new }+ let(:pending) { [true] }+ let(:pending_modifier) { Proc.new { |_pending, _post, _user| pending.first } }++ before do+ Fabricate(:push_subscription, user: user)+ DiscoursePluginRegistry.register_modifier(+ plugin,+ :deliver_push_notification_localization_pending,+ &pending_modifier+ )+ end++ after do+ DiscoursePluginRegistry.unregister_modifier(+ plugin,+ :deliver_push_notification_localization_pending,+ &pending_modifier+ )+ end++ def deliver(**args)+ Jobs::DeliverPushNotification.new.execute(+ user_id: user.id,+ payload: localizable_payload,+ **args,+ )+ end++ def held_token+ Jobs::DeliverPushNotification.jobs.last.dig("args", 0, "localization_wait_token")+ end++ it "holds the push until the wait limit at most" do+ SiteSetting.push_notification_localization_max_wait_mins = 3+ PushNotificationPusher.expects(:push).never++ expect_enqueued_with(job: :deliver_push_notification, at: 3.minutes.from_now) { deliver }+ expect(held_token).to be_present+ end++ it "sends the held push once it is released by a stored translation" do+ PushNotificationPusher.expects(:push).with { |_, p| p[:excerpt] == "ローカライズされた投稿" }.once++ deliver+ token = held_token+ Jobs::DeliverPushNotification.jobs.clear++ pending[0] = false+ Fabricate(+ :post_localization,+ post: localized_post,+ locale: "ja",+ cooked: "<p>ローカライズされた投稿</p>",+ )++ released = Jobs::DeliverPushNotification.jobs.map { |job| job["args"].first }+ expect(released.map { |a| a["localization_wait_token"] }).to eq([token])++ # The release and the wait limit both deliver the held push; only one sends it.+ 2.times { deliver(localization_wait_token: token) }+ end++ it "still sends a held push whose wait state has expired" do+ deliver+ token = held_token+ Discourse.redis.del("push_notification_localization_wait:#{localized_post.id}")++ PushNotificationPusher.expects(:push).once++ deliver(localization_wait_token: token)+ end++ it "lets another delivery send a held push when one fails" do+ deliver+ token = held_token++ PushNotificationPusher.stubs(:push).raises(StandardError).then.returns(nil)++ expect { deliver(localization_wait_token: token) }.to raise_error(StandardError)++ PushNotificationPusher.expects(:push).once+ deliver(localization_wait_token: token)+ end++ it "sends the held push at the wait limit even when the translation never came" do+ deliver+ token = held_token++ PushNotificationPusher.expects(:push).with { |_, p| p[:excerpt] == "Original post content" }++ deliver(localization_wait_token: token)+ end++ it "does not leave a held push behind for its retry to duplicate when holding fails" do+ Post.any_instance.stubs(:reload).raises(ActiveRecord::ConnectionNotEstablished)+ PushNotificationPusher.expects(:push).never++ expect { deliver }.to raise_error(ActiveRecord::ConnectionNotEstablished)++ deliver(localization_wait_token: held_token)+ end++ it "does not release pushes held for other locales" do+ deliver+ Jobs::DeliverPushNotification.jobs.clear++ Fabricate(:post_localization, post: localized_post, locale: "de")++ expect(Jobs::DeliverPushNotification.jobs).to be_empty+ end++ it "releases every held push when the translator is done with the post" do+ deliver+ token = held_token+ Jobs::DeliverPushNotification.jobs.clear++ DiscourseEvent.trigger(:post_localizations_finished, localized_post.id)++ released = Jobs::DeliverPushNotification.jobs.map { |job| job["args"].first }+ expect(released.map { |a| a["localization_wait_token"] }).to eq([token])+ end++ it "does not hold the push once the translator is done with the post" do+ PushNotificationLocalizationWait.finish(localized_post.id)+ PushNotificationPusher.expects(:push).once++ expect_not_enqueued_with(job: :deliver_push_notification) { deliver }+ end++ it "sends right away when the translation was stored while holding the push" do+ PushNotificationLocalizationWait+ .stubs(:hold)+ .with do+ pending[0] = false+ true+ end+ .returns("token")+ PushNotificationLocalizationWait+ .expects(:claim)+ .with(localized_post.id, "token")+ .returns(true)+ PushNotificationPusher.expects(:push).once++ deliver+ end++ it "does not wait when the wait limit is 0" do+ SiteSetting.push_notification_localization_max_wait_mins = 0+ PushNotificationPusher.expects(:push).once++ expect_not_enqueued_with(job: :deliver_push_notification) { deliver }+ end++ it "does not wait when the user understands the language of the post" do+ user.user_option.update!(understood_languages: ["en"])+ PushNotificationPusher.expects(:push).once++ expect_not_enqueued_with(job: :deliver_push_notification) { deliver }+ end+ endendenddiff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rbindex 90d32e7fd75..f2267ed157c 100644--- a/spec/services/post_alerter_spec.rb+++ b/spec/services/post_alerter_spec.rb@@ -1589,6 +1589,56 @@ RSpec.describe PostAlerter do)end+ describe "with content localization" do+ before do+ SiteSetting.content_localization_enabled = true+ SiteSetting.allow_user_locale = true+ evil_trout.update!(locale: "ja")+ post.update_columns(locale: "en")+ post.topic.update_columns(locale: "en")+ Fabricate(:topic_localization, topic: post.topic, locale: "ja", title: "翻訳されたタイトル")+ Fabricate(:post_localization, post: post, locale: "ja", cooked: "<p>翻訳された投稿</p>")+ end++ def publish_alert(**opts)+ MessageBus.track_publish("/notification-alert/#{evil_trout.id}") do+ PostAlerter.create_notification_alert(+ user: evil_trout,+ post: post,+ notification_type: Notification.types[:mentioned],+ **opts,+ )+ end+ end++ it "sends the translation in the recipient's locale" do+ messages = publish_alert++ expect(messages.first.data[:topic_title]).to eq("翻訳されたタイトル")+ expect(messages.first.data[:excerpt]).to eq("翻訳された投稿")+ end++ it "tells the push to keep an excerpt provided by the caller" do+ evil_trout.update_columns(last_seen_at: 1.hour.ago)+ Fabricate(:push_subscription, user: evil_trout)++ expect_enqueued_with(+ job: :deliver_push_notification,+ args: {+ user_id: evil_trout.id,+ keep_excerpt: true,+ },+ ) { publish_alert(excerpt: "You were assigned") }+ end++ it "keeps an excerpt provided by the caller" do+ messages = publish_alert(excerpt: "You were assigned")++ expect(messages.first.data[:topic_title]).to eq("翻訳されたタイトル")+ expect(messages.first.data[:excerpt]).to eq("You were assigned")+ end+ end+it "does nothing for suspended users" doevil_trout.update_columns(suspended_till: 1.year.from_now)