0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Removed filters module

no-issue

This is no longer used now, as it was for apps to extend Ghost
This commit is contained in:
Fabien O'Carroll 2019-04-15 16:13:23 +02:00
parent 90c2dbcd6b
commit 37bcdb0a83
2 changed files with 0 additions and 239 deletions

View file

@ -1,95 +0,0 @@
// # Filters
// Filters are not yet properly used, this system is intended to allow Apps to extend Ghost in various ways.
var Promise = require('bluebird'),
pipeline = require('./lib/promise/pipeline'),
_ = require('lodash'),
defaults;
// ## Default values
/**
* A hash of default values to use instead of 'magic' numbers/strings.
* @type {Object}
*/
defaults = {
filterPriority: 5,
maxPriority: 9
};
function Filters() {
// Holds the filters
this.filterCallbacks = [];
// Holds the filter hooks (that are built in to Ghost Core)
this.filters = [];
}
// Register a new filter callback function
Filters.prototype.registerFilter = function (name, priority, fn) {
// Carry the priority optional parameter to a default of 5
if (_.isFunction(priority)) {
fn = priority;
priority = null;
}
// Null priority should be set to default
if (priority === null) {
priority = defaults.filterPriority;
}
this.filterCallbacks[name] = this.filterCallbacks[name] || {};
this.filterCallbacks[name][priority] = this.filterCallbacks[name][priority] || [];
this.filterCallbacks[name][priority].push(fn);
};
// Unregister a filter callback function
Filters.prototype.deregisterFilter = function (name, priority, fn) {
// Curry the priority optional parameter to a default of 5
if (_.isFunction(priority)) {
fn = priority;
priority = defaults.filterPriority;
}
// Check if it even exists
if (this.filterCallbacks[name] && this.filterCallbacks[name][priority]) {
// Remove the function from the list of filter funcs
this.filterCallbacks[name][priority] = _.without(this.filterCallbacks[name][priority], fn);
}
};
// Execute filter functions in priority order
Filters.prototype.doFilter = function (name, args, context) {
var callbacks = this.filterCallbacks[name],
priorityCallbacks = [];
// Bug out early if no callbacks by that name
if (!callbacks) {
return Promise.resolve(args);
}
// For each priorityLevel
_.times(defaults.maxPriority + 1, function (priority) {
// Add a function that runs its priority level callbacks in a pipeline
priorityCallbacks.push(function (currentArgs) {
var callables;
// Bug out if no handlers on this priority
if (!_.isArray(callbacks[priority])) {
return Promise.resolve(currentArgs);
}
callables = _.map(callbacks[priority], function (callback) {
return function (args) {
return callback(args, context);
};
});
// Call each handler for this priority level, allowing for promises or values
return pipeline(callables, currentArgs);
});
});
return pipeline(priorityCallbacks, args);
};
module.exports = new Filters();
module.exports.Filters = Filters;

View file

@ -1,144 +0,0 @@
var should = require('should'),
sinon = require('sinon'),
Promise = require('bluebird'),
_ = require('lodash'),
// Stuff we are testing
Filters = require('../../server/filters').Filters;
describe('Filters', function () {
var filters;
beforeEach(function () {
filters = new Filters();
});
afterEach(function () {
filters = null;
sinon.restore();
});
it('can register filters with specific priority', function () {
var filterName = 'test',
filterPriority = 9,
testFilterHandler = sinon.spy();
filters.registerFilter(filterName, filterPriority, testFilterHandler);
should.exist(filters.filterCallbacks[filterName]);
should.exist(filters.filterCallbacks[filterName][filterPriority]);
filters.filterCallbacks[filterName][filterPriority].should.containEql(testFilterHandler);
});
it('can register filters with default priority', function () {
var filterName = 'test',
defaultPriority = 5,
testFilterHandler = sinon.spy();
filters.registerFilter(filterName, testFilterHandler);
should.exist(filters.filterCallbacks[filterName]);
should.exist(filters.filterCallbacks[filterName][defaultPriority]);
filters.filterCallbacks[filterName][defaultPriority].should.containEql(testFilterHandler);
});
it('can register filters with priority null with default priority', function () {
var filterName = 'test',
defaultPriority = 5,
testFilterHandler = sinon.spy();
filters.registerFilter(filterName, null, testFilterHandler);
should.exist(filters.filterCallbacks[filterName]);
should.exist(filters.filterCallbacks[filterName][defaultPriority]);
filters.filterCallbacks[filterName][defaultPriority].should.containEql(testFilterHandler);
});
it('executes filters in priority order', function (done) {
var filterName = 'testpriority',
testFilterHandler1 = sinon.spy(),
testFilterHandler2 = sinon.spy(),
testFilterHandler3 = sinon.spy();
filters.registerFilter(filterName, 0, testFilterHandler1);
filters.registerFilter(filterName, 2, testFilterHandler2);
filters.registerFilter(filterName, 9, testFilterHandler3);
filters.doFilter(filterName, null).then(function () {
testFilterHandler1.calledBefore(testFilterHandler2).should.equal(true);
testFilterHandler2.calledBefore(testFilterHandler3).should.equal(true);
testFilterHandler3.called.should.equal(true);
done();
});
});
it('executes filters that return a promise', function (done) {
var filterName = 'testprioritypromise',
testFilterHandler1 = sinon.spy(function (args) {
return new Promise(function (resolve) {
process.nextTick(function () {
args.filter1 = true;
resolve(args);
});
});
}),
testFilterHandler2 = sinon.spy(function (args) {
args.filter2 = true;
return args;
}),
testFilterHandler3 = sinon.spy(function (args) {
return new Promise(function (resolve) {
process.nextTick(function () {
args.filter3 = true;
resolve(args);
});
});
});
filters.registerFilter(filterName, 0, testFilterHandler1);
filters.registerFilter(filterName, 2, testFilterHandler2);
filters.registerFilter(filterName, 9, testFilterHandler3);
filters.doFilter(filterName, {test: true}).then(function (newArgs) {
testFilterHandler1.calledBefore(testFilterHandler2).should.equal(true);
testFilterHandler2.calledBefore(testFilterHandler3).should.equal(true);
testFilterHandler3.called.should.equal(true);
newArgs.filter1.should.equal(true);
newArgs.filter2.should.equal(true);
newArgs.filter3.should.equal(true);
done();
}).catch(done);
});
it('executes filters with a context', function (done) {
var filterName = 'textContext',
testFilterHandler1 = sinon.spy(function (args, context) {
args.context1 = _.isObject(context);
return args;
}),
testFilterHandler2 = sinon.spy(function (args, context) {
args.context2 = _.isObject(context);
return args;
});
filters.registerFilter(filterName, 0, testFilterHandler1);
filters.registerFilter(filterName, 1, testFilterHandler2);
filters.doFilter(filterName, {test: true}, {context: true}).then(function (newArgs) {
newArgs.context1.should.equal(true);
newArgs.context2.should.equal(true);
done();
}).catch(done);
});
});