-
Notifications
You must be signed in to change notification settings - Fork 14
/
feed_aggregated.go
426 lines (331 loc) · 10.2 KB
/
feed_aggregated.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
417
418
419
420
421
422
423
424
425
426
package getstream
import (
"encoding/json"
"errors"
"regexp"
"strings"
)
type postAggregatedFeedOutputActivities struct {
Activities []*Activity `json:"activities"`
}
// GetAggregatedFeedInput is used to Get a list of Activities from a AggregatedFeed
type GetAggregatedFeedInput struct {
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
IDGTE string `json:"id_gte,omitempty"`
IDGT string `json:"id_gt,omitempty"`
IDLTE string `json:"id_lte,omitempty"`
IDLT string `json:"id_lt,omitempty"`
Ranking string `json:"ranking,omitempty"`
}
// GetAggregatedFeedOutput is the response from a AggregatedFeed Activities Get Request
type GetAggregatedFeedOutput struct {
Duration string
Next string
Results []*struct {
Activities []*Activity
ActivityCount int
ActorCount int
CreatedAt string
Group string
ID string
UpdatedAt string
Verb string
}
}
type getAggregatedFeedOutput struct {
Duration string `json:"duration"`
Next string `json:"next"`
Results []*getAggregatedFeedOutputResult `json:"results"`
}
func (a getAggregatedFeedOutput) output() *GetAggregatedFeedOutput {
output := GetAggregatedFeedOutput{
Duration: a.Duration,
Next: a.Next,
}
var results []*struct {
Activities []*Activity
ActivityCount int
ActorCount int
CreatedAt string
Group string
ID string
UpdatedAt string
Verb string
}
for _, result := range a.Results {
outputResult := struct {
Activities []*Activity
ActivityCount int
ActorCount int
CreatedAt string
Group string
ID string
UpdatedAt string
Verb string
}{
ActivityCount: result.ActivityCount,
ActorCount: result.ActorCount,
CreatedAt: result.CreatedAt,
Group: result.Group,
ID: result.ID,
UpdatedAt: result.UpdatedAt,
Verb: result.Verb,
}
for _, activity := range result.Activities {
outputResult.Activities = append(outputResult.Activities, activity)
}
results = append(results, &outputResult)
}
output.Results = results
return &output
}
type getAggregatedFeedOutputResult struct {
Activities []*Activity `json:"activities"`
ActivityCount int `json:"activity_count"`
ActorCount int `json:"actor_count"`
CreatedAt string `json:"created_at"`
Group string `json:"group"`
ID string `json:"id"`
UpdatedAt string `json:"updated_at"`
Verb string `json:"verb"`
}
type getAggregatedFeedFollowersInput struct {
Limit int `json:"limit"`
Skip int `json:"offset"`
}
type getAggregatedFeedFollowersOutput struct {
Duration string `json:"duration"`
Results []*getAggregatedFeedFollowersOutputResult `json:"results"`
}
type getAggregatedFeedFollowersOutputResult struct {
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
FeedID string `json:"feed_id"`
TargetID string `json:"target_id"`
}
type postAggregatedFeedFollowingInput struct {
Target string `json:"target"`
ActivityCopyLimit int `json:"activity_copy_limit"`
}
// AggregatedFeed is a getstream AggregatedFeed
// Use it to for CRUD on AggregatedFeed Groups
type AggregatedFeed struct {
Client *Client
FeedSlug string
UserID string
token string
}
// Signature is used to sign Requests : "FeedSlugUserID Token"
func (f *AggregatedFeed) Signature() string {
if f.Token() == "" {
return f.FeedIDWithoutColon()
}
return f.FeedIDWithoutColon() + " " + f.Token()
}
// FeedID is the combo if the FeedSlug and UserID : "FeedSlug:UserID"
func (f *AggregatedFeed) FeedID() FeedID {
return FeedID(f.FeedSlug + ":" + f.UserID)
}
func (f *AggregatedFeed) FeedIDWithoutColon() string {
return f.FeedSlug + f.UserID
}
// SignFeed sets the token on a Feed
func (f *AggregatedFeed) SignFeed(signer *Signer) {
if f.Client.Signer != nil {
f.token = signer.GenerateToken(f.FeedIDWithoutColon())
}
}
// Token returns the token of a Feed
func (f *AggregatedFeed) Token() string {
return f.token
}
// GenerateToken returns a new Token for a Feed without setting it to the Feed
func (f *AggregatedFeed) GenerateToken(signer *Signer) string {
if f.Client.Signer != nil {
return signer.GenerateToken(f.FeedSlug + f.UserID)
}
return ""
}
// AddActivity is used to add an Activity to a AggregatedFeed
func (f *AggregatedFeed) AddActivity(activity *Activity) (*Activity, error) {
payload, err := json.Marshal(activity)
if err != nil {
return nil, err
}
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/"
resultBytes, err := f.Client.post(f, endpoint, payload, nil)
if err != nil {
return nil, err
}
output := &Activity{}
err = json.Unmarshal(resultBytes, output)
if err != nil {
return nil, err
}
return output, err
}
// AddActivities is used to add multiple Activities to a NotificationFeed
func (f *AggregatedFeed) AddActivities(activities []*Activity) ([]*Activity, error) {
payload, err := json.Marshal(map[string][]*Activity{
"activities": activities,
})
if err != nil {
return nil, err
}
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/"
resultBytes, err := f.Client.post(f, endpoint, payload, nil)
if err != nil {
return nil, err
}
output := &postAggregatedFeedOutputActivities{}
err = json.Unmarshal(resultBytes, output)
if err != nil {
return nil, err
}
return output.Activities, err
}
// Activities returns a list of Activities for a NotificationFeedGroup
func (f *AggregatedFeed) Activities(input *GetAggregatedFeedInput) (*GetAggregatedFeedOutput, error) {
var payload []byte
var err error
if input != nil {
payload, err = json.Marshal(input)
if err != nil {
return nil, err
}
}
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/"
result, err := f.Client.get(f, endpoint, payload, nil)
if err != nil {
return nil, err
}
output := &getAggregatedFeedOutput{}
err = json.Unmarshal(result, output)
if err != nil {
return nil, err
}
return output.output(), err
}
// RemoveActivity removes an Activity from a NotificationFeedGroup
func (f *AggregatedFeed) RemoveActivity(input *Activity) error {
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/" + input.ID + "/"
return f.Client.del(f, endpoint, nil, nil)
}
// RemoveActivityByForeignID removes an Activity from a NotificationFeedGroup by ForeignID
func (f *AggregatedFeed) RemoveActivityByForeignID(input *Activity) error {
if input.ForeignID == "" {
return errors.New("no ForeignID")
}
r, err := regexp.Compile("^[a-z0-9]{8}-[a-z0-9]{4}-[1-5][a-z0-9]{3}-[a-z0-9]{4}-[a-z0-9]{12}$")
if err != nil {
return err
}
if !r.MatchString(input.ForeignID) {
return errors.New("invalid ForeignID")
}
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/" + input.ForeignID + "/"
return f.Client.del(f, endpoint, nil, map[string]string{
"foreign_id": "1",
})
}
// FollowFeedWithCopyLimit sets a Feed to follow another target Feed
// CopyLimit is the maximum number of Activities to Copy from History
func (f *AggregatedFeed) FollowFeedWithCopyLimit(target *FlatFeed, copyLimit int) error {
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/" + "following" + "/"
input := postAggregatedFeedFollowingInput{
Target: target.FeedID().Value(),
ActivityCopyLimit: copyLimit,
}
payload, err := json.Marshal(input)
if err != nil {
return err
}
_, err = f.Client.post(f, endpoint, payload, nil)
return err
}
// Unfollow is used to Unfollow a target Feed
func (f *AggregatedFeed) Unfollow(target *FlatFeed) error {
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/" + "following" + "/" + target.FeedID().Value() + "/"
return f.Client.del(f, endpoint, nil, nil)
}
// UnfollowKeepingHistory is used to Unfollow a target Feed while keeping the History
// this means that Activities already visibile will remain
func (f *AggregatedFeed) UnfollowKeepingHistory(target *FlatFeed) error {
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/" + "following" + "/" + target.FeedID().Value() + "/"
payload, err := json.Marshal(map[string]string{
"keep_history": "1",
})
if err != nil {
return err
}
return f.Client.del(f, endpoint, payload, nil)
}
// FollowersWithLimitAndSkip returns a list of GeneralFeed following the current AggregatedFeed
func (f *AggregatedFeed) FollowersWithLimitAndSkip(limit int, skip int) ([]*GeneralFeed, error) {
var err error
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/" + "followers" + "/"
payload, err := json.Marshal(&getFlatFeedFollowersInput{
Limit: limit,
Skip: skip,
})
if err != nil {
return nil, err
}
resultBytes, err := f.Client.get(f, endpoint, payload, nil)
output := &getAggregatedFeedFollowersOutput{}
err = json.Unmarshal(resultBytes, output)
if err != nil {
return nil, err
}
var outputFeeds []*GeneralFeed
for _, result := range output.Results {
feed := GeneralFeed{}
var match bool
match, err = regexp.MatchString(`^.*?:.*?$`, result.FeedID)
if err != nil {
continue
}
if match {
firstSplit := strings.Split(result.FeedID, ":")
feed.FeedSlug = firstSplit[0]
feed.UserID = firstSplit[1]
}
outputFeeds = append(outputFeeds, &feed)
}
return outputFeeds, err
}
// FollowingWithLimitAndSkip returns a list of GeneralFeed followed by the current FlatFeed
func (f *AggregatedFeed) FollowingWithLimitAndSkip(limit int, skip int) ([]*GeneralFeed, error) {
var err error
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/" + "following" + "/"
payload, err := json.Marshal(&getAggregatedFeedFollowersInput{
Limit: limit,
Skip: skip,
})
if err != nil {
return nil, err
}
resultBytes, err := f.Client.get(f, endpoint, payload, nil)
output := &getAggregatedFeedFollowersOutput{}
err = json.Unmarshal(resultBytes, output)
if err != nil {
return nil, err
}
var outputFeeds []*GeneralFeed
for _, result := range output.Results {
feed := GeneralFeed{}
var match bool
match, err = regexp.MatchString(`^.*?:.*?$`, result.FeedID)
if err != nil {
continue
}
if match {
firstSplit := strings.Split(result.TargetID, ":")
feed.FeedSlug = firstSplit[0]
feed.UserID = firstSplit[1]
}
outputFeeds = append(outputFeeds, &feed)
}
return outputFeeds, err
}