Tuesday Sep 02, 2008

Last time I wrote, I discussed the code changes needed to migrate OpenSocial gadgets from version 0.7 to 0.8. I also showed specific code related to the removal of VIEWER_FRIENDS and OWNER_FRIENDS, and the changes for OWNER and VIEWER.

Today, I want to discuss the code changes for updating gadgets which retrieve activity information. I recently updated the activity gadgets in Project SocialSite after some trial and error. Hopefully the code I'll be showing will save others some time and effort.

What I am going to show

Below I'm going to show how to retrieve the owner's activities and the owner's friend's activities. For reference, I'll first show the 0.7 activity code that needs to be migrated, and then the updated 0.8 activity code.

v0.7 Code Snippet

01    var req = opensocial.newDataRequest();
02
03    // owner activities
04    req.add(req.newFetchActivitiesRequest(opensocial.DataRequest.PersonId.OWNER), 'ownerActivities');
05
06    // friends activities
07    req.add(req.newFetchActivitiesRequest(opensocial.DataRequest.Group.OWNER_FRIENDS), 'friendsActivities');
08
09    req.send(handleActivities);
10
11    ...
12
13  function handleActivities(dataResponse) {
14    var ownerActivities = dataResponse.get('ownerActivities').getData()['activities'];
15    var friendsActivities = dataResponse.get('friendsActivities').getData()['activities'];
16    ...

v0.8 Code Snippet

01    var req = opensocial.newDataRequest();
02
03    // owner activities
04    var spec = new opensocial.IdSpec();
++    spec.setField(opensocial.IdSpec.Field.USER_ID, opensocial.IdSpec.PersonId.OWNER);

++    req.add(req.newFetchActivitiesRequest(spec), 'ownerActivities');
05
06    // friends activities
07    var spec = new opensocial.IdSpec();
++    spec.setField(opensocial.IdSpec.Field.USER_ID, opensocial.IdSpec.PersonId.OWNER);
++    spec.setField(opensocial.IdSpec.Field.GROUP_ID, 'FRIENDS');
++    spec.setField(opensocial.IdSpec.Field.NETWORK_DISTANCE, 1);

++    req.add(req.newFetchActivitiesRequest(spec), 'friendsActivities');
08
09    req.send(handleActivities);
10
11    ...
12
13  function handleActivities(dataResponse) {
14    var ownerActivities = dataResponse.get('ownerActivities').getData();
15    var friendsActivities = dataResponse.get('friendsActivities').getData();
16    ...

Explanation of code changes

I've numbered the code above for reference and indicated with '++' where more lines have been added to accomplish the same functionality as with 0.7. Lines 04 and 07 are where the requests to fetch the activities are set up. The changes to get the friends activities in line 07 are similar to what I discussed in my last blog. Basically you now need an IdSpec as the first argument instead of a string in newFetchActivitiesRequest.

For line 04, the main change is again that the first argument is now an IdSpec as opposed to a string for newFetchActivitiesRequest. I ran into a problem setting the fields for the IdSpec. Initially, I had tried setting the field opensocial.IdSpec.Field.GROUP_ID to 'SELF'. When I did this, there were no activities returned. Looking at the Shindig (the OpenSocial RI) code, I saw that they only set the field opensocial.IdSpec.Field.USER_ID. When I tried this, things worked properly. I found one somewhat related discussion on using SELF here. Based on this, it appears that the code I've included above should be equivalent to setting the GROUP_ID field to SELF. So this may not be an issue in the future, but I wanted to mention it since it caused me some extra work.

The only other changes can be seen in lines 14 and 15. For both of these, you no longer need to specify ['activities'] at the end.

Summary

I hope the information presented here is useful. Once you know how, it's easy to update your gadget to use the 0.8 API. If you want to see the full gadget code, check it out at Project SocialSite. This change was Revision 202 of ownerandfriends_activities.xml.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed