FIX: Disable invite for users who cannot see encrypted topic.

FIX: Disable invite for users who cannot see encrypted topic.

diff --git a/assets/javascripts/discourse/initializers/hook-invite.js.es6 b/assets/javascripts/discourse/initializers/hook-invite.js.es6
index f3bcaf5..afc4289 100644
--- a/assets/javascripts/discourse/initializers/hook-invite.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-invite.js.es6
@@ -45,17 +45,6 @@ export default {
             );
             return Ember.RSVP.Promise.reject(username);
           });
-      },
-
-      createGroupInvite() {
-        // TODO: https://github.com/emberjs/ember.js/issues/15291
-        let { _super } = this;
-        if (!hasTopicKey(this.id)) {
-          return _super.call(this, ...arguments);
-        }
-
-        bootbox.alert(I18n.t("encrypt.cannot_invite_group"));
-        return Ember.RSVP.Promise.reject();
       }
     });
   }
diff --git a/assets/javascripts/lib/discourse.js.es6 b/assets/javascripts/lib/discourse.js.es6
index dfe970b..dfa39be 100644
--- a/assets/javascripts/lib/discourse.js.es6
+++ b/assets/javascripts/lib/discourse.js.es6
@@ -77,7 +77,7 @@ export function getUserIdentities(usernames) {
       userIdentities[username] = promise.then(identities =>
         identities[username]
           ? importIdentity(identities[username])
-          : Ember.RSVP.Promise.reject()
+          : Ember.RSVP.Promise.reject(username)
       );
     });
   }
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index d510c1c..61e6df7 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -18,6 +18,7 @@ en:
       encrypted_uploads: "Uploads cannot be encrypted at this time."
 
       cannot_encrypt: "You cannot encrypt messages in this conversation."
+      cannot_invite: "You cannot invite to this conversation."
       cannot_invite_group: "You cannot invite groups to encrypted messages."
 
       checkbox:
diff --git a/plugin.rb b/plugin.rb
index 697b494..cae140d 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -250,6 +250,12 @@ after_initialize do
          custom_fields &&
          custom_fields['encrypted_title'])
     end
+
+    def remove_allowed_user(removed_by, user)
+      ret = super
+      DiscourseEncrypt::Store.remove("key_#{id}_#{user.id}") if ret
+      ret
+    end
   end
 
   module PostExtensions
@@ -261,10 +267,10 @@ after_initialize do
 
   module TopicsControllerExtensions
     def update
-      if encrypted_title = params[:encrypted_title]
-        @topic ||= Topic.find_by(id: params[:topic_id])
-        guardian.ensure_can_edit!(@topic)
+      @topic ||= Topic.find_by(id: params[:topic_id])
 
+      if @topic.is_encrypted? && encrypted_title = params[:encrypted_title].presence
+        guardian.ensure_can_edit!(@topic)
         @topic.custom_fields['encrypted_title'] = params.delete(:encrypted_title)
         @topic.save_custom_fields
       end
@@ -273,26 +279,31 @@ after_initialize do
     end
 
     def invite
-      if params[:key] && params[:user]
-        @topic = Topic.find_by(id: params[:topic_id])
-        @user = User.find_by_username_or_email(params[:user])
-        guardian.ensure_can_invite_to!(@topic)
+      @topic ||= Topic.find_by(id: params[:topic_id])
 
-        DiscourseEncrypt::Store.set("key_#{@topic.id}_#{@user.id}", params[:key])
+      if @topic.is_encrypted?
+        if params[:key].present?
+          @user ||= User.find_by_username_or_email(params[:user])
+          guardian.ensure_can_invite_to!(@topic)
+          DiscourseEncrypt::Store.set("key_#{@topic.id}_#{@user.id}", params[:key])
+        else
+          return render_json_error(I18n.t("js.encrypt.cannot_invite"))
+        end
       end
 
       super
     end
 
-    def remove_allowed_user
+    def invite_group
       @topic ||= Topic.find_by(id: params[:topic_id])
-      @user ||= User.find_by(username: params[:username])
-      guardian.ensure_can_remove_allowed_users!(@topic, @user)
 
-      DiscourseEncrypt::Store.remove("key_#{@topic.id}_#{@user.id}")
+      if @topic.is_encrypted?
+        return render_json_error(I18n.t("js.encrypt.cannot_invite_group"))
+      end
 
       super
     end
+
   end
 
   reloadable_patch do |plugin|

GitHub sha: 2b9b6629