Beispiel #1
0
// RemoveFenceHandler DELETE /fences/{fenceId}
func RemoveFenceHandler(w http.ResponseWriter, r *http.Request) {
	user, err := auth.ValidateSession(r)

	if err != nil {
		http.Error(w, "Invalid Session token. "+err.Error(), http.StatusUnauthorized)
		return
	}

	vars := mux.Vars(r)

	fenceID, err := strconv.ParseUint(vars["fenceId"], 10, 8)
	if err != nil {
		http.Error(w, "Invalid Fence ID. "+err.Error(), http.StatusBadRequest)
		return
	}

	fence, err, notFound := models.FindFenceByID(fenceID)

	if notFound {
		http.Error(w, "GeoFence Not Found.", http.StatusNotFound)
		return
	}

	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	if fence.UserID != user.ID {
		http.Error(w, "Unauthorized User.", http.StatusUnauthorized)
		return
	}

	err = search.DeleteGeoFence(int(fence.ID))

	if err != nil {
		log.Errorf("Error while deleting Fence from Search: %s", err.Error())
	}

	err = fence.Delete()

	if err != nil {
		InternalServerError(err, w)
		return
	}

	jobs.QueueNotifyUsersSyncRequest(fence.Lat, fence.Lon)

	fmt.Fprintf(w, "{}")
}
// FenceExpireJob executes a FenceExpireJob.
func FenceExpireJob(j *que.Job) error {
	var fer FenceExpireRequest
	err := json.Unmarshal(j.Args, &fer)
	if err != nil {
		log.Error("Unable to unmarshal job arguments into FenceExpireRequest")
		return err
	}

	log.Info("Processing FenceExpireJob")

	fence, err, notFound := models.FindFenceByID(fer.FenceID)

	if notFound {
		return nil
	}

	if err != nil {
		log.Errorf("Fence Finiding error: %v", err)
		return err
	}

	err = activity.AddFenceExpiredActivity(fence.User.ID, fence.ID, fence.Name)
	if err != nil {
		log.Errorf("Activity creation error: %v", err)
	}

	QueueSendGcmRequest(gcm.NewMessage(map[string]interface{}{"type": "onFenceExpired", "fenceId": fence.ID, "fenceName": fence.Name}, fence.User.GCMNotificationID))

	err = search.DeleteGeoFence(int(fence.ID))

	if err != nil {
		return err
	}

	err = fence.Delete()

	if err != nil {
		return err
	}

	QueueNotifyUsersSyncRequest(fence.Lat, fence.Lon)

	return nil
}