From b9b79b799497e270b2831fddc41d0e10cd7645eb Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Fri, 11 Jul 2014 13:45:32 -0700 Subject: [PATCH] added flag to indicate account is currently syncing --- server/database/schema/schema.go | 1 + server/database/testdata/testdata.go | 8 ++++---- server/handler/login.go | 30 +++++++++++++++++----------- shared/model/user.go | 1 + 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/server/database/schema/schema.go b/server/database/schema/schema.go index baa5554c..934d2af6 100644 --- a/server/database/schema/schema.go +++ b/server/database/schema/schema.go @@ -19,6 +19,7 @@ var stmts = []string{` ,user_token VARCHAR(255) ,user_admin BOOLEAN ,user_active BOOLEAN + ,user_syncing BOOLEAN ,user_created INTEGER ,user_updated INTEGER ,user_synced INTEGER diff --git a/server/database/testdata/testdata.go b/server/database/testdata/testdata.go index e5fba6bf..174e100d 100644 --- a/server/database/testdata/testdata.go +++ b/server/database/testdata/testdata.go @@ -7,10 +7,10 @@ import ( var stmts = []string{ // insert user entries - "insert into users values (1, 'github.com', 'smellypooper', 'f0b461ca586c27872b43a0685cbc2847', '976f22a5eef7caacb7e678d6c52f49b1', 'Dr. Cooper', 'drcooper@caltech.edu', 'b9015b0857e16ac4d94a0ffd9a0b79c8', 'e42080dddf012c718e476da161d21ad5', 1, 1, 1398065343, 1398065344, 1398065345);", - "insert into users values (2, 'github.com', 'lhofstadter', 'e4105c3059ac4c466594932dc9a4ffb2', '2257216903d9cd0d3d24772132febf52', 'Dr. Hofstadter', 'leanard@caltech.edu', '23dde632fdece6880f4ff03bb20f05d7', 'a5ad0d75f317f0b0a5dfdb68e5a3079e', 1, 1, 1398065343, 1398065344, 1398065345);", - "insert into users values (3, 'gitlab.com', 'browndynamite', '4821477cc26a0c8c80c6c9b568d98e32', '1dd52c37cf5c63fe5abfd047b5b74a31', 'Dr. Koothrappali', 'rajesh@caltech.edu', 'f9133051f480b7ea88848b9f0a079dae', '7a50ede04637d4a8fce532c7d511226b', 1, 1, 1398065343, 1398065344, 1398065345);", - "insert into users values (4, 'github.com', 'mrwolowitz', '1f6a80bde960e6913bf9b7e61eadd068', '74c40472494ba7f9f6c3ae061ff799ed', 'Mr. Wolowitz', 'wolowitz@caltech.edu', 'ea250570c794d84dc583421bb717be82', '3bd7e7d7411b2978e45919c9ad419984', 1, 1, 1398065343, 1398065344, 1398065345);", + "insert into users values (1, 'github.com', 'smellypooper', 'f0b461ca586c27872b43a0685cbc2847', '976f22a5eef7caacb7e678d6c52f49b1', 'Dr. Cooper', 'drcooper@caltech.edu', 'b9015b0857e16ac4d94a0ffd9a0b79c8', 'e42080dddf012c718e476da161d21ad5', 1, 1, 0, 1398065343, 1398065344, 1398065345);", + "insert into users values (2, 'github.com', 'lhofstadter', 'e4105c3059ac4c466594932dc9a4ffb2', '2257216903d9cd0d3d24772132febf52', 'Dr. Hofstadter', 'leanard@caltech.edu', '23dde632fdece6880f4ff03bb20f05d7', 'a5ad0d75f317f0b0a5dfdb68e5a3079e', 1, 1, 0, 1398065343, 1398065344, 1398065345);", + "insert into users values (3, 'gitlab.com', 'browndynamite', '4821477cc26a0c8c80c6c9b568d98e32', '1dd52c37cf5c63fe5abfd047b5b74a31', 'Dr. Koothrappali', 'rajesh@caltech.edu', 'f9133051f480b7ea88848b9f0a079dae', '7a50ede04637d4a8fce532c7d511226b', 1, 1, 0, 1398065343, 1398065344, 1398065345);", + "insert into users values (4, 'github.com', 'mrwolowitz', '1f6a80bde960e6913bf9b7e61eadd068', '74c40472494ba7f9f6c3ae061ff799ed', 'Mr. Wolowitz', 'wolowitz@caltech.edu', 'ea250570c794d84dc583421bb717be82', '3bd7e7d7411b2978e45919c9ad419984', 1, 1, 0, 1398065343, 1398065344, 1398065345);", // insert repository entries "insert into repos values (1, 0, 'github.com', 'github.com', 'lhofstadter', 'lenwoloppali', '', 'git://github.com/lhofstadter/lenwoloppali.git', '', '', 1, 1, 1, 1, 1, 'publickey', 'privatekey', 'params', 900, 1398065343, 1398065344);", diff --git a/server/handler/login.go b/server/handler/login.go index c02657e6..92c45aa5 100644 --- a/server/handler/login.go +++ b/server/handler/login.go @@ -78,6 +78,7 @@ func (h *LoginHandler) GetLogin(w http.ResponseWriter, r *http.Request) error { u.Secret = login.Secret u.Name = login.Name u.SetEmail(login.Email) + u.Syncing = u.IsStale() if err := h.users.Update(u); err != nil { return badRequest{err} } @@ -85,23 +86,21 @@ func (h *LoginHandler) GetLogin(w http.ResponseWriter, r *http.Request) error { // look at the last synchronized date to determine if // we need to re-sync the account. // - // TODO this should move to a server/sync package and - // should be injected into this struct, just like - // the database code. - if u.IsStale() { + // todo(bradrydzewski) this should move to a server/sync package and + // should be injected into this struct, just like the database code. + // + // todo(bradrydzewski) this login should be a bit more intelligent + // than the current implementation. + // + // todo(bradrydzewski) the github implementation will only sync a + // maximum of 100 repositories due to the api pagination. need to fix. + if u.Syncing { redirect = "/sync" log.Println("sync user account.", u.Login) // sync inside a goroutine. This should eventually be moved to // its own package / sync utility. go func() { - // mark as synced - u.Synced = time.Now().Unix() - if err := h.users.Update(u); err != nil { - log.Println("Error syncing user account, updating sync date", u.Login, err) - return - } - // list all repositories client := remote.GetClient(u.Access, u.Secret) repos, err := client.GetRepos("") @@ -132,6 +131,13 @@ func (h *LoginHandler) GetLogin(w http.ResponseWriter, r *http.Request) error { } log.Println("Successfully syced repo.", u.Login+"/"+remoteRepo.Name) + + u.Synced = time.Now().Unix() + u.Syncing = false + if err := h.users.Update(u); err != nil { + log.Println("Error syncing user account, updating sync date", u.Login, err) + return + } } }() } @@ -148,7 +154,7 @@ func (h *LoginHandler) GetLogin(w http.ResponseWriter, r *http.Request) error { // GET /logout func (h *LoginHandler) GetLogout(w http.ResponseWriter, r *http.Request) error { h.sess.Clear(w, r) - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, "/login", http.StatusSeeOther) return nil } diff --git a/shared/model/user.go b/shared/model/user.go index 1a4b3af4..c030ea62 100644 --- a/shared/model/user.go +++ b/shared/model/user.go @@ -16,6 +16,7 @@ type User struct { Token string `meddler:"user_token" json:"-"` Admin bool `meddler:"user_admin" json:"admin"` Active bool `meddler:"user_active" json:"active"` + Syncing bool `meddler:"user_syncing" json:"syncing"` Created int64 `meddler:"user_created" json:"created_at"` Updated int64 `meddler:"user_updated" json:"updated_at"` Synced int64 `meddler:"user_synced" json:"synced_at"`