From 6c12b31060ddc98849a5328ad85ab6a66d79e11b Mon Sep 17 00:00:00 2001 From: Son Nguyen Kim Date: Thu, 29 Jul 2021 09:55:36 +0200 Subject: [PATCH] make sure to not notify alias in HibpNotifiedAlias --- cron.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/cron.py b/cron.py index 3529d2e2..8980f84c 100644 --- a/cron.py +++ b/cron.py @@ -56,6 +56,7 @@ from app.models import ( DeletedAlias, DomainDeletedAlias, Hibp, + HibpNotifiedAlias, ) from app.utils import sanitize_email from server import create_app @@ -271,7 +272,7 @@ def increase_percent(old, new) -> str: return "N/A" increase = (new - old) / old * 100 - return f"{increase:.1f}%. Delta: {new-old}" + return f"{increase:.1f}%. Delta: {new - old}" def bounce_report() -> List[Tuple[str, int]]: @@ -781,6 +782,7 @@ def notify_hibp(): db.session.query(Alias) .options(joinedload(Alias.hibp_breaches)) .filter(Alias.hibp_breaches.any()) + .filter(Alias.id.notin_(db.session.query(HibpNotifiedAlias.alias_id))) .distinct(Alias.user_id) .all() ) @@ -795,7 +797,11 @@ def notify_hibp(): .all() ) - LOG.d(f"Send new breaches found email to user {user}") + LOG.d( + f"Send new breaches found email to %s for %s breaches aliases", + user, + len(breached_aliases), + ) send_email( user.email, @@ -812,6 +818,11 @@ def notify_hibp(): ), ) + # add the breached aliases to HibpNotifiedAlias to avoid sending another email + for alias in breached_aliases: + HibpNotifiedAlias.create(user_id=user.id, alias_id=alias.id) + db.session.commit() + if __name__ == "__main__": LOG.d("Start running cronjob")