예제 #1
0
func GetIdentity(network, id, username string) (*models.Identity, error) {
	network = strings.ToLower(network)
	username = strings.ToLower(username)
	id = strings.ToLower(id) // ID is not necessarily an integer

	if strings.HasPrefix(username, "@") {
		username = username[1:]
	}

	var ok bool
	if _, ok = Networks[network]; !ok {
		return nil, ErrUnknownNet
	}
	var session, err = mgo.Dial(MongoDSN)
	if err != nil {
		return nil, err
	}
	defer session.Close()
	var identities = session.DB("identities").C(network)
	var one = new(models.Identity)
	var two models.Identity
	var selector bson.M
	if id != "" {
		selector = bson.M{
			"network": network,
			"id":      id,
		}
	} else {
		selector = bson.M{
			"network":  network,
			"username": username,
		}
	}

	err = identities.Find(selector).One(&two)
	one = &two

	if err != nil && err.Error() != "not found" {
		return nil, err
	}

	if err != nil && err.Error() == "not found" {
		one, err = getFrom[network](id, username)
		if err != nil {
			return nil, err
		}
		if err = addTo[network](one, identities); err != nil {
			log.Printf("Failed to obtain profile: %s", err)
			return nil, errors.New("Failed to obtain profile")
		}
		one.Status = "new"
	}
	return one, nil
}
예제 #2
0
func addToTwitter(item *models.Identity, c *mgo.Collection) error {
	if item == nil {
		return errors.New("item is nil.")
	}
	item.Status = "cached"
	var err error
	if err = ensureIdentitySchema(c); err != nil {
		return err
	}
	return c.Insert(item)
}