DEV: Replace magic strings

DEV: Replace magic strings

Make coding style a little more consistent too.

diff --git a/app/controllers/encrypt_controller.rb b/app/controllers/encrypt_controller.rb
index 248662a..adb9389 100644
--- a/app/controllers/encrypt_controller.rb
+++ b/app/controllers/encrypt_controller.rb
@@ -23,20 +23,20 @@ class DiscourseEncrypt::EncryptController < ApplicationController
     private_id_label = params[:label]
 
     # Check if encryption is already enabled (but not changing passphrase).
-    old_identity = current_user.custom_fields['encrypt_public']
+    old_identity = current_user.custom_fields[DiscourseEncrypt::PUBLIC_CUSTOM_FIELD]
     if params[:overwrite].blank? && old_identity && old_identity != public_identity
       return render_json_error(I18n.t('encrypt.enabled_already'), status: 409)
     end
 
-    current_user.custom_fields['encrypt_public'] = public_identity
+    current_user.custom_fields[DiscourseEncrypt::PUBLIC_CUSTOM_FIELD] = public_identity
 
     if private_identity.present?
       if private_id_label.present?
-        data = JSON.parse(current_user.custom_fields['encrypt_private']) rescue {}
+        data = JSON.parse(current_user.custom_fields[DiscourseEncrypt::PRIVATE_CUSTOM_FIELD]) rescue {}
         data[private_id_label.downcase] = private_identity
-        current_user.custom_fields['encrypt_private'] = JSON.dump(data)
+        current_user.custom_fields[DiscourseEncrypt::PRIVATE_CUSTOM_FIELD] = JSON.dump(data)
       else
-        current_user.custom_fields['encrypt_private'] = private_identity
+        current_user.custom_fields[DiscourseEncrypt::PRIVATE_CUSTOM_FIELD] = private_identity
       end
     end
 
@@ -54,9 +54,9 @@ class DiscourseEncrypt::EncryptController < ApplicationController
   def delete_key
     private_id_label = params.require(:label)
 
-    data = JSON.parse(current_user.custom_fields['encrypt_private']) rescue {}
+    data = JSON.parse(current_user.custom_fields[DiscourseEncrypt::PRIVATE_CUSTOM_FIELD]) rescue {}
     if data.delete(private_id_label)
-      current_user.custom_fields['encrypt_private'] = JSON.dump(data)
+      current_user.custom_fields[DiscourseEncrypt::PRIVATE_CUSTOM_FIELD] = JSON.dump(data)
       current_user.save_custom_fields
     end
 
@@ -73,7 +73,7 @@ class DiscourseEncrypt::EncryptController < ApplicationController
   def show_user
     usernames = params.require(:usernames)
 
-    identities = Hash[User.where(username: usernames).map { |u| [u.username, u.custom_fields['encrypt_public']] }]
+    identities = Hash[User.where(username: usernames).map { |u| [u.username, u.custom_fields[DiscourseEncrypt::PUBLIC_CUSTOM_FIELD]] }]
 
     render json: identities
   end
@@ -81,7 +81,8 @@ class DiscourseEncrypt::EncryptController < ApplicationController
   # Resets encryption keys for a user.
   #
   # Params:
-  # +user_id+::   ID of user to be reset
+  # +user_id+::     ID of user to be reset
+  # +everything+::  Whether user should be univited all keys removed
   #
   # Returns status code 200 after user is reset.
   def reset_user
@@ -106,8 +107,8 @@ class DiscourseEncrypt::EncryptController < ApplicationController
     end
 
     # Delete encryption keys.
-    user.custom_fields.delete('encrypt_public')
-    user.custom_fields.delete('encrypt_private')
+    user.custom_fields.delete(DiscourseEncrypt::PUBLIC_CUSTOM_FIELD)
+    user.custom_fields.delete(DiscourseEncrypt::PRIVATE_CUSTOM_FIELD)
     user.save_custom_fields
 
     render json: success_json
diff --git a/lib/encrypted_post_creator.rb b/lib/encrypted_post_creator.rb
index 4c77b90..226f8ba 100644
--- a/lib/encrypted_post_creator.rb
+++ b/lib/encrypted_post_creator.rb
@@ -12,7 +12,7 @@ class EncryptedPostCreator < PostCreator
       # Encrypt title and post contents
       @opts[:raw] = EncryptedPostCreator.encrypt(@opts[:raw], topic_key)
       if title = @opts[:title]
-        @opts[:title] = I18n.t("js.encrypt.encrypted_topic_title")
+        @opts[:title] = I18n.t('js.encrypt.encrypted_topic_title')
         @opts[:topic_opts] ||= {}
         @opts[:topic_opts][:custom_fields] ||= {}
         @opts[:topic_opts][:custom_fields][DiscourseEncrypt::TITLE_CUSTOM_FIELD] = EncryptedPostCreator.encrypt(title, topic_key)
