Ejemplo n.º 1
0
func (p *GoogleContactService) ConvertToDsocialContact(externalContact interface{}, originalDsocialContact *dm.Contact, dsocialUserId string) (dsocialContact *dm.Contact) {
	if externalContact == nil {
		return
	}
	if extContact, ok := externalContact.(*google.Contact); ok && extContact != nil {
		dsocialContact = dm.GoogleContactToDsocial(extContact, originalDsocialContact, dsocialUserId)
	}
	return
}
Ejemplo n.º 2
0
func (p *GoogleContactService) RetrieveContact(client oauth2_client.OAuth2Client, ds DataStoreService, dsocialUserId string, contactId string) (*Contact, os.Error) {
	googleContact, err := google.RetrieveContact(client, contactId, nil)
	if googleContact == nil || err != nil {
		return nil, err
	}
	externalServiceId := p.ServiceId()
	userInfo, err := client.RetrieveUserInfo()
	externalUserId := userInfo.Guid()
	useErr := err
	dsocialContactId := ""
	var origDsocialContact *dm.Contact = nil
	externalContactId := googleContact.ContactId()
	if len(externalContactId) > 0 {
		dsocialContactId, err = ds.DsocialIdForExternalContactId(externalServiceId, externalUserId, dsocialUserId, contactId)
		if err != nil {
			useErr = err
		}
		if dsocialContactId != "" {
			origDsocialContact, _, err = ds.RetrieveDsocialContactForExternalContact(externalServiceId, externalUserId, externalContactId, dsocialUserId)
			if err != nil && useErr == nil {
				useErr = err
			}
		} else {
			ds.StoreExternalContact(externalServiceId, externalUserId, dsocialUserId, externalContactId, googleContact)
		}
	}
	dsocialContact := dm.GoogleContactToDsocial(googleContact, origDsocialContact, dsocialUserId)
	contact := &Contact{
		ExternalServiceId: p.ServiceId(),
		ExternalUserId:    googleContact.ContactUserId(),
		ExternalContactId: googleContact.ContactId(),
		DsocialUserId:     dsocialUserId,
		DsocialContactId:  dsocialContactId,
		Value:             dsocialContact,
	}
	return contact, useErr
}
Ejemplo n.º 3
0
func (p *GoogleContactService) RetrieveContacts(client oauth2_client.OAuth2Client, ds DataStoreService, dsocialUserId string, next NextToken) ([]*Contact, NextToken, os.Error) {
	var m url.Values
	if next == nil {
	} else if s, ok := next.(string); ok {
		if s != "" {
			if strings.HasPrefix(s, "https://www.google.com/") || strings.HasPrefix(s, "http://www.google.com/") {
				uri, err := url.Parse(s)
				if err == nil {
					q, err := url.ParseQuery(uri.RawQuery)
					if err == nil {
						m = q
					}
				}
			}
			if m == nil {
				m = make(url.Values)
				m.Add("q", s)
			}
		}
	} else if maxResults, ok := next.(int); ok {
		m = make(url.Values)
		m.Add("max-results", strconv.Itoa(maxResults))
	} else if maxResults, ok := next.(int64); ok {
		m = make(url.Values)
		m.Add("max-results", strconv.Itoa64(maxResults))
	} else if cq, ok := next.(*google.ContactQuery); ok {
		m = make(url.Values)
		if cq.Alt != "" {
			m.Add("alt", cq.Alt)
		}
		if cq.Q != "" {
			m.Add("q", cq.Q)
		}
		if cq.MaxResults > 0 {
			m.Add("max-results", strconv.Itoa64(cq.MaxResults))
		}
		if cq.StartIndex > 0 {
			m.Add("start-index", strconv.Itoa64(cq.StartIndex))
		}
		if cq.UpdatedMin != "" {
			m.Add("updated-min", cq.UpdatedMin)
		}
		if cq.OrderBy != "" {
			m.Add("orderby", cq.OrderBy)
		}
		if cq.ShowDeleted {
			m.Add("showdeleted", "true")
		}
		if cq.RequireAllDeleted {
			m.Add("requirealldeleted", "true")
		}
		if cq.SortOrder != "" {
			m.Add("sortorder", cq.SortOrder)
		}
		if cq.Group != "" {
			m.Add("group", cq.Group)
		}
	}
	feed, err := google.RetrieveContacts(client, m)
	var theNextToken NextToken = nil
	if feed != nil && feed.Links != nil && len(feed.Links) > 0 {
		for _, link := range feed.Links {
			if link.Rel == "next" {
				theNextToken = link.Href
				break
			}
		}
	}
	if feed == nil || feed.Entries == nil || len(feed.Entries) == 0 || err != nil {
		return make([]*Contact, 0), theNextToken, err
	}
	contacts := make([]*Contact, len(feed.Entries))
	externalServiceId := p.ServiceId()
	userInfo, err := client.RetrieveUserInfo()
	externalUserId := userInfo.Guid()
	var useErr os.Error = nil
	for i, googleContact := range feed.Entries {
		externalContactId := googleContact.ContactId()
		dsocialContactId := ""
		var origDsocialContact *dm.Contact = nil
		if len(externalContactId) > 0 {
			dsocialContactId, err = ds.DsocialIdForExternalContactId(externalServiceId, externalUserId, dsocialUserId, externalContactId)
			if err != nil {
				useErr = err
				continue
			}
			if dsocialContactId != "" {
				origDsocialContact, _, err = ds.RetrieveDsocialContactForExternalContact(externalServiceId, externalUserId, externalContactId, dsocialUserId)
				if err != nil {
					useErr = err
					continue
				}
			} else {
				ds.StoreExternalContact(externalServiceId, externalUserId, dsocialUserId, externalContactId, &googleContact)
			}
		}
		dsocialContact := dm.GoogleContactToDsocial(&googleContact, origDsocialContact, dsocialUserId)
		contacts[i] = &Contact{
			ExternalServiceId: p.ServiceId(),
			ExternalUserId:    googleContact.ContactUserId(),
			ExternalContactId: externalContactId,
			DsocialUserId:     dsocialUserId,
			DsocialContactId:  dsocialContactId,
			Value:             dsocialContact,
		}
	}
	return contacts, theNextToken, useErr
}