dkforest

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

commit 61d5aac9a0915c7a94739485d268dba11c92eb97
parent 12201581d23b99f695bd38fe5d5d3f0bf4c44883
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Wed, 24 May 2023 18:07:14 -0700

count bytes sent and force refresh when we sent over 10Mb of text

Diffstat:
Mpkg/web/handlers/handlers.go | 44++++++++++++++++++++++++++++----------------
1 file changed, 28 insertions(+), 16 deletions(-)

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -4966,13 +4966,19 @@ func ChatStreamMessagesHandler(c echo.Context) error { c.Response().Header().Set("Transfer-Encoding", "chunked") c.Response().Header().Set("Connection", "keep-alive") + bytesSent := 0 + send := func(s string) { + n, _ := c.Response().Write([]byte(s)) + bytesSent += n + } + data := v1.ChatMessagesData{} data.DelayHideDeleteButton = int64(config.EditMessageTimeLimit.Seconds()) data.ChatMenuData.RoomName = room.Name data.ManualRefreshTimeout = 0 - _, _ = c.Response().Write([]byte(v1.GenerateStyle(authUser, data))) - _, _ = c.Response().Write([]byte(`<div id="i"></div>`)) // http alive indicator; green/red dot - _, _ = c.Response().Write([]byte(fmt.Sprintf(`<div style="display:flex;flex-direction:column-reverse;" id="msgs">`))) + send(v1.GenerateStyle(authUser, data)) + send(`<div id="i"></div>`) // http alive indicator; green/red dot + send(fmt.Sprintf(`<div style="display:flex;flex-direction:column-reverse;" id="msgs">`)) c.Response().Flush() csrf, _ := c.Get("csrf").(string) @@ -4987,7 +4993,7 @@ func ChatStreamMessagesHandler(c echo.Context) error { bools = append(bools, authUser.DisplayHellbanButton, authUser.DisplayKickButton) } data.NbButtons = utils.CountBools(bools...) - _, _ = c.Response().Write([]byte("<div>" + v1.RenderMessages(authUser, data, csrf, config.NullUsername) + "</div>")) + send("<div>" + v1.RenderMessages(authUser, data, csrf, config.NullUsername) + "</div>") c.Response().Flush() selfRefreshTopic := "refresh_" + authUser.Username @@ -5008,6 +5014,12 @@ Loop: default: } + // Refresh the page to prevent having it growing infinitely bigger + if bytesSent > 10<<20 { // 10 MB + send(`<meta http-equiv="refresh" content="0" />`) + return nil + } + authUserTmp, _ := db.GetUserByID(authUser.ID) authUser = &authUserTmp managers.ActiveUsers.UpdateUserInRoom(room, managers.NewUserInfo(*authUser, nil)) @@ -5016,8 +5028,8 @@ Loop: db.UpdateChatReadRecord(authUser.ID, room.ID) // Toggle the "http alive indicator" class to keep the dot green - _, _ = c.Response().Write([]byte(fmt.Sprintf(`<style>#i{animation: %s 10s forwards}</style>`, - utils.Ternary(indicatorSelector, "i1", "i2")))) + send(fmt.Sprintf(`<style>#i{animation: %s 10s forwards}</style>`, + utils.Ternary(indicatorSelector, "i1", "i2"))) c.Response().Flush() indicatorSelector = !indicatorSelector @@ -5030,17 +5042,17 @@ Loop: } if topic == selfRefreshTopic { - _, _ = c.Response().Write([]byte(`<meta http-equiv="refresh" content="0" />`)) + send(`<meta http-equiv="refresh" content="0" />`) return nil } if msgTyp.Typ == database.ForceRefresh { - _, _ = c.Response().Write([]byte(`<meta http-equiv="refresh" content="0" />`)) + send(`<meta http-equiv="refresh" content="0" />`) return nil } if msgTyp.Typ == database.DeleteMsg { - _, _ = c.Response().Write([]byte(fmt.Sprintf(`<style>.msgidc-%s-%d{display:none;}</style>`, msgTyp.Msg.UUID, msgTyp.Msg.Rev))) + send(fmt.Sprintf(`<style>.msgidc-%s-%d{display:none;}</style>`, msgTyp.Msg.UUID, msgTyp.Msg.Rev)) c.Response().Flush() continue } @@ -5086,11 +5098,11 @@ Loop: continue } - _, _ = c.Response().Write([]byte(renderedMsg)) + send(renderedMsg) // When editing a message, hide previous revision of the message if msgTyp.Typ == database.EditMsg { - _, _ = c.Response().Write([]byte(fmt.Sprintf(`<style>.msgidc-%s-%d{display:none;}</style>`, msg.UUID, msg.Rev-1))) + send(fmt.Sprintf(`<style>.msgidc-%s-%d{display:none;}</style>`, msg.UUID, msg.Rev-1)) } // Sound notifications @@ -5105,24 +5117,24 @@ Loop: } } if (authUser.NotifyTagged && taggedSound) || (authUser.NotifyPmmed && pmSound) { - _, _ = c.Response().Write([]byte(`<audio src="/public/mp3/sound5.mp3" autoplay></audio>`)) + send(`<audio src="/public/mp3/sound5.mp3" autoplay></audio>`) } else if authUser.NotifyNewMessage && newMessageSound { - _, _ = c.Response().Write([]byte(`<audio src="/public/mp3/sound6.mp3" autoplay></audio>`)) + send(`<audio src="/public/mp3/sound6.mp3" autoplay></audio>`) } c.Response().Flush() // Refresh the page to prevent having it growing infinitely bigger if i == 5000 { - _, _ = c.Response().Write([]byte(`<meta http-equiv="refresh" content="0" />`)) + send(`<meta http-equiv="refresh" content="0" />`) return nil } } // Display a big banner stating the connection is closed. - _, _ = c.Response().Write([]byte(fmt.Sprintf(`<div class="connection-closed">Connection closed</div>`))) + send(fmt.Sprintf(`<div class="connection-closed">Connection closed</div>`)) // Auto refresh the page after 5sec so that the client reconnect after the app has restarted - _, _ = c.Response().Write([]byte(`<meta http-equiv="refresh" content="5" />`)) + send(`<meta http-equiv="refresh" content="5" />`) c.Response().Flush() return nil }