Recently in opensocial-0.7 Category

Sending private messages on Friendster

| | Comments (0) | TrackBacks (0)
I had to write a quick sample today to send a private message to the OWNER on Friendster.  The following sends a message to your Friendster message inbox:

function sendNotification() {
  var params = {};
  params[opensocial.Message.Field.TITLE] =
      "Title of the notification goes here";
  params[opensocial.Message.Field.TYPE] =
      opensocial.Message.Type.PRIVATE_MESSAGE;
  var body="Text of the notification goes here";
  var message = opensocial.newMessage(body, params);
  var recipient = opensocial.DataRequest.PersonId.OWNER;
  opensocial.requestSendMessage(recipient, message,
      onSendNotification);
};

function onSendNotification(resp) {
  if (!resp.hadError() && resp.getData().status == "sent") {
    alert("The message was sent to the OWNER");
  } else {
    alert("There was a problem: " + resp.getErrorMessage());
  }
};

sendNotification();
Note the message type is set to PRIVATE_MESSAGE.  Friendster also supports type NOTIFICATION, but I haven't quite figured out where that shows up, and I get a "Insufficient permissions for action 'publicMessage'" error message when trying PUBLIC_MESSAGE.

Also note that this is for OpenSocial 0.7.  If you're migrating this code to another container on 0.8, you'll need to change the IdSpec stuff (as I've covered before).

Fetch by ID sample

| | Comments (0) | TrackBacks (0)
I needed to write the following sample of code to test fetching people by ID number.  This is an interesting sample because it demonstrates handling an error condition - namely, the "unauthorized" response that you should get when you try to access an account that you don't have permission to request.  On orkut, this means people who don't have your app installed, even if they're your friends.

The following runs in DAfOS:

function onPersonGot(result) {
  var response = result.get("person");
  if (response.hadError()) {
    output("There was a problem fetching this user: " + 
        response.getErrorCode());
  } else {
    output("Fetched " + 
        result.get("person").getData().getDisplayName());
  }
  gadgets.window.adjustHeight();
};

function closeGetPerson(id) {
  return function() {
    var params = {};
    var req = opensocial.newDataRequest();
    req.add(req.newFetchPersonRequest(id, params), "person");
    req.send(onPersonGot);
  };
};


function closePrintFriend(list) {
  return function(friend) {
    var item = document.createElement("li");
    var link = document.createElement("a");
    var text = document.createTextNode(friend.getDisplayName());
    list.appendChild(item);
    item.appendChild(link);
    item.appendChild(document.createTextNode(" [" + 
        friend.getId() + "]"));
    link.appendChild(text);
    link.onclick = closeGetPerson(friend.getId());
    link.href = "javascript:void(0);";
  };
};

function gotFriends(data) {
  var friends = data.get("of").getData();
  var list = document.createElement("ul");
  var main = document.getElementById("dom_handle")
  main.innerHTML = "";
  main.appendChild(list);
  friends.each(closePrintFriend(list));
  gadgets.window.adjustHeight();
};

function getFriends() {
  var params = {};
  params[opensocial.DataRequest.PeopleRequestFields.MAX] = 1000;
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPeopleRequest(
      opensocial.DataRequest.Group.OWNER_FRIENDS, params), "of");
  req.send(gotFriends);
};

getFriends();
Note that the example lists the friends of the owner as links.  Clicking on each name will attempt to retrieve that user by ID number. 

fetch_by_ids_2.png

On orkut, if a user doesn't have DAfOS installed, you'll get an "unauthorized" error:

fetch_by_ids_1.png

In your applications, make sure to handle cases like this, since you won't always be able to guarantee that all containers will have the same policies around which user accounts you may directly access.

About this Archive

This page is a archive of recent entries in the opensocial-0.7 category.

opensocial-0.8 is the next category.

Find recent content on the main index or look in the archives to find all content.