FIX: Do not run initializers if the plugin is not enabled.

FIX: Do not run initializers if the plugin is not enabled.

To make the check more efficient the usage of promises was cut down, by caching the status in local storage.

diff --git a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
index 5f7abde..ea7326f 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
@@ -1,4 +1,4 @@
-{{#if isPluginEnabled}}
+{{#if canEnableEncrypt}}
   <div class="control-group pref-title">
     <label class="control-label">{{i18n 'encrypt.title'}}</label>
     <div class="controls">
diff --git a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6 b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
index 2afb4de..fb029b9 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
@@ -15,7 +15,10 @@ import {
   saveKeyPairToIndexedDb,
   deleteIndexedDb
 } from "discourse/plugins/discourse-encrypt/lib/keys_db";
-import { hideComponentIfDisabled } from "discourse/plugins/discourse-encrypt/lib/discourse";
+import {
+  isEncryptEnabled,
+  hideComponentIfDisabled
+} from "discourse/plugins/discourse-encrypt/lib/discourse";
 import {
   PACKED_KEY_HEADER,
   PACKED_KEY_SEPARATOR,
@@ -27,14 +30,43 @@ import {
 // lead to a lot of JavaScript bloat.
 registerHelper("or", ([a, b]) => a || b);
 
+/**
+ * Checks if a specific user can enable encryption.
+ *
+ * This check ensures that:
+ *    - user already has encryption enabled OR
+ *    - encryption plug-in is enabled AND
+ *    - there is no group restriction or user is in one of the allowed groups.
+ *
+ * @param user
+ *
+ * @return
+ */
+function canEnableEncrypt(user) {
+  if (getEncryptionStatus(user) !== ENCRYPT_DISABLED) {
+    return true;
+  }
+
+  if (Discourse.SiteSettings.encrypt_enabled) {
+    if (Discourse.SiteSettings.encrypt_groups.length === 0) {
+      return true;
+    }
+
+    const encryptGroups = Discourse.SiteSettings.encrypt_groups.split("|");
+    const groups = (user.get("groups") || []).map(group => group.get("name"));
+    if (groups.some(group => encryptGroups.includes(group))) {
+      return true;
+    }
+  }
+
+  return false;
+}
+
 export default {
   setupComponent(args, component) {
     const currentUser = Discourse.User.current();
+    const status = getEncryptionStatus(args.model);
     if (args.model.get("id") === currentUser.get("id")) {
-      const groups = (args.model.get("groups") || []).map(group =>
-        group.get("name")
-      );
-      const encryptGroups = Discourse.SiteSettings.encrypt_groups.split("|");
       component.setProperties({
         model: args.model,
         handler: hideComponentIfDisabled(component),
@@ -53,13 +85,11 @@ export default {
         /** @var Whether current user is the same as model user. */
         isCurrentUser: true,
         /** @var Whether plugin is enabled for current user. */
-        isPluginEnabled:
-          Discourse.SiteSettings.encrypt_groups.length === 0 ||
-          groups.some(group => encryptGroups.includes(group)),
+        canEnableEncrypt: canEnableEncrypt(args.model),
         /** @var Whether the encryption is enabled or not. */
-        isEncryptEnabled: false,
+        isEncryptEnabled: status !== ENCRYPT_DISABLED,
         /** @var Whether the encryption is active on this device. */
-        isEncryptActive: false,
+        isEncryptActive: status === ENCRYPT_ACTIVE,
         /** @var Whether it is an import operation. */
         importKey: false,
         /** @var Key to be imported .*/
@@ -94,6 +124,7 @@ export default {
       component.setProperties({
         model: args.model,
         isCurrentUser: false,
+        canEnableEncrypt: canEnableEncrypt(args.model),
         isEncryptEnabled: !!args.model.get("custom_fields.encrypt_public_key")
       });
     }
diff --git a/assets/javascripts/discourse/initializers/hook-composer.js.es6 b/assets/javascripts/discourse/initializers/hook-composer.js.es6
index ba7ab4e..6fc699e 100644
--- a/assets/javascripts/discourse/initializers/hook-composer.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-composer.js.es6
@@ -19,14 +19,15 @@ import {
   getTopicKey,
   hasTopicKey,
   getPrivateKey,
-  getTopicTitle
+  getTopicTitle,
+  isEncryptEnabled
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
 
 export default {
   name: "hook-composer",
   initialize(container) {
-    const siteSettings = container.lookup("site-settings:main");
-    if (!siteSettings.encrypt_enabled) {
+    const currentUser = container.lookup("current-user:main");
+    if (getEncryptionStatus(currentUser) !== ENCRYPT_ACTIVE) {
       return;
     }
 
diff --git a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6 b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
index 2724061..3ea8d92 100644
--- a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
@@ -5,7 +5,8 @@ import { cookAsync } from "discourse/lib/text";
 import {
   getTopicKey,
   hasTopicKey,
-  hasTopicTitle
+  hasTopicTitle,
+  isEncryptEnabled
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
 import { renderSpinner } from "discourse/helpers/loading-spinner";
 import showModal from "discourse/lib/show-modal";
@@ -14,8 +15,8 @@ export default {
   name: "hook-decrypt-post",
 
   initialize(container) {
-    const siteSettings = container.lookup("site-settings:main");
-    if (!siteSettings.encrypt_enabled) {
+    const currentUser = container.lookup("current-user:main");
+    if (getEncryptionStatus(currentUser) !== ENCRYPT_ACTIVE) {
       return;
     }
 
diff --git a/assets/javascripts/discourse/initializers/hook-decrypt-topic.js.es6 b/assets/javascripts/discourse/initializers/hook-decrypt-topic.js.es6
index a2b7cf1..209a20e 100644
--- a/assets/javascripts/discourse/initializers/hook-decrypt-topic.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-decrypt-topic.js.es6
@@ -1,7 +1,9 @@
+import { escapeExpression } from "discourse/lib/utilities";
 import { iconHTML } from "discourse-common/lib/icon-library";
 import {
   hasTopicTitle,
-  getTopicTitle
+  getTopicTitle,
+  isEncryptEnabled
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
 
 /**
@@ -60,8 +62,8 @@ export default {
   name: "hook-decrypt-topic",
 
   initialize(container) {
-    const siteSettings = container.lookup("site-settings:main");
-    if (!siteSettings.encrypt_enabled) {
+    const currentUser = container.lookup("current-user:main");
+    if (getEncryptionStatus(currentUser) !== ENCRYPT_ACTIVE) {
       return;
     }
 
diff --git a/assets/javascripts/discourse/initializers/hook-draft.js.es6 b/assets/javascripts/discourse/initializers/hook-draft.js.es6
index e9e619a..4a64a13 100644
--- a/assets/javascripts/discourse/initializers/hook-draft.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-draft.js.es6
@@ -1,7 +1,8 @@
 import {
   hasTopicKey,
   getTopicKey,
-  getPublicKey
+  getPublicKey,
+  isEncryptEnabled
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
 import {
   encrypt,
@@ -14,8 +15,8 @@ export default {
   name: "hook-draft",
 
   initialize(container) {
-    const siteSettings = container.lookup("site-settings:main");
-    if (!siteSettings.encrypt_enabled) {
+    const currentUser = container.lookup("current-user:main");
+    if (getEncryptionStatus(currentUser) !== ENCRYPT_ACTIVE) {
       return;
     }
 
diff --git a/assets/javascripts/discourse/initializers/hook-gather-keys.js.es6 b/assets/javascripts/discourse/initializers/hook-gather-keys.js.es6
index 83662ce..ce54b14 100644
--- a/assets/javascripts/discourse/initializers/hook-gather-keys.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-gather-keys.js.es6
@@ -2,15 +2,16 @@ import PreloadStore from "preload-store";
 import Topic from "discourse/models/topic";
 import {

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

GitHub sha: ad1d9eee