// syncTaskJob would do whatever syncing is necessary in the background
func syncTaskJob(j *que.Job) error {
	var synctask sync.SyncTask
	err := json.Unmarshal(j.Args, &synctask)
	if err != nil {
		log.WithField("args", string(j.Args)).Error("Unable to unmarshal job arguments into SyncTask")
		return err
	}

	log.WithField("SyncTask", synctask).Info("Processing Synctask!")

	stvClientImpl := stv.CreateStravaClient(synctask.StravaToken)
	rkClientImpl := rk.CreateRKClient(synctask.RunkeeperToken)
	_, _, err = synctask.Sync(stvClientImpl, rkClientImpl)
	if err != nil {
		log.WithField("args", string(j.Args)).WithField("QueId", j.ID).Error("Error while syncing synctask.")
		return err
	}

	j.Delete()
	j.Done()

	return nil
}
Exemple #2
0
func TokenDisassociate(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	token := vars["token"]
	log.Printf("Disassociating token %s", token)

	if token != "" {
		//validate this token against Strava
		stvClientImpl := stv.CreateStravaClient(token)
		rkClientImpl := rk.CreateRKClient(token)
		authInStrava := stvClientImpl.ValidateToken(token)
		authInRunkeeper := rkClientImpl.ValidateToken(token)

		if authInStrava {
			log.Printf("Token %s is valid for Strava", token)

			//remove from db
			db := sync.CreateSyncDbRepo(DbConnectionString)
			task, err := db.FindSyncTaskByToken(token)
			if err != nil {
				//return 5xx?
				w.WriteHeader(http.StatusInternalServerError)
				return
			}
			task.StravaToken = ""
			db.UpdateSyncTask(*task)
			log.Printf("Removed Strava token from task %d", task.Uid)

			//We should also revoke auth at Strava
			err = stvClientImpl.DeAuthorize(token)
			if err != nil {
				log.Printf("Error while deauthorizing at strava: %s", err)
			}

			//drop cookie
			log.Printf("Removing cookie..")
			cookie := &http.Cookie{Name: "strava", Value: "", MaxAge: -1} //MaxAge will remove the cookie
			cookie.Domain = "www.syncmysport.com"
			http.SetCookie(w, cookie)

			w.Write([]byte("OK")) //200 OK
			return                //hmm
		}
		if authInRunkeeper {
			log.Printf("Token %s is valid for Runkeeper", token)

			//remove from db
			db := sync.CreateSyncDbRepo(DbConnectionString)
			task, err := db.FindSyncTaskByToken(token)
			if err != nil {
				//return 5xx?
				w.WriteHeader(http.StatusInternalServerError)
				return
			}
			task.RunkeeperToken = ""
			db.UpdateSyncTask(*task)
			log.Printf("Removed Runkeeper token from task %d", task.Uid)

			//We should also revoke auth at Runkeeper
			err = rkClientImpl.DeAuthorize(token)
			if err != nil {
				log.Printf("Error while deauthorizing at runkeeper: %s", err)
			}

			w.Write([]byte("OK")) //200 OK
			return                //hmm
		} else {
			log.Printf("Token %s is already no longer valid for Strava or Runkeeper", token)
		}
	}
	w.Write([]byte("OK")) //200 OK
}