From 69fcc91dc72cf3fe4dd5b9170c1aa3867dc8d9e6 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Mon, 1 Sep 2014 22:17:09 -0700 Subject: [PATCH] Add setting for which email address the digest will be sent to. --- app/account.go | 33 +++++++++++++++++++++++++- app/githop.go | 47 +++++++++++++++++++++---------------- app/static/main.css | 9 +++++++ app/templates/settings.html | 26 ++++++++++++++++---- 4 files changed, 90 insertions(+), 25 deletions(-) diff --git a/app/account.go b/app/account.go index 20c3633..c583b41 100644 --- a/app/account.go +++ b/app/account.go @@ -3,12 +3,14 @@ package githop import ( "bytes" "encoding/gob" + "errors" "time" "appengine" "appengine/datastore" "code.google.com/p/goauth2/oauth" + "github.com/google/go-github/github" ) type Account struct { @@ -20,6 +22,7 @@ type Account struct { TimezoneName string `datastore:",noindex"` TimezoneLocation *time.Location `datastore:"-,"` ExcludedRepoIds []int `datastore:",noindex"` + DigestEmailAddress string } func getAccount(c appengine.Context, githubUserId int) (*Account, error) { @@ -76,7 +79,7 @@ func (account *Account) IsRepoIdExcluded(repoId int) bool { return false } -func (account *Account) put(c appengine.Context) error { +func (account *Account) Put(c appengine.Context) error { w := new(bytes.Buffer) err := gob.NewEncoder(w).Encode(&account.OAuthToken) if err != nil { @@ -87,3 +90,31 @@ func (account *Account) put(c appengine.Context) error { _, err = datastore.Put(c, key, account) return err } + +func (account *Account) GetDigestEmailAddress(githubClient *github.Client) (string, error) { + if len(account.DigestEmailAddress) > 0 { + return account.DigestEmailAddress, nil + } + emails, _, err := githubClient.Users.ListEmails(nil) + if err != nil { + return "", err + } + // Prefer the primary, verified email + for _, email := range emails { + if email.Primary != nil && *email.Primary && + email.Verified != nil && *email.Verified { + return *email.Email, nil + } + } + // Then the first verified email + for _, email := range emails { + if email.Verified != nil && *email.Verified { + return *email.Email, nil + } + } + // Then just the first email + for _, email := range emails { + return *email.Email, nil + } + return "", errors.New("No email addresses found in GitHub account") +} diff --git a/app/githop.go b/app/githop.go index f14fbaa..760db5a 100644 --- a/app/githop.go +++ b/app/githop.go @@ -3,7 +3,6 @@ package githop import ( "bytes" "encoding/json" - "errors" "fmt" "html/template" "io/ioutil" @@ -293,25 +292,14 @@ func sendDigestForAccount(account *Account, c appengine.Context) (bool, error) { return false, err } - emails, _, err := githubClient.Users.ListEmails(nil) + emailAddress, err := account.GetDigestEmailAddress(githubClient) if err != nil { return false, err } - 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 { - return false, errors.New("No verified email addresses found in GitHub account") - } digestMessage := &mail.Message{ Sender: "GitHop ", - To: []string{*primaryVerified}, + To: []string{emailAddress}, Subject: "GitHop Digest", HTMLBody: digestHtml.String(), } @@ -341,7 +329,7 @@ func githubOAuthCallbackHandler(w http.ResponseWriter, r *http.Request) { GitHubUserId: *user.ID, OAuthToken: *token, } - err = account.put(c) + err = account.Put(c) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -380,11 +368,28 @@ func settingsHandler(w http.ResponseWriter, r *http.Request) { return } + emails, _, err := githubClient.Users.ListEmails(nil) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + emailAddresses := make([]string, len(emails)) + for i := range emails { + emailAddresses[i] = *emails[i].Email + } + accountEmailAddress, err := account.GetDigestEmailAddress(githubClient) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + var data = map[string]interface{}{ - "Account": account, - "User": user, - "Timezones": timezones, - "Repos": repos, + "Account": account, + "User": user, + "Timezones": timezones, + "Repos": repos, + "EmailAddresses": emailAddresses, + "AccountEmailAddress": accountEmailAddress, } if err := templates["settings"].Execute(w, data); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -433,7 +438,9 @@ func saveSettingsHandler(w http.ResponseWriter, r *http.Request) { } } - err = account.put(c) + account.DigestEmailAddress = r.FormValue("email_address") + + err = account.Put(c) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/app/static/main.css b/app/static/main.css index ea29fdd..f181443 100644 --- a/app/static/main.css +++ b/app/static/main.css @@ -12,6 +12,15 @@ a { display: inline; } +.setting { + margin: 1em 0; +} + +.setting .explanation { + color: #999; + margin: 0; +} + .repos h2 { font-size: 32px; font-weight: bold; diff --git a/app/templates/settings.html b/app/templates/settings.html index 301ffe9..0374213 100644 --- a/app/templates/settings.html +++ b/app/templates/settings.html @@ -15,21 +15,39 @@
-

+

-

+
+ +
+ +
+ Where your digest will be sent to. +
+

You have {{len .Repos.AllRepos}} repositories. Select which you would like to include in your digest: