From 9ac4bd565b2942e5f04bf660fefca5223987c946 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Tue, 13 Aug 2024 23:02:11 +0200 Subject: [PATCH] chore: hash usernames --- api/cammie.go | 12 ++++++------ screen/cammie.go | 45 +++++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/api/cammie.go b/api/cammie.go index 94b6f66..708f524 100644 --- a/api/cammie.go +++ b/api/cammie.go @@ -37,6 +37,7 @@ func cammiePostMessage(app *screen.ScreenApp, c *gin.Context) { // Get structs header := &cammieHeader{} message := &cammieMessage{} + cammieMessage := &screen.CammieMessage{} // Check Header if err := c.ShouldBindHeader(header); err != nil { @@ -57,27 +58,26 @@ func cammiePostMessage(app *screen.ScreenApp, c *gin.Context) { } // Check if sender is blocked and construct message - var newMessage string if header.Name != "" { if slices.Contains(cammieBlockedNames, header.Name) { c.JSON(http.StatusOK, gin.H{"message": "Message received"}) return } - newMessage = fmt.Sprintf("[%s[] %s", header.Name, message.Message) + cammieMessage.Sender = fmt.Sprintf("[%s[]", header.Name) } else if header.IP != "" { if slices.Contains(cammieBlockedIps, header.IP) { c.JSON(http.StatusOK, gin.H{"message": "Message received"}) return } - newMessage = fmt.Sprintf("<%s> %s", header.IP, message.Message) - } else { - newMessage = message.Message + cammieMessage.Sender = fmt.Sprintf("<%s>", header.IP) } + cammieMessage.Message = message.Message + // Increment messages cammieMessages++ - app.Cammie.Update(newMessage) + app.Cammie.Update(cammieMessage) go buzzer.PlayBuzzer() c.JSON(http.StatusOK, gin.H{"message": "Message received"}) diff --git a/screen/cammie.go b/screen/cammie.go index f7a2ece..2a1dad8 100644 --- a/screen/cammie.go +++ b/screen/cammie.go @@ -2,13 +2,27 @@ package screen import ( "fmt" - "scc/utils" + "hash/fnv" "time" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) +type CammieMessage struct { + Sender string + Message string +} + +// Component that displays messages received from the website aka cammie chat +type Cammie struct { + screenApp *ScreenApp + view *tview.TextView + + text string + buffer string +} + // Initial value, gets adjusted once it's known how much space is available var maxMessages = 20 @@ -26,16 +40,6 @@ var colors = [...]tcell.Color{ tcell.ColorYellow, tcell.ColorSalmon, } -var lastColorIndex = 0 - -// Component that displays messages received from the website aka cammie chat -type Cammie struct { - screenApp *ScreenApp - view *tview.TextView - - text string - buffer string -} // Create a new cammie struct func NewCammie(screenApp *ScreenApp) *Cammie { @@ -63,20 +67,21 @@ func (cammie *Cammie) Run() { // Updates the cammie chat // Gets called when a new message is received from the website -func (cammie *Cammie) Update(message string) { - var colorIndex int - for { - colorIndex = utils.RandRange(0, len(colors)) - if colorIndex != lastColorIndex { - break - } - } +func (cammie *Cammie) Update(message *CammieMessage) { + colorIndex := hashColor(message.Sender) color := colors[colorIndex].String() cammie.screenApp.execute(func() { - fmt.Fprintf(cammie.view, "\n[%s]%s[-:-:-:-]", color, message) + fmt.Fprintf(cammie.view, "\n[%s]%s %s[-:-:-:-]", color, message.Sender, message.Message) cammie.view.ScrollToEnd() }) } + +func hashColor(s string) int { + h := fnv.New32a() + h.Write([]byte(s)) + hashNumber := h.Sum32() + return int(hashNumber) % len(colors) +}