diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 5e6e697eaf..a855ab9de8 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -536,13 +536,21 @@ def already_sent_mirrored_message_id(message): return messages[0].id return None -def extract_recipients(raw_recipients): +def extract_recipients(s): # We try to accept multiple incoming formats for recipients. # See test_extract_recipients() for examples of what we allow. try: - recipients = json_to_list(raw_recipients) + data = ujson.loads(s) except ValueError: - recipients = [raw_recipients] + data = s + + if isinstance(data, basestring): + data = data.split(',') + + if not isinstance(data, list): + raise ValueError("Invalid data type for recipients") + + recipients = data # Strip recipients, and then remove any duplicates and any that # are the empty string after being stripped. diff --git a/zerver/tests.py b/zerver/tests.py index 6d32ab3c2b..8e41872177 100644 --- a/zerver/tests.py +++ b/zerver/tests.py @@ -920,9 +920,20 @@ class MutedTopicsTests(AuthedTestCase): class ExtractedRecipientsTest(TestCase): def test_extract_recipients(self): + # JSON list w/dups, empties, and trailing whitespace s = ujson.dumps([' alice@zulip.com ', ' bob@zulip.com ', ' ', 'bob@zulip.com']) self.assertItemsEqual(extract_recipients(s), ['alice@zulip.com', 'bob@zulip.com']) + + # simple string with one name s = 'alice@zulip.com ' self.assertItemsEqual(extract_recipients(s), ['alice@zulip.com']) + # JSON-encoded string + s = '"alice@zulip.com"' + self.assertItemsEqual(extract_recipients(s), ['alice@zulip.com']) + + # JSON-encoded, comma-delimited string + s = '"bob@zulip.com,alice@zulip.com"' + self.assertItemsEqual(extract_recipients(s), ['alice@zulip.com', 'bob@zulip.com']) +