-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathoptions.go
416 lines (341 loc) · 14.4 KB
/
options.go
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package stream
import (
"fmt"
"strings"
)
const (
defaultActivityCopyLimit = 300
)
// requestOption is an interface representing API request optional filters and
// parameters.
type requestOption interface {
valuer
}
type valuer interface {
values() (string, string)
valid() bool
}
type baseRequestOption struct {
key string
value string
}
func makeRequestOption(key string, value any) requestOption {
return baseRequestOption{
key: key,
value: fmt.Sprintf("%v", value),
}
}
func (o baseRequestOption) values() (key, value string) {
return o.key, o.value
}
func (o baseRequestOption) valid() bool {
return o.value != ""
}
func withLimit(limit int) requestOption {
return makeRequestOption("limit", limit)
}
func withOffset(offset int) requestOption {
return makeRequestOption("offset", offset)
}
// GetActivitiesOption is an option usable by GetActivities methods for flat and aggregated feeds.
type GetActivitiesOption struct {
requestOption
}
// WithActivitiesLimit adds the limit parameter to API calls which support it, limiting
// the number of results in the response to the provided limit threshold.
// Supported operations: retrieve activities, retrieve followers, retrieve
// following.
func WithActivitiesLimit(limit int) GetActivitiesOption {
return GetActivitiesOption{withLimit(limit)}
}
// WithActivitiesOffset adds the offset parameter to API calls which support it, getting
// results starting from the provided offset index.
// Supported operations: retrieve activities, retrieve followers, retrieve
// following.
func WithActivitiesOffset(offset int) GetActivitiesOption {
return GetActivitiesOption{withOffset(offset)}
}
// WithActivitiesIDGTE adds the id_gte parameter to API calls, used when retrieving
// paginated activities from feeds, returning activities with ID greater or
// equal than the provided id.
func WithActivitiesIDGTE(id string) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("id_gte", id)}
}
// WithActivitiesIDGT adds the id_gt parameter to API calls, used when retrieving
// paginated activities from feeds, returning activities with ID greater than
// the provided id.
func WithActivitiesIDGT(id string) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("id_gt", id)}
}
// WithActivitiesIDLTE adds the id_lte parameter to API calls, used when retrieving
// paginated activities from feeds, returning activities with ID lesser or equal
// than the provided id.
func WithActivitiesIDLTE(id string) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("id_lte", id)}
}
// WithActivitiesIDLT adds the id_lt parameter to API calls, used when retrieving
// paginated activities from feeds, returning activities with ID lesser than the
// provided id.
func WithActivitiesIDLT(id string) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("id_lt", id)}
}
func WithActivitiesRanking(ranking string) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("ranking", ranking)}
}
func WithRankingScoreVars() GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("withScoreVars", true)}
}
// externalVarJSON should be valid json
func WithExternalRankingVars(externalVarJSON string) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("ranking_vars", externalVarJSON)}
}
// WithNotificationsMarkSeen marks as seen the given activity ids in a notification
// feed. If the all parameter is true, every activity in the feed is marked as seen.
func WithNotificationsMarkSeen(all bool, activityIDs ...string) GetActivitiesOption {
if all {
return GetActivitiesOption{makeRequestOption("mark_seen", true)}
}
return GetActivitiesOption{makeRequestOption("mark_seen", strings.Join(activityIDs, ","))}
}
// WithNotificationsMarkRead marks as read the given activity ids in a notification
// feed. If the all parameter is true, every activity in the feed is marked as read.
func WithNotificationsMarkRead(all bool, activityIDs ...string) GetActivitiesOption {
if all {
return GetActivitiesOption{makeRequestOption("mark_read", true)}
}
return GetActivitiesOption{makeRequestOption("mark_read", strings.Join(activityIDs, ","))}
}
// WithCustomParam adds a custom parameter to the read request.
func WithCustomParam(name, value string) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption(name, value)}
}
// WithEnrichOwnReactions enriches the activities with the reactions to them.
func WithEnrichOwnReactions() GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("withOwnReactions", true)}
}
func WithEnrichUserReactions(userID string) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("user_id", userID)}
}
// WithEnrichRecentReactions enriches the activities with the first reactions to them.
func WithEnrichFirstReactions() GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("withFirstReactions", true)}
}
// WithEnrichRecentReactions enriches the activities with the recent reactions to them.
func WithEnrichRecentReactions() GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("withRecentReactions", true)}
}
// WithEnrichReactionCounts enriches the activities with the reaction counts.
func WithEnrichReactionCounts() GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("withReactionCounts", true)}
}
// WithEnrichOwnChildren enriches the activities with the children reactions.
func WithEnrichOwnChildren() GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("withOwnChildren", true)}
}
// WithEnrichRecentReactionsLimit specifies how many recent reactions to include in the enrichment.
func WithEnrichRecentReactionsLimit(limit int) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("recentReactionsLimit", limit)}
}
// WithEnrichReactionsLimit specifies how many reactions to include in the enrichment.
func WithEnrichReactionsLimit(limit int) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("reaction_limit", limit)}
}
// WithEnrichReactionKindsFilter filters the reactions by the specified kinds
func WithEnrichReactionKindsFilter(kinds ...string) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("reactionKindsFilter", strings.Join(kinds, ","))}
}
// WithEnrichOwnChildrenKindsFilter filters the reactions by the specified kinds for own children
func WithEnrichOwnChildrenKindsFilter(kinds ...string) GetActivitiesOption {
return GetActivitiesOption{makeRequestOption("withOwnChildrenKinds", strings.Join(kinds, ","))}
}
// FollowingOption is an option usable by following feed methods.
type FollowingOption struct {
requestOption
}
// WithFollowingFilter adds the filter parameter to API calls, used when retrieving
// following feeds, allowing the check whether certain feeds are being followed.
func WithFollowingFilter(ids ...string) FollowingOption {
return FollowingOption{makeRequestOption("filter", strings.Join(ids, ","))}
}
// WithFollowingLimit limits the number of followings in the response to the provided limit.
func WithFollowingLimit(limit int) FollowingOption {
return FollowingOption{withLimit(limit)}
}
// WithFollowingOffset returns followings starting from the given offset.
func WithFollowingOffset(offset int) FollowingOption {
return FollowingOption{withOffset(offset)}
}
// FollowersOption is an option usable by followers feed methods.
type FollowersOption struct {
requestOption
}
// WithFollowersLimit limits the number of followers in the response to the provided limit.
func WithFollowersLimit(limit int) FollowersOption {
return FollowersOption{withLimit(limit)}
}
// WithFollowersOffset returns followers starting from the given offset.
func WithFollowersOffset(offset int) FollowersOption {
return FollowersOption{withOffset(offset)}
}
// UnfollowOption is an option usable with the Unfollow feed method.
type UnfollowOption struct {
requestOption
}
// WithUnfollowKeepHistory adds the keep_history parameter to API calls, used to keep
// history when unfollowing feeds, rather than purging it (default behavior).
// If the keepHistory parameter is false, nothing happens.
func WithUnfollowKeepHistory(keepHistory bool) UnfollowOption {
if !keepHistory {
return UnfollowOption{nop{}}
}
return UnfollowOption{makeRequestOption("keep_history", 1)}
}
type followFeedOptions struct {
Target string `json:"target,omitempty"`
ActivityCopyLimit int `json:"activity_copy_limit"`
}
// FollowManyOption is an option to customize behavior of Follow Many calls.
type FollowManyOption struct {
requestOption
}
// WithFollowManyActivityCopyLimit sets how many activities should be copied from the target feed.
func WithFollowManyActivityCopyLimit(activityCopyLimit int) FollowManyOption {
return FollowManyOption{makeRequestOption("activity_copy_limit", activityCopyLimit)}
}
// FollowFeedOption is a function used to customize FollowFeed API calls.
type FollowFeedOption func(*followFeedOptions)
// WithFollowFeedActivityCopyLimit sets the activity copy threshold for Follow Feed API
// calls.
func WithFollowFeedActivityCopyLimit(activityCopyLimit int) FollowFeedOption {
return func(o *followFeedOptions) {
o.ActivityCopyLimit = activityCopyLimit
}
}
// FollowStatOption is an option used to customize FollowStats API calls.
type FollowStatOption struct {
requestOption
}
// WithFollowerSlugs sets the follower feed slugs for filtering in counting.
func WithFollowerSlugs(slugs ...string) FollowStatOption {
return FollowStatOption{makeRequestOption("followers_slugs", strings.Join(slugs, ","))}
}
// WithFollowerSlugs sets the following feed slugs for filtering in counting.
func WithFollowingSlugs(slugs ...string) FollowStatOption {
return FollowStatOption{makeRequestOption("following_slugs", strings.Join(slugs, ","))}
}
// UpdateToTargetsOption determines what operations perform during an UpdateToTargets API call.
type UpdateToTargetsOption func(*updateToTargetsRequest)
// WithToTargetsNew sets the new to targets, replacing all the existing ones. It cannot be used in combination with any other UpdateToTargetsOption.
func WithToTargetsNew(targets ...string) UpdateToTargetsOption {
return func(r *updateToTargetsRequest) {
r.New = targets
}
}
// WithToTargetsAdd sets the add to targets, adding them to the activity's existing ones.
func WithToTargetsAdd(targets ...string) UpdateToTargetsOption {
return func(r *updateToTargetsRequest) {
r.Adds = targets
}
}
// WithToTargetsRemove sets the remove to targets, removing them from activity's the existing ones.
func WithToTargetsRemove(targets ...string) UpdateToTargetsOption {
return func(r *updateToTargetsRequest) {
r.Removes = targets
}
}
// AddObjectOption is an option usable by the Collections.Add method.
type AddObjectOption func(*addCollectionRequest)
// WithUserID adds the user id to the Collections.Add request object.
func WithUserID(userID string) AddObjectOption {
return func(req *addCollectionRequest) {
req.UserID = &userID
}
}
// FilterReactionsOption is an option used by Reactions.Filter() to support pagination.
type FilterReactionsOption struct {
requestOption
}
// WithLimit adds the limit parameter to the Reactions.Filter() call.
func WithLimit(limit int) FilterReactionsOption {
return FilterReactionsOption{withLimit(limit)}
}
// WithIDGTE adds the id_gte parameter to API calls, used when retrieving
// paginated reactions, returning activities with ID greater or
// equal than the provided id.
func WithIDGTE(id string) FilterReactionsOption {
return FilterReactionsOption{makeRequestOption("id_gte", id)}
}
// WithIDGT adds the id_gt parameter to API calls, used when retrieving
// paginated reactions.
func WithIDGT(id string) FilterReactionsOption {
return FilterReactionsOption{makeRequestOption("id_gt", id)}
}
// WithIDLTE adds the id_lte parameter to API calls, used when retrieving
// paginated reactions.
func WithIDLTE(id string) FilterReactionsOption {
return FilterReactionsOption{makeRequestOption("id_lte", id)}
}
// WithIDLT adds the id_lt parameter to API calls, used when retrieving
// paginated reactions.
func WithIDLT(id string) FilterReactionsOption {
return FilterReactionsOption{makeRequestOption("id_lt", id)}
}
// WithActivityData will enable returning the activity data when filtering
// reactions by activity_id.
func WithActivityData() FilterReactionsOption {
return FilterReactionsOption{makeRequestOption("with_activity_data", true)}
}
// WithOwnChildren will enable returning the children reactions when filtering
// reactions by parent ID.
func WithOwnChildren() FilterReactionsOption {
return FilterReactionsOption{makeRequestOption("with_own_children", true)}
}
// WithOwnUserID will enable further filtering by the given user id.
// It's similar to FilterReactionsAttribute user id.
func WithOwnUserID(userID string) FilterReactionsOption {
return FilterReactionsOption{makeRequestOption("user_id", userID)}
}
// WithChildrenUserID will enable further filtering own children by the given user id.
// It's different than FilterReactionsAttribute user id.
func WithChildrenUserID(userID string) FilterReactionsOption {
return FilterReactionsOption{makeRequestOption("children_user_id", userID)}
}
// WithRanking adds the ranking parameter to API calls, used when retrieving
// ranked reactions.
func WithRanking(ranking string) FilterReactionsOption {
return FilterReactionsOption{makeRequestOption("ranking", ranking)}
}
// FilterReactionsAttribute specifies the filtering method of Reactions.Filter()
type FilterReactionsAttribute func() string
// ByKind filters reactions by kind, after the initial desired filtering method was applied.
func (a FilterReactionsAttribute) ByKind(kind string) FilterReactionsAttribute {
return func() string {
base := a()
return fmt.Sprintf("%s/%s", base, kind)
}
}
// ByActivityID will filter reactions based on the specified activity id.
func ByActivityID(activityID string) FilterReactionsAttribute {
return func() string {
return fmt.Sprintf("activity_id/%s", activityID)
}
}
// ByReactionID will filter reactions based on the specified parent reaction id.
func ByReactionID(reactionID string) FilterReactionsAttribute {
return func() string {
return fmt.Sprintf("reaction_id/%s", reactionID)
}
}
// ByUserID will filter reactions based on the specified user id.
func ByUserID(userID string) FilterReactionsAttribute {
return func() string {
return fmt.Sprintf("user_id/%s", userID)
}
}
type nop struct{}
func (nop) values() (key, value string) {
return "", ""
}
func (nop) valid() bool {
return false
}