summaryrefslogtreecommitdiff
path: root/service/pixelated/support
diff options
context:
space:
mode:
authorGiovane <giovaneliberato@gmail.com>2016-01-14 18:34:00 -0200
committerBruno Wagner <bwagner@riseup.net>2016-01-15 11:11:55 -0200
commitcd831d1dfc42c2a0d292fe5c1b7f497b2ca393eb (patch)
tree4069da65211b163f7718b8628349976748a6d0ee /service/pixelated/support
parent36972dc55f64100f4e056130cc1d32a266785a41 (diff)
Removes InputMail.FROM_EMAIL_ADDRESS constant #578
- Created the replier component to generate the reply dict for the email. This was needed to decouple the InputMail from the need to know who is the logged user.
Diffstat (limited to 'service/pixelated/support')
-rw-r--r--service/pixelated/support/replier.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/service/pixelated/support/replier.py b/service/pixelated/support/replier.py
new file mode 100644
index 00000000..0bcb1a27
--- /dev/null
+++ b/service/pixelated/support/replier.py
@@ -0,0 +1,32 @@
+from email.utils import parseaddr
+
+
+def generate_recipients(sender, to, ccs, current_user):
+ result = {'single': None, 'all': {'to-field': [], 'cc-field': []}}
+
+ to.append(sender)
+ to = remove_duplicates(to)
+ ccs = remove_duplicates(ccs)
+
+ result['single'] = swap_recipient_if_needed(sender, remove_address(to, current_user), current_user)
+ result['all']['to-field'] = remove_address(to, current_user) if len(to) > 1 else to
+ result['all']['cc-field'] = remove_address(ccs, current_user) if len(ccs) > 1 else ccs
+ return result
+
+
+def remove_duplicates(recipients):
+ return list(set(recipients))
+
+
+def remove_address(recipients, current_user):
+ return [recipient for recipient in recipients if not parsed_mail_matches(recipient, current_user)]
+
+
+def parsed_mail_matches(to_parse, expected):
+ return parseaddr(to_parse)[1] == expected
+
+
+def swap_recipient_if_needed(sender, recipients, current_user):
+ if len(recipients) == 1 and parsed_mail_matches(sender, current_user):
+ return recipients[0]
+ return sender