mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-15 11:34:24 +03:00
b5cebb9ec6
refs #5604, refs #5463 - deps: ghost-gql@0.0.2 - adds code to wire up the filtering to a paginated query - updated pagination plugin count query to use 'distinct' so it's more robust - rename paginationUtils.query to addLimitAndOffset to be more explicit and make the code clearer - add a new 'advanced browsing spec' set of tests for tracking these features as they are built out
180 lines
6.5 KiB
JavaScript
180 lines
6.5 KiB
JavaScript
/**
|
|
* # Utils
|
|
* Parts of the model code which can be split out and unit tested
|
|
*/
|
|
var _ = require('lodash'),
|
|
collectionQuery,
|
|
processGQLResult,
|
|
filtering,
|
|
addPostCount,
|
|
tagUpdate;
|
|
|
|
addPostCount = function addPostCount(options, itemCollection) {
|
|
if (options.include && options.include.indexOf('post_count') > -1) {
|
|
itemCollection.query('columns', 'tags.*', function (qb) {
|
|
qb.count('posts_tags.post_id').from('posts_tags').whereRaw('tag_id = tags.id').as('post_count');
|
|
});
|
|
|
|
options.withRelated = _.pull([].concat(options.withRelated), 'post_count');
|
|
options.include = _.pull([].concat(options.include), 'post_count');
|
|
}
|
|
};
|
|
|
|
collectionQuery = {
|
|
count: function count(collection, options) {
|
|
addPostCount(options, collection);
|
|
}
|
|
};
|
|
|
|
processGQLResult = function processGQLResult(itemCollection, options) {
|
|
var joinTables = options.filter.joins,
|
|
tagsHasIn = false;
|
|
|
|
if (joinTables && joinTables.indexOf('tags') > -1) {
|
|
// We need to use leftOuterJoin to insure we still include posts which don't have tags in the result
|
|
// The where clause should restrict which items are returned
|
|
itemCollection
|
|
.query('leftOuterJoin', 'posts_tags', 'posts_tags.post_id', '=', 'posts.id')
|
|
.query('leftOuterJoin', 'tags', 'posts_tags.tag_id', '=', 'tags.id');
|
|
|
|
// The order override should ONLY happen if we are doing an "IN" query
|
|
// TODO move the order handling to the query building that is currently inside pagination
|
|
// TODO make the order handling in pagination handle orderByRaw
|
|
// TODO extend this handling to all joins
|
|
_.each(options.filter.statements, function (statement) {
|
|
if (statement.op === 'IN' && statement.prop.match(/tags/)) {
|
|
tagsHasIn = true;
|
|
}
|
|
});
|
|
|
|
if (tagsHasIn) {
|
|
// TODO make this count the number of MATCHING tags, not just the number of tags
|
|
itemCollection.query('orderByRaw', 'count(tags.id) DESC');
|
|
}
|
|
|
|
// We need to add a group by to counter the double left outer join
|
|
// TODO improve on th group by handling
|
|
options.groups = options.groups || [];
|
|
options.groups.push('posts.id');
|
|
}
|
|
|
|
if (joinTables && joinTables.indexOf('author') > -1) {
|
|
itemCollection
|
|
.query('join', 'users as author', 'author.id', '=', 'posts.author_id');
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 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']}));
|
|
},
|
|
|
|
fetchMatchingTags: function fetchMatchingTags(TagModel, tagsToMatch, options) {
|
|
if (_.isEmpty(tagsToMatch)) {
|
|
return false;
|
|
}
|
|
return TagModel.forge()
|
|
.query('whereIn', 'name', _.pluck(tagsToMatch, 'name')).fetchAll(options);
|
|
},
|
|
|
|
detachTagFromPost: function detachTagFromPost(post, tag, options) {
|
|
return function () {
|
|
// See tgriesser/bookshelf#294 for an explanation of _.omit(options, 'query')
|
|
return post.tags().detach(tag.id, _.omit(options, 'query'));
|
|
};
|
|
},
|
|
|
|
attachTagToPost: function attachTagToPost(post, tag, index, options) {
|
|
return function () {
|
|
// See tgriesser/bookshelf#294 for an explanation of _.omit(options, 'query')
|
|
return post.tags().attach({tag_id: tag.id, sort_order: index}, _.omit(options, 'query'));
|
|
};
|
|
},
|
|
|
|
createTagThenAttachTagToPost: function createTagThenAttachTagToPost(TagModel, post, tag, index, options) {
|
|
return function () {
|
|
return TagModel.add({name: tag.name}, options).then(function then(createdTag) {
|
|
return tagUpdate.attachTagToPost(post, createdTag, index, options)();
|
|
});
|
|
};
|
|
},
|
|
|
|
updateTagOrderForPost: function updateTagOrderForPost(post, tag, index, options) {
|
|
return function () {
|
|
return post.tags().updatePivot(
|
|
{sort_order: index}, _.extend({}, options, {query: {where: {tag_id: tag.id}}})
|
|
);
|
|
};
|
|
},
|
|
|
|
// Test if two tags are the same, checking ID first, and falling back to name
|
|
tagsAreEqual: function tagsAreEqual(tag1, tag2) {
|
|
if (tag1.hasOwnProperty('id') && tag2.hasOwnProperty('id')) {
|
|
return parseInt(tag1.id, 10) === parseInt(tag2.id, 10);
|
|
}
|
|
return tag1.name.toString() === tag2.name.toString();
|
|
},
|
|
tagSetsAreEqual: function tagSetsAreEqual(tags1, tags2) {
|
|
// If the lengths are different, they cannot be the same
|
|
if (tags1.length !== tags2.length) {
|
|
return false;
|
|
}
|
|
// Return if no item is not the same (double negative is horrible)
|
|
return !_.any(tags1, function (tag1, index) {
|
|
return !tagUpdate.tagsAreEqual(tag1, tags2[index]);
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports.oldFiltering = filtering;
|
|
module.exports.processGQLResult = processGQLResult;
|
|
module.exports.collectionQuery = collectionQuery;
|
|
module.exports.addPostCount = addPostCount;
|
|
module.exports.tagUpdate = tagUpdate;
|