Ver código fonte

Do not send empty mail

Guénaël Muller 6 anos atrás
pai
commit
eae3eff43b

+ 17 - 4
tracim/tracim/lib/email_fetcher.py Ver arquivo

@@ -331,13 +331,26 @@ class MailFetcher(object):
331 331
         #  if no from address for example) and catch it here
332 332
         while mails:
333 333
             mail = mails.pop()
334
+            body =  mail.get_body(
335
+                           use_html_parsing=self.use_html_parsing,
336
+                           use_txt_parsing=self.use_txt_parsing)
337
+            from_address = mail.get_from_address()
338
+
339
+            # don't create element for 'empty' mail
340
+            if not body:
341
+                logger.warning(
342
+                    self,
343
+                    'Mail from {} has not valable content'.format(
344
+                        from_address
345
+                    ),
346
+                )
347
+                continue
348
+
334 349
             msg = {'token': self.token,
335
-                   'user_mail': mail.get_from_address(),
350
+                   'user_mail': from_address,
336 351
                    'content_id': mail.get_key(),
337 352
                    'payload': {
338
-                       'content': mail.get_body(
339
-                           use_html_parsing=self.use_html_parsing,
340
-                           use_txt_parsing=self.use_txt_parsing),
353
+                       'content': body,
341 354
                    }}
342 355
             try:
343 356
                 logger.debug(

+ 13 - 2
tracim/tracim/lib/email_processing/sanitizer.py Ver arquivo

@@ -1,3 +1,4 @@
1
+import typing
1 2
 from bs4 import BeautifulSoup, Tag
2 3
 from tracim.lib.email_processing.sanitizer_config.attrs_whitelist import \
3 4
     ATTRS_WHITELIST
@@ -31,7 +32,7 @@ class HtmlSanitizer(object):
31 32
     """
32 33
 
33 34
     @classmethod
34
-    def sanitize(cls, html_body: str) -> str:
35
+    def sanitize(cls, html_body: str) -> typing.Optional[str]:
35 36
         soup = BeautifulSoup(html_body, 'html.parser')
36 37
         for tag in soup.findAll():
37 38
             if cls._tag_to_extract(tag):
@@ -43,7 +44,17 @@ class HtmlSanitizer(object):
43 44
                         del tag.attrs[attr]
44 45
             else:
45 46
                 tag.unwrap()
46
-        return str(soup)
47
+
48
+        if cls._is_content_empty(soup):
49
+            return None
50
+        else:
51
+            return str(soup)
52
+
53
+    @classmethod
54
+    def _is_content_empty(cls, soup):
55
+        img = soup.find('img')
56
+        txt = soup.get_text().replace('\n', '').strip()
57
+        return (not img and not txt)
47 58
 
48 59
     @classmethod
49 60
     def _tag_to_extract(cls, tag: Tag) -> bool: