1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-26 01:50:28 +00:00
TiddlyWiki5/core/modules/server/routes/get-tiddlers-json.js
saqimtiaz e2bea854b6
System tiddler syncing (#4987)
* First pass at a fix for system tiddler sync issues

* Add new filter syncFromServer

* Undo previous attempt at a fix

* Added a flag to control sync of system tiddlers from server
2020-11-19 15:59:02 +00:00

52 lines
1.6 KiB
JavaScript

/*\
title: $:/core/modules/server/routes/get-tiddlers-json.js
type: application/javascript
module-type: route
GET /recipes/default/tiddlers.json?filter=<filter>
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var DEFAULT_FILTER = "[all[tiddlers]!is[system]sort[title]]";
exports.method = "GET";
exports.path = /^\/recipes\/default\/tiddlers.json$/;
exports.handler = function(request,response,state) {
var filter = state.queryParameters.filter || DEFAULT_FILTER;
if(state.wiki.getTiddlerText("$:/config/Server/AllowAllExternalFilters") !== "yes") {
if(state.wiki.getTiddlerText("$:/config/Server/ExternalFilters/" + filter) !== "yes") {
console.log("Blocked attempt to GET /recipes/default/tiddlers.json with filter: " + filter);
response.writeHead(403);
response.end();
return;
}
}
if(state.wiki.getTiddlerText("$:/config/SyncSystemTiddlersFromServer") === "no") {
filter += "+[!is[system]]";
}
var excludeFields = (state.queryParameters.exclude || "text").split(","),
titles = state.wiki.filterTiddlers(filter);
response.writeHead(200, {"Content-Type": "application/json"});
var tiddlers = [];
$tw.utils.each(titles,function(title) {
var tiddler = state.wiki.getTiddler(title);
if(tiddler) {
var tiddlerFields = tiddler.getFieldStrings({exclude: excludeFields});
tiddlerFields.revision = state.wiki.getChangeCount(title);
tiddlerFields.type = tiddlerFields.type || "text/vnd.tiddlywiki";
tiddlers.push(tiddlerFields);
}
});
var text = JSON.stringify(tiddlers);
response.end(text,"utf8");
};
}());