Skip to content

Commit

Permalink
Merge pull request #31 from SgtCoDFish/useragentlimit
Browse files Browse the repository at this point in the history
Add user agent limits / order entries by date
  • Loading branch information
SgtCoDFish authored Mar 20, 2024
2 parents 749ed30 + 174db9d commit 0701648
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions guestbook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func censorEmail(s string) string {
}

func allMessages(ctx context.Context, db *sql.DB, w io.Writer) ([]byte, error) {
rows, err := db.QueryContext(ctx, `SELECT email, user_agent, date, message from entries;`)
rows, err := db.QueryContext(ctx, `SELECT email, user_agent, date, message from entries ORDER BY date;`)
if err != nil {
return nil, err
}
Expand All @@ -92,6 +92,9 @@ func allMessages(ctx context.Context, db *sql.DB, w io.Writer) ([]byte, error) {
}

email = censorEmail(email)
if utf8.RuneCountInString(userAgent) > 32 {
userAgent = string([]rune(userAgent)[:30]) + "..."
}

star := "⭐"
if strings.ToLower(userAgent) == "kiosk" {
Expand Down Expand Up @@ -146,10 +149,10 @@ func writePage(db *sql.DB, ntfyTopic *string) http.Handler {

const maxMessageLen = 64

if len(message) < 2 {
if utf8.RuneCountInString(message) < 2 {
http.Error(w, "failed to add message to database: message too short / message missing", http.StatusBadRequest)
return
} else if len(message) > maxMessageLen {
} else if utf8.RuneCountInString(message) > maxMessageLen {
http.Error(w, fmt.Sprintf("failed to add message to database: message too long, maximum is %d chars", maxMessageLen), http.StatusBadRequest)
return
}
Expand Down

0 comments on commit 0701648

Please sign in to comment.