summaryrefslogtreecommitdiff
path: root/internal/platforms
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-10-04 10:57:03 +0300
committerPaul Buetow <paul@buetow.org>2024-10-04 10:57:03 +0300
commit618676cbead6eb6bfdc57ebbc34227306a707dfa (patch)
treee9f1b92e180ea73346e877dc2482f7ab155c792c /internal/platforms
parentc40a0fc690a94ab1e9f3253f4e85b394769b3617 (diff)
can post to mastodon
Diffstat (limited to 'internal/platforms')
-rw-r--r--internal/platforms/mastodon/mastodon.go63
1 files changed, 33 insertions, 30 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
}