slash commands: Refine /day and /night.

These two slash commands now use zcommand to talk to
the server, so we have no Message overhead, and if you're
on a stream, you no longer spam people by accident.

The commands now also give reasonable messages
if you are already in the mode you ask for.

It should be noted that by moving these commands out of
widget.py, they are no longer behind the ALLOW_SUB_MESSAGES
setting guard.
This commit is contained in:
Steve Howell 2018-06-02 14:23:32 +00:00 committed by Tim Abbott
parent 4b2e8b83c4
commit 47b4dd6bdb
4 changed files with 60 additions and 14 deletions

View File

@ -48,6 +48,15 @@ exports.tell_user = function (msg) {
$('#compose-error-msg').text(msg);
};
function update_setting(command) {
exports.send({
command: command,
on_success: function (data) {
exports.tell_user(data.msg);
},
});
}
exports.process = function (message_content) {
var content = message_content.trim();
@ -68,6 +77,16 @@ exports.process = function (message_content) {
return true;
}
if (content === '/day') {
update_setting('day');
return true;
}
if (content === '/night') {
update_setting('night');
return true;
}
// It is incredibly important here to return false
// if we don't see an actual zcommand, so that compose.js
// knows this is a normal message.

View File

@ -11,25 +11,12 @@ def do_widget_pre_save_actions(message: MutableMapping[str, Any]) -> None:
if not settings.ALLOW_SUB_MESSAGES:
return
# this prevents errors of cyclical imports
from zerver.lib.actions import do_set_user_display_setting
content = message['message'].content
user_profile = message['message'].sender
if content == '/stats':
message['message'].content = 'We are running **1 server**.'
return
if content == '/night':
message['message'].content = 'Changed to night mode! To revert night mode, type `/day`.'
do_set_user_display_setting(user_profile, 'night_mode', True)
return
if content == '/day':
message['message'].content = 'Changed to day mode! To revert day mode, type `/night`.'
do_set_user_display_setting(user_profile, 'night_mode', False)
return
def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None:
'''
This is experimental code that only works with the

View File

@ -1010,6 +1010,28 @@ class MessagePOSTTest(ZulipTestCase):
result = self.client_post("/json/zcommand", payload)
self.assert_json_success(result)
user = self.example_user('hamlet')
user.night_mode = False
user.save()
payload = dict(command="night")
result = self.client_post("/json/zcommand", payload)
self.assert_json_success(result)
self.assertIn('Changed to night', result.json()['msg'])
result = self.client_post("/json/zcommand", payload)
self.assert_json_success(result)
self.assertIn('still in night mode', result.json()['msg'])
payload = dict(command="day")
result = self.client_post("/json/zcommand", payload)
self.assert_json_success(result)
self.assertIn('Changed to day', result.json()['msg'])
result = self.client_post("/json/zcommand", payload)
self.assert_json_success(result)
self.assertIn('still in day mode', result.json()['msg'])
def test_message_to_self(self) -> None:
"""
Sending a message to a stream to which you are subscribed is

View File

@ -1,4 +1,3 @@
from django.utils.translation import ugettext as _
from django.utils.timezone import now as timezone_now
from django.conf import settings
@ -19,6 +18,7 @@ from zerver.lib.actions import recipient_for_emails, do_update_message_flags, \
create_mirror_user_if_needed, check_send_message, do_update_message, \
extract_recipients, truncate_body, render_incoming_message, do_delete_message, \
do_mark_all_as_read, do_mark_stream_messages_as_read, \
do_set_user_display_setting, \
get_user_info_for_message_updates, check_schedule_message
from zerver.lib.queue import queue_json_publish
from zerver.lib.message import (
@ -688,6 +688,24 @@ def zcommand_backend(request: HttpRequest, user_profile: UserProfile,
ret = dict() # type: Dict[str, Any]
return json_success(ret)
if command == 'night':
if user_profile.night_mode:
msg = 'You are still in night mode.'
else:
msg = 'Changed to night mode! To revert night mode, type `/day`.'
do_set_user_display_setting(user_profile, 'night_mode', True)
ret = dict(msg=msg)
return json_success(ret)
if command == 'day':
if user_profile.night_mode:
msg = 'Changed to day mode! To revert day mode, type `/night`.'
do_set_user_display_setting(user_profile, 'night_mode', False)
else:
msg = 'You are still in day mode.'
ret = dict(msg=msg)
return json_success(ret)
raise JsonableError(_('No such command: %s') % (command,))
@has_request_variables