DEV: Refactored API endpoints.

DEV: Refactored API endpoints.

From f47006890ca607fade8e39147f80e1fcb71fcb2f Mon Sep 17 00:00:00 2001
From: Dan Ungureanu <dan@ungureanu.me>
Date: Tue, 4 Dec 2018 18:28:44 +0200
Subject: [PATCH] DEV: Refactored API endpoints.


diff --git a/assets/javascripts/discourse/initializers/hook-composer.js.es6 b/assets/javascripts/discourse/initializers/hook-composer.js.es6
index 94c084d..bc85584 100644
--- a/assets/javascripts/discourse/initializers/hook-composer.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-composer.js.es6
@@ -98,7 +98,7 @@ export default {
               const p0 = encrypt(key, reply).then(r => this.set("reply", r));
               const p1 = encrypt(key, title).then(encTitle => {
                 this.set("title", I18n.t("encrypt.encrypted_topic_title"));
-                ajax("/encrypt/topickeys", {
+                ajax("/encrypt/topic", {
                   type: "PUT",
                   data: { topic_id: this.get("topic.id"), title: encTitle }
                 });
@@ -121,7 +121,7 @@ export default {
         // Encrypting user keys.
         const usernames = this.get("recipients");
         const p1 = p0.then(key =>
-          ajax("/encrypt/userkeys", {
+          ajax("/encrypt/user", {
             type: "GET",
             data: { usernames }
           })
@@ -173,7 +173,7 @@ export default {
             const topicId = result.responseJson.post.topic_id;
 
             putTopicKey(topicId, key);
-            ajax("/encrypt/topickeys", {
+            ajax("/encrypt/topic", {
               type: "PUT",
               data: { topic_id: topicId, title: encTitle, keys: userKeys }
             });
@@ -204,7 +204,7 @@ export default {
           return;
         }
 
-        ajax("/encrypt/userkeys", {
+        ajax("/encrypt/user", {
           type: "GET",
           data: { usernames }
         }).then(userKeys => {
diff --git a/assets/javascripts/discourse/initializers/hook-invite.js.es6 b/assets/javascripts/discourse/initializers/hook-invite.js.es6
index 51ad4c4..1094920 100644
--- a/assets/javascripts/discourse/initializers/hook-invite.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-invite.js.es6
@@ -27,7 +27,7 @@ export default {
         );
 
         // Getting user's key.
-        const userKeyPromise = ajax("/encrypt/userkeys", {
+        const userKeyPromise = ajax("/encrypt/user", {
           type: "GET",
           data: { usernames: [username] }
         })
@@ -47,7 +47,7 @@ export default {
         return Promise.all([topicKeyPromise, userKeyPromise])
           .then(([topicKey, userKey]) => exportKey(topicKey, userKey))
           .then(key =>
-            ajax("/encrypt/topickeys", {
+            ajax("/encrypt/topic", {
               type: "PUT",
               data: { topic_id: this.get("id"), keys: { [username]: key } }
             })
@@ -70,7 +70,7 @@ export default {
         // TODO: Re-encrypt and edit all posts in topic.
         // TODO: Re-encrypt and save keys for all users.
 
-        return ajax("/encrypt/topickeys", {
+        return ajax("/encrypt/topic", {
           type: "DELETE",
           data: { topic_id: topic.get("id"), users: [user.username] }
         }).then(() => _super.call(this, ...arguments));
diff --git a/plugin.rb b/plugin.rb
index 4bf3b27..fdf2568 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -43,7 +43,7 @@ after_initialize do
       # +public_key+::  Serialized public key. This parameter is optional when
       #                 the private key is updated (changed passphrase).
       # +private_key+:: Serialized private key.
-      def put
+      def keys_put
         public_key  = params.require(:public_key)
         private_key = params.require(:private_key)
         salt        = params.require(:salt)
@@ -68,7 +68,7 @@ after_initialize do
       # +usernames+::   Array of usernames.
       #
       # Returns a hash of usernames and public keys.
-      def get_userkeys
+      def user_get
         usernames = params.require(:usernames)
 
         keys = Hash[User.where(username: usernames).map { |u| [u.username, u.custom_fields['encrypt_public_key']] }]
@@ -84,7 +84,7 @@ after_initialize do
       #               topic custom field.
       # +keys+::      Hash of usernames and keys to be saved in plugin's store.
       #               This parameter is optional when editing a topic's title.
-      def put_topickeys
+      def topic_put
         topic_id = params.require(:topic_id)
 
         topic = Topic.find_by(id: topic_id)
@@ -111,7 +111,7 @@ after_initialize do
       #
       # Params:
       # +usernames+::   Array of usernames.
-      def delete_topickeys
+      def topic_delete
         topic_id = params.require(:topic_id)
         usernames = params.require(:users)
 
@@ -220,10 +220,10 @@ after_initialize do
   end
 
   DiscourseEncrypt::Engine.routes.draw do
-    put    '/encrypt/keys'      => 'encrypt#put'
-    get    '/encrypt/userkeys'  => 'encrypt#get_userkeys'
-    put    '/encrypt/topickeys' => 'encrypt#put_topickeys'
-    delete '/encrypt/topickeys' => 'encrypt#delete_topickeys'
+    put    '/encrypt/keys'  => 'encrypt#keys_put'
+    get    '/encrypt/user'  => 'encrypt#user_get'
+    put    '/encrypt/topic' => 'encrypt#topic_put'
+    delete '/encrypt/topic' => 'encrypt#topic_delete'
   end
 
   Discourse::Application.routes.append do
diff --git a/test/javascripts/acceptance/encrypt-test.js.es6 b/test/javascripts/acceptance/encrypt-test.js.es6
index 4f2f2d3..623f80b 100644
--- a/test/javascripts/acceptance/encrypt-test.js.es6
+++ b/test/javascripts/acceptance/encrypt-test.js.es6
@@ -90,7 +90,7 @@ acceptance("Encrypt", {
   },
 
   pretend(server, helper) {
-    server.get("/encrypt/userkeys", request => {
+    server.get("/encrypt/user", request => {
       const response = {};
       request.queryParams["usernames"].forEach(u => (response[u] = keys[u][2]));
       return helper.response(response);

GitHub

Not sure Iā€™m a fan of putting the HTTP verb in the name of the controller method :thinking:

2 Likes
put '/encrypt/keys'  => 'encrypt#update_keys'
get    '/encrypt/user'  => 'encrypt#show_user'
put    '/encrypt/topic' => 'encrypt#update_topic'
delete '/encrypt/topic' => 'encrypt#destroy_topic` 

Potentially you could do sub controllers here but it is may be overkill cause you would need 3

2 Likes

A param might be a sensible option. /encypt/:encrypt_type

2 Likes

@udan11 refactored the endpoints here.

2 Likes