80 likes | 194 Views
Activities in OpenSocial. CS 195.35: Survey of Contemporary Technologies. Activities. Actions carried out in a social application Activities are typically posted in an updates page in your container
E N D
Activities inOpenSocial CS 195.35: Survey of Contemporary Technologies
Activities • Actions carried out in a social application • Activities are typically posted in an updates page in your container • The OpenSocial API enables posting of activities and reading from a user’s activity stream (assuming the viewer is authorized to do so)
opensocial.Activity • Class that represents an Activity object • opensocial.newActivity(params) returns a new instance of Activity • params is an array that indicates activity details (title, body, etc) • Methods: • getField( key ): returns a value • setField( key, value): sets field to value • Possible values for key: ‘title’, ‘body’, etc.
Posting an activity • opensocial.requestCreateActivity(a,p,f) • a: Activity object • p: priority (‘high’ or ‘low’) • f: callback function once request has been processed (can be used to refresh container webpage)
Example <script type="text/javascript"> var count = 0; function postActivity() { alert("about to post activity"); var title = 'this is activity post: ' + ++count; var params = {}; params[opensocial.Activity.Field.TITLE] = title; var activity = opensocial.newActivity(params); opensocial.requestCreateActivity(activity, 'high', function() { alert("activity posted; count="+count); }); alert("activity post request sent"); } </script> <br> Click on this button to post an activity: <input type="button" name="myButton“ value="Post" onclick="postActivity();" />
Reading from an activity stream • An Activity stream can be requested through a DataRequest object • newFetchActivitiesRequest(idspec) creates the request item • idspec: specifies from which people to fetch activities • Note: actual data will be retrieved and handled within the callback function • when processed, a Collection<Activity> object is returned
Example function loadActivities() { var req = opensocial.newDataRequest(); var person = opensocial.newIdSpec({ "userId" : "VIEWER"}); req.add(req.newFetchActivitiesRequest(person), 'acts'); req.send(onLoadActivities); } function onLoadActivities(data) { var myActs = data.get('acts').getData(); var html = ‘<p>'; myActs.each(function(activity) { html += gadgets.util.unescapeString(activity.getField('title')); html += '<p>'; }); document.getElementById('acts').innerHTML = html; }
About activities • The OpenSocial API supports activities so that apps may generate app messages to be posted in a container, visible to the user and the user’s friends • Currently supported differently by the different containers • Authorization (e.g., friendster allows activity stream reading, orkut and igoogle do not, by default; orkut and igoogle allow posting, friendster does not) • Rendering of activity content • Try out • the two sample applications out on your respective containers • version 5 of the gift giving application from the opensocial tutorial (wiki.opensocial.org)