From dd57596fd1ff789f439d1cf778e76ce72e8f13f3 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Wed, 3 Jan 2024 12:54:12 +1300 Subject: [PATCH] UI: Automatically refresh connected browsers if Mailpit is upgraded (version change) --- internal/storage/notifications.go | 11 +++++++---- server/server.go | 2 +- server/ui-src/components/Notifications.vue | 11 +++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/internal/storage/notifications.go b/internal/storage/notifications.go index 8b8ccbb..8c59c2f 100644 --- a/internal/storage/notifications.go +++ b/internal/storage/notifications.go @@ -3,6 +3,7 @@ package storage import ( "time" + "github.com/axllent/mailpit/config" "github.com/axllent/mailpit/server/websockets" ) @@ -23,11 +24,13 @@ func BroadcastMailboxStats() { time.Sleep(250 * time.Millisecond) bcStatsDelay = false b := struct { - Total int - Unread int + Total int + Unread int + Version string }{ - Total: CountTotal(), - Unread: CountUnread(), + Total: CountTotal(), + Unread: CountUnread(), + Version: config.Version, } websockets.Broadcast("stats", b) diff --git a/server/server.go b/server/server.go index ac954f9..a91c686 100644 --- a/server/server.go +++ b/server/server.go @@ -290,7 +290,7 @@ func index(w http.ResponseWriter, _ *http.Request) { -
+
diff --git a/server/ui-src/components/Notifications.vue b/server/ui-src/components/Notifications.vue index d10aa17..96ba644 100644 --- a/server/ui-src/components/Notifications.vue +++ b/server/ui-src/components/Notifications.vue @@ -15,10 +15,16 @@ export default { reconnectRefresh: false, socketURI: false, pauseNotifications: false, // prevent spamming + version: false } }, mounted() { + let d = document.getElementById('app') + if (d) { + this.version = d.dataset.version + } + let proto = location.protocol == 'https:' ? 'wss' : 'ws' this.socketURI = proto + "://" + document.location.host + this.resolve(`/api/events`) @@ -79,6 +85,11 @@ export default { // refresh mailbox stats mailbox.total = response.Data.Total mailbox.unread = response.Data.Unread + + // detect version updated, refresh is needed + if (self.version != response.Data.Version) { + location.reload(); + } } }