예제 #1
0
파일: adn.go 프로젝트: patrickToca/unico
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)

}
예제 #2
0
파일: adn.go 프로젝트: patrickToca/unico
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)
}