From e7618b19ef8367c3942d7e4570c6538261439909 Mon Sep 17 00:00:00 2001
From: Sebastian Mark <smark@posteo.net>
Date: Wed, 30 Oct 2024 11:28:42 +0100
Subject: [PATCH 1/2] feat: validate client settings and improve logging
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- add a function to check if Pomodoro settings are valid
- log a warning for invalid client configurations
- log an info message for valid client configurations
- implement Stringer interface for PomodoroConfig model

🤖
---
 internal/websocket/client_commands.go | 9 +++++++++
 pkg/models/config.go                  | 7 +++++++
 2 files changed, 16 insertions(+)

diff --git a/internal/websocket/client_commands.go b/internal/websocket/client_commands.go
index 60158f2..2fba584 100644
--- a/internal/websocket/client_commands.go
+++ b/internal/websocket/client_commands.go
@@ -9,6 +9,10 @@ import (
 	"git.smsvc.net/pomodoro/GoTomato/pkg/models"
 )
 
+func checkSettings(settings models.PomodoroConfig) bool {
+	return settings.Work > 0 && settings.ShortBreak > 0 && settings.LongBreak > 0 && settings.Sessions > 0
+}
+
 // Listens for commands from a client and handles them
 func handleClientCommands(c models.WebsocketClient) {
 	ws := c.Conn
@@ -50,6 +54,11 @@ func handleClientCommands(c models.WebsocketClient) {
 				}
 			case "updateSettings":
 				if !pomodoro.IsPomodoroOngoing() {
+					if !checkSettings(clientCommand.Settings) {
+						log.Warn("Ignoring invalid config:", "msg", clientCommand.Settings, "host", c.Conn.RemoteAddr())
+						break
+					}
+					log.Info("Client send config", "config", clientCommand.Settings, "host", c.Conn.RemoteAddr())
 					pomodoro.UpdateSettings(clientCommand.Settings)
 				}
 			}
diff --git a/pkg/models/config.go b/pkg/models/config.go
index 36b9dbb..1852165 100644
--- a/pkg/models/config.go
+++ b/pkg/models/config.go
@@ -1,5 +1,7 @@
 package models
 
+import "fmt"
+
 // Represents the configuration of a pomodoro
 type PomodoroConfig struct {
 	Work       int `json:"work"`       // Length of work sessions in seconds
@@ -8,6 +10,11 @@ type PomodoroConfig struct {
 	Sessions   int `json:"sessions"`   // Number of total sessions
 }
 
+// Stringer interface for the PomodocoConfig model
+func (c PomodoroConfig) String() string {
+	return fmt.Sprintf("{work: %d, short: %d, long: %d, sessions: %d}", c.Work, c.ShortBreak, c.LongBreak, c.Sessions)
+}
+
 // Represents the server configuration
 type ServerConfig struct {
 	ListenAddress string `json:"listenAddress"` // Server listen address

From dd9490bb3b8e80b0b4b272ac67ecce896b8cdb9b Mon Sep 17 00:00:00 2001
From: Sebastian Mark <smark@posteo.net>
Date: Wed, 30 Oct 2024 11:29:47 +0100
Subject: [PATCH 2/2] feat: bump version to v0.0.6

---
 internal/metadata/version.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/internal/metadata/version.go b/internal/metadata/version.go
index e31bfed..2ab12fa 100644
--- a/internal/metadata/version.go
+++ b/internal/metadata/version.go
@@ -2,5 +2,5 @@ package metadata
 
 import "strings"
 
-const GoTomatoVersion = "v0.0.5"                             // The GoTomato version
+const GoTomatoVersion = "v0.0.6"                             // The GoTomato version
 var ProtocolVersion = strings.Split(GoTomatoVersion, ".")[0] // The protocol version