Пример #1
0
func (p *Pipeline) handleUpdateGroup(client oauth2_client.OAuth2Client, ds DataStoreService, cs ContactsService, dsocialUserId, externalServiceId, externalUserId, dsocialGroupId string, changeset *dm.ChangeSet) (err os.Error) {
	fmt.Printf("[PIPELINE]: Handling Update Group...\n")
	externalGroupId, err := ds.ExternalGroupIdForDsocialId(externalServiceId, externalUserId, dsocialUserId, dsocialGroupId)
	if err != nil {
		return
	}
	if externalGroupId != "" {
		dsocExternalGroup, _, err := ds.RetrieveDsocialGroupForExternalGroup(externalServiceId, externalUserId, externalGroupId, dsocialUserId)
		if err != nil {
			return
		}
		if dsocExternalGroup != nil {
			for _, change := range changeset.Changes {
				dm.ApplyChange(dsocExternalGroup, change)
			}
		}
		origDsocExternalGroup, _, err := ds.RetrieveDsocialGroupForExternalGroup(externalServiceId, externalUserId, externalGroupId, dsocialUserId)
		if err != nil {
			return
		}
		_, err = UpdateGroupOnExternalService(client, cs, ds, dsocialUserId, origDsocExternalGroup, dsocExternalGroup)
	} else {
		err = p.handleCreateGroup(client, ds, cs, dsocialUserId, dsocialGroupId)
	}
	return
}
Пример #2
0
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
}
Пример #3
0
func (p *Pipeline) groupImport(cs ContactsService, ds DataStoreService, dsocialUserId string, group *Group, minimumIncludes *list.List, allowAdd, allowDelete, allowUpdate bool) (*dm.Group, string, os.Error) {
	emptyGroup := new(dm.Group)
	if group == nil || group.Value == nil {
		return nil, "", nil
	}
	//fmt.Printf("[PIPELINE]: Syncing group: %s\n", group.Value.Name)
	if group.Value.ContactIds == nil {
		group.Value.ContactIds = make([]string, 0, 10)
	}
	if group.Value.ContactNames == nil {
		group.Value.ContactNames = make([]string, 0, 10)
	}
	if len(group.Value.ContactIds) == 0 && len(group.Value.ContactNames) == 0 && minimumIncludes != nil {
		sv1 := vector.StringVector(group.Value.ContactIds)
		sv2 := vector.StringVector(group.Value.ContactNames)
		sv1.Resize(sv1.Len(), sv1.Len()+minimumIncludes.Len())
		sv2.Resize(sv2.Len(), sv2.Len()+minimumIncludes.Len())
		for iter := minimumIncludes.Front(); iter != nil; iter = iter.Next() {
			contactRef := iter.Value.(*dm.ContactRef)
			sv1.Push(contactRef.Id)
			sv2.Push(contactRef.Name)
		}
	} else if minimumIncludes != nil {
		for iter := minimumIncludes.Front(); iter != nil; iter = iter.Next() {
			contactRef := iter.Value.(*dm.ContactRef)
			refLocation := -1
			if contactRef.Id != "" {
				for i, id := range group.Value.ContactIds {
					if id == contactRef.Id {
						refLocation = i
						break
					}
				}
			}
			if refLocation == -1 && contactRef.Name != "" {
				for i, name := range group.Value.ContactNames {
					if name == contactRef.Name {
						refLocation = i
						break
					}
				}
			}
			if refLocation == -1 {
				sv1 := vector.StringVector(group.Value.ContactIds)
				sv2 := vector.StringVector(group.Value.ContactNames)
				sv1.Push(contactRef.Id)
				sv2.Push(contactRef.Name)
			} else {
				group.Value.ContactIds[refLocation] = contactRef.Id
				group.Value.ContactNames[refLocation] = contactRef.Name
			}
		}
	}
	extDsocialGroup, _, err := ds.RetrieveDsocialGroupForExternalGroup(group.ExternalServiceId, group.ExternalUserId, group.ExternalGroupId, dsocialUserId)
	if err != nil {
		return nil, "", err
	}
	var matchingGroup *dm.Group
	var isSame bool
	if extDsocialGroup == nil {
		// We don't have a mapping for this external group to an internal group mapping
		// meaning we've never imported this group previously from THIS service, but we may
		// already have the group in our system, so let's see if we can find it
		matchingGroup, isSame, err = p.findMatchingDsocialGroup(ds, dsocialUserId, group)
		if err != nil {
			return matchingGroup, "", err
		}
		if matchingGroup != nil {
			group.DsocialGroupId = matchingGroup.Id
			extGroup := cs.ConvertToExternalGroup(matchingGroup, nil, dsocialUserId)
			ds.StoreExternalGroup(group.ExternalServiceId, group.ExternalUserId, dsocialUserId, group.ExternalGroupId, extGroup)
			extDsocialGroup = cs.ConvertToDsocialGroup(extGroup, matchingGroup, dsocialUserId)
			if extDsocialGroup != nil {
				AddIdsForDsocialGroup(extDsocialGroup, ds, dsocialUserId)
				fmt.Printf("[PIPELINE]: groupImport() before store dsoc group ExternalGroupId: %v and extDsocialGroup.Id %v matchingGroup.Id %v\n", group.ExternalGroupId, extDsocialGroup.Id, matchingGroup.Id)
				//group.ExternalGroupId = extDsocialGroup.Id
				//extDsocialGroup.Id = group.DsocialGroupId
				extDsocialGroup, err = ds.StoreDsocialGroupForExternalGroup(group.ExternalServiceId, group.ExternalUserId, group.ExternalGroupId, dsocialUserId, extDsocialGroup)
				if extDsocialGroup == nil || err != nil {
					return matchingGroup, "", err
				}
				//extDsocialGroup.Id = group.DsocialGroupId
				fmt.Printf("[PIPELINE]: groupImport() before store mapping ExternalGroupId: %v and DsocialGroupId %v\n", group.ExternalGroupId, group.DsocialGroupId)
				if _, _, err = ds.StoreDsocialExternalGroupMapping(group.ExternalServiceId, group.ExternalUserId, group.ExternalGroupId, dsocialUserId, group.DsocialGroupId); err != nil {
					return matchingGroup, "", err
				}
			}
		}
	} else {
		// we have a mapping for this external group to an internal group mapping
		// from THIS service, therefore let's use it
		if group.DsocialGroupId == "" {
			group.DsocialGroupId, err = ds.DsocialIdForExternalGroupId(group.ExternalServiceId, group.ExternalUserId, dsocialUserId, group.ExternalGroupId)
			if err != nil {
				return nil, "", err
			}
		}
		if group.DsocialGroupId != "" {
			matchingGroup, _, err = ds.RetrieveDsocialGroup(dsocialUserId, group.DsocialGroupId)
			if err != nil {
				return nil, "", err
			}
		}
	}
	// ensure we have a contact Id
	if group.DsocialGroupId == "" {
		if matchingGroup != nil {
			group.DsocialGroupId = matchingGroup.Id
			fmt.Printf("[PIPELINE]: Will be using matchingGroup Id: %v\n", matchingGroup.Id)
		}
		if group.DsocialGroupId == "" {
			newGroup := &dm.Group{UserId: dsocialUserId}
			AddIdsForDsocialGroup(newGroup, ds, dsocialUserId)
			thegroup, err := ds.StoreDsocialGroup(dsocialUserId, newGroup.Id, newGroup)
			if err != nil {
				return nil, "", err
			}
			group.DsocialGroupId = thegroup.Id
		}
	}
	if _, isSame = emptyGroup.IsSimilarOrUpdated(extDsocialGroup, group.Value); isSame {
		return matchingGroup, "", nil
	}
	l := new(list.List)
	emptyGroup.GenerateChanges(extDsocialGroup, group.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:      group.ExternalServiceId,
		ChangeImportId: group.ExternalGroupId,
		RecordId:       group.DsocialGroupId,
		Changes:        changes,
	}
	_, err = ds.StoreGroupChangeSet(dsocialUserId, changeset)
	if err != nil {
		return matchingGroup, changeset.Id, nil
	}
	if extDsocialGroup == nil {
		fmt.Printf("[PIPELINE]: OriginalExternalGroup is nil and group.DsocialGroupId is %v and group.Value.Id was %v\n", group.DsocialGroupId, group.Value.Id)
		group.Value.Id = group.DsocialGroupId
		AddIdsForDsocialGroup(group.Value, ds, dsocialUserId)
		group.Value, err = ds.StoreDsocialGroup(dsocialUserId, group.DsocialGroupId, group.Value)
		if err != nil {
			return matchingGroup, changeset.Id, err
		}
		storedDsocialGroup, err := ds.StoreDsocialGroupForExternalGroup(group.ExternalServiceId, group.ExternalUserId, group.ExternalGroupId, dsocialUserId, group.Value)
		_, _, err2 := ds.StoreDsocialExternalGroupMapping(group.ExternalServiceId, group.ExternalUserId, group.ExternalGroupId, dsocialUserId, group.DsocialGroupId)
		if err == nil {
			err = err2
		}
		return storedDsocialGroup, changeset.Id, err
	}
	var storedDsocialGroup *dm.Group = nil
	if group.DsocialGroupId != "" {
		if storedDsocialGroup, _, err = ds.RetrieveDsocialGroup(dsocialUserId, group.DsocialGroupId); err != nil {
			return matchingGroup, changeset.Id, err
		}
	}
	if storedDsocialGroup == nil {
		storedDsocialGroup = new(dm.Group)
	}
	for j, iter := 0, l.Front(); iter != nil; j, iter = j+1, iter.Next() {
		change := iter.Value.(*dm.Change)
		dm.ApplyChange(storedDsocialGroup, change)
		changes[j] = change
	}
	AddIdsForDsocialGroup(storedDsocialGroup, ds, dsocialUserId)
	_, err = ds.StoreDsocialGroup(dsocialUserId, group.DsocialGroupId, storedDsocialGroup)
	return storedDsocialGroup, changeset.Id, err
}