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

---
 hc/api/tests/test_notify_pushover.py         | 4 +++-
 hc/api/transports.py                         | 8 +++++---
 templates/integrations/pushover_message.html | 6 +++---
 templates/integrations/pushover_title.html   | 2 +-
 4 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/hc/api/tests/test_notify_pushover.py b/hc/api/tests/test_notify_pushover.py
index c415e2d1..40152573 100644
--- a/hc/api/tests/test_notify_pushover.py
+++ b/hc/api/tests/test_notify_pushover.py
@@ -21,7 +21,9 @@ class NotifyPushoverTestCase(BaseTestCase):
     ) -> None:
         self.check = Check(project=self.project)
         self.check.name = "Foo"
-        self.check.status = status
+        # 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.save()
 
diff --git a/hc/api/transports.py b/hc/api/transports.py
index 6bcc23d8..50755f6a 100644
--- a/hc/api/transports.py
+++ b/hc/api/transports.py
@@ -670,7 +670,7 @@ class Pushover(HttpTransport):
 
         return int(prio) == -3
 
-    def notify(self, check: Check, notification: Notification) -> None:
+    def notify_flip(self, flip: Flip, notification: Notification) -> None:
         if not settings.PUSHOVER_API_TOKEN:
             raise TransportError("Pushover notifications are not enabled.")
 
@@ -687,21 +687,23 @@ class Pushover(HttpTransport):
         if not TokenBucket.authorize_pushover(user_key):
             raise TransportError("Rate limit exceeded")
 
+        check = flip.owner
         # If down events have the emergency priority,
         # send a cancel call first
-        if check.status == "up" and down_prio == "2":
+        if flip.new_status == "up" and down_prio == "2":
             url = self.CANCEL_TMPL % check.unique_key
             cancel_payload = {"token": settings.PUSHOVER_API_TOKEN}
             self.post(url, data=cancel_payload)
 
         ctx = {
             "check": check,
+            "status": flip.new_status,
             "ping": self.last_ping(check),
             "down_checks": self.down_checks(check),
         }
         text = tmpl("pushover_message.html", **ctx)
         title = tmpl("pushover_title.html", **ctx)
-        prio = up_prio if check.status == "up" else down_prio
+        prio = up_prio if flip.new_status == "up" else down_prio
 
         payload = {
             "token": settings.PUSHOVER_API_TOKEN,
diff --git a/templates/integrations/pushover_message.html b/templates/integrations/pushover_message.html
index 1bd789d2..e5cb1cc0 100644
--- a/templates/integrations/pushover_message.html
+++ b/templates/integrations/pushover_message.html
@@ -1,5 +1,5 @@
 {% load hc_extras humanize linemode %}{% linemode %}
-{% if check.status == "down" %}
+{% if status == "down" %}
     {% line %}The check <b>{{ check.name_then_code }}</b> is <b>DOWN</b>.{% endline %}
 {% else %}
     {% line %}The check <b>{{ check.name_then_code }}</b> is now <b>UP</b>.{% endline %}
@@ -36,9 +36,9 @@
     {% line %}{% endline %}
     {% if down_checks %}
         {% if down_checks|length > 10 %}
-            {% line %}{{ down_checks|length }} other checks are {% if check.status == "down" %}also{% else %}still{% endif %} down.{% endline %}
+            {% line %}{{ down_checks|length }} other checks are {% if status == "down" %}also{% else %}still{% endif %} down.{% endline %}
         {% else %}
-            {% line %}The following checks are {% if check.status == "down" %}also{% else %}still{% endif %} down:{% endline %}
+            {% line %}The following checks are {% if status == "down" %}also{% else %}still{% endif %} down:{% endline %}
             {% for c in down_checks %}
                 {% line %}• <b>{{ c.name_then_code }}</b> (last ping: {{ c.last_ping|naturaltime|default:"never" }}){% endline %}
             {% endfor %}
diff --git a/templates/integrations/pushover_title.html b/templates/integrations/pushover_title.html
index 465dc6e9..bb8e47b9 100644
--- a/templates/integrations/pushover_title.html
+++ b/templates/integrations/pushover_title.html
@@ -1,4 +1,4 @@
-{% if check.status == "down" %}
+{% if status == "down" %}
     🔴 {{ check.name_then_code|safe }}
 {% else %}
     🟢 {{ check.name_then_code|safe }}