-
Notifications
You must be signed in to change notification settings - Fork 0
/
changes.go
104 lines (90 loc) · 2.73 KB
/
changes.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
package gometawebhooks
import (
"context"
"encoding/json"
"errors"
"fmt"
"golang.org/x/sync/errgroup"
)
var (
ErrChangesFieldNotImplemented = errors.New("changes field not implemented")
ErrInstagramMentionHandlerNotDefined = errors.New("instagram mentions handler not defined")
ErrInstagramStoryInsightsHandlerNotDefined = errors.New("instagram story insights handler not defined")
)
type Mention struct {
MediaID string `json:"media_id,omitempty"`
CommentID string `json:"comment_id,omitempty"`
}
type StoryInsights struct {
MediaID string `json:"media_id,omitempty"`
Exits int `json:"exits,omitempty"`
Replies int `json:"replies,omitempty"`
Reach int `json:"reach,omitempty"`
TapsForward int `json:"taps_forward,omitempty"`
TapsBack int `json:"taps_back,omitempty"`
Impressions int `json:"impressions,omitempty"`
}
type Change struct {
Field string `json:"field,omitempty"`
Value interface{} `json:"value,omitempty"`
}
func (c *Change) UnmarshalJSON(data []byte) error {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
if fieldRaw, ok := raw["field"]; ok {
if err := json.Unmarshal(fieldRaw, &c.Field); err != nil {
return err
}
}
if valueRaw, ok := raw["value"]; ok {
switch c.Field {
case "mentions":
var value Mention
if err := json.Unmarshal(valueRaw, &value); err != nil {
return err
}
c.Value = value
case "story_insights":
var value StoryInsights
if err := json.Unmarshal(valueRaw, &value); err != nil {
return err
}
c.Value = value
default:
return fmt.Errorf("'%s': %w", c.Field, ErrChangesFieldNotImplemented)
}
}
return nil
}
func (hooks Webhooks) changes(ctx context.Context, object Object, entry Entry) error {
if len(entry.Changes) == 0 {
return nil
}
g, ctx := errgroup.WithContext(ctx)
g.SetLimit(len(entry.Changes))
for _, change := range entry.Changes {
g.Go(func() error {
return hooks.change(ctx, object, entry, change)
})
}
return g.Wait()
}
func (h Webhooks) change(ctx context.Context, object Object, entry Entry, change Change) error {
switch value := change.Value.(type) {
case Mention:
if h.instagramMentionHandler == nil {
return ErrInstagramMentionHandlerNotDefined
}
return h.instagramMentionHandler.InstagramMention(ctx, object, entry, value)
case StoryInsights:
if h.instagramStoryInsightsHandler == nil {
return ErrInstagramStoryInsightsHandlerNotDefined
}
return h.instagramStoryInsightsHandler.InstagramStoryInsights(ctx, object, entry, value)
default:
// @note should not be hit cause Unmarshall ensures field is supported
return fmt.Errorf("'%s': %w", change.Field, ErrChangesFieldNotImplemented)
}
}