Use a JSON array for recipients in send_message

(imported from commit e2184f92b708cc2e8ef3e9ae79ee4241c0aa12a1)
This commit is contained in:
Zev Benjamin 2012-11-07 11:53:58 -05:00
parent b948473a45
commit 33c23c0113
4 changed files with 39 additions and 28 deletions

View File

@ -227,11 +227,10 @@ def process_notice(notice, log):
if body.startswith("CC:"):
is_huddle = True
# Map "CC: sipbtest espuser" => "starnine@mit.edu,espuser@mit.edu"
huddle_recipients_list = [to_humbug_username(x.strip()) for x in
body.split("\n")[0][4:].split()]
if notice.sender not in huddle_recipients_list:
huddle_recipients_list.append(to_humbug_username(notice.sender))
huddle_recipients = ",".join(huddle_recipients_list)
huddle_recipients = [to_humbug_username(x.strip()) for x in
body.split("\n")[0][4:].split()]
if notice.sender not in huddle_recipients:
huddle_recipients.append(to_humbug_username(notice.sender))
body = body.split("\n", 1)[1]
if (zephyr_class == "mail" and notice.instance.lower() == "inbox"):
is_personal = True

View File

@ -310,7 +310,7 @@ def main(args):
api_key=file(api_key_file).read().strip(),
verbose=True)
client.send_message({'type': "personal",
'recipient': ", ".join(opts.reviewers),
'recipient': [opts.reviewers],
'content': "I just sent you a review request! Check your email for details."})
if os.environ.get('REVIEW_USE_SENDMAIL', ''):

View File

@ -56,6 +56,17 @@ $(function () {
send_options = {
dataType: 'json', // This seems to be ignored. We still get back an xhr.
beforeSubmit: function (arr, form, options) {
$.each(arr, function (idx, elem) {
if (elem.name === 'recipient') {
var recipients = elem.value;
// TODO: this should be collapsed with the code in composebox_typeahead.js
recipients = recipients.split(/\s*[,;]\s*/);
elem.value = JSON.stringify(recipients);
}
});
return true;
},
success: function (resp, statusText, xhr, form) {
form.find('textarea').val('');
send_status.hide();

View File

@ -450,38 +450,41 @@ def same_realm_email(user_profile, email):
except:
return False
# Parse out the sender and huddle/personal recipients
def parse_named_users(request):
sender = {}
recipients = set()
def extract_recipients(request):
raw_recipient = request.POST.get("recipient")
try:
recipients = simplejson.loads(raw_recipient)
except simplejson.decoder.JSONDecodeError:
recipients = [raw_recipient]
return list(set(recipients))
def extract_sender(request):
sender = None
try:
if 'sender' in request.POST:
sender = {'email': request.POST["sender"],
'full_name': request.POST["fullname"],
'short_name': request.POST["shortname"]}
if request.POST['type'] == 'personal':
if ',' in request.POST['recipient']:
# Huddle message
for user_email in request.POST["recipient"].split(","):
recipients.add(user_email.strip().lower())
else:
user_email = request.POST["recipient"].strip().lower()
recipients.add(user_email)
except:
return (False, None, None)
return (True, sender, list(recipients))
return None
return sender
def create_mirrored_message_users(request, user_profile):
(valid_input, sender_data, huddle_recipients) = parse_named_users(request)
if not valid_input:
sender_data = extract_sender(request)
if sender_data is None:
return (False, None)
# First, check that the sender is in our realm:
if 'email' in sender_data and not same_realm_email(user_profile,
sender_data['email']):
return (False, None)
if "recipient" not in request.POST:
return (False, None)
huddle_recipients = extract_recipients(request)
# Then, check that all huddle/personal recipients are in our realm:
for recipient in huddle_recipients:
if not same_realm_email(user_profile, recipient):
@ -571,10 +574,8 @@ def send_message_backend(request, user_profile, sender, message_type_name = POST
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)
elif message_type_name == 'personal':
if "recipient" not in request.POST:
return json_error("Missing recipient")
(valid_input, _, huddle_recipients) = parse_named_users(request)
if not valid_input:
return json_error("Unable to parse recipients")
return json_error("Missing recipients")
huddle_recipients = extract_recipients(request)
if client_name == "zephyr_mirror":
if user_profile.user.email not in huddle_recipients and not forged:
return json_error("User not authorized for this query")