shithub: hell

Download patch

ref: 213ae0b56ce5cc1c2d618d9155fa851039e78712
parent: 6bad701d58b194a4c4f25f266a9fc64ae75e51d0
author: penny <penny@limitedideas.org>
date: Tue Aug 19 18:05:24 EDT 2025

Create an outgoing status dispatcher

--- /dev/null
+++ b/dispatch.go
@@ -1,0 +1,31 @@
+package main
+
+import (
+	"fmt"
+	
+	"github.com/mattn/go-mastodon"
+)
+
+func (hc *Hellclient) clientDispatch() {	
+	for {
+		select {
+			case statustoot := <- hc.dispatch:
+				hc.lock()
+				var err error
+				status, err := postStatusDetailed(*hc.client, *statustoot)
+				if err != nil {
+					fmt.Println(err)
+				}
+				hc.recentpost = status
+				hc.unlock()
+		}
+	}
+}
+
+func (hc *Hellclient) dispatchStatus(status string, visibility string) {
+	hc.dispatch <- postStatus(status, visibility)
+}
+
+func (hc *Hellclient) dispatchReply(posttext string, replyto mastodon.ID, postItem *mastodon.Status) {
+	hc.dispatch <- postReply(posttext, replyto, hc.currentuser.ID, postItem)
+}
\ No newline at end of file
--- a/hellclient.go
+++ b/hellclient.go
@@ -21,7 +21,9 @@
 	rl          *readline.Instance
 	client      *mastodon.Client
 	currentuser *mastodon.Account
+	dispatch    chan *mastodon.Toot
 	block       sync.Mutex
+	recentpost  *mastodon.Status
 
 	homeMap      map[string]*mastodon.Status
 	urlMap       map[string][]string
@@ -44,6 +46,8 @@
 	if err != nil {
 		return nil, err
 	}
+	
+	dispatch := make(chan *mastodon.Toot)
 
 	homeMap := make(map[string]*mastodon.Status)
 	debugMap := make(map[string]interface{})
@@ -55,6 +59,7 @@
 		urlMap:      urlMap,
 		client:      client,
 		currentuser: currentuser,
+		dispatch:    dispatch,
 	}, nil
 }
 
--- a/main.go
+++ b/main.go
@@ -14,6 +14,8 @@
 
 func main() {
 	hc, err := NewHellclient()
+	//start up post dispatcher
+	go hc.clientDispatch()
 	if err != nil {
 		fmt.Printf("Error starting account: %v\n", err)
 		return
@@ -35,7 +37,7 @@
 	postref := "a"
 	lastindex := ""
 	interupted := false //use this to check if we were interupted last turn
-	var recentpost *mastodon.Status
+	recentpost := &hc.recentpost
 
 	if err != nil {
 		return
@@ -79,10 +81,7 @@
 
 			//if we didn't get a slash command then the user is just posting
 			if command == "" && arguments != "" {
-				recentpost, err = postStatus(fmt.Sprintf(line), *client, "public")
-				if err != nil {
-					fmt.Println(err)
-				}
+				hc.dispatchStatus(fmt.Sprintf(line), "public")
 				return
 			}
 
@@ -110,12 +109,12 @@
 				hc.pause(false)
 				return
 			case "rm":
-				if index == "" && recentpost != nil {
-					err = client.DeleteStatus(context.Background(), recentpost.ID)
+				if index == "" && *recentpost != nil {
+					err = client.DeleteStatus(context.Background(), (*recentpost).ID)
 					if err != nil {
 						fmt.Println(err)
 					}
-					recentpost = nil
+					*recentpost = nil
 					return
 				}
 				if !postOK {
@@ -137,17 +136,14 @@
 			//Commands that don't take an index
 			switch command {
 			case "dm":
-				recentpost, err = postStatus(arguments, *client, "direct")
-				if err != nil {
-					fmt.Println(err)
-				}
+				hc.dispatchStatus(arguments, "direct")
 				return
-				if !postOK && !debugOK {
-					fmt.Printf("\"%v\" not a valid index\n", index)
-					return
-				}
 			}
 
+			if !postOK && !debugOK {
+				fmt.Printf("\"%v\" not a valid index\n", index)
+				return
+			}
 			//Commands that accept debug indexes
 			switch command {
 			case "examine":
@@ -234,7 +230,7 @@
 					fmt.Println(err)
 					return
 				}
-				recentpost = rtStatus
+				*recentpost = rtStatus
 				saveWorkRef(homeMap, rtStatus, postref)
 				hc.printPost("?"+postref, rtStatus)
 				postref = IncrementString(postref)
@@ -316,10 +312,7 @@
 
 			switch command {
 			case "reply":
-				recentpost, err = postReply(content, *client, postItem.ID, hc.currentuser.ID, postItem)
-				if err != nil {
-					fmt.Println(err)
-				}
+				hc.dispatchReply(content, postItem.ID, postItem)
 				return
 			}
 		}()
--- a/mastodon.go
+++ b/mastodon.go
@@ -153,7 +153,7 @@
 	*postpointer = posttext
 }
 
-func postReply(posttext string, client mastodon.Client, replyto mastodon.ID, currentuser mastodon.ID, postItem *mastodon.Status) (status *mastodon.Status, err error) {
+func postReply(posttext string, replyto mastodon.ID, currentuser mastodon.ID, postItem *mastodon.Status) (status *mastodon.Toot) {
 	toot := mastodon.Toot{
 		Status:      posttext,
 		InReplyToID: replyto,
@@ -164,8 +164,7 @@
 	processStatusHints(&toot, &posttext)
 
 	if currentuser == postItem.Account.ID {
-		status, err = postStatusDetailed(client, toot)
-		return
+		return &toot
 	}
 	var sb strings.Builder
 	sb.WriteString("@")
@@ -174,11 +173,10 @@
 	sb.WriteString(posttext)
 
 	toot.Status = sb.String()
-	status, err = postStatusDetailed(client, toot)
-	return
+	return &toot
 }
 
-func postStatus(posttext string, client mastodon.Client, visibility string) (status *mastodon.Status, err error) {
+func postStatus(posttext string, visibility string) (status *mastodon.Toot) {
 	// Post a toot
 	toot := mastodon.Toot{
 		Status:     posttext,
@@ -187,8 +185,7 @@
 
 	processStatusHints(&toot, &posttext)
 
-	status, err = postStatusDetailed(client, toot)
-	return
+	return &toot
 }
 
 func postStatusDetailed(client mastodon.Client, toot mastodon.Toot) (status *mastodon.Status, err error) {
--