2016-05-29 16:52:55 +02:00
|
|
|
from zerver.lib.request import JsonableError
|
2016-05-25 15:02:02 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2016-12-21 13:17:53 +01:00
|
|
|
from typing import Any, Callable, Iterable, Mapping, Sequence, Text
|
2016-06-04 20:38:42 +02:00
|
|
|
|
2013-12-10 16:28:16 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def check_supported_events_narrow_filter(narrow: Iterable[Sequence[Text]]) -> None:
|
2013-12-10 16:28:16 +01:00
|
|
|
for element in narrow:
|
|
|
|
operator = element[0]
|
|
|
|
if operator not in ["stream", "topic", "sender", "is"]:
|
2016-05-25 15:02:02 +02:00
|
|
|
raise JsonableError(_("Operator %s not supported.") % (operator,))
|
2013-12-10 16:28:16 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def build_narrow_filter(narrow: Iterable[Sequence[Text]]) -> Callable[[Mapping[str, Any]], bool]:
|
2016-07-20 23:16:28 +02:00
|
|
|
"""Changes to this function should come with corresponding changes to
|
|
|
|
BuildNarrowFilterTest."""
|
2013-12-10 16:28:16 +01:00
|
|
|
check_supported_events_narrow_filter(narrow)
|
2016-11-29 07:22:02 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def narrow_filter(event: Mapping[str, Any]) -> bool:
|
2013-12-10 16:28:16 +01:00
|
|
|
message = event["message"]
|
|
|
|
flags = event["flags"]
|
|
|
|
for element in narrow:
|
|
|
|
operator = element[0]
|
|
|
|
operand = element[1]
|
|
|
|
if operator == "stream":
|
|
|
|
if message["type"] != "stream":
|
|
|
|
return False
|
|
|
|
if operand.lower() != message["display_recipient"].lower():
|
|
|
|
return False
|
|
|
|
elif operator == "topic":
|
|
|
|
if message["type"] != "stream":
|
|
|
|
return False
|
|
|
|
if operand.lower() != message["subject"].lower():
|
|
|
|
return False
|
|
|
|
elif operator == "sender":
|
|
|
|
if operand.lower() != message["sender_email"].lower():
|
|
|
|
return False
|
|
|
|
elif operator == "is" and operand == "private":
|
|
|
|
if message["type"] != "private":
|
|
|
|
return False
|
|
|
|
elif operator == "is" and operand in ["starred"]:
|
|
|
|
if operand not in flags:
|
|
|
|
return False
|
2017-06-19 03:21:48 +02:00
|
|
|
elif operator == "is" and operand == "unread":
|
|
|
|
if "read" in flags:
|
|
|
|
return False
|
2013-12-10 16:28:16 +01:00
|
|
|
elif operator == "is" and operand in ["alerted", "mentioned"]:
|
|
|
|
if "mentioned" not in flags:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
return narrow_filter
|