shithub: hell

Download patch

ref: 418acd5137edd628648ccbfc79a8f9e7e2e10faa
parent: a6ca677b242d01fac6c36c00f755416e98ec296c
author: penny <penny@limitedideas.org>
date: Mon Sep 22 12:14:42 EDT 2025

Add preferences to config file, track statuses received stat

--- a/config.go
+++ b/config.go
@@ -5,20 +5,51 @@
 	"fmt"
 	"io/ioutil"
 	"os"
+	"time"
 )
 
 const configFileName = "config.json"
 
+type Hellprefs struct {
+	Apidelay float32 `json:"apidelay"`
+}
+
 type account struct {
-	MASTODON_CLIENT_ID     string `json:"MASTODON_CLIENT_ID"`
-	MASTODON_CLIENT_SECRET string `json:"MASTODON_CLIENT_SECRET"`
-	MASTODON_ACCESS_TOKEN  string `json:"MASTODON_ACCESS_TOKEN"`
-	URL                    string `json:"URL"`
+	MASTODON_CLIENT_ID     string    `json:"MASTODON_CLIENT_ID"`
+	MASTODON_CLIENT_SECRET string    `json:"MASTODON_CLIENT_SECRET"`
+	MASTODON_ACCESS_TOKEN  string    `json:"MASTODON_ACCESS_TOKEN"`
+	URL                    string    `json:"URL"`
+	Preferences            Hellprefs `json:"preferences"`
 }
 
+func (prefs *Hellprefs) ApiDelayTime() time.Duration {
+	delayms := prefs.Apidelay * 1000
+	i := int64(delayms)
+	return time.Duration(i) * time.Millisecond
+}
+
+func fixPrefs(prefs *Hellprefs) (*Hellprefs, bool) {
+	var fixed bool
+	defaultprefs := initPrefs()
+	if prefs == nil {
+		return &defaultprefs, true
+	}
+	if prefs.Apidelay == 0 {
+		prefs.Apidelay = defaultprefs.Apidelay
+		fixed = true
+	}
+	return prefs, fixed
+}
+
+func initPrefs() Hellprefs {
+	prefs := &Hellprefs{
+		Apidelay: 2,
+	}
+	return *prefs
+}
+
 func loadConfig() (*account, error) {
 	config := &account{}
-
 	// Check if the config file exists
 	if _, err := os.Stat(configFileName); os.IsNotExist(err) {
 		// no config, create account
@@ -28,6 +59,8 @@
 		config.MASTODON_CLIENT_SECRET = client.Config.ClientSecret
 		config.MASTODON_ACCESS_TOKEN = client.Config.AccessToken
 
+		config.Preferences = initPrefs()
+
 		data, err := json.MarshalIndent(config, "", "  ")
 		if err != nil {
 			return nil, fmt.Errorf("error marshalling config: %w", err)
@@ -49,6 +82,21 @@
 	err = json.Unmarshal(data, config)
 	if err != nil {
 		return nil, fmt.Errorf("error unmarshalling config: %w", err)
+	}
+	prefs := &config.Preferences
+	prefs, fixed := fixPrefs(&config.Preferences)
+	config.Preferences = *prefs
+
+	if fixed {
+		data, err = json.MarshalIndent(config, "", "  ")
+		if err != nil {
+			return nil, fmt.Errorf("error marshalling config: %w", err)
+		}
+
+		err = ioutil.WriteFile(configFileName, data, 0644)
+		if err != nil {
+			return nil, fmt.Errorf("error writing config file: %w", err)
+		}
 	}
 	return config, nil
 }
--- a/dispatch.go
+++ b/dispatch.go
@@ -74,7 +74,7 @@
 		case statustoot := <-hc.dispatch:
 			receiveStatus(statustoot)
 		//API delay needs to be tracked without being reset by new inputs
-		case <-time.After(time.Duration((hc.preferences.Apidelay * time.Second)) - time.Since(lastfire)):
+		case <-time.After(hc.preferences.ApiDelayTime() - time.Since(lastfire)):
 			if 1 > len(tootQueue) && 1 > len(jobQueue) {
 				//Queues are empty, block the loop until we get a new input
 				select {
--- a/hellclient.go
+++ b/hellclient.go
@@ -50,8 +50,9 @@
 }
 
 type Stats struct {
-	slock    sync.Mutex
-	APICalls int64
+	slock            sync.Mutex
+	APICalls         int64
+	IncomingStatuses int64
 }
 
 // Use this to make private versions of runes to stop default behavior
--- a/main.go
+++ b/main.go
@@ -101,7 +101,7 @@
 			switch command {
 			case "stats":
 				hc.stats.slock.Lock()
-				fmt.Printf("API Calls: %v\n", hc.stats.APICalls)
+				fmt.Printf("API Calls: %v\nStatuses received: %v\n", hc.stats.APICalls, hc.stats.IncomingStatuses)
 				hc.stats.slock.Unlock()
 				return
 			case "notice":
--- a/mastodon.go
+++ b/mastodon.go
@@ -364,6 +364,11 @@
 				defer hc.unlock()
 				switch post := event.(type) {
 				case *mastodon.UpdateEvent:
+					//Count the statuses
+					hc.stats.slock.Lock()
+					hc.stats.IncomingStatuses++
+					hc.stats.slock.Unlock()
+					//Tell the timeline marker updater the most recent post
 					readchan <- &post.Status.ID
 					if hc.isPaused {
 						currentPostRef := hc.homeref.ref
--