示例#1
0
// GCMAddHandler POST /users/me/gcm
func GCMAddHandler(w http.ResponseWriter, r *http.Request) {
	user, err := auth.ValidateSession(r)

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

	decoder := json.NewDecoder(r.Body)
	var b gcmID
	err = decoder.Decode(&b)

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

	if user.GCMNotificationID != "" {
		err = gcm.AddDeviceToGroup(b.GCMID, user)

		if err != nil {
			err = gcm.CreateDeviceGroup(b.GCMID, user)

			if err != nil {
				InternalServerError(err, w)
				return
			}
		}
	} else {
		err = gcm.CreateDeviceGroup(b.GCMID, user)

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

	err = jobs.QueueSendGcmRequest(gcm.NewMessage(map[string]interface{}{"type": "sync"}, user.GCMNotificationID))

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

	bytes, err := json.Marshal(gcmNotificationKeyResponse{NotificationKey: user.GCMNotificationID})

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

	w.Write(bytes)
}
// 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
}
// NotifyUsersSyncJob executes a NotifyUsersSyncJob.
func NotifyUsersSyncJob(j *que.Job) error {
	var r NotifyUsersSyncRequest
	err := json.Unmarshal(j.Args, &r)
	if err != nil {
		log.Errorf("Unable to unmarshal job arguments into NotifyUsersSyncRequest")
		return err
	}

	log.Info("Processing NotifyUsersSyncJob")

	users, err := models.FindUsersByLastKnownGeoHash(r.GeoHash)
	if err != nil {
		log.Errorf("Unable to Find Users")
		return err
	}

	for _, user := range users {
		QueueSendGcmRequest(gcm.NewMessage(map[string]interface{}{"type": "sync"}, user.GCMNotificationID))
	}

	return nil
}
示例#4
0
// VisitFenceHandler handles POST /fences/{fenceId}/visit
func VisitFenceHandler(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.StatusUnauthorized)
		return
	}

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

	if err != nil || notFound {
		http.Error(w, "Fence not Found", http.StatusNotFound)
		return
	}

	rent := scores.GetGeoFenceRent(fence)

	if user.Balance < rent {
		rent = user.Balance
	}

	// GCM
	err = jobs.QueueSendGcmRequest(gcm.NewMessage(
		map[string]interface{}{"type": "onForeignFenceEntered", "fenceId": fence.ID, "fenceName": fence.Name, "ownerName": fence.User.Name}, user.GCMNotificationID))

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

	err = jobs.QueueSendGcmRequest(gcm.NewMessage(
		map[string]interface{}{"type": "onOwnFenceEntered", "fenceId": fence.ID, "fenceName": fence.Name, "visitorName": user.Name}, fence.User.GCMNotificationID))

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

	// Activity Stream
	err = activity.AddForeignVisitedActivity(user.ID, fence.User.Name, fence.User.ID, fence.Name, fence.ID, rent)

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

	err = activity.AddOwnFenceVisitedActivity(fence.User.ID, user.Name, user.ID, fence.Name, fence.ID, rent)

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

	// Balance Recording
	redis.AddBalanceRecord(redis.GetBalanceRecordName(user.ID, redis.BalanceNameExpenseRent), rent)
	redis.AddBalanceRecord(redis.GetBalanceRecordName(fence.User.ID, redis.BalanceNameEarningsRent), rent)

	// Score Calculation
	err = jobs.QueueRecordVisitRequest(fence.Lat, fence.Lon, time.Now()) //scores.RecordVisit(fence.Lat, fence.Lon)

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

	user.LastKnownGeoHash = geomodel.GeoCell(fence.Lat, fence.Lon, models.LastKnownGeoHashResolution)
	user.Balance = user.Balance - rent

	if user.Balance < 0 { // TODO: Decide what to do here.
		user.Balance = 0
	}

	user.ExpensesRentAllTime = user.ExpensesRentAllTime + rent

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

	fence.User.Balance = fence.User.Balance + rent
	fence.User.EarningsRentAllTime = user.ExpensesRentAllTime + rent

	err = fence.User.Save()

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

	fence.TotalEarnings = fence.TotalEarnings + rent
	fence.TotalVisitors = fence.TotalVisitors + 1

	err = fence.Save()

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

	w.Write([]byte("{}"))
}