1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-12 00:34:00 +00:00

Add basic support for detecting readonly status and avoiding write errors

We now have syncadaptors returning  readonly status and avoid attempting to write to the server if it's going to fail
This commit is contained in:
Jermolene 2018-06-27 11:09:42 +01:00
parent 620116a0b4
commit 740a9b170e
2 changed files with 17 additions and 7 deletions

View File

@ -16,6 +16,7 @@ The syncer tracks changes to the store. If a syncadaptor is used then individual
Defaults
*/
Syncer.prototype.titleIsLoggedIn = "$:/status/IsLoggedIn";
Syncer.prototype.titleIsReadOnly = "$:/status/IsReadOnly";
Syncer.prototype.titleUserName = "$:/status/UserName";
Syncer.prototype.titleSyncFilter = "$:/config/SyncFilter";
Syncer.prototype.titleSavedNotification = "$:/language/Notifications/Save/Done";
@ -169,12 +170,13 @@ Syncer.prototype.getStatus = function(callback) {
// Mark us as not logged in
this.wiki.addTiddler({title: this.titleIsLoggedIn,text: "no"});
// Get login status
this.syncadaptor.getStatus(function(err,isLoggedIn,username) {
this.syncadaptor.getStatus(function(err,isLoggedIn,username,isReadOnly) {
if(err) {
self.logger.alert(err);
return;
}
// Set the various status tiddlers
self.wiki.addTiddler({title: self.titleIsReadOnly,text: isReadOnly ? "yes" : "no"});
self.wiki.addTiddler({title: self.titleIsLoggedIn,text: isLoggedIn ? "yes" : "no"});
if(isLoggedIn) {
self.wiki.addTiddler({title: self.titleUserName,text: username || ""});

View File

@ -21,6 +21,8 @@ function TiddlyWebAdaptor(options) {
this.recipe = undefined;
this.hasStatus = false;
this.logger = new $tw.utils.Logger("TiddlyWebAdaptor");
this.isLoggedIn = false;
this.isReadOnly = false;
}
TiddlyWebAdaptor.prototype.name = "tiddlyweb";
@ -63,8 +65,7 @@ TiddlyWebAdaptor.prototype.getStatus = function(callback) {
return callback(err);
}
// Decode the status JSON
var json = null,
isLoggedIn = false;
var json = null;
try {
json = JSON.parse(data);
} catch (e) {
@ -76,11 +77,12 @@ TiddlyWebAdaptor.prototype.getStatus = function(callback) {
self.recipe = json.space.recipe;
}
// Check if we're logged in
isLoggedIn = json.username !== "GUEST";
self.isLoggedIn = json.username !== "GUEST";
self.isReadOnly = !!json["read_only"];
}
// Invoke the callback if present
if(callback) {
callback(null,isLoggedIn,json.username);
callback(null,self.isLoggedIn,json.username,self.isReadOnly);
}
}
});
@ -165,6 +167,9 @@ Save a tiddler and invoke the callback with (err,adaptorInfo,revision)
*/
TiddlyWebAdaptor.prototype.saveTiddler = function(tiddler,callback) {
var self = this;
if(this.isReadOnly) {
return callback(null);
}
$tw.utils.httpRequest({
url: this.host + "recipes/" + encodeURIComponent(this.recipe) + "/tiddlers/" + encodeURIComponent(tiddler.fields.title),
type: "PUT",
@ -209,9 +214,12 @@ options include:
tiddlerInfo: the syncer's tiddlerInfo for this tiddler
*/
TiddlyWebAdaptor.prototype.deleteTiddler = function(title,callback,options) {
var self = this,
bag = options.tiddlerInfo.adaptorInfo.bag;
var self = this;
if(this.isReadOnly) {
return callback(null);
}
// If we don't have a bag it means that the tiddler hasn't been seen by the server, so we don't need to delete it
var bag = options.tiddlerInfo.adaptorInfo.bag;
if(!bag) {
return callback(null);
}