From 64eb2c17c19c9a7ff37b24c245c0751ce56c0ba8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felix=20Sch=C3=A4fer?= <felix@thegcat.net>
Date: Sun, 18 Jun 2023 23:46:22 +0200
Subject: [PATCH] Gracefully handle non-hostname matrix servers #16

---
 pretix_matrix_inviter/helpers.py | 62 ++++++++++++++++----------------
 1 file changed, 32 insertions(+), 30 deletions(-)

diff --git a/pretix_matrix_inviter/helpers.py b/pretix_matrix_inviter/helpers.py
index 59989ad..0cefd3c 100644
--- a/pretix_matrix_inviter/helpers.py
+++ b/pretix_matrix_inviter/helpers.py
@@ -1,5 +1,7 @@
 import requests
 from json import JSONDecodeError
+from requests.exceptions import ConnectionError
+from urllib3.exceptions import LocationParseError
 from urllib.parse import quote as url_quote
 
 
@@ -49,16 +51,16 @@ def matrix_room_id_from_alias(server, alias):
     if not server or not alias:
         return ""
 
-    r = requests.get(
-        "https://{}/_matrix/client/v3/directory/room/{}".format(
-            url_quote(server),
-            url_quote(alias),
-        ),
-    )
-
     try:
+        r = requests.get(
+            "https://{}/_matrix/client/v3/directory/room/{}".format(
+                url_quote(server),
+                url_quote(alias),
+            ),
+        )
+
         return r.json().get("room_id")
-    except JSONDecodeError:
+    except (JSONDecodeError, ConnectionError, LocationParseError):
         return ""
 
 
@@ -66,19 +68,19 @@ def matrix_room_canonical_alias_from_id(server, token, room_id):
     if not server or not token or not room_id:
         return ""
 
-    r = requests.get(
-        "https://{}/_matrix/client/v3/rooms/{}/state/m.room.canonical_alias".format(
-            url_quote(server),
-            url_quote(room_id),
-        ),
-        headers={
-            "Authorization": "Bearer {}".format(token),
-        },
-    )
-
     try:
+        r = requests.get(
+            "https://{}/_matrix/client/v3/rooms/{}/state/m.room.canonical_alias".format(
+                url_quote(server),
+                url_quote(room_id),
+            ),
+            headers={
+                "Authorization": "Bearer {}".format(token),
+            },
+        )
+
         return r.json().get("alias")
-    except JSONDecodeError:
+    except (JSONDecodeError, ConnectionError, LocationParseError):
         return ""
 
 
@@ -86,17 +88,17 @@ def matrix_room_name_from_id(server, token, room_id):
     if not server or not token or not room_id:
         return ""
 
-    r = requests.get(
-        "https://{}/_matrix/client/v3/rooms/{}/state/m.room.name".format(
-            url_quote(server),
-            url_quote(room_id),
-        ),
-        headers={
-            "Authorization": "Bearer {}".format(token),
-        },
-    )
-
     try:
+        r = requests.get(
+            "https://{}/_matrix/client/v3/rooms/{}/state/m.room.name".format(
+                url_quote(server),
+                url_quote(room_id),
+            ),
+            headers={
+                "Authorization": "Bearer {}".format(token),
+            },
+        )
+
         return r.json().get("name")
-    except JSONDecodeError:
+    except (JSONDecodeError, ConnectionError, LocationParseError):
         return ""
-- 
GitLab