DEV: Move include condition to include_? method.

DEV: Move include condition to include_? method.
diff --git a/plugin.rb b/plugin.rb
index aa61f0e..7a28dfb 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -169,22 +169,38 @@ after_initialize do
   #
   # Topic title encrypted with topic key.
 
-  add_to_serializer(:topic_view, :encrypted_title) do
+  add_to_serializer(:topic_view, :encrypted_title, false) do
     object.topic.custom_fields["encrypted_title"]
   end
 
-  add_to_serializer(:basic_topic, :encrypted_title) do
+  add_to_serializer(:topic_view, :include_encrypted_title?) do
+    scope&.user.present? && object.topic.private_message?
+  end
+
+  add_to_serializer(:basic_topic, :encrypted_title, false) do
     object.custom_fields["encrypted_title"]
   end
 
-  add_to_serializer(:listable_topic, :encrypted_title) do
+  add_to_serializer(:basic_topic, :include_encrypted_title?) do
+    scope&.user.present? && object.private_message?
+  end
+
+  add_to_serializer(:listable_topic, :encrypted_title, false) do
     object.custom_fields["encrypted_title"]
   end
 
-  add_to_serializer(:topic_list_item, :encrypted_title) do
+  add_to_serializer(:listable_topic, :include_encrypted_title?) do
+    scope&.user.present? && object.private_message?
+  end
+
+  add_to_serializer(:topic_list_item, :encrypted_title, false) do
     object.custom_fields["encrypted_title"]
   end
 
+  add_to_serializer(:topic_list_item, :include_encrypted_title?) do
+    scope&.user.present? && object.private_message?
+  end
+
   # +topic_key+
   #
   # Topic's key encrypted with user's public key.
@@ -193,43 +209,35 @@ after_initialize do
   # paired private key.
 
   add_to_serializer(:topic_view, :topic_key, false) do
-    if object.topic.private_message?
-      PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.topic.id}_#{scope.user.id}")
-    end
+    PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.topic.id}_#{scope.user.id}")
   end
 
   add_to_serializer(:topic_view, :include_topic_key?) do
-    scope&.user.present?
+    scope&.user.present? && object.topic.private_message?
   end
 
   add_to_serializer(:basic_topic, :topic_key, false) do
-    if object.private_message?
-      PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
-    end
+    PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
   end
 
   add_to_serializer(:basic_topic, :include_topic_key?) do
-    scope&.user.present?
+    scope&.user.present? && object.private_message?
   end
 
   add_to_serializer(:listable_topic, :topic_key, false) do
-    if object.private_message?
-      PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
-    end
+    PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
   end
 
   add_to_serializer(:listable_topic, :include_topic_key?) do
-    scope&.user.present?
+    scope&.user.present? && object.private_message?
   end
 
   add_to_serializer(:topic_list_item, :topic_key, false) do
-    if object.private_message?
-      PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
-    end
+    PluginStore.get(DiscourseEncrypt::PLUGIN_NAME, "key_#{object.id}_#{scope.user.id}")
   end
 
   add_to_serializer(:topic_list_item, :include_topic_key?) do
-    scope&.user.present?
+    scope&.user.present? && object.private_message?
   end
 
   DiscourseEncrypt::Engine.routes.draw do
diff --git a/spec/requests/encrypt_controller_spec.rb b/spec/requests/encrypt_controller_spec.rb
index e7a0c6e..06ecbcb 100644
--- a/spec/requests/encrypt_controller_spec.rb
+++ b/spec/requests/encrypt_controller_spec.rb
@@ -125,6 +125,7 @@ describe ::DiscourseEncrypt::EncryptController do
         }
       }
 
+      expect(response.status).to eq(403)
       expect(topic.custom_fields['encrypted_title']).to eq('-- the encrypted title --')
       expect(store.get("key_#{topic.id}_#{user.id}")).to eq('-- the key of user --')
       expect(store.get("key_#{topic.id}_#{user2.id}")).to eq('-- the key of user2 --')
@@ -142,6 +143,7 @@ describe ::DiscourseEncrypt::EncryptController do
         }
       }
 
+      expect(response.status).to eq(403)
       expect(topic.custom_fields['encrypted_title']).to eq('-- the encrypted title --')
       expect(store.get("key_#{topic.id}_#{user.id}")).to eq('-- the key of user --')
       expect(store.get("key_#{topic.id}_#{user2.id}")).to eq('-- the key of user2 --')
diff --git a/spec/serializers/basic_topic_serializer_spec.rb b/spec/serializers/basic_topic_serializer_spec.rb
index 4a1cec2..8698164 100644
--- a/spec/serializers/basic_topic_serializer_spec.rb
+++ b/spec/serializers/basic_topic_serializer_spec.rb
@@ -16,10 +16,10 @@ describe BasicTopicSerializer do
     store.set("key_#{topic.id}_#{user.id}", '-- the key of user --')
   end
 
-  it 'does not contain user-specific encrypted fields when scope is missing' do
+  it 'does not contain encrypted fields when scope is missing' do
     serialized = described_class.new(topic, root: false).as_json
 
-    expect(serialized[:encrypted_title]).to eq('-- the encrypted title --')
+    expect(serialized[:encrypted_title]).to eq(nil)
     expect(serialized[:topic_key]).to eq(nil)
   end
 
diff --git a/spec/serializers/topic_serializers_spec.rb b/spec/serializers/topic_serializers_spec.rb
index 52ffe6a..a31ab45 100644
--- a/spec/serializers/topic_serializers_spec.rb
+++ b/spec/serializers/topic_serializers_spec.rb
@@ -28,7 +28,7 @@ require 'rails_helper'
       expect(serialized[:topic_key]).to eq(nil)
 
       serialized = described_class.new(topic, scope: Guardian.new, root: false).as_json
-      expect(serialized[:encrypted_title]).to eq('-- the encrypted title --')
+      expect(serialized[:encrypted_title]).to eq(nil)
       expect(serialized[:topic_key]).to eq(nil)
     end
 
diff --git a/spec/serializers/topic_view_serializer_spec.rb b/spec/serializers/topic_view_serializer_spec.rb
index 63b40b1..7e7b3a3 100644
--- a/spec/serializers/topic_view_serializer_spec.rb
+++ b/spec/serializers/topic_view_serializer_spec.rb
@@ -29,7 +29,7 @@ describe TopicViewSerializer do
     expect(serialized[:topic_key]).to eq(nil)
 
     serialized = described_class.new(topic_view, scope: Guardian.new, root: false).as_json
-    expect(serialized[:encrypted_title]).to eq('-- the encrypted title --')
+    expect(serialized[:encrypted_title]).to eq(nil)
     expect(serialized[:topic_key]).to eq(nil)
   end

GitHub

1 Like