@@ -68,7 +68,7 @@ class EncryptedPostCreator < PostCreator
     cipher = OpenSSL::Cipher::AES.new(256, :GCM).encrypt
     cipher.key = key
     cipher.iv = iv
-    cipher.auth_data = ""
+    cipher.auth_data = ''
 
     plaintext = JSON.dump(raw: raw)
     ciphertext = cipher.update(plaintext)
diff --git a/lib/post_extensions.rb b/lib/post_extensions.rb
index afa4a38..e5ee2f2 100644
--- a/lib/post_extensions.rb
+++ b/lib/post_extensions.rb
@@ -3,6 +3,6 @@
 module PostExtensions
   def is_encrypted?
     !!(topic&.is_encrypted? &&
-        raw.match(/\A[A-Za-z0-9+\\\/=$]+(\n.*)?\Z/))
+       raw.match(/\A[A-Za-z0-9+\\\/=$]+(\n.*)?\Z/))
   end
 end
diff --git a/lib/topic_extensions.rb b/lib/topic_extensions.rb
index 0288346..7dac210 100644
--- a/lib/topic_extensions.rb
+++ b/lib/topic_extensions.rb
@@ -3,8 +3,8 @@
 module TopicExtensions
   def is_encrypted?
     !!(private_message? &&
-        custom_fields &&
-        custom_fields[DiscourseEncrypt::TITLE_CUSTOM_FIELD])
+       custom_fields &&
+       custom_fields[DiscourseEncrypt::TITLE_CUSTOM_FIELD])
   end
 
   def remove_allowed_user(removed_by, user)
diff --git a/lib/topics_controller_extensions.rb b/lib/topics_controller_extensions.rb
index 5f82bed..0c0b057 100644
--- a/lib/topics_controller_extensions.rb
+++ b/lib/topics_controller_extensions.rb
@@ -22,7 +22,7 @@ module TopicsControllerExtensions
         guardian.ensure_can_invite_to!(@topic)
         DiscourseEncrypt::set_key(@topic.id, @user.id, params[:key])
       else
-        return render_json_error(I18n.t("js.encrypt.cannot_invite"))
+        return render_json_error(I18n.t('js.encrypt.cannot_invite'))
       end
     end
 
@@ -33,7 +33,7 @@ module TopicsControllerExtensions
     @topic ||= Topic.find_by(id: params[:topic_id])
 
     if @topic.is_encrypted?
-      return render_json_error(I18n.t("js.encrypt.cannot_invite_group"))
+      return render_json_error(I18n.t('js.encrypt.cannot_invite_group'))
     end
 
     super
diff --git a/lib/user_extensions.rb b/lib/user_extensions.rb
index eb7385b..1590a90 100644
--- a/lib/user_extensions.rb
+++ b/lib/user_extensions.rb
@@ -3,7 +3,7 @@
 module UserExtensions
   def encrypt_key
     @encrypt_key ||= begin
-      identity = self.custom_fields['encrypt_public']
+      identity = self.custom_fields[DiscourseEncrypt::PUBLIC_CUSTOM_FIELD]
       return nil if !identity
 
       # Check identity version
diff --git a/plugin.rb b/plugin.rb
index bc594d2..0954a18 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -24,26 +24,26 @@ after_initialize do
     Store = PluginStore.new(PLUGIN_NAME)
 
     def self.set_key(topic_id, user_id, key)
-      DiscourseEncrypt::Store.set("key_#{topic_id}_#{user_id}", key)
+      Store.set("key_#{topic_id}_#{user_id}", key)
     end
 
     def self.get_key(topic_id, user_id)
-      DiscourseEncrypt::Store.get("key_#{topic_id}_#{user_id}")
+      Store.get("key_#{topic_id}_#{user_id}")
     end
 
     def self.del_key(topic_id, user_id)
-      DiscourseEncrypt::Store.remove("key_#{topic_id}_#{user_id}")
+      Store.remove("key_#{topic_id}_#{user_id}")
     end
   end
 
-  load File.expand_path("../app/controllers/encrypt_controller.rb", __FILE__)
-  load File.expand_path("../app/jobs/scheduled/encrypt_consistency.rb", __FILE__)
-  load File.expand_path("../lib/encrypted_post_creator.rb", __FILE__)
-  load File.expand_path("../lib/openssl.rb", __FILE__)
-  load File.expand_path("../lib/post_extensions.rb", __FILE__)
-  load File.expand_path("../lib/topic_extensions.rb", __FILE__)
-  load File.expand_path("../lib/topics_controller_extensions.rb", __FILE__)
-  load File.expand_path("../lib/user_extensions.rb", __FILE__)
+  load File.expand_path('../app/controllers/encrypt_controller.rb', __FILE__)
+  load File.expand_path('../app/jobs/scheduled/encrypt_consistency.rb', __FILE__)

[... diff too long, it was truncated ...]

GitHub sha: eb019851

2 Likes