Author: doll
Date: Wed Jun 11 17:39:45 2008
New Revision: 666933
URL: http://svn.apache.org/viewvc?rev=666933&view=rev
Log:
Updated the restfulcontainer to speak the opensocial 0.8 format. (ie the new
idSpec and removeAppData).
Also implemented createActivity and fixed some bugs in the other methods.
Modified:
incubator/shindig/trunk/features/opensocial-current/jsonactivity.js
incubator/shindig/trunk/features/opensocial-current/restfulcontainer.js
Modified: incubator/shindig/trunk/features/opensocial-current/jsonactivity.js
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/opensocial-current/jsonactivity.js?rev=666933&r1=666932&r2=666933&view=diff
==============================================================================
--- incubator/shindig/trunk/features/opensocial-current/jsonactivity.js
(original)
+++ incubator/shindig/trunk/features/opensocial-current/jsonactivity.js Wed Jun
11 17:39:45 2008
@@ -35,7 +35,7 @@
JsonActivity.prototype.toJsonObject = function() {
var jsonObject = JsonActivity.copyFields(this.fields_);
- var oldMediaItems = jsonObject['mediaItems'];
+ var oldMediaItems = jsonObject['mediaItems'] || [];
var newMediaItems = [];
for (var i = 0; i < oldMediaItems.length; i++) {
newMediaItems[i] = oldMediaItems[i].toJsonObject();
Modified:
incubator/shindig/trunk/features/opensocial-current/restfulcontainer.js
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/opensocial-current/restfulcontainer.js?rev=666933&r1=666932&r2=666933&view=diff
==============================================================================
--- incubator/shindig/trunk/features/opensocial-current/restfulcontainer.js
(original)
+++ incubator/shindig/trunk/features/opensocial-current/restfulcontainer.js Wed
Jun 11 17:39:45 2008
@@ -59,7 +59,13 @@
RestfulContainer.prototype.requestCreateActivity = function(activity,
priority, opt_callback) {
opt_callback = opt_callback || {};
- // TODO: Implement this
+
+ var req = opensocial.newDataRequest();
+ var viewer = new opensocial.IdSpec({'userId' : 'VIEWER'});
+ req.add(this.newCreateActivityRequest(viewer, activity), 'key');
+ req.send(function(response) {
+ opt_callback(response.get('key'));
+ });
};
RestfulContainer.prototype.requestData = function(dataRequest, callback) {
@@ -89,12 +95,19 @@
var makeRequestParams = {
"CONTENT_TYPE" : "JSON",
"METHOD" : requestObject.request.method
- // TODO: Handle post data
};
+ if (requestObject.request.postData) {
+ makeRequestParams["POST_DATA"]
+ = gadgets.io.encodeValues(requestObject.request.postData);
+ }
+
+ var url = requestObject.request.url;
+ var separator = url.indexOf("?") != -1 ? "&" : "?";
+
// TODO: Use batching instead of doing this one by one
gadgets.io.makeNonProxiedRequest(
- baseUrl + requestObject.request.url + "&st=" + st,
+ baseUrl + url + separator + "st=" + st,
function(result) {
result = result.data;
@@ -116,32 +129,39 @@
};
-RestfulContainer.prototype.translateIdSpec = function(idSpec) {
- // This will get cleaner in 0.8 because idSpec will be an object
+RestfulContainer.prototype.makeIdSpec = function(id) {
+ return new opensocial.IdSpec({'userId' : id});
+}
- // TODO: Some of these rest urls return "feeds" and some return "entries"..
- // this is going to get complicated unless we can get them all in the same
- // format
- if (idSpec == "VIEWER") {
- return this.viewerId_ + "/@self";
- } else if (idSpec == "VIEWER_FRIENDS") {
- return this.viewerId_ + "/@friends";
- } else if (idSpec == "OWNER") {
- return this.ownerId_ + "/@self";
- } else if (idSpec == "OWNER_FRIENDS") {
- return this.ownerId_ + "/@friends";
+RestfulContainer.prototype.translateIdSpec = function(newIdSpec) {
+ var userId = newIdSpec.getField('userId');
+ var groupId = newIdSpec.getField('groupId');
+
+ if (userId == 'OWNER') {
+ userId = this.ownerId_;
+ } else if (userId == 'VIEWER') {
+ userId = this.viewerId_;
} else if (opensocial.Container.isArray(idSpec)) {
for (var i = 0; i < idSpec.length; i++) {
// TODO: We will need multiple urls here....don't want to think about
// that yet
}
- } else {
- return idSpec + "/@self";
}
+
+ if (groupId == 'FRIENDS') {
+ groupId = "@friends";
+ } else if (!groupId) {
+ groupId = "@self";
+ }
+
+ // TODO: Network distance
+
+ return userId + "/" + groupId;
};
RestfulContainer.prototype.newFetchPersonRequest = function(id, opt_params) {
- var peopleRequest = this.newFetchPeopleRequest(id, opt_params);
+ var peopleRequest = this.newFetchPeopleRequest(
+ this.makeIdSpec(id), opt_params);
var me = this;
return new RestfulRequestItem(peopleRequest.url, peopleRequest.method, null,
@@ -154,13 +174,12 @@
opt_params) {
var url = "/people/" + this.translateIdSpec(idSpec);
- // TODO: Add sortOrder, filter
- // 'sortOrder': opt_params['sortOrder'] || 'topFriends',
- // 'filter': opt_params['filter'] || 'all',
-
url += "?fields=" + (opt_params['profileDetail'].join(','));
- url += "&startPage=" + (opt_params['first'] || 0);
+ url += "&startIndex=" + (opt_params['first'] || 0);
url += "&count=" + (opt_params['max'] || 20);
+ url += "&orderBy=" + (opt_params['sortOrder'] || 'topFriends');
+ // TODO: This filterBy isn't in the spec
+ url += "&filterBy=" + (opt_params['filter'] || 'all');
var me = this;
return new RestfulRequestItem(url, "GET", null,
@@ -179,10 +198,18 @@
return new JsonPerson(serverJson);
}
+RestfulContainer.prototype.getFieldsList = function(keys) {
+ if (opensocial.Container.isArray(keys)) {
+ return keys.join(',');
+ } else {
+ return keys;
+ }
+}
+
RestfulContainer.prototype.newFetchPersonAppDataRequest = function(idSpec,
keys) {
- var url = "/appdata/" + this.translateIdSpec(idSpec) + "/" + this.appId_
- + "?fields=" + keys.join(',');
+ var url = "/appdata/" + this.translateIdSpec(idSpec) + "/" + this.appId_
+ + "?fields=" + this.getFieldsList(keys);
return new RestfulRequestItem(url, "GET", null,
function (appData) {
return gadgets.util.escape(appData['entry'], true);
@@ -191,10 +218,19 @@
RestfulContainer.prototype.newUpdatePersonAppDataRequest = function(id, key,
value) {
- var url = "/appdata/" + this.translateIdSpec(id) + "/" + this.appId_
- + "?fields=" + key;
- // TODO: Or should we use POST?
- return new RestfulRequestItem(url, "PUT", {key: value});
+ var url = "/appdata/" + this.translateIdSpec(this.makeIdSpec(id))
+ + "/" + this.appId_ + "?fields=" + key;
+ var data = {};
+ data[key] = value;
+ var postData = {};
+ postData['entry'] = gadgets.json.stringify(data);
+ return new RestfulRequestItem(url, "POST", postData);
+};
+
+RestfulContainer.prototype.newRemovePersonAppDataRequest = function(id, keys) {
+ var url = "/appdata/" + this.translateIdSpec(this.makeIdSpec(id))
+ + "/" + this.appId_ + "?fields=" + this.getFieldsList(keys);
+ return new RestfulRequestItem(url, "DELETE");
};
RestfulContainer.prototype.newFetchActivitiesRequest = function(idSpec,
@@ -208,14 +244,28 @@
for (var i = 0; i < rawJson.length; i++) {
activities.push(new JsonActivity(rawJson[i]));
}
- return {'activities' : new opensocial.Collection(activities)};
+ return new opensocial.Collection(activities);
});
};
+RestfulContainer.prototype.newActivity = function(opt_params) {
+ return new JsonActivity(opt_params, true);
+};
+
+RestfulContainer.prototype.newMediaItem = function(mimeType, url, opt_params) {
+ opt_params = opt_params || {};
+ opt_params['mimeType'] = mimeType;
+ opt_params['url'] = url;
+ return new JsonMediaItem(opt_params);
+};
+
RestfulContainer.prototype.newCreateActivityRequest = function(idSpec,
activity) {
- // TODO: no idea how to do this yet
- return new RestfulRequestItem("TODO", "POST", {});
+ var url = "/activities/" + this.translateIdSpec(idSpec)
+ + "/" + this.appId_;
+ var postData = {};
+ postData['entry'] = gadgets.json.stringify(activity.toJsonObject());
+ return new RestfulRequestItem(url, "POST", postData);
};
RestfulRequestItem = function(url, method, postData, processData) {