2017-01-07 21:34:32 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from django.http import HttpResponse, HttpRequest
|
|
|
|
from typing import List, Text
|
|
|
|
|
2017-03-13 22:05:35 +01:00
|
|
|
import ujson
|
|
|
|
|
|
|
|
from django.utils.translation import ugettext as _
|
2017-01-07 21:34:32 +01:00
|
|
|
from zerver.decorator import authenticated_json_post_view
|
2017-03-13 22:05:35 +01:00
|
|
|
from zerver.lib.actions import do_set_muted_topics, do_update_muted_topic
|
2017-01-07 21:34:32 +01:00
|
|
|
from zerver.lib.request import has_request_variables, REQ
|
2017-03-13 22:05:35 +01:00
|
|
|
from zerver.lib.response import json_success, json_error
|
2017-01-07 21:34:32 +01:00
|
|
|
from zerver.lib.validator import check_string, check_list
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
@has_request_variables
|
2017-01-10 01:23:53 +01:00
|
|
|
def set_muted_topics(request, user_profile,
|
|
|
|
muted_topics=REQ(validator=check_list(
|
|
|
|
check_list(check_string, length=2)), default=[])):
|
2017-01-07 21:34:32 +01:00
|
|
|
# type: (HttpRequest, UserProfile, List[List[Text]]) -> HttpResponse
|
|
|
|
do_set_muted_topics(user_profile, muted_topics)
|
|
|
|
return json_success()
|
2017-03-13 22:05:35 +01:00
|
|
|
|
|
|
|
@has_request_variables
|
|
|
|
def update_muted_topic(request, user_profile, stream=REQ(),
|
|
|
|
topic=REQ(), op=REQ()):
|
|
|
|
# type: (HttpRequest, UserProfile, str, str, str) -> HttpResponse
|
|
|
|
muted_topics = ujson.loads(user_profile.muted_topics)
|
|
|
|
if op == 'add':
|
|
|
|
if [stream, topic] in muted_topics:
|
|
|
|
return json_error(_("Topic already muted"))
|
|
|
|
muted_topics.append([stream, topic])
|
|
|
|
elif op == 'remove':
|
|
|
|
if [stream, topic] not in muted_topics:
|
|
|
|
return json_error(_("Topic is not there in the muted_topics list"))
|
|
|
|
muted_topics.remove([stream, topic])
|
|
|
|
|
|
|
|
do_update_muted_topic(user_profile, stream, topic, op)
|
|
|
|
return json_success()
|