From 2ba5dbfa754a2f8efa603be67d43966a03b8d70e Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 16 Jan 2015 17:15:23 +1030 Subject: [PATCH] Change the way we fetch multiple posts by ID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JSON-API specifies that multiple resources should be fetched with a comma-separated list of IDs, i.e. discussions/1,2,3,4. But this is problematic because if we do a findQuery with only one ID, then a single object will come back from the API where the serializer is expecting an array containing a single object. Instead, I’ve just implemented an ids “filter” on the discussions index API route (which is the default way that the adapter finds multiple IDs.) --- .../core/ember/app/adapters/application.js | 39 +++---------------- .../core/ember/app/serializers/application.js | 1 + .../src/Flarum/Api/Actions/Posts/Index.php | 16 ++++++-- 3 files changed, 18 insertions(+), 38 deletions(-) diff --git a/framework/core/ember/app/adapters/application.js b/framework/core/ember/app/adapters/application.js index da2a2d7d4..ec39c711d 100644 --- a/framework/core/ember/app/adapters/application.js +++ b/framework/core/ember/app/adapters/application.js @@ -1,37 +1,8 @@ import JsonApiAdapter from 'ember-json-api/json-api-adapter'; + export default JsonApiAdapter.extend({ - host: '/api', - findQuery: function(store, type, query) { - var ids = null; - if (query.ids) { - ids = query.ids.join(','); - delete query.ids; - } - return this.ajax(this.buildURL(type.typeKey, ids), 'GET', {data: query}); - }, -}); - -// export default DS.JsonApiAdapter.extend({ -// host: '/api', - -// // xhr: [], - -// // ajax: function(url, type, hash) { -// // var adapter = this; - -// // return new Ember.RSVP.Promise(function(resolve, reject) { -// // hash = adapter.ajaxOptions(url, type, hash); - -// // hash.success = function(json) { -// // Ember.run(null, resolve, json); -// // }; - -// // hash.error = function(jqXHR, textStatus, errorThrown) { -// // Ember.run(null, reject, adapter.ajaxError(jqXHR)); -// // }; - -// // adapter.xhr.push(Ember.$.ajax(hash)); -// // }, "DS: RestAdapter#ajax " + type + " to " + url); -// // }, -// }); + // Todo: make this loaded via an environment variable or something + host: '/api' + +}); \ No newline at end of file diff --git a/framework/core/ember/app/serializers/application.js b/framework/core/ember/app/serializers/application.js index 31200acf3..62de25084 100644 --- a/framework/core/ember/app/serializers/application.js +++ b/framework/core/ember/app/serializers/application.js @@ -1,4 +1,5 @@ import JsonApiSerializer from 'ember-json-api/json-api-serializer'; + export default JsonApiSerializer.extend({ normalize: function(type, hash, property) { var json = {}; diff --git a/framework/core/src/Flarum/Api/Actions/Posts/Index.php b/framework/core/src/Flarum/Api/Actions/Posts/Index.php index 6b53b0f23..862e71dbb 100644 --- a/framework/core/src/Flarum/Api/Actions/Posts/Index.php +++ b/framework/core/src/Flarum/Api/Actions/Posts/Index.php @@ -8,13 +8,14 @@ use Flarum\Api\Serializers\PostSerializer; class Index extends Base { /** - * Show posts from a discussion. + * Show posts from a discussion, or by providing an array of IDs. * * @return Response */ protected function run() { $discussionId = $this->input('discussions'); + $postIds = (array) $this->input('ids'); $count = $this->count(20, 50); @@ -40,9 +41,16 @@ class Index extends Base // @todo move to post repository $posts = Post::with($relations) - ->whereCanView() - ->where('discussion_id', $discussionId) - ->skip($start) + ->whereCanView(); + + if ($discussionId) { + $posts->where('discussion_id', $discussionId); + } + if (count($postIds)) { + $posts->whereIn('id', $postIds); + } + + $posts = $posts->skip($start) ->take($count) ->orderBy($sort['by'], $sort['order'] ?: 'asc') ->get();