This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
forked from mrhenry/go-getstream
-
Notifications
You must be signed in to change notification settings - Fork 10
/
feed_notification.go
467 lines (363 loc) · 11.4 KB
/
feed_notification.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package getstream
import (
"encoding/json"
"errors"
"regexp"
"strconv"
"strings"
)
type postNotificationFeedOutputActivities struct {
Activities []*Activity `json:"activities"`
}
// GetNotificationFeedInput is used to Get a list of Activities from a NotificationFeed
type GetNotificationFeedInput 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"`
}
// GetNotificationFeedOutput is the response from a NotificationFeed Activities Get Request
type GetNotificationFeedOutput struct {
Duration string
Next string
Results []*struct {
Activities []*Activity
ActivityCount int
ActorCount int
CreatedAt string
Group string
ID string
IsRead bool
IsSeen bool
UpdatedAt string
Verb string
}
Unread int
Unseen int
}
type getNotificationFeedOutput struct {
Duration string `json:"duration"`
Next string `json:"next"`
Results []*getNotificationFeedOutputResult `json:"results"`
Unread int `json:"unread"`
Unseen int `json:"unseen"`
}
func (a getNotificationFeedOutput) output() *GetNotificationFeedOutput {
output := GetNotificationFeedOutput{
Duration: a.Duration,
Next: a.Next,
Unread: a.Unread,
Unseen: a.Unseen,
}
var results []*struct {
Activities []*Activity
ActivityCount int
ActorCount int
CreatedAt string
Group string
ID string
IsRead bool
IsSeen bool
UpdatedAt string
Verb string
}
for _, result := range a.Results {
outputResult := struct {
Activities []*Activity
ActivityCount int
ActorCount int
CreatedAt string
Group string
ID string
IsRead bool
IsSeen bool
UpdatedAt string
Verb string
}{
ActivityCount: result.ActivityCount,
ActorCount: result.ActorCount,
CreatedAt: result.CreatedAt,
Group: result.Group,
ID: result.ID,
IsRead: result.IsRead,
IsSeen: result.IsSeen,
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 getNotificationFeedOutputResult 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"`
IsRead bool `json:"is_read"`
IsSeen bool `json:"is_seen"`
UpdatedAt string `json:"updated_at"`
Verb string `json:"verb"`
}
type getNotificationFeedFollowersInput struct {
Limit int `json:"limit"`
Skip int `json:"offset"`
}
type getNotificationFeedFollowersOutput struct {
Duration string `json:"duration"`
Results []*getNotificationFeedFollowersOutputResult `json:"results"`
}
type getNotificationFeedFollowersOutputResult struct {
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
FeedID string `json:"feed_id"`
TargetID string `json:"target_id"`
}
type postNotificationFeedFollowingInput struct {
Target string `json:"target"`
ActivityCopyLimit int `json:"activity_copy_limit"`
}
// NotificationFeed is a getstream NotificationFeed
// Use it to for CRUD on NotificationFeed Groups
type NotificationFeed struct {
Client *Client
FeedSlug string
UserID string
token string
}
// Signature is used to sign Requests : "FeedSlugUserID Token"
func (f *NotificationFeed) 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 *NotificationFeed) FeedID() FeedID {
return FeedID(f.FeedSlug + ":" + f.UserID)
}
func (f *NotificationFeed) FeedIDWithoutColon() string {
return f.FeedSlug + f.UserID
}
// SignFeed sets the token on a Feed
func (f *NotificationFeed) SignFeed(signer *Signer) {
if f.Client.Signer != nil {
f.token = signer.GenerateToken(f.FeedIDWithoutColon())
}
}
// Token returns the token of a Feed
func (f *NotificationFeed) Token() string {
return f.token
}
// GenerateToken returns a new Token for a Feed without setting it to the Feed
func (f *NotificationFeed) 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 NotificationFeed
func (f *NotificationFeed) 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 *NotificationFeed) 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 := &postNotificationFeedOutputActivities{}
err = json.Unmarshal(resultBytes, output)
if err != nil {
return nil, err
}
return output.Activities, err
}
// MarkActivitiesAsRead marks activities as read for this feed
func (f *NotificationFeed) MarkActivitiesAsRead(activities []*Activity) error {
var ids []string
for _, activity := range activities {
ids = append(ids, activity.ID)
}
idStr := strings.Join(ids, ",")
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/"
_, err := f.Client.get(f, endpoint, nil, map[string]string{
"mark_read": idStr,
})
return err
}
// MarkActivitiesAsSeenWithLimit marks activities as seen for this feed
func (f *NotificationFeed) MarkActivitiesAsSeenWithLimit(limit int) error {
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/"
_, err := f.Client.get(f, endpoint, nil, map[string]string{
"mark_seen": "true",
"limit": strconv.Itoa(limit),
})
return err
}
// Activities returns a list of Activities for a NotificationFeedGroup
func (f *NotificationFeed) Activities(input *GetNotificationFeedInput) (*GetNotificationFeedOutput, 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 := &getNotificationFeedOutput{}
err = json.Unmarshal(result, output)
if err != nil {
return nil, err
}
return output.output(), err
}
// RemoveActivity removes an Activity from a NotificationFeedGroup
func (f *NotificationFeed) 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 *NotificationFeed) RemoveActivityByForeignID(input *Activity) error {
if input.ForeignID == "" {
return errors.New("no 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 *NotificationFeed) FollowFeedWithCopyLimit(target *FlatFeed, copyLimit int) error {
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/" + "following" + "/"
input := postNotificationFeedFollowingInput{
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 *NotificationFeed) 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 *NotificationFeed) 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)
}
// FollowingWithLimitAndSkip returns a list of GeneralFeed followed by the current FlatFeed
func (f *NotificationFeed) FollowingWithLimitAndSkip(limit int, skip int) ([]*GeneralFeed, error) {
var err error
endpoint := "feed/" + f.FeedSlug + "/" + f.UserID + "/" + "following" + "/"
payload, err := json.Marshal(&getNotificationFeedFollowersInput{
Limit: limit,
Skip: skip,
})
if err != nil {
return nil, err
}
resultBytes, err := f.Client.get(f, endpoint, payload, nil)
output := &getNotificationFeedFollowersOutput{}
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
}
// FollowersWithLimitAndSkip returns a list of GeneralFeed following the current FlatFeed
func (f *NotificationFeed) 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 := &getFlatFeedFollowersOutput{}
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
}