Send single email button.

This commit is contained in:
Mihai Parparita 2014-07-29 22:52:36 -07:00
parent 1ffac114ff
commit 1bc9d53325
3 changed files with 72 additions and 4 deletions

2
TODO
View file

@ -1,6 +1,6 @@
TODO TODO
- Break up indexHandler - Break up indexHandler
- Single email sending button - Flash message when sending a single email
- Loop over registered accounts and send them email - Loop over registered accounts and send them email
- Flash message and sign out when OAuth token has expired/is invalid - Flash message and sign out when OAuth token has expired/is invalid
- Handle pagination for user repository list - Handle pagination for user repository list

View file

@ -1,6 +1,7 @@
package githop package githop
import ( import (
"bytes"
"encoding/json" "encoding/json"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
@ -8,6 +9,7 @@ import (
"net/http" "net/http"
"appengine" "appengine"
"appengine/mail"
"appengine/urlfetch" "appengine/urlfetch"
"code.google.com/p/goauth2/oauth" "code.google.com/p/goauth2/oauth"
@ -35,7 +37,7 @@ func initGithubOAuthConfig() {
if err != nil { if err != nil {
log.Panicf("Could not parse GitHub OAuth config %s: %s", configBytes, err.Error()) log.Panicf("Could not parse GitHub OAuth config %s: %s", configBytes, err.Error())
} }
githubOauthConfig.Scope = "repo" githubOauthConfig.Scope = "repo, user:email"
githubOauthConfig.AuthURL = "https://github.com/login/oauth/authorize" githubOauthConfig.AuthURL = "https://github.com/login/oauth/authorize"
githubOauthConfig.TokenURL = "https://github.com/login/oauth/access_token" githubOauthConfig.TokenURL = "https://github.com/login/oauth/access_token"
} }
@ -46,6 +48,7 @@ func init() {
router = mux.NewRouter() router = mux.NewRouter()
router.HandleFunc("/", indexHandler).Name("index") router.HandleFunc("/", indexHandler).Name("index")
router.HandleFunc("/digest/send", sendDigestHandler).Name("send-digest").Methods("POST")
router.HandleFunc("/session/sign-in", signInHandler).Name("sign-in") router.HandleFunc("/session/sign-in", signInHandler).Name("sign-in")
router.HandleFunc("/session/sign-out", signOutHandler).Name("sign-out") router.HandleFunc("/session/sign-out", signOutHandler).Name("sign-out")
router.HandleFunc("/github/callback", githubOAuthCallbackHandler) router.HandleFunc("/github/callback", githubOAuthCallbackHandler)
@ -100,15 +103,76 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
signOutUrl, _ := router.Get("sign-out").URL() signOutUrl, _ := router.Get("sign-out").URL()
sendDigestUrl, _ := router.Get("send-digest").URL()
var data = map[string]interface{}{ var data = map[string]interface{}{
"SignOutUrl": signOutUrl.String(), "SignOutUrl": signOutUrl.String(),
"Digest": digest, "SendDigestUrl": sendDigestUrl.String(),
"Digest": digest,
} }
if err := templates.ExecuteTemplate(w, "index", data); err != nil { if err := templates.ExecuteTemplate(w, "index", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
} }
func sendDigestHandler(w http.ResponseWriter, r *http.Request) {
session, _ := sessionStore.Get(r, sessionConfig.CookieName)
userId := session.Values[sessionConfig.UserIdKey].(int)
c := appengine.NewContext(r)
account, err := getAccount(c, userId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
oauthTransport := githubOAuthTransport(r)
oauthTransport.Token = &account.OAuthToken
githubClient := github.NewClient(oauthTransport.Client())
digest, err := newDigest(githubClient)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var digestHtml bytes.Buffer
if err := templates.ExecuteTemplate(&digestHtml, "digest", digest); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
emails, _, err := githubClient.Users.ListEmails(nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var primaryVerified *string
for _, email := range emails {
if email.Primary != nil && *email.Primary &&
email.Verified != nil && *email.Verified {
primaryVerified = email.Email
break
}
}
if primaryVerified == nil {
http.Error(w, "No verified email addresses found in GitHub account", http.StatusBadRequest)
return
}
digestMessage := &mail.Message{
Sender: "GitHop <mihai.parparita@gmail.com>",
To: []string{*primaryVerified},
Subject: "GitHop Digest",
HTMLBody: digestHtml.String(),
}
if err := mail.Send(c, digestMessage); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
indexUrl, _ := router.Get("index").URL()
http.Redirect(w, r, indexUrl.String(), http.StatusFound)
}
func githubOAuthCallbackHandler(w http.ResponseWriter, r *http.Request) { func githubOAuthCallbackHandler(w http.ResponseWriter, r *http.Request) {
code := r.FormValue("code") code := r.FormValue("code")
oauthTransport := githubOAuthTransport(r) oauthTransport := githubOAuthTransport(r)

View file

@ -16,6 +16,10 @@
</a> </a>
</p> </p>
<form method="POST" action="{{.SendDigestUrl}}">
<input type="submit" value="Email Digest">
</form>
{{template "digest" .Digest}} {{template "digest" .Digest}}
</body> </body>
</html> </html>