]> source.dussan.org Git - gitea.git/commitdiff
Drop the event source if we are unauthorized (#15275) (#15280)
authorzeripath <art27@cantab.net>
Mon, 5 Apr 2021 00:39:22 +0000 (01:39 +0100)
committerGitHub <noreply@github.com>
Mon, 5 Apr 2021 00:39:22 +0000 (20:39 -0400)
Backport #15275

A previous commit that sent unauthorized if the user is unauthorized
simply leads to the repeated reopening of the eventsource. #

This PR changes the event returned to tell the client to close the
eventsource and thus prevents the repeated reopening.

Signed-off-by: Andrew Thornton <art27@cantab.net>
routers/events/events.go
web_src/js/features/eventsource.sharedworker.js
web_src/js/features/notification.js
web_src/js/features/stopwatch.js

index aa8e2c8c74df88a5962097d98f20029324e9ff4a..7542f5681ae04ea885b324c2fa50ae821393e900 100644 (file)
@@ -33,8 +33,8 @@ func Events(ctx *context.Context) {
        if !ctx.IsSigned {
                // Return unauthorized status event
                event := (&eventsource.Event{
-                       Name: "unauthorized",
-                       Data: "sorry",
+                       Name: "close",
+                       Data: "unauthorized",
                })
                _, _ = event.WriteTo(ctx)
                ctx.Resp.Flush()
index a94551e826904b40c42abf16150837a2158fe7a9..99d92c57df2d168becd694c46afd0e6489cf0f56 100644 (file)
@@ -10,6 +10,7 @@ class Source {
     this.listening = {};
     this.clients = [];
     this.listen('open');
+    this.listen('close');
     this.listen('logout');
     this.listen('notification-count');
     this.listen('stopwatches');
index a0793d228656fc4ef324e7efdb697215b2601fc7..fca1ddc5439de119a90c64c2608a5ce3755878e6 100644 (file)
@@ -74,6 +74,11 @@ export async function initNotificationCount() {
         });
         worker.port.close();
         window.location.href = AppSubUrl;
+      } else if (event.data.type === 'close') {
+        worker.port.postMessage({
+          type: 'close',
+        });
+        worker.port.close();
       }
     });
     worker.port.addEventListener('error', (e) => {
index 61f19bd7951e9e590b897208fffc83785ce19439..9352ef292d508921f4a663e7484fc94914a9d168 100644 (file)
@@ -55,6 +55,11 @@ export async function initStopwatch() {
         });
         worker.port.close();
         window.location.href = AppSubUrl;
+      } else if (event.data.type === 'close') {
+        worker.port.postMessage({
+          type: 'close',
+        });
+        worker.port.close();
       }
     });
     worker.port.addEventListener('error', (e) => {