-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontainer_builder.go
More file actions
249 lines (222 loc) · 6.9 KB
/
container_builder.go
File metadata and controls
249 lines (222 loc) · 6.9 KB
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
package threads
import (
"encoding/json"
"net/url"
"strings"
)
// ContainerBuilder helps build container creation parameters
type ContainerBuilder struct {
params url.Values
}
// NewContainerBuilder creates a new container builder
func NewContainerBuilder() *ContainerBuilder {
return &ContainerBuilder{
params: url.Values{},
}
}
// SetMediaType sets the media type.
// If a non-TEXT type is set, any previously set is_ghost_post flag is cleared
// since ghost posts are only supported for TEXT.
func (b *ContainerBuilder) SetMediaType(mediaType string) *ContainerBuilder {
b.params.Set("media_type", mediaType)
if mediaType != "" && mediaType != MediaTypeText {
b.params.Del("is_ghost_post")
}
return b
}
// SetText sets the text content
func (b *ContainerBuilder) SetText(text string) *ContainerBuilder {
if strings.TrimSpace(text) != "" {
b.params.Set("text", text)
}
return b
}
// SetImageURL sets the image URL for image posts
func (b *ContainerBuilder) SetImageURL(imageURL string) *ContainerBuilder {
if imageURL != "" {
b.params.Set("image_url", imageURL)
}
return b
}
// SetVideoURL sets the video URL for video posts
func (b *ContainerBuilder) SetVideoURL(videoURL string) *ContainerBuilder {
if videoURL != "" {
b.params.Set("video_url", videoURL)
}
return b
}
// SetAltText sets the alt text for media
func (b *ContainerBuilder) SetAltText(altText string) *ContainerBuilder {
if altText != "" {
b.params.Set("alt_text", altText)
}
return b
}
// SetReplyControl sets who can reply to the post
func (b *ContainerBuilder) SetReplyControl(replyControl ReplyControl) *ContainerBuilder {
if replyControl != "" {
b.params.Set("reply_control", string(replyControl))
}
return b
}
// SetReplyTo sets the ID of the post being replied to
func (b *ContainerBuilder) SetReplyTo(replyToID string) *ContainerBuilder {
if replyToID != "" {
b.params.Set("reply_to_id", replyToID)
}
return b
}
// SetTopicTag sets the topic tag
func (b *ContainerBuilder) SetTopicTag(tag string) *ContainerBuilder {
if tag != "" {
b.params.Set("topic_tag", tag)
}
return b
}
// SetLocationID sets the location ID
func (b *ContainerBuilder) SetLocationID(locationID string) *ContainerBuilder {
if locationID != "" {
b.params.Set("location_id", locationID)
}
return b
}
// SetQuotePostID sets the quoted post ID
func (b *ContainerBuilder) SetQuotePostID(quotePostID string) *ContainerBuilder {
if quotePostID != "" {
b.params.Set("quote_post_id", quotePostID)
}
return b
}
// SetLinkAttachment sets the link attachment
func (b *ContainerBuilder) SetLinkAttachment(linkURL string) *ContainerBuilder {
if linkURL != "" {
b.params.Set("link_attachment", linkURL)
}
return b
}
// SetPollAttachment sets the poll attachment
func (b *ContainerBuilder) SetPollAttachment(poll *PollAttachment) *ContainerBuilder {
if poll != nil {
pollJSON, err := json.Marshal(poll)
if err == nil {
b.params.Set("poll_attachment", string(pollJSON))
}
}
return b
}
// SetAllowlistedCountryCodes sets geo-gating country codes
func (b *ContainerBuilder) SetAllowlistedCountryCodes(codes []string) *ContainerBuilder {
for _, code := range codes {
b.params.Add("allowlisted_country_codes", code)
}
return b
}
// AddChild adds a child container ID (for carousel posts)
func (b *ContainerBuilder) AddChild(childID string) *ContainerBuilder {
if childID == "" {
return b
}
existing := b.params.Get("children")
if existing != "" {
b.params.Set("children", existing+","+childID)
} else {
b.params.Set("children", childID)
}
return b
}
// SetChildren sets all children container IDs at once (for carousel posts)
func (b *ContainerBuilder) SetChildren(childIDs []string) *ContainerBuilder {
if len(childIDs) == 0 {
b.params.Del("children")
return b
}
b.params.Set("children", strings.Join(childIDs, ","))
return b
}
// SetAutoPublishText sets whether to auto-publish text posts
func (b *ContainerBuilder) SetAutoPublishText(autoPublish bool) *ContainerBuilder {
if autoPublish {
b.params.Set("auto_publish_text", "true")
}
return b
}
// SetIsCarouselItem marks this as a carousel item
func (b *ContainerBuilder) SetIsCarouselItem(isCarouselItem bool) *ContainerBuilder {
if isCarouselItem {
b.params.Set("is_carousel_item", "true")
}
return b
}
// SetTextEntities adds text spoiler entities
// Marks specific text ranges as spoilers using offset and length
// Max 10 entities per post
func (b *ContainerBuilder) SetTextEntities(entities []TextEntity) *ContainerBuilder {
if len(entities) > 0 {
entitiesJSON, err := json.Marshal(entities)
if err == nil {
b.params.Set("text_entities", string(entitiesJSON))
}
}
return b
}
// SetIsSpoilerMedia marks media (IMAGE, VIDEO, CAROUSEL) as spoilers
// For CAROUSEL media type, this marks ALL media in the carousel as spoilers
func (b *ContainerBuilder) SetIsSpoilerMedia(isSpoilerMedia bool) *ContainerBuilder {
if isSpoilerMedia {
b.params.Set("is_spoiler_media", "true")
}
return b
}
// SetTextAttachment adds a text attachment to the post
// Can only be used with TEXT-only posts (not with polls or other media)
// Max 10,000 characters in the plaintext field
func (b *ContainerBuilder) SetTextAttachment(textAttachment *TextAttachment) *ContainerBuilder {
if textAttachment != nil {
attachmentJSON, err := json.Marshal(textAttachment)
if err == nil {
b.params.Set("text_attachment", string(attachmentJSON))
}
}
return b
}
// SetGIFAttachment adds a GIF attachment to the post
// Can only be used with TEXT-only posts (not with image, video, or carousel posts)
// Supported providers: TENOR (deprecated, sunset March 31, 2026) and GIPHY
func (b *ContainerBuilder) SetGIFAttachment(gifAttachment *GIFAttachment) *ContainerBuilder {
if gifAttachment != nil {
attachmentJSON, err := json.Marshal(gifAttachment)
if err == nil {
b.params.Set("gif_attachment", string(attachmentJSON))
}
}
return b
}
// SetIsGhostPost marks the post as a ghost post (text-only, expires in 24h, no replies allowed).
// Ghost posts are only supported for TEXT media type. If the builder's media_type is already set
// to a non-TEXT value this call is silently ignored; if a non-TEXT type is set after this call,
// SetMediaType will automatically clear the flag.
func (b *ContainerBuilder) SetIsGhostPost(isGhostPost bool) *ContainerBuilder {
if isGhostPost {
// Ghost posts are only allowed for TEXT media type
mediaType := b.params.Get("media_type")
if mediaType != "" && mediaType != MediaTypeText {
// Silently ignore - ghost post flag is not applicable to non-text posts
return b
}
b.params.Set("is_ghost_post", "true")
} else {
b.params.Del("is_ghost_post")
}
return b
}
// SetEnableReplyApprovals enables reply approvals on the post
func (b *ContainerBuilder) SetEnableReplyApprovals(enable bool) *ContainerBuilder {
if enable {
b.params.Set("enable_reply_approvals", "true")
}
return b
}
// Build returns the built parameters
func (b *ContainerBuilder) Build() url.Values {
return b.params
}