Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DELETE functionality to bags and recipe cards #8822

Open
wants to merge 4 commits into
base: multi-wiki-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions plugins/tiddlywiki/multiwikiserver/modules/mws-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,14 @@ Server.prototype.findMatchingRoute = function(request,state) {
} else {
match = potentialRoute.path.exec(pathname);
}
// Allow POST as a synonym for PUT because HTML doesn't allow PUT forms
if(match && (request.method === potentialRoute.method || (request.method === "POST" && potentialRoute.method === "PUT"))) {
// Allow POST as a synonym for PUT and DELETE because HTML doesn't allow these methods in forms
if(match && (
request.method === potentialRoute.method ||
(request.method === "POST" && (
potentialRoute.method === "PUT" ||
potentialRoute.method === "DELETE"
))
)) {
state.params = [];
for(var p=1; p<match.length; p++) {
state.params.push(match[p]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*\
title: $:/plugins/tiddlywiki/multiwikiserver/routes/handlers/delete-bag.js
type: application/javascript
module-type: mws-route

DELETE /bags/:bag-name

\*/
(function() {

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

exports.method = "DELETE";

exports.path = /^\/bags\/([^\/]+)$/;

exports.csrfDisable = true;

exports.useACL = true;

exports.entityName = "bag"

exports.handler = function(request,response,state) {
const bagName = state.params[0];
if(bagName) {
const result = $tw.mws.store.deleteBag(bagName);
if(!result) {
state.sendResponse(302,{
"Content-Type": "text/plain",
"Location": "/"
});
} else {
state.sendResponse(400,{
"Content-Type": "text/plain"
},
result.message,
"utf8");
}
} else {
state.sendResponse(400,{
"Content-Type": "text/plain"
});
}
};

}());
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*\
title: $:/plugins/tiddlywiki/multiwikiserver/routes/handlers/delete-recipe.js
type: application/javascript
module-type: mws-route

DELETE /recipes/:recipe-name

\*/
(function() {

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

exports.method = "DELETE";

exports.path = /^\/recipes\/([^\/]+)$/;

exports.csrfDisable = true;

exports.useACL = true;

exports.entityName = "recipe"

exports.handler = function(request,response,state) {
const recipeName = state.params[0];
if(recipeName) {
const result = $tw.mws.store.deleteRecipe(recipeName);
if(!result) {
state.sendResponse(302,{
"Content-Type": "text/plain",
"Location": "/"
});
} else {
state.sendResponse(400,{
"Content-Type": "text/plain"
},
result.message,
"utf8");
}
} else {
state.sendResponse(400,{
"Content-Type": "text/plain"
});
}
};

}());
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ exports.handler = function(request,response,state) {
}
};

}());
}());
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module-type: mws-route
POST /recipes

Parameters:

recipe_name
description
bag_names: space separated list of bags
Expand All @@ -32,7 +31,8 @@ exports.entityName = "recipe"

exports.handler = function(request,response,state) {
var server = state.server,
sqlTiddlerDatabase = server.sqlTiddlerDatabase
sqlTiddlerDatabase = server.sqlTiddlerDatabase;

if(state.data.recipe_name && state.data.bag_names) {
const result = $tw.mws.store.createRecipe(state.data.recipe_name,$tw.utils.parseStringArray(state.data.bag_names),state.data.description);
if(!result) {
Expand All @@ -57,4 +57,4 @@ exports.handler = function(request,response,state) {
}
};

}());
}());
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,31 @@ SqlTiddlerDatabase.prototype.listBags = function() {
return rows;
};

/*
Delete a recipe and its bag associations
*/
SqlTiddlerDatabase.prototype.deleteRecipe = function(recipe_name) {
// Delete recipe_bags entries first (due to foreign key constraints)
this.engine.runStatement(`
DELETE FROM recipe_bags
WHERE recipe_id = (
SELECT recipe_id
FROM recipes
WHERE recipe_name = $recipe_name
)
`, {
$recipe_name: recipe_name
});

// Then delete the recipe itself
this.engine.runStatement(`
DELETE FROM recipes
WHERE recipe_name = $recipe_name
`, {
$recipe_name: recipe_name
});
};

/*
Create or update a bag
Returns the bag_id of the bag
Expand All @@ -229,6 +254,19 @@ SqlTiddlerDatabase.prototype.createBag = function(bag_name,description,accesscon
return updateBags.lastInsertRowid;
};

/*
Delete a bag and all its associated data
*/
SqlTiddlerDatabase.prototype.deleteBag = function(bag_name) {
// Delete the bag (cascade will handle related records)
this.engine.runStatement(`
DELETE FROM bags
WHERE bag_name = $bag_name
`, {
$bag_name: bag_name
});
};

/*
Returns array of {recipe_name:,recipe_id:,description:,bag_names: []}
*/
Expand Down
122 changes: 81 additions & 41 deletions plugins/tiddlywiki/multiwikiserver/modules/store/sql-tiddler-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,46 @@ SqlTiddlerStore.prototype.validateItemName = function(name,allowPrivilegedCharac
return null;
};

/*
Delete a recipe. Returns null on success, or {message:} on error
*/
SqlTiddlerStore.prototype.deleteRecipe = function(recipe_name) {
var self = this;
return this.sqlTiddlerDatabase.transaction(function() {
// Check if recipe exists
const recipes = self.sqlTiddlerDatabase.listRecipes();
const recipeExists = recipes.some(recipe => recipe.recipe_name === recipe_name);
if(!recipeExists) {
return {message: "Recipe does not exist"};
}

// Delete the recipe
self.sqlTiddlerDatabase.deleteRecipe(recipe_name);
self.dispatchEvent("change");
return null;
});
};

