コード例 #1
0
ファイル: pipeline.go プロジェクト: pombredanne/dsocial.go
func (p *Pipeline) findMatchingDsocialContact(ds DataStoreService, dsocialUserId string, contact *Contact) (extDsocialContact *dm.Contact, isSame bool, err os.Error) {
	emptyContact := new(dm.Contact)
	if contact.DsocialContactId != "" {
		extDsocialContact, _, _ = ds.RetrieveDsocialContact(dsocialUserId, contact.DsocialContactId)
		if extDsocialContact != nil {
			_, isSame = emptyContact.IsSimilarOrUpdated(extDsocialContact, contact.Value)
			fmt.Printf("[PIPELINE]: findMatchingDsocialContact for %s with based on existing contact id will use %s and isSame %v\n", contact.Value.DisplayName, extDsocialContact.DisplayName, isSame)
		}
	}
	if extDsocialContact == nil {
		// this is a new contact from an existing service
		potentialMatches, err := ds.SearchForDsocialContacts(dsocialUserId, contact.Value)
		if err != nil {
			return nil, false, err
		}
		for _, potentialMatch := range potentialMatches {
			var isSimilar bool
			if isSimilar, isSame = emptyContact.IsSimilarOrUpdated(potentialMatch, contact.Value); isSimilar {
				extDsocialContact = potentialMatch
				break
			}
		}
		if extDsocialContact != nil {
			fmt.Printf("[PIPELINE]: findMatchingDsocialContact for %s was %s and isSame %v\n\tStoring mapping: %s/%s/%s -> %s\n", contact.Value.DisplayName, extDsocialContact.DisplayName, isSame, contact.ExternalServiceId, contact.ExternalUserId, contact.ExternalContactId, extDsocialContact.Id)
			_, _, err = ds.StoreDsocialExternalContactMapping(contact.ExternalServiceId, contact.ExternalUserId, contact.ExternalContactId, dsocialUserId, extDsocialContact.Id)
			contact.DsocialContactId = extDsocialContact.Id
		} else {
			fmt.Printf("[PIPELINE]: findMatchingDsocialContact cannot find similar for %s\n", contact.Value.DisplayName)
		}
	}
	return extDsocialContact, isSame, err
}
コード例 #2
0
func (p *InMemoryDataStore) SearchForDsocialContacts(dsocialUserId string, contact *dm.Contact) (contacts []*dm.Contact, err os.Error) {
	if contact == nil {
		return make([]*dm.Contact, 0), nil
	}
	collection := p.retrieveContactCollection()
	l := list.New()
	for _, v := range collection.Data {
		if c, ok := v.(*dm.Contact); ok && c != nil {
			if isSimilar, _ := contact.IsSimilarOrUpdated(contact, c); isSimilar {
				c2 := new(dm.Contact)
				*c2 = *c
				l.PushBack(c2)
			}
		}
	}
	rc := make([]*dm.Contact, l.Len())
	for i, iter := 0, l.Front(); iter != nil; i, iter = i+1, iter.Next() {
		if iter.Value != nil {
			rc[i] = iter.Value.(*dm.Contact)
		}
	}
	return rc, nil
}
コード例 #3
0
ファイル: pipeline.go プロジェクト: pombredanne/dsocial.go
func (p *Pipeline) contactImport(cs ContactsService, ds DataStoreService, dsocialUserId string, contact *Contact, allowAdd, allowDelete, allowUpdate bool) (*dm.Contact, string, os.Error) {
	emptyContact := new(dm.Contact)
	if contact == nil || contact.Value == nil {
		return nil, "", nil
	}
	fmt.Printf("[PIPELINE]: Importing contact with ExternalServiceId = %v, ExternalUserId = %v, ExternalContactId = %v, DsocialUserId = %v, DsocialContactId = %v\n", contact.ExternalServiceId, contact.ExternalUserId, contact.ExternalContactId, contact.DsocialUserId, contact.DsocialContactId)
	extDsocialContact, _, err := ds.RetrieveDsocialContactForExternalContact(contact.ExternalServiceId, contact.ExternalUserId, contact.ExternalContactId, dsocialUserId)
	if err != nil {
		return nil, "", err
	}
	var matchingContact *dm.Contact
	var isSame bool
	if extDsocialContact == nil {
		// We don't have a mapping for this external contact to an internal contact mapping
		// meaning we've never imported this contact previously from THIS service, but we may
		// already have the contact in our system, so let's see if we can find it
		matchingContact, isSame, err = p.findMatchingDsocialContact(ds, dsocialUserId, contact)
		if err != nil {
			return matchingContact, "", err
		}
		if matchingContact != nil {
			contact.DsocialContactId = matchingContact.Id
			extContact := cs.ConvertToExternalContact(matchingContact, nil, dsocialUserId)
			ds.StoreExternalContact(contact.ExternalServiceId, contact.ExternalUserId, dsocialUserId, contact.ExternalContactId, extContact)
			extDsocialContact = cs.ConvertToDsocialContact(extContact, matchingContact, dsocialUserId)
			if extDsocialContact != nil {
				AddIdsForDsocialContact(extDsocialContact, ds, dsocialUserId)
				//contact.ExternalContactId = extDsocialContact.Id
				extDsocialContact, err = ds.StoreDsocialContactForExternalContact(contact.ExternalServiceId, contact.ExternalUserId, contact.ExternalContactId, dsocialUserId, extDsocialContact)
				if extDsocialContact == nil || err != nil {
					return matchingContact, "", err
				}
				if contact.DsocialContactId != "" {
					if _, _, err = ds.StoreDsocialExternalContactMapping(contact.ExternalServiceId, contact.ExternalUserId, contact.ExternalContactId, dsocialUserId, contact.DsocialContactId); err != nil {
						return matchingContact, "", err
					}
				}
			}
		}
	} else {
		// we have a mapping for this external contact to an internal contact mapping
		// from THIS service, therefore let's use it
		if contact.DsocialContactId == "" {
			contact.DsocialContactId, err = ds.DsocialIdForExternalContactId(contact.ExternalServiceId, contact.ExternalUserId, dsocialUserId, contact.ExternalContactId)
			if err != nil {
				return nil, "", err
			}
		}
		if contact.DsocialContactId != "" {
			matchingContact, _, err = ds.RetrieveDsocialContact(dsocialUserId, contact.DsocialContactId)
			if err != nil {
				return nil, "", err
			}
		}
	}
	// ensure we have a contact Id
	if contact.DsocialContactId == "" {
		if matchingContact != nil {
			contact.DsocialContactId = matchingContact.Id
			fmt.Printf("[PIPELINE]: Will be using matchingContact Id: %v\n", matchingContact.Id)
		}
		if contact.DsocialContactId == "" {
			newContact := &dm.Contact{UserId: dsocialUserId}
			AddIdsForDsocialContact(newContact, ds, dsocialUserId)
			thecontact, err := ds.StoreDsocialContact(dsocialUserId, newContact.Id, newContact)
			if err != nil {
				return nil, "", err
			}
			contact.DsocialContactId = thecontact.Id
		}
	}
	if _, isSame = emptyContact.IsSimilarOrUpdated(extDsocialContact, contact.Value); isSame {
		return matchingContact, "", nil
	}
	l := new(list.List)
	emptyContact.GenerateChanges(extDsocialContact, contact.Value, nil, l)
	l = p.removeUnacceptedChanges(l, allowAdd, allowDelete, allowUpdate)
	changes := make([]*dm.Change, l.Len())
	for i, iter := 0, l.Front(); iter != nil; i, iter = i+1, iter.Next() {
		changes[i] = iter.Value.(*dm.Change)
	}
	changeset := &dm.ChangeSet{
		CreatedAt:      time.UTC().Format(dm.UTC_DATETIME_FORMAT),
		ChangedBy:      contact.ExternalServiceId,
		ChangeImportId: contact.ExternalContactId,
		RecordId:       contact.DsocialContactId,
		Changes:        changes,
	}
	_, err = ds.StoreContactChangeSet(dsocialUserId, changeset)
	if err != nil {
		return matchingContact, changeset.Id, err
	}
	if extDsocialContact == nil {
		fmt.Printf("[PIPELINE]: OriginalExternalContact is nil and contact.DsocialContactId is %v and contact.Value.Id was %v\n", contact.DsocialContactId, contact.Value.Id)
		contact.Value.Id = contact.DsocialContactId
		AddIdsForDsocialContact(contact.Value, ds, dsocialUserId)
		contact.Value, err = ds.StoreDsocialContact(dsocialUserId, contact.DsocialContactId, contact.Value)
		fmt.Printf("[PIPELINE]: After storing contact.Value, contact.Value.Id is %v\n", contact.Value.Id)
		if err != nil {
			return matchingContact, changeset.Id, err
		}
		storedDsocialContact, err := ds.StoreDsocialContactForExternalContact(contact.ExternalServiceId, contact.ExternalUserId, contact.ExternalContactId, dsocialUserId, contact.Value)
		fmt.Printf("[PIPELINE]: After storing external contact, contact.Value.Id is %v\n", contact.Value.Id)
		_, _, err2 := ds.StoreDsocialExternalContactMapping(contact.ExternalServiceId, contact.ExternalUserId, contact.ExternalContactId, dsocialUserId, contact.DsocialContactId)
		if err == nil {
			err = err2
		}
		return storedDsocialContact, changeset.Id, err
	}
	var storedDsocialContact *dm.Contact = nil
	if contact.DsocialContactId != "" {
		if storedDsocialContact, _, err = ds.RetrieveDsocialContact(dsocialUserId, contact.DsocialContactId); err != nil {
			return matchingContact, changeset.Id, err
		}
	}
	if storedDsocialContact == nil {
		storedDsocialContact = new(dm.Contact)
	}
	for j, iter := 0, l.Front(); iter != nil; j, iter = j+1, iter.Next() {
		change := iter.Value.(*dm.Change)
		dm.ApplyChange(storedDsocialContact, change)
		changes[j] = change
	}
	AddIdsForDsocialContact(storedDsocialContact, ds, dsocialUserId)
	_, err = ds.StoreDsocialContact(dsocialUserId, contact.DsocialContactId, storedDsocialContact)
	return storedDsocialContact, changeset.Id, err
}