mirror of
https://github.com/by-jp/www.byjp.me.git
synced 2025-08-12 12:37:21 +01:00
Syndicate is working! Instagra & Pixelfed demonstrated with the new post attached. Lots of TODOs, but functional enough :)
39 lines
918 B
Go
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
|
|
}
|