So I've been working on an OpenSocial app for a while. A lot of things come in quite handy and are needed relatively often. One of those things is checking if the current viewer is the owner of the application (if the viewer is the person that has added the application to his/her profile and is viewing the app on his/her profile or canvas page at the moment). On Facebook you have a tag for that:
<fb:visible-to-owner>
Welcome back to your profile!
</fb:visible-to-owner>
How useful! Well, this doesn't exist on OpenSocial. So what you have to do is compare the OpenSocial ID for the current view to the ID of the owner. If both are the same, the viewer is the owner and the owner is the viewer and everyone is happy.
So what you do is get both IDs when the app is loaded and you remember them in a bunch of variables. I added one more thing, though: Getting the Orkut UID since the Orkut UID is not the same as the OpenSocial ID of the user. You need the Orkut UID to construct links to a person's canvas page. It's a really ugly hack, but
that's how you're supposed to do it according to Google.
Having the Orkut UID, you can construct links to the canvas page of your app for a certain user. This is useful when you have a canvas page you want to link to for your friends to look at (showing the latest photos or videos you've uploaded or some other useless crap nobody will look at anyway).
Here's a little code sample (note: SAMPLE, you still need to add error checking and stuff) for constructing an Activity Stream update on Orkut that has a link to the canvas page of the person who's posting the activity:
const CANVAS_BASE_URL =
'http://sandbox.orkut.com/Application.aspx'; //Orkut specific!
function postActivity(title, body) {
var params = {};
params[opensocial.Activity.Field.TITLE] = title;
if (body != null) {
params[opensocial.Activity.Field.BODY] = body;
}
var activity = opensocial.newActivity(params);
opensocial.requestCreateActivity(activity,
opensocial.CreateActivityPriority.HIGH,
postActivityCallback);
};
function postActivityCallback(data) {
if (DEBUG) console.log(data);
};
function createASampleActivity() {
var title = 'Something to waste 2 minutes of your life!';
var body = '<a href="' + CANVAS_BASE_URL +
'?uid=' + viewer_network_uid +
'&appId=' + stored_app_id +
'">Check them out!</a>';
postActivity(title, body);
}
Ok, and here's the code for getting all the IDs and a useful function to check if the viewer is the owner or not. Just call it during the initialization of your gadget with gadgets.util.registerOnLoadHandler(setupViewerOwnerIds);
const DEBUG = true;
/** not the opensocial uid, but the users
uid on the current social network, ie Orkut or MySpace */
var viewer_network_uid = '';
/** current viewer's opensocial id */
var viewer_os_id = null;
/** owner's opensocial id */
var owner_os_id = null;
var stored_app_id = '1111111111111'; //for orkut!
function setupViewerOwnerIds() {
var req=opensocial.newDataRequest();
req.add(req.newFetchPersonRequest("VIEWER"), "viewer");
req.add(req.newFetchPersonRequest("OWNER"), "owner");
req.send(setupViewerOwnerIdsCallback);
}
function setupViewerOwnerIdsCallback(data) {
if (data.hadError()) {
if (DEBUG) console.log("setupViewerOwnerIdsCallback had an error");
viewer_os_id = null;
owner_os_id = null;
} else {
var viewer = data.get("viewer").getData();
var owner = data.get("owner").getData();
viewer_os_id = viewer.getId();
owner_os_id = owner.getId();
//Orkut specific
if (opensocial.getEnvironment().getDomain() == 'orkut.com') {
var profile_url =
viewer.getField(opensocial.Person.Field.PROFILE_URL);
var regex = /uid=([^&#]+)/;
var result = profile_url.match(regex);
if (result.length == 2) {
viewer_network_uid = result[1];
/* uid now contains the viewer's orkut UID */
} else {
/* there was a problem getting the UID */
}
}
if (DEBUG) console.log('Viewer: ' + viewer_os_id);
if (DEBUG) console.log('Viewer Network UID: ' +
viewer_network_uid);
if (DEBUG) console.log('Owner: ' + owner_os_id);
}
if (DEBUG) console.log('isViewerOwner: ' + isViewerOwner());
}
function isViewerOwner() {
if (viewer_os_id != null &&
owner_os_id != null &&
viewer_os_id == owner_os_id) {
return true;
} else {
return false;
}
}
I can't take credit for most of this, since a lot of stuff is taken from the Orkut and OpenSocial wikis and mailing lists. My thanks go out to all the great people out there sharing their code and knowledge!
UPDATE: The code now also works when someone visits your app who doesn't have the app installed (produced an "unauthorized" error before while trying to fetch the viewer data).