From 4bc76b2d6de75193a0e21a67494cfee4a062e8c1 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Fri, 14 Feb 2014 13:39:11 -0500 Subject: [PATCH] Handle more types of data in extract_recipients. We now allow the list of recipients to be sent as a comma-delimited string with optional JSON encoding. (imported from commit e928b037bbd258348eb5b2ecca486d0bb77f593e) --- zerver/lib/actions.py | 14 +++++++++++--- zerver/tests.py | 11 +++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) 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']) +