aboutsummaryrefslogtreecommitdiff
path: root/web/bskybot/post.go
blob: 73111a681c3965b0e88e4ec49f4a42b9709f2aff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package bskybot

import (
	"errors"
	"fmt"
	"net/url"
	"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"
)

var FeedPost_Embed appbsky.FeedPost_Embed

type Facet_Type int

const (
	Facet_Link Facet_Type = iota + 1
	Facet_Mention
	Facet_Tag
)

// construct the post
type PostBuilder struct {
	Text  string
	Facet []Facet
	Embed Embed
	Time  time.Time
	Reply *appbsky.FeedPost_ReplyRef
}

type Facet struct {
	Ftype   Facet_Type
	Value   string
	T_facet string
}

type Embed struct {
	Link           Link
	Images         []Image
	UploadedImages []lexutil.LexBlob
}

type Link struct {
	Title       string
	Uri         url.URL
	Description string
}

type Image struct {
	Title string
	Uri   url.URL
}

// Create a simple post with text
func NewPostBuilder(text string) PostBuilder {
	return PostBuilder{
		Text:  text,
		Facet: []Facet{},
	}
}

// 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,
		T_facet: text,
	})

	return pb
}

// 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

	return pb
}

// 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"
	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

	Facets := []*appbsky.RichtextFacet{}

	for _, f := range pb.Facet {
		facet := &appbsky.RichtextFacet{}
		features := []*appbsky.RichtextFacet_Features_Elem{}
		feature := &appbsky.RichtextFacet_Features_Elem{}

		switch f.Ftype {

		case Facet_Link:
			{
				feature = &appbsky.RichtextFacet_Features_Elem{
					RichtextFacet_Link: &appbsky.RichtextFacet_Link{
						LexiconTypeID: f.Ftype.String(),
						Uri:           f.Value,
					},
				}
			}

		case Facet_Mention:
			{
				feature = &appbsky.RichtextFacet_Features_Elem{
					RichtextFacet_Mention: &appbsky.RichtextFacet_Mention{
						LexiconTypeID: f.Ftype.String(),
						Did:           f.Value,
					},
				}
			}

		case Facet_Tag:
			{
				feature = &appbsky.RichtextFacet_Features_Elem{
					RichtextFacet_Tag: &appbsky.RichtextFacet_Tag{
						LexiconTypeID: f.Ftype.String(),
						Tag:           f.Value,
					},
				}
			}

		}

		features = append(features, feature)
		facet.Features = features

		ByteStart, ByteEnd, err := findSubstring(post.Text, f.T_facet)
		if err != nil {
			return post, fmt.Errorf("unable to find the substring: %v , %v", f.T_facet, err)
		}

		index := &appbsky.RichtextFacet_ByteSlice{
			ByteStart: int64(ByteStart),
			ByteEnd:   int64(ByteEnd),
		}
		facet.Index = index

		Facets = append(Facets, facet)
	}

	post.Facets = Facets

	// Embed Section (either external links or images)
	// 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{
				Title:       pb.Embed.Link.Title,
				Uri:         pb.Embed.Link.Uri.String(),
				Description: pb.Embed.Link.Description,
			},
		}

	} else {
		if len(pb.Embed.Images) != 0 && len(pb.Embed.Images) == len(pb.Embed.UploadedImages) {

			EmbedImages := appbsky.EmbedImages{
				LexiconTypeID: "app.bsky.embed.images",
				Images:        make([]*appbsky.EmbedImages_Image, len(pb.Embed.Images)),
			}

			for i, img := range pb.Embed.Images {
				EmbedImages.Images[i] = &appbsky.EmbedImages_Image{
					Alt:   img.Title,
					Image: &pb.Embed.UploadedImages[i],
				}
			}

			FeedPost_Embed.EmbedImages = &EmbedImages

		}
	}

	// avoid error when trying to marshal empty field (*bsky.FeedPost_Embed)
	if len(pb.Embed.Images) != 0 || pb.Embed.Link.Title != "" {
		post.Embed = &FeedPost_Embed
	}

	return post, nil
}

func (f Facet_Type) String() string {
	switch f {
	case Facet_Link:
		return "app.bsky.richtext.facet#link"
	case Facet_Mention:
		return "app.bsky.richtext.facet#mention"
	case Facet_Tag:
		return "app.bsky.richtext.facet#tag"
	default:
		return "Unknown"
	}
}

func findSubstring(s, substr string) (int, int, error) {
	index := strings.Index(s, substr)
	if index == -1 {
		return 0, 0, errors.New("substring not found")
	}
	return index, index + len(substr), nil
}