www.byjp.me/tools/syndicate/services/mastodon/post.go
JP Hastings-Spital c135910850 Working pixelfed & insta posting
Syndicate is working! Instagra & Pixelfed demonstrated with the new post attached.

Lots of TODOs, but functional enough :)
2023-11-12 07:43:08 +00:00

39 lines
918 B
Go

package mastodon
import (
"context"
"fmt"
"github.com/by-jp/www.byjp.me/tools/syndicate/shared"
"github.com/by-jp/www.byjp.me/tools/syndicate/shared/images"
"github.com/by-jp/www.byjp.me/tools/syndicate/shared/text"
"github.com/mattn/go-mastodon"
)
func (s *service) Post(p shared.Post) (string, error) {
ctx := context.Background()
jpgIOs, err := images.ToJPEGs(p.Images)
if err != nil {
return "", fmt.Errorf("failed to convert images to JPEGs: %w", err)
}
var mediaIDs []mastodon.ID
for _, img := range jpgIOs {
a, err := s.masto.UploadMediaFromReader(ctx, img)
if err != nil {
return "", fmt.Errorf("couldn't upload image: %w", err)
}
mediaIDs = append(mediaIDs, a.ID)
}
post, err := s.masto.PostStatus(ctx, &mastodon.Toot{
Status: text.Caption(p),
MediaIDs: mediaIDs,
})
if err != nil {
return "", fmt.Errorf("couldn't post status: %w", err)
}
return post.URL, nil
}