Skip to content

Commit 090a936

Browse files
authored
Moved raw order for author filtering to correct place (TryGhost#10166) (TryGhost#10171)
refs TryGhost#10105 - ordering !== filtering
1 parent e89a27f commit 090a936

File tree

3 files changed

+6
-10
lines changed

3 files changed

+6
-10
lines changed

core/server/models/plugins/filter.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,6 @@ filter = function filter(Bookshelf) {
139139
.query('leftOuterJoin', 'posts_authors', 'posts_authors.post_id', '=', 'posts.id')
140140
.query('leftOuterJoin', 'users as authors', 'posts_authors.author_id', '=', 'authors.id');
141141

142-
// The order override should ONLY happen if we are doing an "IN" query
143-
// TODO move the order handling to the query building that is currently inside pagination
144-
// TODO make the order handling in pagination handle orderByRaw
145-
// TODO extend this handling to all joins
146-
if (gql.json.findStatement(this._filters.statements, {prop: /^authors/, op: 'IN'})) {
147-
// TODO make this count the number of MATCHING authors, not just the number of authors
148-
this.query('orderByRaw', 'count(authors.id) DESC');
149-
}
150-
151142
// We need to add a group by to counter the double left outer join
152143
// TODO improve on the group by handling
153144
options.groups = options.groups || [];

core/server/models/post.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,11 @@ Post = ghostBookshelf.Model.extend({
590590
order = `count(tags.id) DESC, ${order}`;
591591
}
592592

593+
// CASE: if the filter contains an `IN` operator, we should return the posts first, which match both authors
594+
if (options.filter && options.filter.match(/(authors|author):\s?\[.*\]/)) {
595+
order = `count(authors.id) DESC, ${order}`;
596+
}
597+
593598
return order;
594599
},
595600

core/test/unit/models/post_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('Unit: models/post', function () {
8686
withRelated: ['authors', 'tags']
8787
}).then(() => {
8888
queries.length.should.eql(2);
89-
queries[0].sql.should.eql('select count(distinct posts.id) as aggregate from `posts` left outer join `posts_tags` on `posts_tags`.`post_id` = `posts`.`id` left outer join `tags` on `posts_tags`.`tag_id` = `tags`.`id` left outer join `posts_authors` on `posts_authors`.`post_id` = `posts`.`id` left outer join `users` as `authors` on `posts_authors`.`author_id` = `authors`.`id` where (`posts`.`page` = ? and `posts`.`status` = ?) and (`authors`.`slug` in (?, ?) and (`tags`.`slug` = ? or `posts`.`feature_image` is not null)) order by count(authors.id) DESC');
89+
queries[0].sql.should.eql('select count(distinct posts.id) as aggregate from `posts` left outer join `posts_tags` on `posts_tags`.`post_id` = `posts`.`id` left outer join `tags` on `posts_tags`.`tag_id` = `tags`.`id` left outer join `posts_authors` on `posts_authors`.`post_id` = `posts`.`id` left outer join `users` as `authors` on `posts_authors`.`author_id` = `authors`.`id` where (`posts`.`page` = ? and `posts`.`status` = ?) and (`authors`.`slug` in (?, ?) and (`tags`.`slug` = ? or `posts`.`feature_image` is not null))');
9090
queries[0].bindings.should.eql([
9191
false,
9292
'published',

0 commit comments

Comments
 (0)