Files
zulip/zerver/views/alert_words.py
Yago González 0e135b69f9 alert words: Return the updated list when adding/removing words.
POST and DELETE operations in /users/me/alert_words may leave the
user's list of alert words in an unknown state: POSTing adds words to a
list that the client may not know from the begining, and the same with
DELETE.

Replying with the current status of the alert words list is the best way
of letting the client alter the list and knowing its contents after
being updated with a single query.

This is especially useful taking into account that POSTing words that
were already present and DELETing non-existing words both produce a
successful response.

An extra test has been added to avoid leaving GET /users/me/alert_words
too untested.
2018-07-13 01:01:00 +05:30

34 lines
1.4 KiB
Python

from django.http import HttpResponse, HttpRequest
from typing import List
from zerver.models import UserProfile
from zerver.lib.request import has_request_variables, REQ
from zerver.lib.response import json_success
from zerver.lib.validator import check_list, check_string
from zerver.lib.actions import do_add_alert_words, do_remove_alert_words
from zerver.lib.alert_words import user_alert_words
def list_alert_words(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
return json_success({'alert_words': user_alert_words(user_profile)})
def clean_alert_words(alert_words: List[str]) -> List[str]:
alert_words = [w.strip() for w in alert_words]
return [w for w in alert_words if w != ""]
@has_request_variables
def add_alert_words(request: HttpRequest, user_profile: UserProfile,
alert_words: List[str]=REQ(validator=check_list(check_string))
) -> HttpResponse:
do_add_alert_words(user_profile, clean_alert_words(alert_words))
return json_success({'alert_words': user_alert_words(user_profile)})
@has_request_variables
def remove_alert_words(request: HttpRequest, user_profile: UserProfile,
alert_words: List[str]=REQ(validator=check_list(check_string))
) -> HttpResponse:
do_remove_alert_words(user_profile, alert_words)
return json_success({'alert_words': user_alert_words(user_profile)})