89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
package output
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/bluesky-social/indigo/api/atproto"
|
|
"github.com/bluesky-social/indigo/api/bsky"
|
|
lexutil "github.com/bluesky-social/indigo/lex/util"
|
|
"github.com/bluesky-social/indigo/util"
|
|
"github.com/bluesky-social/indigo/xrpc"
|
|
)
|
|
|
|
type blueskyoutput struct {
|
|
identifier string
|
|
password string
|
|
}
|
|
|
|
func BlueskyOutput(identifier string, password string) *blueskyoutput {
|
|
blueskyoutput := &blueskyoutput{
|
|
identifier: identifier,
|
|
password: password,
|
|
}
|
|
|
|
return blueskyoutput
|
|
}
|
|
|
|
func (bo *blueskyoutput) Write(str string, images []Image) error {
|
|
cli := &xrpc.Client{
|
|
Host: "https://bsky.social",
|
|
}
|
|
|
|
input := &atproto.ServerCreateSession_Input{
|
|
Identifier: bo.identifier,
|
|
Password: bo.password,
|
|
}
|
|
|
|
output, err := atproto.ServerCreateSession(context.TODO(), cli, input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cli.Auth = &xrpc.AuthInfo{
|
|
AccessJwt: output.AccessJwt,
|
|
RefreshJwt: output.RefreshJwt,
|
|
Handle: output.Handle,
|
|
Did: output.Did,
|
|
}
|
|
|
|
post := &bsky.FeedPost{
|
|
Text: str,
|
|
CreatedAt: time.Now().Format(util.ISO8601),
|
|
Langs: []string{"ja"},
|
|
}
|
|
|
|
if len(images) > 0 {
|
|
embedimages := make([]*bsky.EmbedImages_Image, 0, len(images))
|
|
for _, img := range images {
|
|
blob, err := atproto.RepoUploadBlob(context.TODO(), cli, bytes.NewReader(img.Data))
|
|
if err != nil {
|
|
return fmt.Errorf("uploading %s: %w", img.Filename, err)
|
|
}
|
|
embedimages = append(embedimages, &bsky.EmbedImages_Image{
|
|
Alt: img.Filename,
|
|
Image: blob.Blob,
|
|
})
|
|
}
|
|
post.Embed = &bsky.FeedPost_Embed{
|
|
EmbedImages: &bsky.EmbedImages{
|
|
Images: embedimages,
|
|
},
|
|
}
|
|
}
|
|
|
|
Recordinput := &atproto.RepoCreateRecord_Input{
|
|
Collection: "app.bsky.feed.post",
|
|
Repo: cli.Auth.Did, // "matope.bsky.social" のDID
|
|
Record: &lexutil.LexiconTypeDecoder{Val: post},
|
|
}
|
|
_, recerr := atproto.RepoCreateRecord(context.TODO(), cli, Recordinput)
|
|
|
|
return recerr
|
|
}
|
|
|
|
func (bo *blueskyoutput) GetName() string {
|
|
return "bluesky"
|
|
}
|