Skip to content

Commit

Permalink
web/bskybot: add InReplyTo method for posts
Browse files Browse the repository at this point in the history
Signed-off-by: Xe Iaso <[email protected]>
  • Loading branch information
Xe committed Oct 26, 2024
1 parent f1df6b0 commit 4425f34
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
23 changes: 3 additions & 20 deletions cmd/stealthmountain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"regexp"
"time"

comatproto "github.com/bluesky-social/indigo/api/atproto"
bskyData "github.com/bluesky-social/indigo/api/bsky"
jsModels "github.com/bluesky-social/jetstream/pkg/models"
"github.com/goccy/go-json"
Expand Down Expand Up @@ -94,27 +92,12 @@ func main() {

actorID := m.Header.Get("bsky-actor-did")
slog.Info("found a stealth mountain!", "id", commit.Rev, "actor", actorID)
reply, err := bsky.NewPostBuilder(`I think you mean "sneak peek"`).Build()
reply, err := bsky.NewPostBuilder(`I think you mean "sneak peek"`).
InReplyTo(post, actorID, commit.CID, commit.RKey).
Build()
if err != nil {
slog.Error("can't build reply post", "err", err)
}
parent := comatproto.RepoStrongRef{
LexiconTypeID: "app.bsky.feed.post",
Uri: fmt.Sprintf("at://%s/app.bsky.feed.post/%s", actorID, commit.RKey),
Cid: commit.CID,
}
root := parent

if post.Reply != nil {
root = *post.Reply.Root
}

reply.Reply = &bskyData.FeedPost_ReplyRef{
Parent: &parent,
Root: &root,
}

reply.CreatedAt = time.Now().UTC().Format(time.RFC3339)

cid, uri, err := bsAgent.PostToFeed(ctx, reply)
if err != nil {
Expand Down
11 changes: 8 additions & 3 deletions web/bskybot/gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"log"
"log/slog"
"net/http"
Expand Down Expand Up @@ -74,7 +74,7 @@ func (c *BskyAgent) Connect(ctx context.Context) error {
c.lock.Lock()
defer c.lock.Unlock()

if c.t == nil {
if c.t != nil {
return nil
}

Expand Down Expand Up @@ -102,6 +102,7 @@ func (c *BskyAgent) refreshAuth(ctx context.Context) error {
c.lock.Lock()
defer c.lock.Unlock()

c.client.Auth.AccessJwt = c.client.Auth.RefreshJwt
resp, err := atproto.ServerRefreshSession(ctx, c.client)
if err != nil {
return err
Expand Down Expand Up @@ -157,6 +158,10 @@ func (c *BskyAgent) PostToFeed(ctx context.Context, post appbsky.FeedPost) (stri
return response.Cid, response.Uri, nil
}

func (c *BskyAgent) Client() *xrpc.Client {
return c.client
}

func getImageAsBuffer(imageURL string) ([]byte, error) {
// Fetch image
response, err := http.Get(imageURL)
Expand All @@ -171,7 +176,7 @@ func getImageAsBuffer(imageURL string) ([]byte, error) {
}

// Read response body
imageData, err := ioutil.ReadAll(response.Body)
imageData, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
Expand Down
42 changes: 36 additions & 6 deletions web/bskybot/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/bluesky-social/indigo/api/atproto"
appbsky "github.com/bluesky-social/indigo/api/bsky"
lexutil "github.com/bluesky-social/indigo/lex/util"
"github.com/bluesky-social/indigo/util"
Expand All @@ -27,6 +28,8 @@ type PostBuilder struct {
Text string
Facet []Facet
Embed Embed
Time time.Time
Reply *appbsky.FeedPost_ReplyRef
}

type Facet struct {
Expand Down Expand Up @@ -62,7 +65,6 @@ func NewPostBuilder(text string) PostBuilder {

// Create a Richtext Post with facests
func (pb PostBuilder) WithFacet(ftype Facet_Type, value string, text string) PostBuilder {

pb.Facet = append(pb.Facet, Facet{
Ftype: ftype,
Value: value,
Expand All @@ -74,7 +76,6 @@ func (pb PostBuilder) WithFacet(ftype Facet_Type, value string, text string) Pos

// Create a Post with external links
func (pb PostBuilder) WithExternalLink(title string, link url.URL, description string) PostBuilder {

pb.Embed.Link.Title = title
pb.Embed.Link.Uri = link
pb.Embed.Link.Description = description
Expand All @@ -84,21 +85,50 @@ func (pb PostBuilder) WithExternalLink(title string, link url.URL, description s

// Create a Post with images
func (pb PostBuilder) WithImages(blobs []lexutil.LexBlob, images []Image) PostBuilder {

pb.Embed.Images = images
pb.Embed.UploadedImages = blobs

return pb
}

// Create a post in reply to another post
func (pb PostBuilder) InReplyTo(post appbsky.FeedPost, actorID, cid, rkey string) PostBuilder {
parent := atproto.RepoStrongRef{
LexiconTypeID: "app.bsky.feed.post",
Uri: fmt.Sprintf("at://%s/app.bsky.feed.post/%s", actorID, rkey),
Cid: cid,
}
root := parent

if post.Reply != nil {
root = *post.Reply.Root
}

pb.Reply = &appbsky.FeedPost_ReplyRef{
Parent: &parent,
Root: &root,
}

return pb
}

func (pb PostBuilder) AtTime(t time.Time) PostBuilder {
pb.Time = t
return pb
}

// Build the request
func (pb PostBuilder) Build() (appbsky.FeedPost, error) {

post := appbsky.FeedPost{}

post.Text = pb.Text
post.LexiconTypeID = "app.bsky.feed.post"
post.CreatedAt = time.Now().Format(util.ISO8601)
if pb.Time.IsZero() {
pb.Time = time.Now().UTC()
}
post.CreatedAt = pb.Time.UTC().Format(util.ISO8601)

post.Reply = pb.Reply

// RichtextFacet Section
// https://docs.bsky.app/docs/advanced-guides/post-richtext
Expand Down Expand Up @@ -167,7 +197,6 @@ func (pb PostBuilder) Build() (appbsky.FeedPost, error) {
// As of now it allows only one Embed type per post:
// https://github.com/bluesky-social/indigo/blob/main/api/bsky/feedpost.go
if pb.Embed.Link != (Link{}) {

FeedPost_Embed.EmbedExternal = &appbsky.EmbedExternal{
LexiconTypeID: "app.bsky.embed.external",
External: &appbsky.EmbedExternal_External{
Expand Down Expand Up @@ -217,6 +246,7 @@ func (f Facet_Type) String() string {
return "Unknown"
}
}

func findSubstring(s, substr string) (int, int, error) {
index := strings.Index(s, substr)
if index == -1 {
Expand Down

0 comments on commit 4425f34

Please sign in to comment.