FIX: private message email without in reply to section (#85)

FIX: private message email without in reply to section (#85)

Both “In replay to” and “Previous replies” sections should be removed from email as they don’t contain any useful information in encrypted messages.

diff --git a/app/mailers/user_notifications_extensions.rb b/app/mailers/user_notifications_extensions.rb
index 16cb4dd..2e450a4 100644
--- a/app/mailers/user_notifications_extensions.rb
+++ b/app/mailers/user_notifications_extensions.rb
@@ -8,4 +8,15 @@ module UserNotificationsExtensions
     end
     super
   end
+
+  module ClassMethods
+    def get_context_posts(post, topic_user, user)
+      return [] if post.is_encrypted?
+      super
+    end
+  end
+
+  def self.prepended(mod)
+    mod.singleton_class.prepend(ClassMethods)
+  end
 end
diff --git a/lib/user_notification_renderer_extensions.rb b/lib/user_notification_renderer_extensions.rb
new file mode 100644
index 0000000..cec5757
--- /dev/null
+++ b/lib/user_notification_renderer_extensions.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module UserNotificationRendererExtensions
+  def render(*args)
+    post = args[0]&.dig(:locals, :post)
+    args[0][:locals][:in_reply_to_post] = nil if post&.is_encrypted?
+    super(*args)
+  end
+end
diff --git a/plugin.rb b/plugin.rb
index dee733d..65108cf 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -40,6 +40,7 @@ after_initialize do
   load File.expand_path('../lib/topics_controller_extensions.rb', __FILE__)
   load File.expand_path('../lib/upload_validator_extensions.rb', __FILE__)
   load File.expand_path('../lib/user_extensions.rb', __FILE__)
+  load File.expand_path('../lib/user_notification_renderer_extensions.rb', __FILE__)
 
   class DiscourseEncrypt::Engine < Rails::Engine
     engine_name DiscourseEncrypt::PLUGIN_NAME
@@ -76,6 +77,7 @@ after_initialize do
     UserNotifications.class_eval     { prepend UserNotificationsExtensions }
 
     SiteSetting.singleton_class.prepend SiteSettingExtensions
+    UserNotificationRenderer.singleton_class.prepend UserNotificationRendererExtensions
   end
 
   # Send plugin-specific topic data to client via serializers.
diff --git a/spec/lib/email_sender_spec.rb b/spec/lib/email_sender_spec.rb
index 3cbeb2a..0a5cb95 100644
--- a/spec/lib/email_sender_spec.rb
+++ b/spec/lib/email_sender_spec.rb
@@ -19,6 +19,13 @@ describe Email::Sender do
       .create_for(Discourse.system_user.id)
   end
 
+  fab!(:user) { Fabricate(:user) }
+
+  before do
+    user.user_option.update!(email_in_reply_to: true,
+                             email_previous_replies: UserOption.previous_replies_type[:always])
+  end
+
   context "encrypted" do
     fab!(:encrypted_topic) { Fabricate(:encrypt_topic) }
     fab!(:encrypted_post) { Fabricate(:encrypt_post, topic: encrypted_topic) }
@@ -28,14 +35,18 @@ describe Email::Sender do
       Hello world!
       #{UploadMarkdown.new(small_pdf).attachment_markdown}
       RAW
-      reply = Fabricate(:encrypt_post, raw: raw, topic: encrypted_post.topic, user: Fabricate(:user))
+      reply = Fabricate(:encrypt_post,
+                        raw: raw,
+                        topic: encrypted_post.topic,
+                        user: Fabricate(:user),
+                        reply_to_post_number: encrypted_post.post_number)
       reply.link_post_uploads
       reply
     end
     fab!(:notification) { Fabricate(:posted_notification, user: encrypted_post.user, post: encrypted_reply) }
     let(:message) do
-      UserNotifications.user_posted(
-        encrypted_post.user,
+      UserNotifications.user_replied(
+        user,
         post: encrypted_reply,
         notification_type: notification.notification_type,
         notification_data_hash: notification.data_hash
@@ -50,6 +61,9 @@ describe Email::Sender do
       expect(message.reply_to).to eq(["noreply@test.localhost"])
       expect(message.body.raw_source).not_to match("or reply to this email to respond")
       expect(message.subject).to match("[Discourse] [PM] A secret message ##{encrypted_topic.id}")
+      renderer = Email::Renderer.new(message, {})
+      expect(renderer.html).not_to match("In Reply To")
+      expect(renderer.html).not_to match("Previous Replies")
     end
   end
 
@@ -60,14 +74,18 @@ describe Email::Sender do
       Hello world!
       #{UploadMarkdown.new(small_pdf).attachment_markdown}
       RAW
-      reply = Fabricate(:post, raw: raw, topic: post.topic, user: Fabricate(:user))
+      reply = Fabricate(:post,
+                        raw: raw,
+                        topic: post.topic,
+                        user: Fabricate(:user),
+                        reply_to_post_number: post.post_number)
       reply.link_post_uploads
       reply
     end
     fab!(:notification) { Fabricate(:posted_notification, user: post.user, post: reply) }
     let(:message) do
-      UserNotifications.user_posted(
-        post.user,
+      UserNotifications.user_replied(
+        user,
         post: reply,
         notification_type: notification.notification_type,
         notification_data_hash: notification.data_hash
@@ -82,6 +100,9 @@ describe Email::Sender do
       expect(message.reply_to).to eq(["test+%{reply_key}@example.com"])
       expect(message.body.raw_source).to match("or reply to this email to respond")
       expect(message.subject).to match("[Discourse] #{post.topic.title}")
+      renderer = Email::Renderer.new(message, {})
+      expect(renderer.html).to match("In Reply To")
+      expect(renderer.html).to match("Previous Replies")
     end
   end
 end

GitHub sha: 3c6d3996

1 Like

This commit appears in #85 which was approved by udan11. It was merged by lis2.