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

Remove featured, tag, author & role API params #6005

Merged
merged 1 commit into from
Oct 27, 2015
Merged
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
2 changes: 1 addition & 1 deletion core/server/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ posts = {
* @returns {Promise<Posts>} Posts Collection with Meta
*/
browse: function browse(options) {
var extraOptions = ['tag', 'author', 'status', 'staticPages', 'featured'],
var extraOptions = ['status', 'staticPages'],
permittedOptions = utils.browseDefaultOptions.concat(extraOptions),
tasks;

Expand Down
2 changes: 1 addition & 1 deletion core/server/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ users = {
* @returns {Promise<Users>} Users Collection
*/
browse: function browse(options) {
var extraOptions = ['role', 'status'],
var extraOptions = ['status'],
permittedOptions = utils.browseDefaultOptions.concat(extraOptions),
tasks;

Expand Down
8 changes: 0 additions & 8 deletions core/server/helpers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ function resolvePaths(data, value) {
* @returns {*}
*/
function parseOptions(data, options) {
if (_.isArray(options.tag)) {
options.tag = _.pluck(options.tag, 'slug').join(',');
}

if (_.isObject(options.author)) {
options.author = options.author.slug;
}

if (_.isString(options.filter)) {
options.filter = resolvePaths(data, options.filter);
}
Expand Down
51 changes: 22 additions & 29 deletions core/server/models/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({

var self = this,
itemCollection = this.forge(),
tableName = _.result(this.prototype, 'tableName'),
filterObjects = self.setupFilters(options);
tableName = _.result(this.prototype, 'tableName');

// Filter options so that only permitted ones remain
options = this.filterOptions(options, 'findPage');
Expand All @@ -283,39 +282,33 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
options.columns = _.intersection(options.columns, this.prototype.permittedAttributes());
}

// Prefetch filter objects
return Promise.all(baseUtils.oldFiltering.preFetch(filterObjects)).then(function doQuery() {
// If there are `where` conditionals specified, add those to the query.
if (options.where) {
itemCollection.query('where', options.where);
}

// Apply FILTER
if (options.filter) {
options.filter = gql.parse(options.filter);
itemCollection.query(function (qb) {
gql.knexify(qb, options.filter);
});
// If there are `where` conditionals specified, add those to the query.
if (options.where) {
itemCollection.query('where', options.where);
}

baseUtils.processGQLResult(itemCollection, options);
}
// Apply FILTER
if (options.filter) {
options.filter = gql.parse(options.filter);
itemCollection.query(function (qb) {
gql.knexify(qb, options.filter);
});

// Setup filter joins / queries
baseUtils.oldFiltering.query(filterObjects, itemCollection);
baseUtils.processGQLResult(itemCollection, options);
}

// Handle related objects
// TODO: this should just be done for all methods @ the API level
options.withRelated = _.union(options.withRelated, options.include);
// Handle related objects
// TODO: this should just be done for all methods @ the API level
options.withRelated = _.union(options.withRelated, options.include);

options.order = self.orderDefaultOptions();
options.order = self.orderDefaultOptions();

return itemCollection.fetchPage(options).then(function formatResponse(response) {
var data = {};
data[tableName] = response.collection.toJSON(options);
data.meta = {pagination: response.pagination};
return itemCollection.fetchPage(options).then(function formatResponse(response) {
var data = {};
data[tableName] = response.collection.toJSON(options);
data.meta = {pagination: response.pagination};

return baseUtils.oldFiltering.formatResponse(filterObjects, options, data);
});
return data;
}).catch(errors.logAndThrowError);
},

Expand Down
48 changes: 0 additions & 48 deletions core/server/models/base/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
var _ = require('lodash'),
collectionQuery,
processGQLResult,
filtering,
addPostCount,
tagUpdate;

Expand Down Expand Up @@ -64,52 +63,6 @@ processGQLResult = function processGQLResult(itemCollection, options) {
}
};

/**
* All of this can be removed once the filter parameter is in place
* And the current filtering methods are removed
*/
filtering = {
preFetch: function preFetch(filterObjects) {
var promises = [];
_.forOwn(filterObjects, function (obj) {
promises.push(obj.fetch());
});

return promises;
},
query: function query(filterObjects, itemCollection) {
if (filterObjects.tags) {
itemCollection
.query('join', 'posts_tags', 'posts_tags.post_id', '=', 'posts.id')
.query('where', 'posts_tags.tag_id', '=', filterObjects.tags.id);
}

if (filterObjects.author) {
itemCollection
.query('where', 'author_id', '=', filterObjects.author.id);
}

if (filterObjects.roles) {
itemCollection
.query('join', 'roles_users', 'roles_users.user_id', '=', 'users.id')
.query('where', 'roles_users.role_id', '=', filterObjects.roles.id);
}
},
formatResponse: function formatResponse(filterObjects, options, data) {
if (!_.isEmpty(filterObjects)) {
data.meta.filters = {};
}

_.forOwn(filterObjects, function (obj, key) {
if (!filterObjects[key].isNew()) {
data.meta.filters[key] = [filterObjects[key].toJSON(options)];
}
});

return data;
}
};

tagUpdate = {
fetchCurrentPost: function fetchCurrentPost(PostModel, id, options) {
return PostModel.forge({id: id}).fetch(_.extend({}, options, {withRelated: ['tags']}));
Expand Down Expand Up @@ -172,7 +125,6 @@ tagUpdate = {
}
};

module.exports.oldFiltering = filtering;
module.exports.processGQLResult = processGQLResult;
module.exports.collectionQuery = collectionQuery;
module.exports.addPostCount = addPostCount;
Expand Down
16 changes: 1 addition & 15 deletions core/server/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,20 +328,6 @@ Post = ghostBookshelf.Model.extend({
return attrs;
}
}, {
setupFilters: function setupFilters(options) {
var filterObjects = {};
// Deliberately switch from singular 'tag' to 'tags' and 'role' to 'roles' here
// TODO: make this consistent
if (options.tag !== undefined) {
filterObjects.tags = ghostBookshelf.model('Tag').forge({slug: options.tag});
}
if (options.author !== undefined) {
filterObjects.author = ghostBookshelf.model('User').forge({slug: options.author});
}

return filterObjects;
},

findPageDefaultOptions: function findPageDefaultOptions() {
return {
staticPages: false, // include static pages
Expand Down Expand Up @@ -400,7 +386,7 @@ Post = ghostBookshelf.Model.extend({
// these are the only options that can be passed to Bookshelf / Knex.
validOptions = {
findOne: ['importing', 'withRelated'],
findPage: ['page', 'limit', 'columns', 'filter', 'status', 'staticPages', 'featured'],
findPage: ['page', 'limit', 'columns', 'filter', 'status', 'staticPages'],
add: ['importing']
};

Expand Down
4 changes: 0 additions & 4 deletions core/server/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ Tag = ghostBookshelf.Model.extend({
return attrs;
}
}, {
setupFilters: function setupFilters() {
return {};
},

findPageDefaultOptions: function findPageDefaultOptions() {
return {
where: {}
Expand Down
11 changes: 0 additions & 11 deletions core/server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,6 @@ User = ghostBookshelf.Model.extend({
}

}, {
setupFilters: function setupFilters(options) {
var filterObjects = {};
// Deliberately switch from singular 'tag' to 'tags' and 'role' to 'roles' here
// TODO: make this consistent
if (options.role !== undefined) {
filterObjects.roles = ghostBookshelf.model('Role').forge({name: options.role});
}

return filterObjects;
},

findPageDefaultOptions: function findPageDefaultOptions() {
return {
status: 'active',
Expand Down
2 changes: 1 addition & 1 deletion core/test/functional/routes/api/posts_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('Post API', function () {
});

it('can retrieve just featured posts', function (done) {
request.get(testUtils.API.getApiQuery('posts/?featured=true'))
request.get(testUtils.API.getApiQuery('posts/?filter=featured:true'))
.set('Authorization', 'Bearer ' + accesstoken)
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
Expand Down
64 changes: 0 additions & 64 deletions core/test/integration/api/advanced_browse_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,38 +259,6 @@ describe('Filter Param Spec', function () {
describe('Old Use Cases', function () {
// Please note: these tests are mostly here to help prove certain things whilst building out new behaviour
describe('Old post "filters"', function () {
it('OLD STYLE TAG FILTER For checking against.. to be removed', function (done) {
PostAPI.browse({tag: 'photo', include: 'tag,author'}).then(function (result) {
var ids;
// 1. Result should have the correct base structure
should.exist(result);
result.should.have.property('posts');
result.should.have.property('meta');

// 2. The data part of the response should be correct
// We should have 4 matching items
result.posts.should.be.an.Array.with.lengthOf(4);

ids = _.pluck(result.posts, 'id');
ids.should.eql([11, 9, 3, 2]);

// 3. The meta object should contain the right details
result.meta.should.have.property('pagination');
result.meta.pagination.should.be.an.Object.with.properties(['page', 'limit', 'pages', 'total', 'next', 'prev']);
result.meta.pagination.page.should.eql(1);
result.meta.pagination.limit.should.eql(15);
result.meta.pagination.pages.should.eql(1);
result.meta.pagination.total.should.eql(4);
should.equal(result.meta.pagination.next, null);
should.equal(result.meta.pagination.prev, null);

// NOTE: old query has meta filter
result.meta.should.have.property('filters');

done();
}).catch(done);
});

it('Will fetch posts with a given tag', function (done) {
PostAPI.browse({filter: 'tag:photo', include: 'tag,author'}).then(function (result) {
var ids;
Expand Down Expand Up @@ -323,38 +291,6 @@ describe('Filter Param Spec', function () {
}).catch(done);
});

it('OLD STYLE AUTHOR FILTER For checking against.. to be removed', function (done) {
PostAPI.browse({author: 'leslie', include: 'tag,author', limit: 5, page: 2}).then(function (result) {
var ids;
// 1. Result should have the correct base structure
should.exist(result);
result.should.have.property('posts');
result.should.have.property('meta');

// 2. The data part of the response should be correct
// We should have 5 matching items
result.posts.should.be.an.Array.with.lengthOf(5);

ids = _.pluck(result.posts, 'id');
ids.should.eql([13, 12, 11, 10, 9]);

// 3. The meta object should contain the right details
result.meta.should.have.property('pagination');
result.meta.pagination.should.be.an.Object.with.properties(['page', 'limit', 'pages', 'total', 'next', 'prev']);
result.meta.pagination.page.should.eql(2);
result.meta.pagination.limit.should.eql(5);
result.meta.pagination.pages.should.eql(3);
result.meta.pagination.total.should.eql(15);
result.meta.pagination.next.should.eql(3);
result.meta.pagination.prev.should.eql(1);

// NOTE: old query has meta filter
result.meta.should.have.property('filters');

done();
}).catch(done);
});

it('Will fetch posts with a given author', function (done) {
PostAPI.browse({filter: 'author:leslie', include: 'tag,author', limit: 5, page: 2}).then(function (result) {
var ids;
Expand Down
28 changes: 18 additions & 10 deletions core/test/integration/api/api_posts_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Post API', function () {

describe('Browse', function () {
it('can fetch featured posts', function (done) {
PostAPI.browse({context: {user: 1}, featured: true}).then(function (results) {
PostAPI.browse({context: {user: 1}, filter: 'featured:true'}).then(function (results) {
should.exist(results.posts);
results.posts.length.should.eql(4);
results.posts[0].featured.should.eql(true);
Expand All @@ -31,7 +31,7 @@ describe('Post API', function () {
});

it('can exclude featured posts', function (done) {
PostAPI.browse({context: {user: 1}, status: 'all', featured: false}).then(function (results) {
PostAPI.browse({context: {user: 1}, status: 'all', filter: 'featured:false'}).then(function (results) {
should.exist(results.posts);
results.posts.length.should.eql(1);
results.posts[0].featured.should.eql(false);
Expand Down Expand Up @@ -202,26 +202,34 @@ describe('Post API', function () {
});

it('can fetch all posts for a tag', function (done) {
PostAPI.browse({context: {user: 1}, status: 'all', tag: 'kitchen-sink'}).then(function (results) {
PostAPI.browse({context: {user: 1}, status: 'all', filter: 'tags:kitchen-sink', include: 'tags'}).then(function (results) {
results.posts.length.should.be.eql(2);
results.meta.filters.tags[0].slug.should.eql('kitchen-sink');

_.each(results.posts, function (post) {
var slugs = _.pluck(post.tags, 'slug');
slugs.should.containEql('kitchen-sink');
});

done();
}).catch(done);
});

it('can fetch all posts for an author', function (done) {
PostAPI.browse({context: {user: 1}, status: 'all', author: 'joe-bloggs'}).then(function (results) {
PostAPI.browse({context: {user: 1}, status: 'all', filter: 'author:joe-bloggs', include: 'author'}).then(function (results) {
should.exist(results.posts);
results.posts.length.should.eql(5);
results.meta.filters.author[0].slug.should.eql('joe-bloggs');

_.each(results.posts, function (post) {
post.author.slug.should.eql('joe-bloggs');
});

done();
}).catch(done);
});

it('cannot fetch all posts for a tag with invalid slug', function (done) {
PostAPI.browse({tag: 'invalid!'}).then(function () {
// @TODO: ensure filters are fully validated
it.skip('cannot fetch all posts for a tag with invalid slug', function (done) {
PostAPI.browse({filter: 'tags:invalid!'}).then(function () {
done(new Error('Should not return a result with invalid tag'));
}).catch(function (err) {
should.exist(err);
Expand All @@ -231,8 +239,8 @@ describe('Post API', function () {
});
});

it('cannot fetch all posts for an author with invalid slug', function (done) {
PostAPI.browse({author: 'invalid!'}).then(function () {
it.skip('cannot fetch all posts for an author with invalid slug', function (done) {
PostAPI.browse({filter: 'author:invalid!'}).then(function () {
done(new Error('Should not return a result with invalid author'));
}).catch(function (err) {
should.exist(err);
Expand Down
Loading