From 8372abf019f2223fb355e442e820098c4f2ace59 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?= <cuu508@gmail.com>
Date: Fri, 12 Apr 2024 14:05:44 +0300
Subject: [PATCH] Fix the email integration to use Flip.new_status

---
 hc/api/tests/test_notify_email.py     | 11 +++++++----
 hc/api/transports.py                  |  7 ++++---
 templates/emails/alert-body-html.html |  2 +-
 templates/emails/alert-body-text.html |  2 +-
 templates/emails/alert-subject.html   |  2 +-
 5 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/hc/api/tests/test_notify_email.py b/hc/api/tests/test_notify_email.py
index ea22c0cf..1624a9d8 100644
--- a/hc/api/tests/test_notify_email.py
+++ b/hc/api/tests/test_notify_email.py
@@ -24,7 +24,9 @@ class NotifyEmailTestCase(BaseTestCase):
         self.check.name = "Daily Backup"
         self.check.desc = "Line 1\nLine2"
         self.check.tags = "foo bar"
-        self.check.status = "down"
+        # Transport classes should use flip.new_status,
+        # so the status "paused" should not appear anywhere
+        self.check.status = "paused"
         self.check.last_ping = now() - td(minutes=61)
         self.check.n_pings = 112233
         self.check.save()
@@ -64,6 +66,7 @@ class NotifyEmailTestCase(BaseTestCase):
         self.assertEqual(len(mail.outbox), 1)
 
         email = mail.outbox[0]
+        self.assertEqual(email.subject, "DOWN | Daily Backup")
         self.assertEqual(email.to[0], "alice@example.org")
         self.assertNotIn("X-Bounce-ID", email.extra_headers)
         self.assertTrue("List-Unsubscribe" in email.extra_headers)
@@ -71,9 +74,9 @@ class NotifyEmailTestCase(BaseTestCase):
         self.assertTrue(email.extra_headers["Message-ID"].endswith("@example.org>"))
 
         html = self.get_html(email)
-        # Name
-        self.assertIn("Daily Backup", email.body)
-        self.assertIn("Daily Backup", html)
+        # Message
+        self.assertIn("""The check "Daily Backup" has gone down.""", email.body)
+        self.assertIn(""""Daily Backup" is DOWN.""", html)
 
         # Description
         self.assertIn("Line 1\nLine2", email.body)
diff --git a/hc/api/transports.py b/hc/api/transports.py
index e6ffc895..9c8e16e7 100644
--- a/hc/api/transports.py
+++ b/hc/api/transports.py
@@ -144,7 +144,7 @@ class RemovedTransport(Transport):
 
 
 class Email(Transport):
-    def notify(self, check: Check, notification: Notification) -> None:
+    def notify_flip(self, flip: Flip, notification: Notification) -> None:
         if not self.channel.email_verified:
             raise TransportError("Email not verified")
 
@@ -166,7 +166,7 @@ class Email(Transport):
         except Profile.DoesNotExist:
             projects = None
 
-        ping = self.last_ping(check)
+        ping = self.last_ping(flip.owner)
         body = get_ping_body(ping)
         subject = None
         if ping is not None and ping.scheme == "email" and body:
@@ -175,7 +175,8 @@ class Email(Transport):
             subject = parsed.get("subject", "")
 
         ctx = {
-            "check": check,
+            "flip": flip,
+            "check": flip.owner,
             "ping": ping,
             "body": body,
             "subject": subject,
diff --git a/templates/emails/alert-body-html.html b/templates/emails/alert-body-html.html
index 0b56c9bc..060a6a62 100644
--- a/templates/emails/alert-body-html.html
+++ b/templates/emails/alert-body-html.html
@@ -2,7 +2,7 @@
 {% load hc_extras humanize %}
 
 <p>
-    "{{ check.name_then_code }}" is {{ check.status|upper }}.
+    "{{ check.name_then_code }}" is {{ flip.new_status|upper }}.
     <a href="{{ check.cloaked_url }}">View on {% site_name %}&hellip;</a>
 </p>
 
diff --git a/templates/emails/alert-body-text.html b/templates/emails/alert-body-text.html
index 21743bcb..a78851be 100644
--- a/templates/emails/alert-body-text.html
+++ b/templates/emails/alert-body-text.html
@@ -1,6 +1,6 @@
 {% load hc_extras humanize linemode %}{% linemode %}
 
-{% line %}The check "{{ check.name_then_code|safe }}" has gone {{ check.status }}.{% endline %}
+{% line %}The check "{{ check.name_then_code|safe }}" has gone {{ flip.new_status }}.{% endline %}
 {% line %}{% endline %}
 
 {% line %}View on {% site_name %}:{% endline %}
diff --git a/templates/emails/alert-subject.html b/templates/emails/alert-subject.html
index a4ea9b14..60e77f1b 100644
--- a/templates/emails/alert-subject.html
+++ b/templates/emails/alert-subject.html
@@ -1,2 +1,2 @@
-{{ check.status|upper }} | {{ check.name_then_code|safe }}
+{{ flip.new_status|upper }} | {{ check.name_then_code|safe }}