dkforest

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

commit 99de0da128d990eb2f4804d5eaaea083a3e18eae
parent 2aadb722d99dfe1135838a0f1b3d89540cfd5e07
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu, 28 Dec 2023 14:31:00 -0500

cleanup

Diffstat:
Mpkg/web/handlers/api/v1/chat.go | 26++++++++++++++++++--------
1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/pkg/web/handlers/api/v1/chat.go b/pkg/web/handlers/api/v1/chat.go @@ -84,6 +84,20 @@ func applyUserFilters(db *database.DkfDB, authUser *database.User, msg *database return true } +type Alternator struct { + state bool + fmt, class string +} + +func newAlternator(fmt, class string) *Alternator { + return &Alternator{fmt: fmt, class: class} +} + +func (a *Alternator) alternate() string { + a.state = !a.state + return fmt.Sprintf(a.fmt, a.class+utils.Ternary(a.state, "1", "2")) +} + func ChatStreamMessagesHandler(c echo.Context) error { db := c.Get("database").(*database.DkfDB) authUser := c.Get("authUser").(*database.User) @@ -219,8 +233,8 @@ func ChatStreamMessagesHandler(c echo.Context) error { // Toggle between true/false every 5sec. This bool keep track of which class to send for our "online indicator" // We need to change the css class in order for the css to never actually complete the animation and stay "green". - var indicatorSelector bool - var wizzSelector bool + indicatorAlt := newAlternator(`<style>#i{animation: %s 30s forwards}</style>`, "i") + wizzAlt := newAlternator(`<style>#msgs{animation: %s 0.25s linear 7;}</style>`, "horizontal-shaking") const metaRefresh = `<meta http-equiv="refresh" content="0" />` @@ -249,9 +263,7 @@ Loop: db.UpdateChatReadRecord(authUser.ID, room.ID) // Toggle the "http alive indicator" class to keep the dot green - send(fmt.Sprintf(`<style>#i{animation: %s 30s forwards}</style>`, - utils.Ternary(indicatorSelector, "i1", "i2"))) - indicatorSelector = !indicatorSelector + send(indicatorAlt.alternate()) topic, msgTyp, err := sub.ReceiveTimeout2(5*time.Second, quit) if err != nil { @@ -277,9 +289,7 @@ Loop: } if topic == selfWizzTopic || msgTyp.Typ == database.Wizz { - send(fmt.Sprintf(`<style>#msgs{animation: %s 0.25s linear 7;}</style>`, - utils.Ternary(wizzSelector, "horizontal-shaking1", "horizontal-shaking2"))) - wizzSelector = !wizzSelector + send(wizzAlt.alternate()) c.Response().Flush() continue }