forked from rainycape/gondola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.go
138 lines (134 loc) · 3.39 KB
/
share.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
package social
import (
"bytes"
"fmt"
"net/url"
"gnd.la/app"
"gnd.la/social/facebook"
"gnd.la/social/pinterest"
"gnd.la/social/twitter"
)
func Share(ctx *app.Context, s Service, item *Item, config interface{}) (interface{}, error) {
if err := validateConfig(s, config); err != nil {
return nil, err
}
switch s {
case Twitter:
conf := config.(*TwitterConfig)
var buf bytes.Buffer
buf.WriteString(item.Title)
for _, v := range item.Links {
buf.WriteByte(' ')
buf.WriteString(v.String())
}
for _, v := range item.Images {
buf.WriteByte(' ')
buf.WriteString(v.String())
}
tweet, err := conf.App.Clone(ctx).Update(buf.String(), conf.Token, &twitter.TweetOptions{Truncate: true})
return tweet, err
case Facebook:
conf := config.(*FacebookConfig)
path := "/me/feed"
parameters := make(url.Values)
parameters.Set("title", item.Title)
parameters.Set("message", item.Description)
if len(item.Links) > 0 {
// Post to /me/links rather than /me/feed, so we get
// share buttons. See http://stackoverflow.com/questions/10770103
path = "/me/links"
parameters.Set("link", item.Links[0].String())
}
if len(item.Images) > 0 {
parameters.Set("picture", item.Images[0].String())
}
var out interface{}
err := conf.App.Clone(ctx).Post(path, parameters, conf.AccessToken, &out)
return out, err
case Pinterest:
conf := config.(*PinterestConfig)
sess, err := conf.Account.Clone(ctx).SignIn()
if err != nil {
return nil, err
}
var board *pinterest.Board
var boardName string
switch b := conf.Board.(type) {
case *pinterest.Board:
board = b
case string:
boardName = b
}
if board == nil {
boards, err := sess.Boards()
if err != nil {
return nil, err
}
if len(boards) > 0 {
if boardName != "" {
for _, v := range boards {
if v.Name == boardName {
board = v
break
}
}
} else {
board = boards[0]
}
}
}
if board == nil {
return nil, fmt.Errorf("can't find Pinterest board")
}
pin := &pinterest.Pin{
Link: item.Links[0].String(),
Image: item.Images[0].String(),
Description: item.Title,
}
return sess.Post(board, pin)
}
return nil, fmt.Errorf("Share() does not support service %s", s)
}
func validateConfig(s Service, config interface{}) error {
switch s {
case Twitter:
conf, ok := config.(*TwitterConfig)
if !ok {
return fmt.Errorf("%s config must be *TwitterConfig, it's %T", s, config)
}
if conf.App == nil {
return fmt.Errorf("twitter app can't be nil")
}
if conf.Token == nil {
return fmt.Errorf("twitter token can't be nil")
}
case Facebook:
conf, ok := config.(*FacebookConfig)
if !ok {
return fmt.Errorf("%s config must be *FacebookConfig, it's %T", s, config)
}
if conf.App == nil {
conf.App = &facebook.App{}
}
if conf.AccessToken == "" {
return fmt.Errorf("facebook access_token can't be empty")
}
case Pinterest:
conf, ok := config.(*PinterestConfig)
if !ok {
return fmt.Errorf("%s config must be *PinterestConfig, it's %T", s, config)
}
if conf.Account == nil {
return fmt.Errorf("pinterest account can't be empty")
}
if conf.Account.Username == "" {
return fmt.Errorf("pinterest username can't be empty")
}
if conf.Account.Password == "" {
return fmt.Errorf("pinterest password can't be empty")
}
default:
return fmt.Errorf("service %s is not supported", s)
}
return nil
}