This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.go
152 lines (127 loc) · 3.58 KB
/
post.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
package fbintegration
import (
"encoding/json"
"fmt"
"strings"
facebookLib "github.com/huandu/facebook"
)
type (
// Post contains details for a given post including the name, id and insights.
Post struct {
AdType string `json:"ad_type"`
Name string `facebook:"message" json:"name"`
ID string `facebook:"id" json:"post_id"`
AdID string `json:"ad_id"`
AdsetID string `json:"adset_id"`
ObjectID string `json:"object_id"`
Results *PostResults `json:"-"`
Data *PostData `json:"data,omitempty"`
}
)
// NewPostFromResult creates a new Post struct from a facebookLib.Result.
func NewPostFromResult(result facebookLib.Result) Post {
var post Post
post.Results = &PostResults{}
result.DecodeField("", &post)
return post
}
// GenerateCommentsParams create the params for getting comments for a post.
func (p Post) GenerateCommentsParams() BatchParams {
uri := fmt.Sprintf("%s/comments?summary=true&limit=0&period=lifetime", p.ID)
return NewBatchParams(uri)
}
// GenerateInsightParams create the params for getting insights for a post from the Graph API.
func (p Post) GenerateInsightParams() BatchParams {
fields := []string{
"post_consumptions",
"post_engaged_users",
"post_impressions",
"post_impressions_paid",
"post_impressions_paid_unique",
"post_impressions_unique",
"post_stories_by_action_type",
"post_video_avg_time_watched",
"post_video_complete_views_organic",
"post_video_complete_views_paid",
"post_video_view_time",
"post_video_views",
"post_video_views_organic",
"post_video_views_paid",
"post_video_views_unique",
}
uri := fmt.Sprintf(
"%s/insights/%s?period=lifetime&limit=20",
p.ID,
strings.Join(fields, ","),
)
return NewBatchParams(uri)
}
// GenerateParams creates the params for getting information on a post.
func (p *Post) GenerateParams() BatchParams {
fields := []string{
"object_id",
"message",
}
uri := fmt.Sprintf(
"%s?fields=%s",
p.ID,
strings.Join(fields, ","),
)
return NewBatchParams(uri)
}
// GeneratePostParams creates params for getting back data on a
// particular post.
func (p *Post) GeneratePostParams() BatchParams {
fields := []string{
"created_time",
"shares",
}
uri := fmt.Sprintf(
"%s?fields=%s",
p.ID,
strings.Join(fields, ","),
)
return NewBatchParams(uri)
}
// GenerateReactionBreakdownParams creates params for getting the reaction breakdown for a post.
func (p Post) GenerateReactionBreakdownParams() []BatchParams {
var params []BatchParams
for _, reaction := range p.ReactionTypes() {
uri := fmt.Sprintf(
"%s/reactions?limit=0&summary=total_count&type=%s",
p.ID,
reaction,
)
params = append(params, NewBatchParams(uri))
}
return params
}
// GenerateSharesParams creates params for getting the shared count for a post.
func (p Post) GenerateSharesParams() BatchParams {
return NewBatchParams(fmt.Sprintf("%s/sharedposts", p.ID))
}
// GenerateTotalReactionsParams creates params for gettting the total reaction count.
func (p Post) GenerateTotalReactionsParams() BatchParams {
return NewBatchParams(fmt.Sprintf("%s/reactions?limit=0&summary=total_count", p.ID))
}
// ReactionTypes slice of currently supported reaction type for facebook posts.
func (p Post) ReactionTypes() []string {
return []string{
"ANGRY",
"HAHA",
"LIKE",
"LOVE",
"SAD",
"THANKFUL",
"WOW",
}
}
// ToJSON marshal the current struct into a byte array then
// return a string representation of that.
func (p *Post) ToJSON() (string, error) {
b, err := json.Marshal(p)
if err != nil {
return "", err
}
return string(b), nil
}