Asynchronous bookmark sharing (bug 449113, r=thunder)

This commit is contained in:
Anant Narayanan 2008-08-04 17:23:23 -07:00
parent 691d0bae83
commit 8d3528d073
3 changed files with 63 additions and 7 deletions

View File

@ -253,6 +253,14 @@ DAVCollection.prototype = {
return this._makeRequest.async(this, onComplete, "POST", path, return this._makeRequest.async(this, onComplete, "POST", path,
this._defaultHeaders, data); this._defaultHeaders, data);
}, },
formPost: function DC_formPOST(path, data, onComplete) {
let headers = {'Content-type': 'application/x-www-form-urlencoded'};
headers.__proto__ = this._defaultHeaders;
return this._makeRequest.async(this, onComplete, "POST", path,
headers, data);
},
PUT: function DC_PUT(path, data, onComplete) { PUT: function DC_PUT(path, data, onComplete) {
return this._makeRequest.async(this, onComplete, "PUT", path, return this._makeRequest.async(this, onComplete, "PUT", path,

View File

@ -20,6 +20,7 @@
* Contributor(s): * Contributor(s):
* Dan Mills <thunder@mozilla.com> * Dan Mills <thunder@mozilla.com>
* Jono DiCarlo <jdicarlo@mozilla.org> * Jono DiCarlo <jdicarlo@mozilla.org>
* Anant Narayanan <anant@kix.in>
* *
* Alternatively, the contents of this file may be used under the terms of * Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or * either the GNU General Public License Version 2 or later (the "GPL"), or
@ -290,6 +291,34 @@ BookmarksSharingManager.prototype = {
self.done( true ); self.done( true );
}, },
/* FIXME! Gets all shares, not just the new ones. Doesn't impact
functionality because _incomingShareOffer does not create
duplicates, but annoys the user by showing notification of ALL
shares on EVERY sync :(
*/
getNewShares: function BmkSharing_getNewShares(onComplete) {
this._getNewShares.async(this, onComplete);
},
_getNewShares: function BmkSharing__getNewShares() {
let self = yield;
let sharingApi = new Sharing.Api( DAV );
let result = yield sharingApi.getShares(self.cb);
this._log.info("Got Shares: " + result);
let shares = result.split(',');
if (shares.length > 1) {
this._log.info('Found shares');
for (var i = 0; i < shares.length - 1; i++) {
let share = shares[i].split(':');
let name = share[0];
let user = share[1];
let path = share[2];
this._incomingShareOffer(user, '/user/' + user + '/' + path, name);
}
}
},
updateAllIncomingShares: function BmkSharing_updateAllIncoming(onComplete) { updateAllIncomingShares: function BmkSharing_updateAllIncoming(onComplete) {
this._updateAllIncomingShares.async(this, onComplete); this._updateAllIncomingShares.async(this, onComplete);
}, },
@ -436,8 +465,9 @@ BookmarksSharingManager.prototype = {
// Call Atul's js api for setting htaccess: // Call Atul's js api for setting htaccess:
let sharingApi = new Sharing.Api( DAV ); let sharingApi = new Sharing.Api( DAV );
let result = yield sharingApi.shareWithUsers( serverPath, let result = yield sharingApi.shareWithUsers( serverPath,
[username], [username], folderName,
self.cb ); self.cb );
this._log.info(result.errorText);
// return the server path: // return the server path:
self.done( serverPath ); self.done( serverPath );
}, },
@ -725,6 +755,7 @@ BookmarksEngine.prototype = {
/* After syncing the regular bookmark folder contents, /* After syncing the regular bookmark folder contents,
* also update both the incoming and outgoing shared folders. */ * also update both the incoming and outgoing shared folders. */
let self = yield; let self = yield;
let ret = yield this._sharing.getNewShares(self.cb);
this.__proto__.__proto__._sync.async(this, self.cb ); this.__proto__.__proto__._sync.async(this, self.cb );
yield; yield;
this._sharing.updateAllOutgoingShares(self.cb); this._sharing.updateAllOutgoingShares(self.cb);

View File

@ -19,6 +19,7 @@
* *
* Contributor(s): * Contributor(s):
* Atul Varma <varmaa@toolness.com> * Atul Varma <varmaa@toolness.com>
* Anant Narayanan <anant@kix.in>
* *
* Alternatively, the contents of this file may be used under the terms of * Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or * either the GNU General Public License Version 2 or later (the "GPL"), or
@ -50,14 +51,29 @@ function Api(dav) {
} }
Api.prototype = { Api.prototype = {
shareWithUsers: function Api_shareWithUsers(path, users, onComplete) { shareWithUsers: function Api_shareWithUsers(path, users, folder, onComplete) {
this._shareGenerator.async(this, return this._shareGenerator.async(this,
onComplete, onComplete,
path, path,
users); users, folder);
}, },
_shareGenerator: function Api__shareGenerator(path, users) { getShares: function Api_getShares(onComplete) {
return this._getShareGenerator.async(this, onComplete);
},
_getShareGenerator: function Api__getShareGenerator() {
let self = yield;
let id = ID.get(this._dav.identity);
this._dav.formPost("/api/share/get.php", ("uid=" + escape(id.username) +
"&password=" + escape(id.password)),
self.cb);
let xhr = yield;
self.done(xhr.responseText);
},
_shareGenerator: function Api__shareGenerator(path, users, folder) {
let self = yield; let self = yield;
let id = ID.get(this._dav.identity); let id = ID.get(this._dav.identity);
@ -67,10 +83,11 @@ Api.prototype = {
let jsonSvc = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON); let jsonSvc = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
let json = jsonSvc.encode(cmd); let json = jsonSvc.encode(cmd);
this._dav.POST("/api/share/", this._dav.formPost("/api/share/",
("cmd=" + escape(json) + ("cmd=" + escape(json) +
"&uid=" + escape(id.username) + "&uid=" + escape(id.username) +
"&password=" + escape(id.password)), "&password=" + escape(id.password) +
"&name=" + escape(folder)),
self.cb); self.cb);
let xhr = yield; let xhr = yield;