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)
This commit is contained in:
Steve Howell 2014-02-14 13:39:11 -05:00
parent 6edb758136
commit 4bc76b2d6d
2 changed files with 22 additions and 3 deletions

View File

@ -536,13 +536,21 @@ def already_sent_mirrored_message_id(message):
return messages[0].id return messages[0].id
return None return None
def extract_recipients(raw_recipients): def extract_recipients(s):
# We try to accept multiple incoming formats for recipients. # We try to accept multiple incoming formats for recipients.
# See test_extract_recipients() for examples of what we allow. # See test_extract_recipients() for examples of what we allow.
try: try:
recipients = json_to_list(raw_recipients) data = ujson.loads(s)
except ValueError: 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 # Strip recipients, and then remove any duplicates and any that
# are the empty string after being stripped. # are the empty string after being stripped.

View File

@ -920,9 +920,20 @@ class MutedTopicsTests(AuthedTestCase):
class ExtractedRecipientsTest(TestCase): class ExtractedRecipientsTest(TestCase):
def test_extract_recipients(self): 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']) s = ujson.dumps([' alice@zulip.com ', ' bob@zulip.com ', ' ', 'bob@zulip.com'])
self.assertItemsEqual(extract_recipients(s), ['alice@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 ' s = 'alice@zulip.com '
self.assertItemsEqual(extract_recipients(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'])