-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathposts_validation.go
More file actions
277 lines (226 loc) · 7.59 KB
/
posts_validation.go
File metadata and controls
277 lines (226 loc) · 7.59 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package threads
import (
"fmt"
"strings"
)
// ValidateTextPostContent validates text post content according to Threads API limits
func (c *Client) ValidateTextPostContent(content *TextPostContent) error {
validator := NewValidator()
if content == nil {
return NewValidationError(400, "Content cannot be nil", "Text post content is required", "content")
}
// Validate text length (500-character limit)
if err := validator.ValidateTextLength(content.Text, "Text"); err != nil {
return err
}
// Validate link count (including link_attachment)
if err := validator.ValidateLinkCount(content.Text, content.LinkAttachment); err != nil {
return err
}
// Validate text entities (spoilers) if present
if err := validator.ValidateTextEntities(content.TextEntities); err != nil {
return err
}
// Validate text attachment if present
if err := validator.ValidateTextAttachment(content.TextAttachment); err != nil {
return err
}
// Validate GIF attachment if present
if err := validator.ValidateGIFAttachment(content.GIFAttachment); err != nil {
return err
}
// Validate poll attachment if present
if err := validator.ValidatePollAttachment(content.PollAttachment); err != nil {
return err
}
// Text attachment can only be used with TEXT-only posts
if content.TextAttachment != nil {
// Cannot be used with polls
if content.PollAttachment != nil {
return NewValidationError(400,
"Text attachment incompatible with poll",
"Text attachments cannot be used with polls",
"text_attachment")
}
// If main post has link_attachment, text attachment cannot have link_attachment_url
if content.LinkAttachment != "" && content.TextAttachment.LinkAttachmentURL != "" {
return NewValidationError(400,
"Duplicate link attachments",
"If the main post has a link_attachment, the text attachment cannot have a link_attachment_url",
"text_attachment.link_attachment_url")
}
}
// Validate topic tag if present
if content.TopicTag != "" {
if err := validator.ValidateTopicTag(content.TopicTag); err != nil {
return err
}
}
// Validate country codes if present
if len(content.AllowlistedCountryCodes) > 0 {
if err := validator.ValidateCountryCodes(content.AllowlistedCountryCodes); err != nil {
return err
}
}
// Validate Ghost Post constraints
if content.IsGhostPost {
if content.ReplyTo != "" {
return NewValidationError(400,
"Invalid ghost post",
"Ghost posts cannot be replies",
"is_ghost_post")
}
if content.EnableReplyApprovals {
return NewValidationError(400,
"Invalid ghost post",
"Ghost posts cannot have reply approvals enabled",
"enable_reply_approvals")
}
}
return nil
}
// ValidateImagePostContent validates image post content according to Threads API limits
func (c *Client) ValidateImagePostContent(content *ImagePostContent) error {
validator := NewValidator()
if content == nil {
return NewValidationError(400, "Content cannot be nil", "Image post content is required", "content")
}
// Validate text length if present (500-character limit)
if err := validator.ValidateTextLength(content.Text, "Text"); err != nil {
return err
}
// Validate link count
if err := validator.ValidateLinkCount(content.Text, ""); err != nil {
return err
}
// Validate text entities (spoilers) if present
if err := validator.ValidateTextEntities(content.TextEntities); err != nil {
return err
}
// Validate image URL
if err := validator.ValidateMediaURL(content.ImageURL, "image"); err != nil {
return err
}
// Validate alt text if present
if err := validator.ValidateAltText(content.AltText); err != nil {
return err
}
// Validate topic tag if present
if content.TopicTag != "" {
if err := validator.ValidateTopicTag(content.TopicTag); err != nil {
return err
}
}
// Validate country codes if present
if len(content.AllowlistedCountryCodes) > 0 {
if err := validator.ValidateCountryCodes(content.AllowlistedCountryCodes); err != nil {
return err
}
}
return nil
}
// ValidateVideoPostContent validates video post content according to Threads API limits
func (c *Client) ValidateVideoPostContent(content *VideoPostContent) error {
validator := NewValidator()
if content == nil {
return NewValidationError(400, "Content cannot be nil", "Video post content is required", "content")
}
// Validate text length if present (500-character limit)
if err := validator.ValidateTextLength(content.Text, "Text"); err != nil {
return err
}
// Validate link count
if err := validator.ValidateLinkCount(content.Text, ""); err != nil {
return err
}
// Validate text entities (spoilers) if present
if err := validator.ValidateTextEntities(content.TextEntities); err != nil {
return err
}
// Validate video URL
if err := validator.ValidateMediaURL(content.VideoURL, "video"); err != nil {
return err
}
// Validate alt text if present
if err := validator.ValidateAltText(content.AltText); err != nil {
return err
}
// Validate topic tag if present
if content.TopicTag != "" {
if err := validator.ValidateTopicTag(content.TopicTag); err != nil {
return err
}
}
// Validate country codes if present
if len(content.AllowlistedCountryCodes) > 0 {
if err := validator.ValidateCountryCodes(content.AllowlistedCountryCodes); err != nil {
return err
}
}
return nil
}
// ValidateCarouselPostContent validates carousel post content according to Threads API limits
func (c *Client) ValidateCarouselPostContent(content *CarouselPostContent) error {
validator := NewValidator()
if content == nil {
return NewValidationError(400, "Content cannot be nil", "Carousel post content is required", "content")
}
// Validate text length if present (500-character limit)
if err := validator.ValidateTextLength(content.Text, "Text"); err != nil {
return err
}
// Validate link count
if err := validator.ValidateLinkCount(content.Text, ""); err != nil {
return err
}
// Validate text entities (spoilers) if present
if err := validator.ValidateTextEntities(content.TextEntities); err != nil {
return err
}
// Validate children count (2-20 limit)
if err := validator.ValidateCarouselChildren(len(content.Children)); err != nil {
return err
}
// Validate topic tag if present
if content.TopicTag != "" {
if err := validator.ValidateTopicTag(content.TopicTag); err != nil {
return err
}
}
// Validate country codes if present
if len(content.AllowlistedCountryCodes) > 0 {
if err := validator.ValidateCountryCodes(content.AllowlistedCountryCodes); err != nil {
return err
}
}
return nil
}
// ValidateCarouselChildren validates carousel children containers
func (c *Client) ValidateCarouselChildren(childrenIDs []string) error {
validator := NewValidator()
// Validate that children IDs are provided
if len(childrenIDs) == 0 {
return NewValidationError(400, "Children required", "Carousel must have at least one child container", "children")
}
// Validate the count using the existing validator method
if err := validator.ValidateCarouselChildren(len(childrenIDs)); err != nil {
return err
}
// Validate each child ID is not empty
for i, childID := range childrenIDs {
if strings.TrimSpace(childID) == "" {
return NewValidationError(400, "Invalid child ID", fmt.Sprintf("Child ID at index %d cannot be empty", i), "children")
}
}
return nil
}
// ValidateTopicTag validates a topic tag format
func (c *Client) ValidateTopicTag(tag string) error {
validator := NewValidator()
return validator.ValidateTopicTag(tag)
}
// ValidateCountryCodes validates country codes
func (c *Client) ValidateCountryCodes(codes []string) error {
validator := NewValidator()
return validator.ValidateCountryCodes(codes)
}