diff --git a/app/account.go b/app/account.go index 87c9a99..85d872c 100644 --- a/app/account.go +++ b/app/account.go @@ -3,6 +3,7 @@ package githop import ( "bytes" "encoding/gob" + "time" "appengine" "appengine/datastore" @@ -15,7 +16,9 @@ type Account struct { // The datastore API doesn't store maps, and the token contains one. We // thefore store a gob-serialized version instead. OAuthTokenSerialized []byte - OAuthToken oauth.Token `datastore:"-,"` + OAuthToken oauth.Token `datastore:"-,"` + TimezoneName string `datastore:",noindex"` + TimezoneLocation *time.Location `datastore:"-,"` } func getAccount(c appengine.Context, gitHubUserId int) (*Account, error) { @@ -25,9 +28,22 @@ func getAccount(c appengine.Context, gitHubUserId int) (*Account, error) { if err != nil { return nil, err } + r := bytes.NewBuffer(account.OAuthTokenSerialized) err = gob.NewDecoder(r).Decode(&account.OAuthToken) - return account, err + if err != nil { + return nil, err + } + + if len(account.TimezoneName) == 0 { + account.TimezoneName = "America/Los_Angeles" + } + account.TimezoneLocation, err = time.LoadLocation(account.TimezoneName) + if err != nil { + return nil, err + } + + return account, nil } func getAllAccounts(c appengine.Context, accounts *[]Account) error { diff --git a/app/digest.go b/app/digest.go index c7e3d56..7dfdd50 100644 --- a/app/digest.go +++ b/app/digest.go @@ -14,11 +14,11 @@ type DigestCommit struct { URL string Title string Message string - Date *time.Time + Date time.Time RepositoryCommit *github.RepositoryCommit } -func newDigestCommit(commit *github.RepositoryCommit, repo *github.Repository) DigestCommit { +func newDigestCommit(commit *github.RepositoryCommit, repo *github.Repository, location *time.Location) DigestCommit { messagePieces := strings.SplitN(*commit.Commit.Message, "\n", 2) title := messagePieces[0] message := "" @@ -30,11 +30,15 @@ func newDigestCommit(commit *github.RepositoryCommit, repo *github.Repository) D URL: fmt.Sprintf("https://github.com/%s/commit/%s", *repo.FullName, *commit.SHA), Title: title, Message: message, - Date: commit.Commit.Author.Date, + Date: commit.Commit.Author.Date.In(location), RepositoryCommit: commit, } } +func (commit DigestCommit) DisplayDate() string { + return commit.Date.Format("3:04pm") +} + type RepoDigest struct { Repo *github.Repository Commits []DigestCommit @@ -48,13 +52,14 @@ func (a ByRepoFullName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByRepoFullName) Less(i, j int) bool { return *a[i].Repo.FullName < *a[j].Repo.FullName } type Digest struct { - User *github.User - StartTime time.Time - EndTime time.Time - RepoDigests []*RepoDigest + User *github.User + StartTime time.Time + EndTime time.Time + TimezoneLocation *time.Location + RepoDigests []*RepoDigest } -func newDigest(githubClient *github.Client) (*Digest, error) { +func newDigest(githubClient *github.Client, account *Account) (*Digest, error) { user, _, err := githubClient.Users.Get("") if err != nil { return nil, err @@ -82,7 +87,7 @@ func newDigest(githubClient *github.Client) (*Digest, error) { repos = newRepos } - now := time.Now() + now := time.Now().In(account.TimezoneLocation) digestStartTime := time.Date(now.Year()-1, now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) digestEndTime := digestStartTime.AddDate(0, 0, 1) @@ -95,10 +100,11 @@ func newDigest(githubClient *github.Client) (*Digest, error) { } repos = digestRepos digest := &Digest{ - User: user, - RepoDigests: make([]*RepoDigest, 0, len(repos)), - StartTime: digestStartTime, - EndTime: digestEndTime, + User: user, + RepoDigests: make([]*RepoDigest, 0, len(repos)), + StartTime: digestStartTime, + EndTime: digestEndTime, + TimezoneLocation: account.TimezoneLocation, } err = digest.fetch(repos, githubClient) return digest, err @@ -125,7 +131,7 @@ func (digest *Digest) fetch(repos []github.Repository, githubClient *github.Clie } else { digestCommits := make([]DigestCommit, 0, len(commits)) for i, _ := range commits { - digestCommits = append(digestCommits, newDigestCommit(&commits[i], &repo)) + digestCommits = append(digestCommits, newDigestCommit(&commits[i], &repo, digest.TimezoneLocation)) } ch <- &RepoDigestResponse{&RepoDigest{&repo, digestCommits}, nil} } @@ -145,3 +151,7 @@ func (digest *Digest) fetch(repos []github.Repository, githubClient *github.Clie sort.Sort(ByRepoFullName(digest.RepoDigests)) return nil } + +func (digest *Digest) DisplayDate() string { + return digest.StartTime.Format("January 2, 2006 was a Monday") +} diff --git a/app/githop.go b/app/githop.go index 9dbd6ef..0995dbd 100644 --- a/app/githop.go +++ b/app/githop.go @@ -102,7 +102,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { oauthTransport.Token = &account.OAuthToken githubClient := github.NewClient(oauthTransport.Client()) - digest, err := newDigest(githubClient) + digest, err := newDigest(githubClient, account) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } @@ -159,7 +159,7 @@ func sendDigestForAccount(account *Account, c appengine.Context) error { oauthTransport.Token = &account.OAuthToken githubClient := github.NewClient(oauthTransport.Client()) - digest, err := newDigest(githubClient) + digest, err := newDigest(githubClient, account) if err != nil { return err } diff --git a/app/templates/digest.html b/app/templates/digest.html index 2baac84..5947d41 100644 --- a/app/templates/digest.html +++ b/app/templates/digest.html @@ -2,7 +2,7 @@
-

Activity a year ago for {{.User.Login}}.

+

{{.DisplayDate}}. Here's the GitHub activity on that day for {{.User.Login}}.

{{range $index, $repoDigest := .RepoDigests}}

@@ -20,7 +20,7 @@ {{end}}
{{.DisplaySHA}} - {{.Date}} + {{.DisplayDate}}
{{end}}