Ejemplo n.º 1
0
func findTriggerForApplicationIdAndEvent(application_id_bson bson.ObjectId, event string) Trigger {
	application_id := application_id_bson.Hex()

	key := application_id + ".triggers." + event
	result := Trigger{}

	cache_result, err := cache.Get(key)

	if err {
		query := bson.M{"event": event, "type": "trigger"}

		collection := application_id
		collection += "_triggers"

		database.FindBy(collection, &result, query)

		b, _ := json.Marshal(result)
		cache.Set(key, string(b), 5*time.Minute)
	} else if cache_result.CachedItem == nil {
		json.Unmarshal([]byte(cache_result.Value.(string)), &result)
		cache_result.CachedItem = result
	} else {
		result = cache_result.CachedItem.(Trigger)
	}
	return result
}
Ejemplo n.º 2
0
func (application Application) FindEndpointByName(name string) Endpoint {
	result := Endpoint{}

	key := application.Id.Hex() + ".endpoints." + name
	cache_result, err := cache.Get(key)

	if err {
		query := bson.M{"name": name}
		database.FindBy(application.Id.Hex()+"_endpoints", &result, query)

		b, _ := json.Marshal(result)
		cache.Set(key, string(b), 5*time.Minute)

		// Set a second cache item for the endpoint id (This stores reference map data)
		key_id := application.Id.Hex() + ".endpoints." + result.Id.Hex()
		cache.Set(key_id, string(b), 5*time.Minute)
	} else if cache_result.CachedItem == nil {
		json.Unmarshal([]byte(cache_result.Value.(string)), &result)
		cache_result.CachedItem = result
	} else {
		result = cache_result.CachedItem.(Endpoint)
	}

	return result
}
Ejemplo n.º 3
0
func (application Application) FindGroupById(id bson.ObjectId) (Group, bool) {
	result := Group{}
	query := bson.M{"_id": bson.ObjectId(id)}

	database.FindBy(application.Id.Hex()+"_groups", &result, query)

	return result, result.Id == bson.ObjectId("")
}
Ejemplo n.º 4
0
func (session *Session) GetUser() User {
	result := User{}

	query := bson.M{"_id": bson.ObjectId(session.UserId)}
	database.FindBy(session.ApplicationId.Hex()+"_users", &result, query)

	return result
}
Ejemplo n.º 5
0
func (application Application) FindUserByFacebookId(facebook_id string) User {
	result := User{}
	query := bson.M{"facebook_id": facebook_id}

	database.FindBy(application.Id.Hex()+"_users", &result, query)

	return result
}
Ejemplo n.º 6
0
func (application Application) FindUserById(id bson.ObjectId) User {
	result := User{}
	query := bson.M{"_id": bson.ObjectId(id)}

	database.FindBy(application.Id.Hex()+"_users", &result, query)

	return result
}
Ejemplo n.º 7
0
func (application Application) FindUserByTransferId(transfer_id string) User {
	result := User{}

	if transfer_id != "" {
		query := bson.M{"transfer_id": transfer_id}
		database.FindBy(application.Id.Hex()+"_users", &result, query)
	}

	return result
}
Ejemplo n.º 8
0
func (application Application) FindUserByEmail(email string) User {
	result := User{}

	email = strings.ToLower(email)

	query := bson.M{"email": email}

	database.FindBy(application.Id.Hex()+"_users", &result, query)

	return result
}
Ejemplo n.º 9
0
func (record *Record) FindFileById(id bson.ObjectId) File {
	result := File{}

	query := bson.M{"_id": id}

	collection := record.ApplicationId.Hex()
	collection += "_" + record.EndpointId.Hex()
	collection += "_files"

	database.FindBy(collection, &result, query)

	return result
}
Ejemplo n.º 10
0
func (application Application) GetSuperUser() User {
	result := User{}
	query := bson.M{"super_user": true}

	database.FindBy(application.Id.Hex()+"_users", &result, query)

	if result.Id == bson.ObjectId("") {
		result = User{}
		result.SuperUser = true
		result.ApplicationId = bson.ObjectId(application.Id)
		result.Save()
	}

	return result
}
Ejemplo n.º 11
0
func (application Application) FindSessionById(id bson.ObjectId) (Session, bool) {
	result := Session{}

	key := application.Id.Hex() + ".sessions." + id.Hex()
	cache_result, err := cache.Get(key)

	if err {
		query := bson.M{"_id": bson.ObjectId(id)}
		database.FindBy(application.Id.Hex()+"_sessions", &result, query)

		b, _ := json.Marshal(result)
		cache.Set(key, string(b), 5*time.Minute)
	} else if cache_result.CachedItem == nil {
		json.Unmarshal([]byte(cache_result.Value.(string)), &result)
		cache_result.CachedItem = result
	} else {
		result = cache_result.CachedItem.(Session)
	}

	return result, result.Id == bson.ObjectId("")
}
Ejemplo n.º 12
0
func (user *User) NewTriggerSession() {
	session := Session{}

	expire := make(map[string]int64)
	expire["$gt"] = int64(time.Now().Unix())

	query := bson.M{"expires_at": expire, "trigger_session": true, "user_id": bson.ObjectId(user.Id)}

	database.FindBy(user.ApplicationId.Hex()+"_sessions", &session, query)

	if session.Id == bson.ObjectId("") {
		session.UserId = user.Id
		session.ApplicationId = user.ApplicationId
		session.ExpiresAt = int64(time.Now().Unix()) + 3600
		session.TriggerSession = true
		session.GenerateToken()
		session.Save()
	}

	user.SessionId = session.Id.Hex()
	user.AuthToken = session.AuthToken
}