summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/platforms/mastodon/mastodon.go63
-rw-r--r--internal/queue/queue.go14
-rw-r--r--internal/run.go18
3 files changed, 61 insertions, 34 deletions
diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go
index e31947c..a933ea6 100644
--- a/internal/platforms/mastodon/mastodon.go
+++ b/internal/platforms/mastodon/mastodon.go
@@ -1,42 +1,45 @@
package mastodon
import (
+ "bytes"
"context"
+ "encoding/json"
+ "fmt"
+ "net/http"
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
)
func Post(ctx context.Context, args config.Args, ent entry.Entry) error {
- // payload := map[string]string{"status": status}
- // payloadBytes, err := json.Marshal(payload)
- // if err != nil {
- // return fmt.Errorf("failed to marshal payload: %w", err)
- // }
-
- // // Create the HTTP request
- // req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
- // if err != nil {
- // return fmt.Errorf("failed to create request: %w", err)
- // }
-
- // // Set headers
- // req.Header.Set("Authorization", "Bearer "+config.AccessToken)
- // req.Header.Set("Content-Type", "application/json")
-
- // // Execute the request
- // client := &http.Client{}
- // resp, err := client.Do(req)
- // if err != nil {
- // return fmt.Errorf("request failed: %w", err)
- // }
- // defer resp.Body.Close()
-
- // // Check for HTTP errors
- // if resp.StatusCode != http.StatusOK {
- // return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
- // }
-
- // fmt.Println("Message posted to Mastodon successfully")
+ content, err := ent.Content()
+ if err != nil {
+ return err
+ }
+ payload := map[string]string{"status": content}
+ payloadBytes, err := json.Marshal(payload)
+ if err != nil {
+ return fmt.Errorf("failed to marshal payload: %w", err)
+ }
+
+ req, err := http.NewRequest("POST", args.Secrets.MastodonURL, bytes.NewBuffer(payloadBytes))
+ if err != nil {
+ return fmt.Errorf("failed to create request: %w", err)
+ }
+
+ req.Header.Set("Authorization", "Bearer "+args.Secrets.MastodonAccessToken)
+ req.Header.Set("Content-Type", "application/json")
+
+ client := &http.Client{}
+ resp, err := client.Do(req)
+ if err != nil {
+ return fmt.Errorf("request failed: %w", err)
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
+ }
+
return nil
}
diff --git a/internal/queue/queue.go b/internal/queue/queue.go
index 8d6350e..98a30ac 100644
--- a/internal/queue/queue.go
+++ b/internal/queue/queue.go
@@ -14,6 +14,9 @@ import (
"codeberg.org/snonux/gos/internal/oi"
)
+// Strictly, we only operate on .txt files, but we also accept .md as Obsidian creates only .md files.
+var validExtensions = []string{".txt", ".md"}
+
func Run(args config.Args) error {
if err := queueEntries(args); err != nil {
return err
@@ -26,9 +29,6 @@ func Run(args config.Args) error {
// Queue all *.txt into ./db/*.txt.STAMP.queued
func queueEntries(args config.Args) error {
- // Strictly, we only operate on .txt files, but we also accept .md as Obsidian creates only .md files.
- var validExtensions = []string{".txt", ".md"}
-
ch, err := oi.ReadDirCh(args.GosDir, func(file os.DirEntry) (string, bool) {
filePath := filepath.Join(args.GosDir, file.Name())
return filePath, slices.Contains(validExtensions, filepath.Ext(file.Name())) &&
@@ -42,6 +42,10 @@ func queueEntries(args config.Args) error {
for filePath := range ch {
destPath := fmt.Sprintf("%s/db/%s.%s.queued", args.GosDir,
filepath.Base(filePath), now.Format(format.Time))
+ if args.DryRun {
+ log.Println("Not queueing entry", filePath, "to", destPath, "as dry-run mode enabled")
+ continue
+ }
if err := oi.Rename(filePath, destPath); err != nil {
return err
}
@@ -68,11 +72,13 @@ func queuePlatforms(args config.Args) error {
return err
}
}
+ if args.DryRun {
+ continue
+ }
log.Println("Removing", filePath)
if err := os.Remove(filePath); err != nil {
return err
}
-
}
return nil
diff --git a/internal/run.go b/internal/run.go
index 4d600f0..3f27300 100644
--- a/internal/run.go
+++ b/internal/run.go
@@ -4,8 +4,10 @@ import (
"context"
"errors"
"log"
+ "strings"
"codeberg.org/snonux/gos/internal/config"
+ "codeberg.org/snonux/gos/internal/platforms/mastodon"
"codeberg.org/snonux/gos/internal/queue"
"codeberg.org/snonux/gos/internal/schedule"
)
@@ -29,6 +31,22 @@ func Run(ctx context.Context, args config.Args) error {
}
log.Println("Scheduling", ent)
+ switch strings.ToLower(platform) {
+ case "mastodon":
+ if args.DryRun {
+ log.Println("Not posting", ent, "to", platform, "as dry-run enabled")
+ continue
+ }
+ if err := mastodon.Post(ctx, args, ent); err != nil {
+ return err
+ }
+ if err := ent.MarkPosted(); err != nil {
+ return err
+ }
+ log.Println("Posted", ent, "to", platform)
+ default:
+ log.Println("WARNING: Platform", platform, "not yet implemented")
+ }
}
return nil