Skip to content

Commit

Permalink
chore: hash usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Aug 13, 2024
1 parent c218eed commit 9ac4bd5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
12 changes: 6 additions & 6 deletions api/cammie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"})
Expand Down
45 changes: 25 additions & 20 deletions screen/cammie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}

0 comments on commit 9ac4bd5

Please sign in to comment.