From 66b19013380f9dde2eb91b09a84dc7a271da3ac4 Mon Sep 17 00:00:00 2001
From: Tom Smeding <tom@tomsmeding.com>
Date: Fri, 25 Oct 2024 18:45:04 +0200
Subject: statusbot: Fix well-known fetch loop

---
 modules/statusbot/statusbot.js | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

(limited to 'modules/statusbot')

diff --git a/modules/statusbot/statusbot.js b/modules/statusbot/statusbot.js
index a518b17..eb554e6 100644
--- a/modules/statusbot/statusbot.js
+++ b/modules/statusbot/statusbot.js
@@ -158,11 +158,19 @@ function augmentConfig(config, cb) {
   // wait a bit with requesting this in case the homeserver in question is the same server as us
   pretry(500, 1.2, 10,
     cb => {
+      console.log("statusbot: Attempting to get matrix server info");
       fetch("GET", {}, home_server, "/.well-known/matrix/server", "", (status, body) => {
         if (status != 200) {
           cb(false);  // attempt failed (perhaps a later attempt will succeed)
+          return;
+        }
+        let mserver;
+        try {
+          mserver = JSON.parse(body)["m.server"];
+        } catch (e) {
+          cb(false);  // invalid JSON response, try again later
+          return;
         }
-        const mserver = JSON.parse(body)["m.server"];
         const m = mserver.match(/^([^:]*)(:443)?$/);
         if (!m) {
           throw new Error(`statusbot: Matrix server port not 443 (sorry): <${mserver}>`);
@@ -173,8 +181,10 @@ function augmentConfig(config, cb) {
       });
     },
     (success, state) => {
-      if (success) cb(state);
-      else throw new Error(`statusbot: Failed getting https://${home_server}/.well-known/matrix/server`);
+      if (success) {
+        console.log("statusbot: Successfully got matrix server info.");
+        cb(state);
+      } else throw new Error(`statusbot: Failed getting https://${home_server}/.well-known/matrix/server`);
     }
   );
 }
-- 
cgit v1.2.3-70-g09d2