mirror of
https://github.com/samsonjs/retrogit.git
synced 2026-04-03 10:55:52 +00:00
Send single email button.
This commit is contained in:
parent
1ffac114ff
commit
1bc9d53325
3 changed files with 72 additions and 4 deletions
2
TODO
2
TODO
|
|
@ -1,6 +1,6 @@
|
|||
TODO
|
||||
- Break up indexHandler
|
||||
- Single email sending button
|
||||
- Flash message when sending a single email
|
||||
- Loop over registered accounts and send them email
|
||||
- Flash message and sign out when OAuth token has expired/is invalid
|
||||
- Handle pagination for user repository list
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package githop
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
|
|
@ -8,6 +9,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
"appengine"
|
||||
"appengine/mail"
|
||||
"appengine/urlfetch"
|
||||
|
||||
"code.google.com/p/goauth2/oauth"
|
||||
|
|
@ -35,7 +37,7 @@ func initGithubOAuthConfig() {
|
|||
if err != nil {
|
||||
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.TokenURL = "https://github.com/login/oauth/access_token"
|
||||
}
|
||||
|
|
@ -46,6 +48,7 @@ func init() {
|
|||
|
||||
router = mux.NewRouter()
|
||||
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-out", signOutHandler).Name("sign-out")
|
||||
router.HandleFunc("/github/callback", githubOAuthCallbackHandler)
|
||||
|
|
@ -100,15 +103,76 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
signOutUrl, _ := router.Get("sign-out").URL()
|
||||
sendDigestUrl, _ := router.Get("send-digest").URL()
|
||||
var data = map[string]interface{}{
|
||||
"SignOutUrl": signOutUrl.String(),
|
||||
"Digest": digest,
|
||||
"SignOutUrl": signOutUrl.String(),
|
||||
"SendDigestUrl": sendDigestUrl.String(),
|
||||
"Digest": digest,
|
||||
}
|
||||
if err := templates.ExecuteTemplate(w, "index", data); err != nil {
|
||||
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) {
|
||||
code := r.FormValue("code")
|
||||
oauthTransport := githubOAuthTransport(r)
|
||||
|
|
|
|||
|
|
@ -16,6 +16,10 @@
|
|||
</a>
|
||||
</p>
|
||||
|
||||
<form method="POST" action="{{.SendDigestUrl}}">
|
||||
<input type="submit" value="Email Digest">
|
||||
</form>
|
||||
|
||||
{{template "digest" .Digest}}
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
Loading…
Reference in a new issue