dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit bc60d935c4bc0da78249180080f4d8730a22639c
parent 86874b7f9c096a3abe9840e4d9a34f6d4ff99c47
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Fri, 16 Dec 2022 00:41:11 -0800

will never have SSE

Diffstat:
Mpkg/template/templates.go | 15---------------
Mpkg/web/web.go | 110-------------------------------------------------------------------------------
2 files changed, 0 insertions(+), 125 deletions(-)

diff --git a/pkg/template/templates.go b/pkg/template/templates.go @@ -60,7 +60,6 @@ type templateDataStruct struct { BaseKeywords string MustUpdateSoon bool CanReadExpireDate bool - SSETopics string NotifCount int64 TmplName string AuthUser *database.User @@ -87,7 +86,6 @@ func (t *Templates) Render(w io.Writer, name string, data any, c echo.Context) e d.AcceptLanguage = c.Get("accept-language").(string) d.Lang = c.Get("lang").(string) d.BaseKeywords = strings.Join(getBaseKeywords(), ", ") - d.SSETopics = "" d.Development = config.Development.Load() d.AuthUser = c.Get("authUser").(*database.User) @@ -98,7 +96,6 @@ func (t *Templates) Render(w io.Writer, name string, data any, c echo.Context) e } if d.AuthUser != nil { - d.SSETopics = strings.Join(GetBaseSSETopics(c), ",") var sessionToken string if authCookie, err := c.Cookie(hutils.AuthCookieName); err == nil { sessionToken = authCookie.Value @@ -109,18 +106,6 @@ func (t *Templates) Render(w io.Writer, name string, data any, c echo.Context) e return tmpl.ExecuteTemplate(w, "base", d) } -// GetBaseSSETopics ... -func GetBaseSSETopics(c echo.Context) []string { - authUser := c.Get("authUser").(*database.User) - sseTopics := make([]string, 0) - if test, ok := c.Get("SSETopics").([]string); ok { - sseTopics = append(sseTopics, test...) - } - sseTopics = append(sseTopics, "global_notifications") - sseTopics = append(sseTopics, "user_"+utils.FormatInt64(int64(authUser.ID))) - return sseTopics -} - // Keywords use for html meta tag, for SEO func getBaseKeywords() []string { return []string{} diff --git a/pkg/web/web.go b/pkg/web/web.go @@ -7,19 +7,14 @@ import ( "github.com/ulule/limiter/drivers/store/memory" "net" "net/http" - "os" - "os/signal" "regexp" "strconv" "strings" - "sync/atomic" "time" "dkforest/bindata" "dkforest/bindata/locals" "dkforest/pkg/config" - "dkforest/pkg/database" - pubsub2 "dkforest/pkg/pubsub" "dkforest/pkg/staticbin" tmp "dkforest/pkg/template" "dkforest/pkg/utils" @@ -90,7 +85,6 @@ func getMainServer() echo.HandlerFunc { maybeAuthGroup.GET("/u/:username/pgp", handlers.PublicUserProfilePGPHandler, middlewares.GenericRateLimitMiddleware(time.Second, 2)) maybeAuthGroup.GET("/t/:threadUUID", handlers.ThreadHandler, middlewares.GenericRateLimitMiddleware(time.Second, 2)) authGroup := e.Group("", middlewares.IsAuthMiddleware, middlewares.ForceCaptchaMiddleware) - authGroup.GET("/sse/:topics", SSEHandler) authGroup.GET("/public/css/meta.css", handlers.MetaCss) authGroup.GET("/public/img/signal/:signal/:data", handlers.SignalCss1) authGroup.GET("/captcha-required", handlers.CaptchaRequiredHandler, middlewares.AuthRateLimitMiddleware(time.Second, 1)) @@ -391,107 +385,3 @@ func getI18nBundle() *i18n.Bundle { } return bundle } - -// SSEHandler handlers for Server Sent Events. -// topics is a comma separated list of topics to subscribe to. -func SSEHandler(c echo.Context) error { - user := c.Get("authUser").(*database.User) - topics := strings.Split(c.Param("topics"), ",") - - respWriter := c.Response().Writer - flusher, ok := respWriter.(http.Flusher) - if !ok { - return c.JSON(http.StatusInternalServerError, "Streaming not supported") - } - - quit := make(chan bool) - quit1 := make(chan bool) - - // Listen to the closing of HTTP connection via CloseNotifier - notify := c.Request().Context().Done() - utils.SGo(func() { - select { - case <-notify: - case <-quit1: - } - close(quit) - }) - - notify1 := make(chan os.Signal) - signal.Notify(notify1, os.Interrupt) - utils.SGo(func() { - select { - case <-notify1: - case <-quit: - } - close(quit1) - }) - - // Set header to event streaming - respWriter.Header().Set("Content-Type", "text/event-stream") - respWriter.Header().Set("Cache-Control", "no-cache") - respWriter.Header().Set("Connection", "keep-alive") - respWriter.Header().Set("Access-Control-Allow-Origin", "*") - - authorizedChannels := make([]string, 0) - for _, channel := range topics { - if isUserAuthorized(user, channel) { - authorizedChannels = append(authorizedChannels, channel) - } - } - - var msgID int32 - atomic.AddInt32(&msgID, 1) - _, _ = fmt.Fprintf(respWriter, "id: %d\r\ndata: {\"start\": 1}\r\n\r\n", msgID) - flusher.Flush() - - pubsub := pubsub2.Subscribe(authorizedChannels) - defer pubsub.Close() - - //for _, channel := range authorizedChannels { - // if strings.HasPrefix(channel, "item_") { - // itemID := utils.DoParseInt64(strings.TrimPrefix(channel, "item_")) - // item := global.ItemsManagerInst.GetItem(itemID) - // if item == nil { - // continue - // } - // - // evt := bot.GetStateChangeEvent() - // marshalled, _ := json.Marshal(evt) - // atomic.AddInt32(&msgID, 1) - // _, _ = fmt.Fprintf(respWriter, "id: %d\r\ndata: %s\r\n\r\n", msgID, string(marshalled)) - // flusher.Flush() - // } - //} - - // Listening -Loop: - for { - select { - case <-quit: - break Loop - case <-quit1: - break Loop - default: - _, msgi, err := pubsub.ReceiveTimeout(1 * time.Second) - if err != nil { - continue - } - - atomic.AddInt32(&msgID, 1) - _, _ = fmt.Fprintf(respWriter, "id: %d\r\ndata: %s\r\n\r\n", msgID, msgi) - flusher.Flush() - } - } - - return nil -} - -// Either or not a user is authorized to access an SSE topic -func isUserAuthorized(user *database.User, topic string) bool { - var itemID int64 - if n, err := fmt.Sscanf(topic, "item_%d", itemID); n == 1 && err == nil { - return true - } - return true -}