コード例 #1
0
ファイル: unico.go プロジェクト: patrickToca/unico
func syncStream(w http.ResponseWriter, r *http.Request, user *User) {
	c := appengine.NewContext(r)
	tr := transport(*user)
	tr.Transport = &urlfetch.Transport{Context: c}

	httpClient := tr.Client()
	p, err := plus.New(httpClient)
	if err != nil {
		serveError(c, w, err)
		return
	}

	latest := user.GoogleLatest
	c.Debugf("syncStream: fetching for %s\n", user.Id)
	activityFeed, err := p.Activities.List(user.Id, "public").MaxResults(5).Do()
	if err != nil {
		c.Debugf("syncStream: activity fetch failed for %s. Err: %v\n", user.Id, err)
		return
	}

	for _, act := range activityFeed.Items {
		published, _ := time.Parse(time.RFC3339, act.Published)
		nPub := published.UnixNano()

		c.Debugf("syncStream: user: %s, nPub: %v, Latest: %v\n", user.Id, nPub, user.GoogleLatest)

		baba, _ := json.Marshal(act)
		c.Debugf("\n\nActivity: %s\n\n", baba)
		if nPub > user.GoogleLatest {
			if user.HasFacebook() {
				publishActivityToFacebook(w, r, act, user)
			}
			if user.HasTwitter() {
				publishActivityToTwitter(w, r, act, user)
			}
			if user.HasADN() {
				publishActivityToADN(w, r, act, user)
			}
		}
		if nPub > latest {
			latest = nPub
		}
	}

	if latest > user.GoogleLatest ||
		user.GoogleAccessToken != tr.Token.AccessToken ||
		user.GoogleRefreshToken != tr.Token.RefreshToken ||
		user.GoogleTokenExpiry != tr.Token.Expiry.UnixNano() {
		user.GoogleLatest = latest
		user.GoogleAccessToken = tr.Token.AccessToken
		user.GoogleRefreshToken = tr.Token.RefreshToken
		user.GoogleTokenExpiry = tr.Token.Expiry.UnixNano()
		saveUser(r, user)
	}
}
コード例 #2
0
ファイル: google.go プロジェクト: patrickToca/unico
func googleCallbackHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	code := r.FormValue("code")
	tr := emptyTransport()
	tr.Transport = &urlfetch.Transport{Context: c}
	if _, err := tr.Exchange(code); err != nil {
		c.Debugf("tr: %v\n", tr.Token)
		serveError(c, w, err)
		return
	}
	// get info on the user
	httpClient := tr.Client()
	p, err := plus.New(httpClient)
	if err != nil {
		serveError(c, w, err)
		return
	}

	person, err := p.People.Get("me").Do()
	if err != nil {
		serveError(c, w, err)
		return
	}
	cookie := &http.Cookie{Name: "userId", Value: person.Id, Domain: appConfig.AppDomain, Path: "/", MaxAge: 30000000 /* about a year */}
	http.SetCookie(w, cookie)

	user := loadUser(r, person.Id)

	user.GoogleAccessToken = tr.Token.AccessToken
	user.GoogleTokenExpiry = tr.Token.Expiry.UnixNano()
	user.GoogleRefreshToken = tr.Token.RefreshToken
	if user.Id == "" {
		user.Id = person.Id
		user.GoogleLatest = time.Now().UnixNano()

	}
	saveUser(r, &user)

	http.Redirect(w, r, "/", http.StatusFound)
}
コード例 #3
0
ファイル: unico.go プロジェクト: patrickToca/unico
// Displays the home page.
func homeHandler(w http.ResponseWriter, r *http.Request) {
	if appConfig.AppHost == "" {
		appConfig.AppHost = r.Host
	}
	c := appengine.NewContext(r)
	if r.Method != "GET" || r.URL.Path != "/" {
		serve404(w)
		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")

	params := make(map[string]string)

	// Look for a browser cookie containing the user id
	// We can use this to load the user information
	var user User
	user, err := loadUserCookie(r)
	if err == nil {
		if user.TwitterId != "" {

			item := new(memcache.Item)
			item, err := memcache.Get(c, "pic"+user.Id)

			if err != nil {
				// get the user profile pic
				conf := &tweetlib.Config{
					ConsumerKey:    appConfig.TwitterConsumerKey,
					ConsumerSecret: appConfig.TwitterConsumerSecret}
				tok := &tweetlib.Token{
					OAuthSecret: user.TwitterOAuthSecret,
					OAuthToken:  user.TwitterOAuthToken}
				tr := &tweetlib.Transport{Config: conf,
					Token:     tok,
					Transport: &urlfetch.Transport{Context: c}}

				tl, _ := tweetlib.New(tr)
				opts := tweetlib.NewOptionals()
				opts.Add("user_id", user.TwitterId)
				u, err := tl.User.Show(user.TwitterScreenName, opts)
				if err == nil {
					params["pic"] = u.ProfileImageUrl
					memcache.Add(c, &memcache.Item{Key: "pic" + user.Id, Value: []byte(u.ProfileImageUrl)})
				}

			} else {
				params["pic"] = string(item.Value)
			}

		}
		params["adnid"] = user.ADNId
		params["adnname"] = user.ADNScreenName
		params["twitterid"] = user.TwitterId
		params["twittername"] = user.TwitterScreenName
		params["googleid"] = user.Id
		params["fbid"] = user.FBId
		params["fbname"] = user.FBName

		mu := memUser(c, user.Id)
		if mu.Name == "" {
			tr := transport(user)
			tr.Transport = &urlfetch.Transport{Context: c}
			p, _ := plus.New(tr.Client())
			person, err := p.People.Get(user.Id).Do()
			if err == nil {
				mu.Image = person.Image.Url
				mu.Name = person.DisplayName
				memUserSave(c, user.Id, mu)
			}

		}
		params["googleimg"] = mu.Image
		params["googlename"] = mu.Name

	}

	if err := templates.ExecuteTemplate(w, "home", params); err != nil {
		serveError(c, w, err)
		return
	}

}