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.Nanoseconds() c.Debugf("syncStream: user: %s, nPub: %v, Latest: %v\n", user.Id, nPub, user.GoogleLatest) if nPub > user.GoogleLatest { if user.HasFacebook() { publishActivityToFacebook(w, r, act, user) } if user.HasTwitter() { publishActivityToTwitter(w, r, act, user) } } if nPub > latest { latest = nPub } } if latest > user.GoogleLatest { user.GoogleLatest = latest saveUser(r, user) } }
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) if session, err := sessions.Session(r, "", "datastore"); err == nil { session["userID"] = person.Id f := sessions.Save(r, w) c.Debugf("callback: sessionSave: %v\n", f) } user := loadUser(r, person.Id) if user.Id == "" { user = User{Id: person.Id, GoogleAccessToken: tr.Token.AccessToken, GoogleTokenExpiry: tr.Token.TokenExpiry, GoogleRefreshToken: tr.Token.RefreshToken} user.GoogleLatest = time.Nanoseconds() } saveUser(r, &user) http.Redirect(w, r, "/", http.StatusFound) }
// 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 userCookie, err := r.Cookie("userId") var user User if err == nil { user = loadUser(r, userCookie.Value) } c.Debugf("loadUser: %v\n", user) if user.Id != "" { if session, err := sessions.Session(r, "", "datastore"); err == nil { session["userID"] = user.Id f := sessions.Save(r, w) c.Debugf("saveSession: %v\n", f) } 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.Client()) u, err := tl.Users.Show().UserId(user.TwitterId).Do() 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["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() c.Debugf("Home people get: %v,(%v)\n", person, err) 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.Execute(w, "home", params); err != nil { serveError(c, w, err) c.Errorf("%v", err) return } }