forked from rselbach/unico
/
adn.go
187 lines (162 loc) · 4.6 KB
/
adn.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
// unico - Send Google+ activities to other networks
//
// Copyright 2011 The Unico Authors. All rights reserved.
// Use of this source code is governed by the Simplified
// BSD license that can be found in the LICENSE file.
package unico
import (
"appengine"
"appengine/urlfetch"
plus "code.google.com/p/google-api-go-client/plus/v1"
"errors"
"fmt"
"github.com/robteix/adnlib"
"net/http"
)
var _ = fmt.Println
func adnHandler(w http.ResponseWriter, r *http.Request) {
if r.FormValue("code") != "" {
adnVerify(w, r)
} else {
signInADNHandler(w, r)
}
}
func adnVerify(w http.ResponseWriter, r *http.Request) {
id := r.FormValue("id")
c := appengine.NewContext(r)
if id == "" {
serveError(c, w, errors.New("Missing ID parameter"))
return
}
code := r.FormValue("code")
conf := &adnlib.Config{
ConsumerKey: appConfig.ADNConsumerKey,
ConsumerSecret: appConfig.ADNConsumerSecret,
Callback: "http://" + appConfig.AppDomain + "/adnauth?id=" + id}
tok := &adnlib.Token{}
tr := &adnlib.Transport{Config: conf,
Token: tok,
Transport: &urlfetch.Transport{Context: c}}
c.Debugf("Requesting ADN Token with code %s\n", code)
tok, err := tr.RequestAccessToken(code)
if err != nil {
c := appengine.NewContext(r)
serveError(c, w, err)
c.Errorf("%v", err)
return
}
tr.Token = tok
tl, _ := adnlib.New(tr.Client())
adnTok, err := tl.Stream.Token().Do()
if err != nil {
fmt.Printf("err=%v\n", err)
serveError(c, w, err)
return
}
user := loadUser(r, id)
user.ADNAccessToken = tok.AccessToken
user.ADNId = adnTok.Data.User.Id
user.ADNScreenName = adnTok.Data.User.UserName
if err := saveUser(r, &user); err != nil {
serveError(c, w, err)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
func signInADNHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
id := r.FormValue("id")
if id == "" {
serveError(c, w, errors.New("Missing ID parameter"))
return
}
conf := &adnlib.Config{
ConsumerKey: appConfig.ADNConsumerKey,
ConsumerSecret: appConfig.ADNConsumerSecret,
Callback: "http://" + appConfig.AppHost + "/adnauth?id=" + id}
tok := &adnlib.Token{}
tr := &adnlib.Transport{Config: conf,
Token: tok,
Transport: &urlfetch.Transport{Context: c}}
http.Redirect(w, r, tr.AuthURL(), http.StatusFound)
}
func publishActivityToADN(w http.ResponseWriter, r *http.Request, act *plus.Activity, user *User) {
c := appengine.NewContext(r)
conf := &adnlib.Config{
ConsumerKey: appConfig.ADNConsumerKey,
ConsumerSecret: appConfig.ADNConsumerSecret}
tok := &adnlib.Token{AccessToken: user.ADNAccessToken}
tr := &adnlib.Transport{Config: conf,
Token: tok,
Transport: &urlfetch.Transport{Context: c}}
tl, _ := adnlib.New(tr.Client())
var attachment *plus.ActivityObjectAttachments
obj := act.Object
kind := ""
content := ""
if act.Verb == "share" {
content = act.Annotation
if content == "" {
content = "Resharing " + obj.Actor.DisplayName
}
kind = "status_share"
} else {
kind = "status"
if obj != nil {
if len(obj.Attachments) > 0 {
attachment = obj.Attachments[0]
kind = attachment.ObjectType
}
content = obj.Content
} else {
content = act.Title
}
}
content = removeTags(content)
c.Debugf("Post (%s):\n\tkind: %s\n\tcontent: %s\n", user.ADNId, kind, content)
var err error
switch kind {
case "status":
// post a status update
_, err = tl.Stream.Post(adnShorten(140, content, act.Url)).Do()
case "status_share":
_, err = tl.Stream.Post(adnShortenLink(140, content, act.Url)).Do()
case "article":
// post a link
c.Debugf("Article (%s):\n\tcontent: %s\n\turl: %s\n", user.ADNId, content, attachment.Url)
if content == attachment.Url || content == "" {
if attachment.DisplayName != "" {
content = attachment.DisplayName
} else {
content = "Shared a link."
}
}
_, err = tl.Stream.Post(adnShortenLink(140, content, attachment.Url)).Do()
default:
if obj != nil {
_, err = tl.Stream.Post(adnShortenLink(140, content, obj.Url)).Do()
}
}
if err == adnlib.ErrOAuth {
user.DisableADN()
saveUser(r, user)
}
c.Debugf("publishActivityToADN(%s): err=%v\n", kind, err)
}
func adnShortenLink(max int, content, url string) string {
// maximum size for the context itself
contentMax := max - len(url) - 1
if len(content) <= contentMax {
return content + " " + url
}
return content[:contentMax] + " " + url
}
func adnShorten(max int, content, url string) string {
if len(content) < max {
return content
}
// maximum size for the context itself
contentMax := max - len(url) - 1
// leave 25 for URL (adnShortened by adn)
return content[:contentMax] + " " + url
}