Refactor _canonical_uri handling out of route handlers

This commit is contained in:
Jeremy Ruston 2024-01-23 10:51:12 +00:00
parent da5b316358
commit e343eccdc3
5 changed files with 45 additions and 21 deletions

View File

@ -39,8 +39,6 @@ exports.handler = function(request,response,state) {
tiddlerFields.fields[name] = value;
}
});
tiddlerFields.revision = "" + result.tiddler_id;
tiddlerFields.bag = bag_name;
tiddlerFields.type = tiddlerFields.type || "text/vnd.tiddlywiki";
state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(tiddlerFields),"utf8");
} else {

View File

@ -39,8 +39,6 @@ exports.handler = function(request,response,state) {
tiddlerFields.fields[name] = value;
}
});
tiddlerFields.revision = "" + tiddlerInfo.tiddler_id;
tiddlerFields.bag = tiddlerInfo.bag_name;
tiddlerFields.type = tiddlerFields.type || "text/vnd.tiddlywiki";
state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(tiddlerFields),"utf8");
} else {

View File

@ -29,7 +29,7 @@ exports.handler = function(request,response,state) {
var tiddlers = [];
$tw.utils.each(recipeTiddlers,function(recipeTiddlerInfo) {
var tiddlerInfo = $tw.sqlTiddlerStore.getRecipeTiddler(recipeTiddlerInfo.title,recipe_name);
tiddlers.push(Object.assign({},tiddlerInfo.tiddler,{text: undefined, revision: "" + tiddlerInfo.tiddler_id, bag: recipeTiddlerInfo.bag_name}));
tiddlers.push(Object.assign({},tiddlerInfo.tiddler,{text: undefined}));
});
var text = JSON.stringify(tiddlers);
state.sendResponse(200,{"Content-Type": "application/json"},text,"utf8");

View File

@ -50,20 +50,7 @@ exports.handler = function(request,response,state) {
}
response.write(template.substring(0,markerPos + marker.length));
$tw.utils.each(recipeTiddlers,function(recipeTiddlerInfo) {
var tiddlerInfo = $tw.sqlTiddlerStore.getRecipeTiddler(recipeTiddlerInfo.title,recipe_name);
if((tiddlerInfo.tiddler.text || "").length > 10 * 1024 * 1024) {
response.write(JSON.stringify(Object.assign({},tiddlerInfo.tiddler,{
revision: "" + tiddlerInfo.tiddler_id,
bag: recipeTiddlerInfo.bag_name,
text: undefined,
_canonical_uri: `/wiki/${recipe_name}/recipes/${recipe_name}/tiddlers/${title}`
})));
} else {
response.write(JSON.stringify(Object.assign({},tiddlerInfo.tiddler,{
revision: "" + tiddlerInfo.tiddler_id,
bag: recipeTiddlerInfo.bag_name
})));
}
response.write(JSON.stringify($tw.sqlTiddlerStore.getRecipeTiddler(recipeTiddlerInfo.title,recipe_name).tiddler));
response.write(",")
});
response.write(JSON.stringify({title: "$:/config/tiddlyweb/host",text: "$protocol$//$host$$pathname$/"}));

View File

@ -8,6 +8,7 @@ Higher level functions to perform basic tiddler operations with a sqlite3 databa
This class is largely a wrapper for the sql-tiddler-database.js class, adding the following functionality:
* Synchronising bag and recipe names to the admin wiki
* Handling _canonical_uri tiddlers
\*/
@ -61,6 +62,30 @@ SqlTiddlerStore.prototype.updateAdminWiki = function() {
}
};
/*
Given tiddler fields, tiddler_id and a bagname, return the tiddler fields after the following process:
- If the text field is over a threshold, modify the tiddler to use _canonical_uri
- Apply the tiddler_id as the revision field
- Apply the bag_name as the bag field
*/
SqlTiddlerStore.prototype.processOutgoingTiddler = function(tiddlerFields,tiddler_id,bag_name,recipe_name) {
if((tiddlerFields.text || "").length > 10 * 1024 * 1024) {
return Object.assign({},tiddlerFields,{
revision: "" + tiddler_id,
bag: bag_name,
text: undefined,
_canonical_uri: recipe_name
? `/wiki/${recipe_name}/recipes/${recipe_name}/tiddlers/${title}`
: `/wiki/${bag_name}/bags/${bag_name}/tiddlers/${title}`
});
} else {
return Object.assign({},tiddlerFields,{
revision: "" + tiddler_id,
bag: bag_name
});
}
};
SqlTiddlerStore.prototype.logTables = function() {
this.sqlTiddlerDatabase.logTables();
};
@ -114,14 +139,30 @@ SqlTiddlerStore.prototype.deleteTiddler = function(title,bagname) {
returns {tiddler_id:,tiddler:}
*/
SqlTiddlerStore.prototype.getBagTiddler = function(title,bagname) {
return this.sqlTiddlerDatabase.getBagTiddler(title,bagname);
var tiddlerInfo = this.sqlTiddlerDatabase.getBagTiddler(title,bagname);
if(tiddlerInfo) {
return Object.assign(
{},
tiddlerInfo,
{
tiddler: this.processOutgoingTiddler(tiddlerInfo.tiddler,tiddlerInfo.tiddler_id,bagname,null)
});
} else {
return null;
}
};
/*
Returns {bag_name:, tiddler: {fields}, tiddler_id:}
*/
SqlTiddlerStore.prototype.getRecipeTiddler = function(title,recipename) {
return this.sqlTiddlerDatabase.getRecipeTiddler(title,recipename);
var tiddlerInfo = this.sqlTiddlerDatabase.getRecipeTiddler(title,recipename);
return Object.assign(
{},
tiddlerInfo,
{
tiddler: this.processOutgoingTiddler(tiddlerInfo.tiddler,tiddlerInfo.tiddler_id,tiddlerInfo.bag_name,recipename)
});
};
/*