From dedc7e6d5669248fedb2dee3538ba3ffa526095a Mon Sep 17 00:00:00 2001
From: Henrik Levkowetz <henrik@levkowetz.com>
Date: Sat, 16 Sep 2017 09:19:20 +0000
Subject: [PATCH] Modified the mailtrigger clean_duplicates to reduce email
 address list entries with the same address but different names to one
 instance, and use consistent unicode names for authors if known.  -
 Legacy-Id: 14122

---
 ietf/mailtrigger/models.py | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/ietf/mailtrigger/models.py b/ietf/mailtrigger/models.py
index 006db903d..3b5c7b6e9 100644
--- a/ietf/mailtrigger/models.py
+++ b/ietf/mailtrigger/models.py
@@ -5,22 +5,29 @@ from django.template import Template, Context
 
 from email.utils import parseaddr
 from ietf.utils.mail import formataddr
+from ietf.person.models import Email
 
 import debug                            # pyflakes:ignore
 
 from ietf.group.models import Role
 
 def clean_duplicates(addrlist):
-    retval = set()
+    address_info = {}
     for a in addrlist:
         (name,addr) = parseaddr(a)
+        # This collapses duplicate addresses to one, using (arbitrarily) the
+        # name from the last one:
+        address_info[addr] = (name, a)
+    addresses = []
+    for addr, info in address_info.items():
+        name, a = info
         if (name,addr)==('',''):
-            retval.add(a)
+            addresses.append(a)
         elif name:
-            retval.add(formataddr((name,addr)))
+            addresses.append(formataddr((name,addr)))
         else:
-            retval.add(addr)
-    return list(retval) 
+            addresses.append(addr)
+    return addresses
 
 class MailTrigger(models.Model):
     slug = models.CharField(max_length=32, primary_key=True)
@@ -223,8 +230,13 @@ class Recipient(models.Model):
                     if doc.stream_id and doc.stream_id not in ['ietf']:
                         addrs.extend(Recipient.objects.get(slug='stream_managers').gather(**{'streams':[doc.stream_id]}))
             else:
-                addrs.extend([formataddr((author["name"], author["email"])) for author in submission.authors if author.get("email")])
-                if submission.submitter_parsed()["email"]:
+                # This is a bit roundabout, but we do it to get consistent and unicode-compliant
+                # email names for known persons, without relying on the name parsed from the
+                # draft (which might be ascii, also for persons with non-ascii names)
+                emails = [ Email.objects.filter(address=author['email']).first() or author for author in submission.authors if author.get('email') ]
+                addrs.extend([ e.formatted_email() if isinstance(e, Email) else formataddr((e["name"], e["email"])) for e in emails ] )
+                submitter_email = submission.submitter_parsed()["email"]
+                if submitter_email and not submitter_email in [ parseaddr(a)[1] for a in addrs ]:
                     addrs.append(submission.submitter)
         return addrs