From d066c11b479d81fc7fd2ba199dffed22b07f5685 Mon Sep 17 00:00:00 2001 From: Rhea Parekh Date: Thu, 21 Jun 2018 00:16:33 +0530 Subject: [PATCH] zcommand: Strip the slash in the backend. The slash in command is stripped in the backend, rather than in the client to make the client code cleaner. This would make client code cleaner in the slash commands which include parameters. --- static/js/zcommand.js | 6 +++--- zerver/lib/zcommand.py | 3 ++- zerver/tests/test_zcommand.py | 8 ++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/static/js/zcommand.js b/static/js/zcommand.js index 2e850f278f..28029edef9 100644 --- a/static/js/zcommand.js +++ b/static/js/zcommand.js @@ -65,7 +65,7 @@ exports.process = function (message_content) { var start_time = new Date(); exports.send({ - command: 'ping', + command: content, on_success: function () { var end_time = new Date(); var diff = end_time - start_time; @@ -78,12 +78,12 @@ exports.process = function (message_content) { } if (content === '/day') { - update_setting('day'); + update_setting(content); return true; } if (content === '/night') { - update_setting('night'); + update_setting(content); return true; } diff --git a/zerver/lib/zcommand.py b/zerver/lib/zcommand.py index 8d198ffaa4..e304bece75 100644 --- a/zerver/lib/zcommand.py +++ b/zerver/lib/zcommand.py @@ -5,7 +5,8 @@ from zerver.models import UserProfile from zerver.lib.actions import do_set_user_display_setting from zerver.lib.exceptions import JsonableError -def process_zcommands(command: str, user_profile: UserProfile) -> Dict[str, Any]: +def process_zcommands(content: str, user_profile: UserProfile) -> Dict[str, Any]: + command = content.replace('/', '') if command == 'ping': ret = dict() # type: Dict[str, Any] diff --git a/zerver/tests/test_zcommand.py b/zerver/tests/test_zcommand.py index 3007fff91c..00baa4766f 100644 --- a/zerver/tests/test_zcommand.py +++ b/zerver/tests/test_zcommand.py @@ -9,14 +9,14 @@ class ZcommandTest(ZulipTestCase): def test_invalid_zcommand(self) -> None: self.login(self.example_email("hamlet")) - payload = dict(command="boil-ocean") + payload = dict(command="/boil-ocean") result = self.client_post("/json/zcommand", payload) self.assert_json_error(result, "No such command: boil-ocean") def test_ping_zcommand(self) -> None: self.login(self.example_email("hamlet")) - payload = dict(command="ping") + payload = dict(command="/ping") result = self.client_post("/json/zcommand", payload) self.assert_json_success(result) @@ -26,7 +26,7 @@ class ZcommandTest(ZulipTestCase): user.night_mode = False user.save() - payload = dict(command="night") + payload = dict(command="/night") result = self.client_post("/json/zcommand", payload) self.assert_json_success(result) self.assertIn('Changed to night', result.json()['msg']) @@ -41,7 +41,7 @@ class ZcommandTest(ZulipTestCase): user.night_mode = True user.save() - payload = dict(command="day") + payload = dict(command="/day") result = self.client_post("/json/zcommand", payload) self.assert_json_success(result) self.assertIn('Changed to day', result.json()['msg'])