/*\ title: $:/plugins/tiddlywiki/tiddlyweb/tiddlywebadaptor.js type: application/javascript module-type: syncadaptor A sync adaptor module for synchronising with TiddlyWeb compatible servers \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; function TiddlyWebAdaptor(syncer) { this.syncer = syncer; this.host = document.location.protocol + "//" + document.location.host + "/"; this.recipe = undefined; } TiddlyWebAdaptor.prototype.getTiddlerInfo = function(tiddler) { return { bag: tiddler.fields["bag"] }; }; /* Get the current status of the TiddlyWeb connection */ TiddlyWebAdaptor.prototype.getStatus = function(callback) { // Get status var self = this, wiki = self.syncer.wiki; this.syncer.log("Getting status"); $tw.utils.httpRequest({ url: this.host + "status", callback: function(err,data) { if(err) { return callback(err); } // Decode the status JSON var json = null, isLoggedIn = false; try { json = JSON.parse(data); } catch (e) { } if(json) { // Record the recipe if(json.space) { self.recipe = json.space.recipe; } // Check if we're logged in isLoggedIn = json.username !== "GUEST"; } // Invoke the callback if present if(callback) { callback(null,isLoggedIn,json.username); } } }); }; /* Attempt to login and invoke the callback(err) */ TiddlyWebAdaptor.prototype.login = function(username,password,callback) { var self = this; $tw.utils.httpRequest({ url: this.host + "challenge/tiddlywebplugins.tiddlyspace.cookie_form", type: "POST", data: { user: username, password: password, tiddlyweb_redirect: "/status" // workaround to marginalize automatic subsequent GET }, callback: function(err) { callback(err); } }); }; /* */ TiddlyWebAdaptor.prototype.logout = function(callback) { var self = this; $tw.utils.httpRequest({ url: this.host + "logout", type: "POST", data: { csrf_token: this.getCsrfToken(), tiddlyweb_redirect: "/status" // workaround to marginalize automatic subsequent GET }, callback: function(err,data) { callback(err); } }); }; /* Retrieve the CSRF token from its cookie */ TiddlyWebAdaptor.prototype.getCsrfToken = function() { var regex = /^(?:.*; )?csrf_token=([^(;|$)]*)(?:;|$)/, match = regex.exec(document.cookie), csrf = null; if (match && (match.length === 2)) { csrf = match[1]; } return csrf; }; /* Get an array of skinny tiddler fields from the server */ TiddlyWebAdaptor.prototype.getSkinnyTiddlers = function(callback) { $tw.utils.httpRequest({ url: this.host + "recipes/" + this.recipe + "/tiddlers.json", callback: function(err,data) { // Check for errors if(err) { return callback(err); } // Process the tiddlers to make sure the revision is a string var tiddlers = JSON.parse(data); for(var t=0; t//<revision>:<hash> ``` */ TiddlyWebAdaptor.prototype.parseEtag = function(etag) { var firstSlash = etag.indexOf("/"), lastSlash = etag.lastIndexOf("/"), colon = etag.lastIndexOf(":"); if(firstSlash === -1 || lastSlash === -1 || colon === -1) { return null; } else { return { bag: decodeURIComponent(etag.substring(1,firstSlash)), title: decodeURIComponent(etag.substring(firstSlash + 1,lastSlash)), revision: etag.substring(lastSlash + 1,colon) }; } }; if($tw.browser) { exports.adaptorClass = TiddlyWebAdaptor; } })();