Skip to content

Commit

Permalink
slash commands: Add /mute_topic command (via zcommand).
Browse files Browse the repository at this point in the history
  • Loading branch information
rheaparekh committed Jun 14, 2018
1 parent 1f1d085 commit ee8f751
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
7 changes: 7 additions & 0 deletions static/js/zcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function update_setting(command) {
exports.process = function (message_content) {

var content = message_content.trim();
var tokens = content.split(' ');

if (content === '/ping') {
var start_time = new Date();
Expand All @@ -77,6 +78,12 @@ exports.process = function (message_content) {
return true;
}

if (tokens[0] === '/mute_topic') {
tokens[0] = tokens[0].replace('/', '');
update_setting(tokens.join(' '));
return true;
}

if (content === '/day') {
update_setting('day');
return true;
Expand Down
30 changes: 30 additions & 0 deletions zerver/tests/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,36 @@ def test_zcommand(self) -> None:
self.assert_json_success(result)
self.assertIn('still in day mode', result.json()['msg'])

payload = dict(command="mute_topic help")
result = self.client_post("/json/zcommand", payload)
self.assert_json_success(result)
self.assertIn('Usage: /mute_topic <stream_name> <topic_name>.', result.json()['msg'])

payload = dict(command="mute_topic invalid")
result = self.client_post("/json/zcommand", payload)
self.assert_json_success(result)
self.assertIn('Usage: /mute_topic <stream_name> <topic_name>.', result.json()['msg'])

payload = dict(command="mute_topic invalid_stream topic")
result = self.client_post("/json/zcommand", payload)
self.assert_json_success(result)
self.assertIn('A valid stream name is required.', result.json()['msg'])

payload = dict(command="mute_topic Denmark invalid_topic")
result = self.client_post("/json/zcommand", payload)
self.assert_json_success(result)
self.assertIn('A valid topic name is required.', result.json()['msg'])

payload = dict(command="mute_topic Verona Verona1")
result = self.client_post("/json/zcommand", payload)
self.assert_json_success(result)
self.assertIn("The topic 'Verona1' has been muted.", result.json()['msg'])

payload = dict(command="mute_topic Verona Verona1")
result = self.client_post("/json/zcommand", payload)
self.assert_json_success(result)
self.assertIn("The topic 'Verona1' is already muted.", result.json()['msg'])

def test_message_to_self(self) -> None:
"""
Sending a message to a stream to which you are subscribed is
Expand Down
37 changes: 32 additions & 5 deletions zerver/views/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.utils.timezone import now as timezone_now
from django.conf import settings
from django.core import validators
from django.core.exceptions import ValidationError
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.db import connection
from django.http import HttpRequest, HttpResponse
from typing import Dict, List, Set, Any, Callable, Iterable, \
Expand All @@ -18,7 +18,7 @@
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, \
do_set_user_display_setting, do_mute_topic, \
get_user_info_for_message_updates, check_schedule_message
from zerver.lib.queue import queue_json_publish
from zerver.lib.message import (
Expand All @@ -32,14 +32,14 @@
from zerver.lib.streams import access_stream_by_id, can_access_stream_history_by_name
from zerver.lib.timestamp import datetime_to_timestamp, convert_to_UTC
from zerver.lib.timezone import get_timezone
from zerver.lib.topic_mutes import exclude_topic_mutes
from zerver.lib.topic_mutes import exclude_topic_mutes, topic_is_muted
from zerver.lib.utils import statsd
from zerver.lib.validator import \
check_list, check_int, check_dict, check_string, check_bool
from zerver.models import Message, UserProfile, Stream, Subscription, Client,\
Realm, RealmDomain, Recipient, UserMessage, bulk_get_recipients, get_personal_recipient, \
get_stream, email_to_domain, get_realm, get_active_streams, \
get_user_including_cross_realm, get_stream_recipient
get_stream, email_to_domain, get_realm, get_active_streams, get_realm_stream, \
get_user_including_cross_realm, get_stream_recipient, topic_exists

from sqlalchemy import func
from sqlalchemy.sql import select, join, column, literal_column, literal, and_, \
Expand Down Expand Up @@ -684,6 +684,9 @@ def find_first_unread_anchor(sa_conn: Any,
@has_request_variables
def zcommand_backend(request: HttpRequest, user_profile: UserProfile,
command: str=REQ('command')) -> HttpResponse:

tokens = command.split(' ')

if command == 'ping':
ret = dict() # type: Dict[str, Any]
return json_success(ret)
Expand All @@ -706,6 +709,30 @@ def zcommand_backend(request: HttpRequest, user_profile: UserProfile,
ret = dict(msg=msg)
return json_success(ret)

if tokens[0] == 'mute_topic':
if len(tokens) < 3 or tokens[1] == 'help':
ret = dict(msg="Usage: /mute_topic <stream_name> <topic_name>.")
return json_success(ret)

topic = ' '.join(tokens[2:])
stream_name = tokens[1]
try:
stream = get_realm_stream(stream_name=stream_name,
realm_id=user_profile.realm.id)
recipient = get_stream_recipient(stream.id)
if not topic_exists(topic, recipient):
msg = ("A valid topic name is required.")
else:
if topic_is_muted(user_profile, stream.id, topic):
msg = ("The topic '%s' is already muted." % (topic))
else:
do_mute_topic(user_profile, stream, recipient, topic)
msg = ("The topic '%s' has been muted." % (topic))
except ObjectDoesNotExist:
msg = ('A valid stream name is required.')
ret = dict(msg=msg)
return json_success(ret)

raise JsonableError(_('No such command: %s') % (command,))

@has_request_variables
Expand Down

0 comments on commit ee8f751

Please sign in to comment.