From dd02267f9b3e314ed04948c24da172477af9d26a Mon Sep 17 00:00:00 2001 From: nimbleghost <132819643+nimbleghost@users.noreply.github.com> Date: Sat, 24 Jun 2023 20:11:10 +0200 Subject: [PATCH 1/2] Disable PWA (manifest) when web push is disabled --- server/server.go | 2 +- server/server_middleware.go | 2 +- server/server_test.go | 30 +++++++++++++++++++++++------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/server/server.go b/server/server.go index 9e26e257..5b0de3ca 100644 --- a/server/server.go +++ b/server/server.go @@ -434,7 +434,7 @@ func (s *Server) handleInternal(w http.ResponseWriter, r *http.Request, v *visit } else if r.Method == http.MethodGet && r.URL.Path == webConfigPath { return s.ensureWebEnabled(s.handleWebConfig)(w, r, v) } else if r.Method == http.MethodGet && r.URL.Path == webManifestPath { - return s.ensureWebEnabled(s.handleWebManifest)(w, r, v) + return s.ensureWebPushEnabled(s.handleWebManifest)(w, r, v) } else if r.Method == http.MethodGet && r.URL.Path == apiUsersPath { return s.ensureAdmin(s.handleUsersGet)(w, r, v) } else if r.Method == http.MethodPut && r.URL.Path == apiUsersPath { diff --git a/server/server_middleware.go b/server/server_middleware.go index b9d1bb88..b1428154 100644 --- a/server/server_middleware.go +++ b/server/server_middleware.go @@ -60,7 +60,7 @@ func (s *Server) ensureWebEnabled(next handleFunc) handleFunc { func (s *Server) ensureWebPushEnabled(next handleFunc) handleFunc { return func(w http.ResponseWriter, r *http.Request, v *visitor) error { - if s.config.WebPushPublicKey == "" { + if s.config.WebRoot == "" || s.config.WebPushPublicKey == "" { return errHTTPNotFound } return next(w, r, v) diff --git a/server/server_test.go b/server/server_test.go index d4f266ea..e9ff6fcb 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -239,9 +239,6 @@ func TestServer_WebEnabled(t *testing.T) { rr = request(t, s, "GET", "/config.js", "", nil) require.Equal(t, 404, rr.Code) - rr = request(t, s, "GET", "/manifest.webmanifest", "", nil) - require.Equal(t, 404, rr.Code) - rr = request(t, s, "GET", "/sw.js", "", nil) require.Equal(t, 404, rr.Code) @@ -261,10 +258,6 @@ func TestServer_WebEnabled(t *testing.T) { rr = request(t, s2, "GET", "/config.js", "", nil) require.Equal(t, 200, rr.Code) - rr = request(t, s2, "GET", "/manifest.webmanifest", "", nil) - require.Equal(t, 200, rr.Code) - require.Equal(t, "application/manifest+json", rr.Header().Get("Content-Type")) - rr = request(t, s2, "GET", "/sw.js", "", nil) require.Equal(t, 200, rr.Code) @@ -272,6 +265,29 @@ func TestServer_WebEnabled(t *testing.T) { require.Equal(t, 200, rr.Code) } +func TestServer_WebPushEnabled(t *testing.T) { + conf := newTestConfig(t) + conf.WebRoot = "" // Disable web app + s := newTestServer(t, conf) + + rr := request(t, s, "GET", "/manifest.webmanifest", "", nil) + require.Equal(t, 404, rr.Code) + + conf2 := newTestConfig(t) + s2 := newTestServer(t, conf2) + + rr = request(t, s2, "GET", "/manifest.webmanifest", "", nil) + require.Equal(t, 404, rr.Code) + + conf3 := newTestConfigWithWebPush(t) + s3 := newTestServer(t, conf3) + + rr = request(t, s3, "GET", "/manifest.webmanifest", "", nil) + require.Equal(t, 200, rr.Code) + require.Equal(t, "application/manifest+json", rr.Header().Get("Content-Type")) + +} + func TestServer_PublishLargeMessage(t *testing.T) { c := newTestConfig(t) c.AttachmentCacheDir = "" // Disable attachments From 48d1f7887d475dae5f4e894660f5fff9b5b3afee Mon Sep 17 00:00:00 2001 From: nimbleghost <132819643+nimbleghost@users.noreply.github.com> Date: Sat, 24 Jun 2023 20:18:02 +0200 Subject: [PATCH 2/2] Default web push to enabled on PWAs --- web/src/app/Prefs.js | 3 ++- web/src/app/utils.js | 13 +++++++++++++ web/src/components/Preferences.jsx | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/web/src/app/Prefs.js b/web/src/app/Prefs.js index f580c620..632c9adc 100644 --- a/web/src/app/Prefs.js +++ b/web/src/app/Prefs.js @@ -1,4 +1,5 @@ import db from "./db"; +import { isLaunchedPWA } from "./utils"; class Prefs { constructor(dbImpl) { @@ -34,7 +35,7 @@ class Prefs { async webPushEnabled() { const webPushEnabled = await this.db.prefs.get("webPushEnabled"); - return webPushEnabled?.value ?? false; + return webPushEnabled?.value ?? isLaunchedPWA(); } async setWebPushEnabled(enabled) { diff --git a/web/src/app/utils.js b/web/src/app/utils.js index 244d3321..afb153ff 100644 --- a/web/src/app/utils.js +++ b/web/src/app/utils.js @@ -263,3 +263,16 @@ export const urlB64ToUint8Array = (base64String) => { } return outputArray; }; + +export const isLaunchedPWA = () => { + if (window.matchMedia("(display-mode: standalone)").matches) { + return true; + } + + // iOS + if (window.navigator.standalone === true) { + return true; + } + + return false; +}; diff --git a/web/src/components/Preferences.jsx b/web/src/components/Preferences.jsx index e12fdadb..f6bd9963 100644 --- a/web/src/components/Preferences.jsx +++ b/web/src/components/Preferences.jsx @@ -36,7 +36,7 @@ import { Info } from "@mui/icons-material"; import { useOutletContext } from "react-router-dom"; import theme from "./theme"; import userManager from "../app/UserManager"; -import { playSound, shuffle, sounds, validUrl } from "../app/utils"; +import { isLaunchedPWA, playSound, shuffle, sounds, validUrl } from "../app/utils"; import session from "../app/Session"; import routes from "./routes"; import accountApi, { Permission, Role } from "../app/AccountApi"; @@ -86,7 +86,7 @@ const Notifications = () => { - {notifier.pushPossible() && } + {!isLaunchedPWA() && notifier.pushPossible() && } );