MySpace Open Platform

A Place For Developers

Welcome Developers!

in

Welcome!

in

Understanding opensocial.IdSpec

The latest iteration of OpenSocial on MySpace, OpenSocial 0.8, sets a number of places where you now need to understand this JavaScript class known as opensocial.IdSpec. Before, when you needed something like the viewer or owner from an OpenSocial 0.7 method, you would just pass a string indicating 'VIEWER' or 'OWNER' and all would be fine. However, to make the ids more informative and to allow for passing IDs that belong to people or groups, the IdSpec was created. Many methods in OpenSocial take an IdSpec as a parameter. Let’s take a simple example (simple because I happen to have the 0.7 and 0.8 version handy!). In 0.7, if I wanted to get a list of the viewer’s friends and display that list to an HTML select control, I would have the following BLOCKED SCRIPT

 

function loadFriends(selectId) {
    var friendSelect = document.getElementById(selectId);
    var dr = opensocial.newDataRequest();
    var pplReq = dr.newFetchPeopleRequest('VIEWER_FRIENDS');
    dr.add(pplReq, 'fetchPeople');
    dr.send(function(response) {
        var ppl = response.get('fetchPeople').getData().asArray();
        var i;
        var html = "";
        for (i = 0; i < ppl.length; ++i) {
            var person = ppl[ i ];
            html += "<option id='" + person.getField(opensocial.Person.Field.ID) + "' value='" +
                person.getDisplayName() + "'>" + person.getDisplayName() + ": " +
                person.getField(opensocial.Person.Field.ID) + "</option>";
        }
        friendSelect.innerHTML = html;
    });
}
 

In that code, note that to access the list of friends, I just pass in the string 'VIEWER_FRIENDS' and everything works. For 0.8, the code needs a modest change. It needs an IdSpec. The code to create the DataRequest and FetchPeopleRequest becomes either

 

var dr = opensocial.newDataRequest();
var params = {};
params[opensocial.IdSpec.Field.USER_ID] = opensocial.IdSpec.PersonId.VIEWER;
params[opensocial.IdSpec.Field.GROUP_ID] = opensocial.IdSpec.GroupId.FRIENDS;
var idspec = opensocial.newIdSpec(params);
var pplReq = dr.newFetchPeopleRequest(idspec);

 

or, to get the friends whose network distance is 1 from the viewer (just the immediate friends), you can use:

var dr = opensocial.newDataRequest();
var params = {};
params[opensocial.IdSpec.Field.USER_ID] = opensocial.IdSpec.PersonId.VIEWER;
params[opensocial.IdSpec.Field.NETWORK_DISTANCE] = 1;
var idspec = opensocial.newIdSpec(params);
var pplReq = dr.newFetchPeopleRequest(idspec);

 

This is a bit more wordy, but it allows for a uniform API across the methods that accept an IdSpec. For a full explanation of the mapping from the 0.7 to the 0.8 API, please see the OpenSocial v0.8 Breaking Changes Document.

 

Published Dec 01 2008, 10:00 AM by Scott
Filed under:

Comments

 

Devon said:

hi

December 12, 2008 3:33 AM
 

Plamen Paskov said:

Hi Scott,

First of all thanks for the tutorials you post!

I tried your example for 0.8 but always get : opensocial.IdSpec.GroupId is undefined . I try to get the viewer friends.

Could you please point me on that ?

Thanks

January 6, 2009 5:43 AM
 

Tom said:

Plamen, use the second method (NETWORK_DISTANCE = 1 method) to get friends. The first method is broken as far as I can tell from my tests.

The problem though, now, is that in the response data, getTotalSize() gives the wrong size! Say a person has 150 friends. You can only get 100 friends at a time. Normally this is okay, in OS0.7, we just pull friends in batches of 100, relying on getTotalSize() to give us the real total (150 in this case).

However, in OS0.8, this gives "100". I will make a post on the forum about this.

January 19, 2009 5:30 PM