/*
Delete a bag. Returns null on success, or {message:} on error
*/
SqlTiddlerStore.prototype.deleteBag = function(bag_name) {
var self = this;
return this.sqlTiddlerDatabase.transaction(function() {
// Check if bag exists
const bags = self.sqlTiddlerDatabase.listBags();
const bagExists = bags.some(bag => bag.bag_name === bag_name);
if(!bagExists) {
return {message: "Bag does not exist"};
}

// Delete the bag
self.sqlTiddlerDatabase.deleteBag(bag_name);
self.dispatchEvent("change");
return null;
});
};

/*
Returns null if the argument is an array of valid bag/recipe names, or a string error message if not
*/
Expand Down Expand Up @@ -144,50 +184,50 @@ SqlTiddlerStore.prototype.processOutgoingTiddler = function(tiddlerFields,tiddle
/*
*/
SqlTiddlerStore.prototype.processIncomingTiddler = function(tiddlerFields, existing_attachment_blob, existing_canonical_uri) {
let attachmentSizeLimit = $tw.utils.parseNumber(this.adminWiki.getTiddlerText("$:/config/MultiWikiServer/AttachmentSizeLimit"));
let attachmentSizeLimit = $tw.utils.parseNumber(this.adminWiki.getTiddlerText("$:/config/MultiWikiServer/AttachmentSizeLimit"));
if(attachmentSizeLimit < 100 * 1024) {
attachmentSizeLimit = 100 * 1024;
}
const attachmentsEnabled = this.adminWiki.getTiddlerText("$:/config/MultiWikiServer/EnableAttachments", "yes") === "yes";
const contentTypeInfo = $tw.config.contentTypeInfo[tiddlerFields.type || "text/vnd.tiddlywiki"];
const isBinary = !!contentTypeInfo && contentTypeInfo.encoding === "base64";

let shouldProcessAttachment = tiddlerFields.text && tiddlerFields.text.length > attachmentSizeLimit;

if(existing_attachment_blob) {
const fileSize = this.attachmentStore.getAttachmentFileSize(existing_attachment_blob);
if(fileSize <= attachmentSizeLimit) {
const existingAttachmentMeta = this.attachmentStore.getAttachmentMetadata(existing_attachment_blob);
const hasCanonicalField = !!tiddlerFields._canonical_uri;
const skipAttachment = hasCanonicalField && (tiddlerFields._canonical_uri === (existingAttachmentMeta ? existingAttachmentMeta._canonical_uri : existing_canonical_uri));
shouldProcessAttachment = !skipAttachment;
} else {
shouldProcessAttachment = false;
}
}

if(attachmentsEnabled && isBinary && shouldProcessAttachment) {
const attachment_blob = existing_attachment_blob || this.attachmentStore.saveAttachment({
text: tiddlerFields.text,
type: tiddlerFields.type,
reference: tiddlerFields.title,
_canonical_uri: tiddlerFields._canonical_uri
});
if(tiddlerFields && tiddlerFields._canonical_uri) {
delete tiddlerFields._canonical_uri;
}
return {
tiddlerFields: Object.assign({}, tiddlerFields, { text: undefined }),
attachment_blob: attachment_blob
};
} else {
return {
tiddlerFields: tiddlerFields,
attachment_blob: existing_attachment_blob
};
}
const attachmentsEnabled = this.adminWiki.getTiddlerText("$:/config/MultiWikiServer/EnableAttachments", "yes") === "yes";
const contentTypeInfo = $tw.config.contentTypeInfo[tiddlerFields.type || "text/vnd.tiddlywiki"];
const isBinary = !!contentTypeInfo && contentTypeInfo.encoding === "base64";

let shouldProcessAttachment = tiddlerFields.text && tiddlerFields.text.length > attachmentSizeLimit;

if(existing_attachment_blob) {
const fileSize = this.attachmentStore.getAttachmentFileSize(existing_attachment_blob);
if(fileSize <= attachmentSizeLimit) {
const existingAttachmentMeta = this.attachmentStore.getAttachmentMetadata(existing_attachment_blob);
const hasCanonicalField = !!tiddlerFields._canonical_uri;
const skipAttachment = hasCanonicalField && (tiddlerFields._canonical_uri === (existingAttachmentMeta ? existingAttachmentMeta._canonical_uri : existing_canonical_uri));
shouldProcessAttachment = !skipAttachment;
} else {
shouldProcessAttachment = false;
}
}

if(attachmentsEnabled && isBinary && shouldProcessAttachment) {
const attachment_blob = existing_attachment_blob || this.attachmentStore.saveAttachment({
text: tiddlerFields.text,
type: tiddlerFields.type,
reference: tiddlerFields.title,
_canonical_uri: tiddlerFields._canonical_uri
});
if(tiddlerFields && tiddlerFields._canonical_uri) {
delete tiddlerFields._canonical_uri;
}
return {
tiddlerFields: Object.assign({}, tiddlerFields, { text: undefined }),
attachment_blob: attachment_blob
};
} else {
return {
tiddlerFields: tiddlerFields,
attachment_blob: existing_attachment_blob
};
}
};

SqlTiddlerStore.prototype.saveTiddlersFromPath = function(tiddler_files_path,bag_name) {
Expand Down
Loading
Loading