示例#1
0
func DeleteGroupOnExternalService(client oauth2_client.OAuth2Client, cs ContactsService, ds DataStoreService, dsocialUserId, dsocialGroupId string) (bool, os.Error) {
	if dsocialGroupId == "" || dsocialUserId == "" {
		return false, nil
	}
	userInfo, err := client.RetrieveUserInfo()
	if err != nil {
		return true, err
	}
	externalServiceId := cs.ServiceId()
	externalUserId := userInfo.Guid()
	externalGroupId, err := ds.ExternalGroupIdForDsocialId(externalServiceId, externalUserId, dsocialUserId, dsocialGroupId)
	if externalGroupId == "" || err != nil {
		return externalGroupId == "", err
	}
	externalGroup, _, err := ds.RetrieveExternalGroup(externalServiceId, externalUserId, dsocialUserId, externalGroupId)
	if err != nil {
		return true, err
	}
	if externalGroup == nil {
		return false, err
	}
	_, err = cs.DeleteGroupOnExternalService(client, externalGroup)
	if err != nil {
		return true, err
	}
	_, err = ds.DeleteDsocialGroupForExternalGroup(externalServiceId, externalUserId, externalGroupId, dsocialUserId)
	if err != nil {
		return true, err
	}
	_, err = ds.DeleteExternalGroup(externalServiceId, externalUserId, dsocialUserId, externalGroupId)
	return true, err
}
示例#2
0
func (p *SmugMugContactService) RetrieveContacts(client oauth2_client.OAuth2Client, ds DataStoreService, dsocialUserId string, next NextToken) ([]*Contact, NextToken, os.Error) {
	l := list.New()
	var useErr os.Error
	famResp, err := smugmug.RetrieveFamily(client, nil)
	if err != nil {
		useErr = err
	}
	if famResp != nil && famResp.Family != nil {
		for _, u := range famResp.Family {
			contact, err := p.handleRetrievedContact(client, ds, dsocialUserId, u.NickName, &u)
			if contact != nil {
				l.PushBack(contact)
			}
			if err != nil && useErr == nil {
				useErr = err
			}
		}
	}
	if useErr != nil {
		return p.listToContacts(l), nil, useErr
	}
	fansResp, err := smugmug.RetrieveFans(client, nil)
	if err != nil {
		useErr = err
	}
	if fansResp != nil && fansResp.Fans != nil {
		for _, u := range fansResp.Fans {
			contact, err := p.handleRetrievedContact(client, ds, dsocialUserId, u.NickName, &u)
			if contact != nil {
				l.PushBack(contact)
			}
			if err != nil && useErr == nil {
				useErr = err
			}
		}
	}
	if useErr != nil {
		return p.listToContacts(l), nil, useErr
	}
	userInfo, err := client.RetrieveUserInfo()
	if err != nil {
		return p.listToContacts(l), nil, err
	}
	userResp, err := smugmug.RetrieveUserInfo(client, userInfo.Username(), nil)
	if err != nil {
		useErr = err
	}
	if userResp != nil {
		contact, err := p.handleRetrievedContact(client, ds, dsocialUserId, userResp.User.NickName, &userResp.User)
		if contact != nil {
			l.PushBack(contact)
		}
		if err != nil && useErr == nil {
			useErr = err
		}
	}
	return p.listToContacts(l), nil, useErr
}
func HandleGenericOauthTestRequest(w http.ResponseWriter, req *http.Request, c oauth2_client.OAuth2Client, method, test_url_property, body string, useTemplate *template.Template) {
	props := getProperties()
	if req.Method == oauth2_client.POST {
		if err := req.ParseForm(); err != nil {
			w.Header().Set("Content-Type", "text/plain")
			w.WriteHeader(500)
			io.WriteString(w, "Unable to parse form:\n\n")
			io.WriteString(w, err.Error())
			return
		}
		for k, arr := range req.Form {
			for _, v := range arr {
				props.Set(k, v)
			}
		}
	}
	for k, arr := range req.URL.Query() {
		for _, v := range arr {
			props.Set(k, v)
		}
	}
	c.Initialize(props)
	uri := props.GetAsString(test_url_property)
	log.Printf("Client is: %T -> %#v", c, c)
	var reader io.Reader = nil
	if len(body) > 0 {
		reader = bytes.NewBufferString(body)
	}
	resp, _, err := oauth2_client.AuthorizedRequest(c, method, nil, uri, nil, reader)
	m := make(map[string]interface{})
	isError := false
	m["c"] = c
	m["url"] = uri
	if err != nil {
		m["output"] = err.Error()
		isError = true
	} else {
		b, err := httputil.DumpResponse(resp, true)
		if err != nil {
			m["output"] = err.Error()
			isError = true
		} else {
			m["output"] = string(b)
		}
	}
	if isError {
		w.Header().Set("Content-Type", "text/plain")
		w.WriteHeader(500)
	} else {
		w.Header().Set("Content-Type", "text/html")
		w.WriteHeader(200)
	}
	err = useTemplate.Execute(w, m)
	if err != nil {
		oauth2_client.LogErrorf("Error: %T %v", err, err)
	}
}
func HandleGenericOauthRequest(c oauth2_client.OAuth2Client, w http.ResponseWriter, req *http.Request) {
	uri := c.GenerateRequestTokenUrl(jsonhelper.NewJSONObject())
	if len(uri) > 0 {
		w.Header().Set("Location", uri)
		w.WriteHeader(302)
	} else {
		w.WriteHeader(500)
	}
}
示例#5
0
func (p *YahooContactService) RetrieveGroups(client oauth2_client.OAuth2Client, ds DataStoreService, dsocialUserId string, next NextToken) ([]*Group, NextToken, error) {
	var m url.Values
	m = make(url.Values)
	m.Add("count", "max")
	if next == nil {
	} else if start, ok := next.(int); ok {
		m.Add("start", strconv.Itoa(start))
	}
	resp, err := yahoo.RetrieveCategories(client, m)
	if resp == nil || resp.Categories.Categories == nil || len(resp.Categories.Categories) == 0 || err != nil {
		return make([]*Group, 0), nil, err
	}
	groups := make([]*Group, len(resp.Categories.Categories))
	externalServiceId := p.ServiceId()
	userInfo, err := client.RetrieveUserInfo()
	externalUserId := userInfo.Guid()
	var useErr error = nil
	for i, yahooGroup := range resp.Categories.Categories {
		var externalGroupId string
		if yahooGroup.Id > 0 {
			externalGroupId = strconv.FormatInt(yahooGroup.Id, 10)
		}
		var origDsocialGroup *dm.Group = nil
		dsocialGroupId := ""
		if len(externalGroupId) > 0 {
			dsocialGroupId, err = ds.DsocialIdForExternalGroupId(externalServiceId, externalUserId, dsocialUserId, externalGroupId)
			if err != nil {
				if useErr == nil {
					useErr = err
				}
				continue
			}
			if dsocialGroupId != "" {
				origDsocialGroup, _, err = ds.RetrieveDsocialGroupForExternalGroup(externalServiceId, externalUserId, externalGroupId, dsocialUserId)
				if err != nil {
					if useErr == nil {
						useErr = err
					}
					continue
				}
			} else {
				ds.StoreExternalGroup(externalServiceId, externalUserId, dsocialUserId, externalGroupId, &yahooGroup)
			}
		}
		var dsocialGroup *dm.Group = dm.YahooCategoryToDsocial(&yahooGroup, origDsocialGroup, dsocialUserId)
		groups[i] = &Group{
			ExternalServiceId: p.ServiceId(),
			ExternalUserId:    externalUserId,
			ExternalGroupId:   externalGroupId,
			DsocialUserId:     dsocialUserId,
			DsocialGroupId:    dsocialGroupId,
			Value:             dsocialGroup,
		}
	}
	return groups, nil, useErr
}
示例#6
0
func CreateContactOnExternalService(client oauth2_client.OAuth2Client, cs ContactsService, ds DataStoreService, dsocialUserId string, contact *dm.Contact) (*Contact, os.Error) {
	if contact == nil {
		return nil, nil
	}
	userInfo, err := client.RetrieveUserInfo()
	if err != nil {
		return nil, err
	}
	externalServiceId := cs.ServiceId()
	externalUserId := userInfo.Guid()
	externalContactId, err := ds.ExternalContactIdForDsocialId(externalServiceId, externalUserId, dsocialUserId, contact.Id)
	if err != nil {
		return nil, err
	}
	if externalContactId != "" {
		originalContact, _, err := ds.RetrieveDsocialContact(dsocialUserId, contact.Id)
		if err != nil {
			return nil, err
		}
		return UpdateContactOnExternalService(client, cs, ds, dsocialUserId, originalContact, contact)
	}
	externalContact := cs.ConvertToExternalContact(contact, nil, dsocialUserId)
	externalContact, externalContactId, err = cs.CreateContactOnExternalService(client, externalContact)
	if err != nil {
		return nil, err
	}
	externalContactId2, err := ds.StoreExternalContact(externalServiceId, externalUserId, dsocialUserId, externalContactId, externalContact)
	if err != nil {
		return nil, err
	}
	fmt.Printf("[SERVICE]: extContactId: %s, extContactId2: %s\n", externalContactId, externalContactId2)
	if externalContactId2 != "" {
		externalContactId = externalContactId2
	}
	dsocialContactForExternal := cs.ConvertToDsocialContact(externalContact, contact, dsocialUserId)
	dsocialContactForExternal, err = ds.StoreDsocialContactForExternalContact(externalServiceId, externalUserId, externalContactId, dsocialUserId, dsocialContactForExternal)
	if err != nil {
		return nil, err
	}
	_, _, err = ds.StoreDsocialExternalContactMapping(externalServiceId, externalUserId, externalContactId, dsocialUserId, dsocialContactForExternal.Id)
	outContact := &Contact{
		ExternalServiceId: externalServiceId,
		ExternalUserId:    externalUserId,
		ExternalContactId: externalContactId,
		DsocialUserId:     dsocialUserId,
		DsocialContactId:  dsocialContactForExternal.Id,
		Value:             dsocialContactForExternal,
	}
	return outContact, err
}
示例#7
0
func (p *YahooContactService) RetrieveContacts(client oauth2_client.OAuth2Client, ds DataStoreService, dsocialUserId string, next NextToken) ([]*Contact, NextToken, error) {
	var m url.Values
	m = make(url.Values)
	m.Add("count", "max")
	if next == nil {
	} else if start, ok := next.(int); ok {
		m.Add("start", strconv.Itoa(start))
	}
	resp, err := yahoo.RetrieveContacts(client, m)
	if resp == nil || resp.Contacts.Contacts == nil || len(resp.Contacts.Contacts) == 0 || err != nil {
		return make([]*Contact, 0), nil, err
	}
	contacts := make([]*Contact, len(resp.Contacts.Contacts))
	externalServiceId := p.ServiceId()
	userInfo, err := client.RetrieveUserInfo()
	externalUserId := userInfo.Guid()
	var useErr error = nil
	for i, yahooContact := range resp.Contacts.Contacts {
		externalContactId := strconv.FormatInt(yahooContact.Id, 10)
		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, &yahooContact)
			}
		}
		dsocialContact := dm.YahooContactToDsocial(&yahooContact, origDsocialContact, dsocialUserId)
		contacts[i] = &Contact{
			ExternalServiceId: p.ServiceId(),
			ExternalUserId:    externalUserId,
			ExternalContactId: externalContactId,
			DsocialUserId:     dsocialUserId,
			DsocialContactId:  dsocialContactId,
			Value:             dsocialContact,
		}
	}
	return contacts, nil, useErr
}
示例#8
0
func UpdateGroupOnExternalService(client oauth2_client.OAuth2Client, cs ContactsService, ds DataStoreService, dsocialUserId string, originalGroup, group *dm.Group) (*Group, os.Error) {
	if group == nil || originalGroup == nil {
		return nil, nil
	}
	userInfo, err := client.RetrieveUserInfo()
	if err != nil {
		return nil, err
	}
	externalServiceId := cs.ServiceId()
	externalUserId := userInfo.Guid()
	externalGroupId, err := ds.ExternalGroupIdForDsocialId(externalServiceId, externalUserId, dsocialUserId, originalGroup.Id)
	if err != nil {
		return nil, err
	}
	if externalGroupId == "" {
		return CreateGroupOnExternalService(client, cs, ds, dsocialUserId, group)
	}
	originalExternalGroup, _, err := ds.RetrieveExternalGroup(externalServiceId, externalUserId, dsocialUserId, externalGroupId)
	if err != nil {
		return nil, err
	}
	latestExternalGroup := cs.ConvertToExternalGroup(group, originalExternalGroup, dsocialUserId)
	latestExternalGroup2, externalGroupId2, err := cs.UpdateGroupOnExternalService(client, originalExternalGroup, latestExternalGroup)
	if err != nil {
		return nil, err
	}
	if latestExternalGroup2 != nil {
		latestExternalGroup = latestExternalGroup2
	}
	if externalGroupId2 != "" {
		externalGroupId = externalGroupId2
	}
	latestDsocialGroupForExternal := cs.ConvertToDsocialGroup(latestExternalGroup, originalGroup, dsocialUserId)
	_, err = ds.StoreExternalGroup(externalServiceId, externalUserId, dsocialUserId, externalGroupId, latestExternalGroup)
	if err != nil {
		return nil, err
	}
	latestDsocialGroupForExternal, err = ds.StoreDsocialGroupForExternalGroup(externalServiceId, externalUserId, externalGroupId, dsocialUserId, latestDsocialGroupForExternal)
	outGroup := &Group{
		ExternalServiceId: externalServiceId,
		ExternalUserId:    externalUserId,
		ExternalGroupId:   externalGroupId,
		DsocialUserId:     dsocialUserId,
		DsocialGroupId:    latestDsocialGroupForExternal.Id,
		Value:             latestDsocialGroupForExternal,
	}
	return outGroup, err
}
示例#9
0
func UpdateContactOnExternalService(client oauth2_client.OAuth2Client, cs ContactsService, ds DataStoreService, dsocialUserId string, originalContact, contact *dm.Contact) (*Contact, os.Error) {
	if contact == nil || originalContact == nil {
		return nil, nil
	}
	userInfo, err := client.RetrieveUserInfo()
	if err != nil {
		return nil, err
	}
	externalServiceId := cs.ServiceId()
	externalUserId := userInfo.Guid()
	externalContactId, err := ds.ExternalContactIdForDsocialId(externalServiceId, externalUserId, dsocialUserId, originalContact.Id)
	if err != nil {
		return nil, err
	}
	if externalContactId == "" {
		return CreateContactOnExternalService(client, cs, ds, dsocialUserId, contact)
	}
	originalExternalContact, _, err := ds.RetrieveExternalContact(externalServiceId, externalUserId, dsocialUserId, externalContactId)
	if err != nil {
		return nil, err
	}
	latestExternalContact := cs.ConvertToExternalContact(contact, originalExternalContact, dsocialUserId)
	latestExternalContact2, externalContactId2, err := cs.UpdateContactOnExternalService(client, originalExternalContact, latestExternalContact)
	if err != nil {
		return nil, err
	}
	if latestExternalContact2 != nil {
		latestExternalContact = latestExternalContact2
	}
	if externalContactId2 != "" {
		externalContactId = externalContactId2
	}
	latestDsocialContactForExternal := cs.ConvertToDsocialContact(latestExternalContact, originalContact, dsocialUserId)
	_, err = ds.StoreExternalContact(externalServiceId, externalUserId, dsocialUserId, externalContactId, latestExternalContact)
	if err != nil {
		return nil, err
	}
	latestDsocialContactForExternal, err = ds.StoreDsocialContactForExternalContact(externalServiceId, externalUserId, externalContactId, dsocialUserId, latestDsocialContactForExternal)
	outContact := &Contact{
		ExternalServiceId: externalServiceId,
		ExternalUserId:    externalUserId,
		ExternalContactId: externalContactId,
		DsocialUserId:     dsocialUserId,
		DsocialContactId:  latestDsocialContactForExternal.Id,
		Value:             latestDsocialContactForExternal,
	}
	return outContact, err
}
示例#10
0
func CreateGroupOnExternalService(client oauth2_client.OAuth2Client, cs ContactsService, ds DataStoreService, dsocialUserId string, group *dm.Group) (*Group, os.Error) {
	if group == nil {
		return nil, nil
	}
	userInfo, err := client.RetrieveUserInfo()
	if err != nil {
		return nil, err
	}
	externalServiceId := cs.ServiceId()
	externalUserId := userInfo.Guid()
	externalGroupId, err := ds.ExternalGroupIdForDsocialId(externalServiceId, externalUserId, dsocialUserId, group.Id)
	if err != nil {
		return nil, err
	}
	if externalGroupId != "" {
		originalGroup, _, err := ds.RetrieveDsocialGroup(dsocialUserId, group.Id)
		if err != nil {
			return nil, err
		}
		return UpdateGroupOnExternalService(client, cs, ds, dsocialUserId, originalGroup, group)
	}
	externalGroup := cs.ConvertToExternalGroup(group, nil, dsocialUserId)
	externalGroup, externalGroupId, err = cs.CreateGroupOnExternalService(client, externalGroup)
	if err != nil {
		return nil, err
	}
	externalGroupId, err = ds.StoreExternalGroup(externalServiceId, externalUserId, dsocialUserId, externalGroupId, externalGroup)
	if err != nil {
		return nil, err
	}
	dsocialGroupForExternal := cs.ConvertToDsocialGroup(externalGroup, group, dsocialUserId)
	dsocialGroupForExternal, err = ds.StoreDsocialGroupForExternalGroup(externalServiceId, externalUserId, externalGroupId, dsocialUserId, dsocialGroupForExternal)
	if err != nil {
		return nil, err
	}
	_, _, err = ds.StoreDsocialExternalGroupMapping(externalServiceId, externalUserId, externalGroupId, dsocialUserId, dsocialGroupForExternal.Id)
	outGroup := &Group{
		ExternalServiceId: externalServiceId,
		ExternalUserId:    externalUserId,
		ExternalGroupId:   externalGroupId,
		DsocialUserId:     dsocialUserId,
		DsocialGroupId:    dsocialGroupForExternal.Id,
		Value:             dsocialGroupForExternal,
	}
	return outGroup, err
}
示例#11
0
func (p *YahooContactService) RetrieveGroup(client oauth2_client.OAuth2Client, ds DataStoreService, dsocialUserId string, groupId string) (*Group, error) {
	resp, err := yahoo.RetrieveCategory(client, groupId, nil)
	if resp == nil || err != nil {
		return nil, err
	}
	yahooGroup := &resp.Category
	externalServiceId := p.ServiceId()
	userInfo, err := client.RetrieveUserInfo()
	externalUserId := userInfo.Guid()
	useErr := err
	var externalGroupId string
	if yahooGroup.Id > 0 {
		externalGroupId = strconv.FormatInt(yahooGroup.Id, 10)
	}
	dsocialGroupId := ""
	var origDsocialGroup *dm.Group = nil
	if len(externalGroupId) > 0 {
		dsocialGroupId, err = ds.DsocialIdForExternalGroupId(externalServiceId, externalUserId, dsocialUserId, externalGroupId)
		if err != nil {
			if useErr == nil {
				useErr = err
			}
		}
		if dsocialGroupId != "" {
			origDsocialGroup, _, err = ds.RetrieveDsocialGroupForExternalGroup(externalServiceId, externalUserId, externalGroupId, dsocialUserId)
			if err != nil && useErr == nil {
				useErr = err
			}
		} else {
			ds.StoreExternalGroup(externalServiceId, externalUserId, dsocialUserId, externalGroupId, yahooGroup)
		}
	}
	var dsocialGroup *dm.Group = dm.YahooCategoryToDsocial(yahooGroup, origDsocialGroup, dsocialUserId)
	group := &Group{
		ExternalServiceId: p.ServiceId(),
		ExternalUserId:    externalUserId,
		ExternalGroupId:   externalGroupId,
		DsocialUserId:     dsocialUserId,
		DsocialGroupId:    dsocialGroupId,
		Value:             dsocialGroup,
	}
	return group, useErr
}
示例#12
0
func (p *YahooContactService) RetrieveContact(client oauth2_client.OAuth2Client, ds DataStoreService, dsocialUserId string, contactId string) (*Contact, error) {
	resp, err := yahoo.RetrieveContact(client, contactId, nil)
	if resp == nil || err != nil {
		return nil, err
	}
	yahooContact := &resp.Contact
	externalServiceId := p.ServiceId()
	userInfo, err := client.RetrieveUserInfo()
	externalUserId := userInfo.Guid()
	useErr := err
	dsocialContactId := ""
	var origDsocialContact *dm.Contact = nil
	var externalContactId string
	if yahooContact.Id > 0 {
		externalContactId = strconv.FormatInt(yahooContact.Id, 10)
	}
	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, yahooContact)
		}
	}
	dsocialContact := dm.YahooContactToDsocial(yahooContact, origDsocialContact, dsocialUserId)
	contact := &Contact{
		ExternalServiceId: p.ServiceId(),
		ExternalUserId:    externalUserId,
		ExternalContactId: externalContactId,
		DsocialUserId:     dsocialUserId,
		DsocialContactId:  dsocialContactId,
		Value:             dsocialContact,
	}
	return contact, useErr
}
示例#13
0
func (p *GoogleContactService) RetrieveGroup(client oauth2_client.OAuth2Client, ds DataStoreService, dsocialUserId string, groupId string) (*Group, os.Error) {
	resp, err := google.RetrieveGroup(client, groupId, nil)
	if resp == nil || resp.Entry == nil || err != nil {
		return nil, err
	}
	googleGroup := resp.Entry
	externalServiceId := p.ServiceId()
	userInfo, err := client.RetrieveUserInfo()
	externalUserId := userInfo.Guid()
	useErr := err
	externalGroupId := googleGroup.GroupId()
	dsocialGroupId := ""
	var origDsocialGroup *dm.Group = nil
	if len(externalGroupId) > 0 {
		dsocialGroupId, err = ds.DsocialIdForExternalGroupId(externalServiceId, externalUserId, dsocialUserId, externalGroupId)
		if err != nil {
			if useErr == nil {
				useErr = err
			}
		}
		if dsocialGroupId != "" {
			origDsocialGroup, _, err = ds.RetrieveDsocialGroupForExternalGroup(externalServiceId, externalUserId, externalGroupId, dsocialUserId)
			if err != nil && useErr == nil {
				useErr = err
			}
		} else {
			ds.StoreExternalGroup(externalServiceId, externalUserId, dsocialUserId, externalGroupId, googleGroup)
		}
	}
	var dsocialGroup *dm.Group = dm.GoogleGroupToDsocial(googleGroup, origDsocialGroup, dsocialUserId)
	group := &Group{
		ExternalServiceId: p.ServiceId(),
		ExternalUserId:    externalUserId,
		ExternalGroupId:   externalGroupId,
		DsocialUserId:     dsocialUserId,
		DsocialGroupId:    dsocialGroupId,
		Value:             dsocialGroup,
	}
	return group, useErr
}
示例#14
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
}
示例#15
0
func (p *FacebookContactService) handleRetrievedContact(client oauth2_client.OAuth2Client, ds DataStoreService, dsocialUserId string, contactId string, extContact *facebook.Contact) (contact *Contact, err os.Error) {
	if extContact == nil {
		return nil, nil
	}
	externalServiceId := p.ServiceId()
	userInfo, err := client.RetrieveUserInfo()
	externalUserId := userInfo.Guid()
	var useErr os.Error = nil
	dsocialContactId := ""
	var origDsocialContact *dm.Contact = nil
	externalContactId := extContact.Id
	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, extContact)
		}
	}
	dsocialContact := dm.FacebookContactToDsocial(extContact, origDsocialContact, dsocialUserId)
	contact = &Contact{
		ExternalServiceId: p.ServiceId(),
		ExternalUserId:    externalUserId,
		ExternalContactId: externalContactId,
		DsocialUserId:     dsocialUserId,
		DsocialContactId:  dsocialContactId,
		Value:             dsocialContact,
	}
	return contact, useErr
}
func HandleClientAccept(w http.ResponseWriter, req *http.Request) {
	var c oauth2_client.OAuth2Client = nil
	method := "GET"
	headers := make(http.Header)
	uri := ""
	query := make(url.Values)
	var reader io.Reader = nil
	props := getProperties()
	q := req.URL.Query()
	oauth2_client.LogInfo("=================================")
	oauth2_client.LogInfo("Received request from User: "******"=================================")
	var useTemplate *template.Template = nil
	var useTemplateData interface{} = nil
	if site := q.Get("site"); len(site) > 0 {
		if index := strings.Index(site, "?"); index >= 0 {
			site = site[0:index]
		}
		m := make(map[string]interface{})
		switch site {
		case "facebook.com":
			c = NewFacebookOauth2ClientTester(props)
			uri = props.GetAsString("facebook.client.test_url")
			useTemplate = PARSED_FACEBOOK_TEMPLATE
		case "google.com":
			c = NewGoogleOauth2ClientTester(props)
			uri = props.GetAsString("google.client.test_url")
			useTemplate = PARSED_GOOGLE_TEMPLATE
		case "plus.google.com":
			c = NewGooglePlusOauth2ClientTester(props)
			uri = props.GetAsString("googleplus.client.test_url")
			useTemplate = PARSED_GOOGLEPLUS_TEMPLATE
		case "linkedin.com":
			c = NewLinkedInOauth2ClientTester(props)
			uri = props.GetAsString("linkedin.client.test_url")
			useTemplate = PARSED_LINKEDIN_TEMPLATE
		case "smugmug.com":
			// smugmug doesn't support query strings properly
			newRawUrl := strings.Replace(req.URL.String(), "site=smugmug.com?", "site=smugmug.com&", 1)
			newUrl, _ := url.Parse(newRawUrl)
			if newUrl != nil {
				req.URL = newUrl
				q = newUrl.Query()
			}
			c = NewSmugMugOauth2ClientTester(props)
			uri = props.GetAsString("smugmug.client.test_url")
			useTemplate = PARSED_SMUGMUG_TEMPLATE
		case "twitter.com":
			c = NewTwitterOauth2ClientTester(props)
			uri = props.GetAsString("twitter.client.test_url")
			useTemplate = PARSED_TWITTER_TEMPLATE
		case "yahoo.com":
			c = NewYahooOauth2ClientTester(props)
			uri = props.GetAsString("yahoo.client.test_url")
			useTemplate = PARSED_YAHOO_TEMPLATE
		default:
			log.Fatal("Unable to determine OAuth client to handle response: ", req.URL.String())
		}
		m["c"] = c
		m["url"] = uri
		m["output"] = ""
		useTemplateData = m
	} else {
		log.Fatal("Unable to determine OAuth client to handle response: ", req.URL.String())
	}
	err := c.ExchangeRequestTokenForAccess(req)
	if err != nil {
		w.Header().Set("Content-Type", "text/plain")
		w.WriteHeader(500)
		io.WriteString(w, "Error exchanging request token for access token\n\n")
		io.WriteString(w, err.Error())
		return
	}
	if useTemplate != nil {
		w.Header().Set("Content-Type", "text/html")
		w.WriteHeader(200)
		err = useTemplate.Execute(w, useTemplateData)
		if err != nil {
			oauth2_client.LogErrorf("Error: %T %v", err, err)
		}
	} else {
		oauth2_client.LogInfo("Retrieving User Info...")
		userInfo, err3 := c.RetrieveUserInfo()
		oauth2_client.LogInfof("UserInfo: %T %v", userInfo, userInfo)
		oauth2_client.LogInfof("Error: %T %v", err3, err3)

		r, _, err2 := oauth2_client.AuthorizedRequest(c, method, headers, uri, query, reader)
		if err2 != nil {
			w.Header().Set("Content-Type", "text/plain")
			w.WriteHeader(500)
			io.WriteString(w, "Error retrieving authorized response for "+uri+"?"+query.Encode()+"\n\n")
			io.WriteString(w, err.Error())
			return
		}
		h := w.Header()
		for k, v := range r.Header {
			for _, v1 := range v {
				h.Add(k, v1)
			}
		}
		w.WriteHeader(r.StatusCode)
		io.Copy(w, r.Body)
		return
	}
}
示例#17
0
func (p *GoogleContactService) RetrieveGroups(client oauth2_client.OAuth2Client, ds DataStoreService, dsocialUserId string, next NextToken) ([]*Group, 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/") {
				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 gq, ok := next.(*google.GroupQuery); ok {
		m = make(url.Values)
		if gq.Alt != "" {
			m.Add("alt", gq.Alt)
		}
		if gq.Q != "" {
			m.Add("q", gq.Q)
		}
		if gq.MaxResults > 0 {
			m.Add("max-results", strconv.Itoa64(gq.MaxResults))
		}
		if gq.StartIndex > 0 {
			m.Add("start-index", strconv.Itoa64(gq.StartIndex))
		}
		if gq.UpdatedMin != "" {
			m.Add("updated-min", gq.UpdatedMin)
		}
		if gq.OrderBy != "" {
			m.Add("orderby", gq.OrderBy)
		}
		if gq.ShowDeleted {
			m.Add("showdeleted", "true")
		}
		if gq.RequireAllDeleted {
			m.Add("requirealldeleted", "true")
		}
		if gq.SortOrder != "" {
			m.Add("sortorder", gq.SortOrder)
		}
	}
	resp, err := google.RetrieveGroups(client, m)
	var theNextToken NextToken = nil
	if resp != nil && resp.Feed != nil && resp.Feed.Links != nil && len(resp.Feed.Links) > 0 {
		for _, link := range resp.Feed.Links {
			if link.Rel == "next" {
				theNextToken = link.Href
			}
		}
	}
	if resp == nil || resp.Feed == nil || resp.Feed.Entries == nil || len(resp.Feed.Entries) == 0 || err != nil {
		return make([]*Group, 0), theNextToken, err
	}
	groups := make([]*Group, len(resp.Feed.Entries))
	externalServiceId := p.ServiceId()
	userInfo, err := client.RetrieveUserInfo()
	externalUserId := userInfo.Guid()
	var useErr os.Error = nil
	for i, googleGroup := range resp.Feed.Entries {
		externalGroupId := googleGroup.GroupId()
		var origDsocialGroup *dm.Group = nil
		dsocialGroupId := ""
		if len(externalGroupId) > 0 {
			dsocialGroupId, err = ds.DsocialIdForExternalGroupId(externalServiceId, externalUserId, dsocialUserId, externalGroupId)
			if err != nil {
				if useErr == nil {
					useErr = err
				}
				continue
			}
			if dsocialGroupId != "" {
				origDsocialGroup, _, err = ds.RetrieveDsocialGroupForExternalGroup(externalServiceId, externalUserId, externalGroupId, dsocialUserId)
				if err != nil {
					if useErr == nil {
						useErr = err
					}
					continue
				}
			} else {
				ds.StoreExternalGroup(externalServiceId, externalUserId, dsocialUserId, externalGroupId, &googleGroup)
			}
		}
		var dsocialGroup *dm.Group = dm.GoogleGroupToDsocial(&googleGroup, origDsocialGroup, dsocialUserId)
		groups[i] = &Group{
			ExternalServiceId: p.ServiceId(),
			ExternalUserId:    googleGroup.GroupUserId(),
			ExternalGroupId:   googleGroup.GroupId(),
			DsocialUserId:     dsocialUserId,
			DsocialGroupId:    dsocialGroupId,
			Value:             dsocialGroup,
		}
	}
	return groups, theNextToken, useErr
}
示例#18
0
func (p *GoogleContactService) RetrieveContacts(client oauth2_client.OAuth2Client, ds DataStoreService, dsocialUserId string, next NextToken) ([]*Contact, NextToken, 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.FormatInt(maxResults, 10))
	} 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.FormatInt(cq.MaxResults, 10))
		}
		if cq.StartIndex > 0 {
			m.Add("start-index", strconv.FormatInt(cq.StartIndex, 10))
		}
		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 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
}