Log notification payloads to debug logs.
[kraftakt.git] / kraftakt.go
index 976bbd4..f6b3d9c 100644 (file)
@@ -25,6 +25,7 @@ var delayedHandleNotifications = delay.Func("handleNotifications", handleNotific
 var templates *template.Template
 
 func init() {
+       http.Handle("/login", AuthenticatedHandler(loginHandler))
        http.Handle("/fitbit/connect", AuthenticatedHandler(fitbitConnectHandler))
        http.Handle("/fitbit/grant", AuthenticatedHandler(fitbitGrantHandler))
        http.Handle("/fitbit/disconnect", AuthenticatedHandler(fitbitDisconnectHandler))
@@ -125,8 +126,23 @@ func indexHandler(ctx context.Context, w http.ResponseWriter, _ *http.Request) e
        return templates.ExecuteTemplate(w, templateName, &templateData)
 }
 
-func fitbitConnectHandler(_ context.Context, w http.ResponseWriter, r *http.Request, _ *app.User) error {
-       http.Redirect(w, r, fitbit.AuthURL(), http.StatusTemporaryRedirect)
+func loginHandler(_ context.Context, w http.ResponseWriter, r *http.Request, _ *app.User) error {
+       // essentially a nop; all the heavy lifting (i.e. logging in) has been done by the AuthenticatedHandler wrapper.
+       redirectURL := r.URL
+       redirectURL.Path = "/"
+       redirectURL.RawQuery = ""
+       redirectURL.Fragment = ""
+       http.Redirect(w, r, redirectURL.String(), http.StatusTemporaryRedirect)
+       return nil
+}
+
+func fitbitConnectHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error {
+       c, err := fitbit.NewClient(ctx, "", u)
+       if err != nil {
+               return err
+       }
+
+       http.Redirect(w, r, c.AuthURL(ctx), http.StatusTemporaryRedirect)
        return nil
 }
 
@@ -161,8 +177,13 @@ func fitbitDisconnectHandler(ctx context.Context, w http.ResponseWriter, r *http
        }
 
        var errs appengine.MultiError
-       if err := c.Unsubscribe(ctx); err != nil {
-               errs = append(errs, fmt.Errorf("Unsubscribe() = %v", err))
+
+       for _, collection := range []string{"activities", "sleep"} {
+               if err := c.Unsubscribe(ctx, collection); err != nil {
+                       errs = append(errs, fmt.Errorf("Unsubscribe(%q) = %v", collection, err))
+                       continue
+               }
+               log.Infof(ctx, "Successfully unsubscribed from %q", collection)
        }
 
        if err := c.DeleteToken(ctx); err != nil {
@@ -180,8 +201,13 @@ func fitbitDisconnectHandler(ctx context.Context, w http.ResponseWriter, r *http
        return nil
 }
 
-func googleConnectHandler(_ context.Context, w http.ResponseWriter, r *http.Request, _ *app.User) error {
-       http.Redirect(w, r, gfit.AuthURL(), http.StatusTemporaryRedirect)
+func googleConnectHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error {
+       c, err := gfit.NewClient(ctx, u)
+       if err != nil {
+               return err
+       }
+
+       http.Redirect(w, r, c.AuthURL(ctx), http.StatusTemporaryRedirect)
        return nil
 }
 
@@ -261,6 +287,8 @@ func fitbitNotifyHandler(ctx context.Context, w http.ResponseWriter, r *http.Req
 // handleNotifications parses fitbit notifications and requests the individual
 // activities from Fitbit. It is executed asynchronously via the delay package.
 func handleNotifications(ctx context.Context, payload []byte) error {
+       log.Debugf(ctx, "NOTIFY -> %s", payload)
+
        if err := app.LoadConfig(ctx); err != nil {
                return err
        }