From 85d7dc8752df89c4b3b6f2cbbb53190ba3ae80e9 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 15 Jun 2015 12:20:26 +0930 Subject: [PATCH] Add new fields, include tags on the new forum API action --- extensions/tags/js/src/models/tag.js | 8 +++++++- extensions/tags/src/Handlers/TagPreloader.php | 19 ------------------- extensions/tags/src/Tag.php | 12 ++++++++++++ extensions/tags/src/TagSerializer.php | 9 ++++++++- extensions/tags/src/TagsServiceProvider.php | 9 ++++++++- 5 files changed, 35 insertions(+), 22 deletions(-) delete mode 100755 extensions/tags/src/Handlers/TagPreloader.php diff --git a/extensions/tags/js/src/models/tag.js b/extensions/tags/js/src/models/tag.js index 0ce8c1271..85b010da5 100644 --- a/extensions/tags/js/src/models/tag.js +++ b/extensions/tags/js/src/models/tag.js @@ -6,13 +6,19 @@ Tag.prototype.id = Model.prop('id'); Tag.prototype.name = Model.prop('name'); Tag.prototype.slug = Model.prop('slug'); Tag.prototype.description = Model.prop('description'); + Tag.prototype.color = Model.prop('color'); Tag.prototype.backgroundUrl = Model.prop('backgroundUrl'); +Tag.prototype.backgroundMode = Model.prop('backgroundMode'); Tag.prototype.iconUrl = Model.prop('iconUrl'); -Tag.prototype.discussionsCount = Model.prop('discussionsCount'); + Tag.prototype.position = Model.prop('position'); Tag.prototype.parent = Model.one('parent'); Tag.prototype.defaultSort = Model.prop('defaultSort'); Tag.prototype.isChild = Model.prop('isChild'); +Tag.prototype.discussionsCount = Model.prop('discussionsCount'); +Tag.prototype.lastTime = Model.prop('lastTime', Model.date); +Tag.prototype.lastDiscussion = Model.one('lastDiscussion'); + export default Tag; diff --git a/extensions/tags/src/Handlers/TagPreloader.php b/extensions/tags/src/Handlers/TagPreloader.php deleted file mode 100755 index cb238cb1c..000000000 --- a/extensions/tags/src/Handlers/TagPreloader.php +++ /dev/null @@ -1,19 +0,0 @@ -listen('Flarum\Forum\Events\RenderView', __CLASS__.'@renderForum'); - } - - public function renderForum(RenderView $event) - { - $serializer = new TagSerializer($event->action->actor, null, ['parent']); - $event->view->data = array_merge($event->view->data, $serializer->collection(Tag::orderBy('position')->get())->toArray()); - } -} diff --git a/extensions/tags/src/Tag.php b/extensions/tags/src/Tag.php index 2ed3b74db..40db19454 100644 --- a/extensions/tags/src/Tag.php +++ b/extensions/tags/src/Tag.php @@ -5,4 +5,16 @@ use Flarum\Core\Models\Model; class Tag extends Model { protected $table = 'tags'; + + protected $dates = ['last_time']; + + public function parent() + { + return $this->belongsTo('Flarum\Tags\Tag', 'parent_id'); + } + + public function lastDiscussion() + { + return $this->belongsTo('Flarum\Core\Models\Discussion', 'last_discussion_id'); + } } diff --git a/extensions/tags/src/TagSerializer.php b/extensions/tags/src/TagSerializer.php index bdc38ba50..197bc7a17 100644 --- a/extensions/tags/src/TagSerializer.php +++ b/extensions/tags/src/TagSerializer.php @@ -25,11 +25,13 @@ class TagSerializer extends BaseSerializer 'slug' => $tag->slug, 'color' => $tag->color, 'backgroundUrl' => $tag->background_path, + 'backgroundMode' => $tag->background_mode, 'iconUrl' => $tag->icon_path, 'discussionsCount' => (int) $tag->discussions_count, 'position' => $tag->position === null ? null : (int) $tag->position, 'defaultSort' => $tag->default_sort, - 'isChild' => (bool) $tag->parent_id + 'isChild' => (bool) $tag->parent_id, + 'lastTime' => $tag->last_time ? $tag->last_time->toRFC3339String() : null ]; return $this->extendAttributes($tag, $attributes); @@ -39,4 +41,9 @@ class TagSerializer extends BaseSerializer { return $this->hasOne('Flarum\Tags\TagSerializer'); } + + protected function lastDiscussion() + { + return $this->hasOne('Flarum\Api\Serializers\DiscussionSerializer'); + } } diff --git a/extensions/tags/src/TagsServiceProvider.php b/extensions/tags/src/TagsServiceProvider.php index 2a43f0c09..4a0b97768 100644 --- a/extensions/tags/src/TagsServiceProvider.php +++ b/extensions/tags/src/TagsServiceProvider.php @@ -27,7 +27,6 @@ class TagsServiceProvider extends ServiceProvider new EventSubscribers([ 'Flarum\Tags\Handlers\DiscussionTaggedNotifier', - 'Flarum\Tags\Handlers\TagPreloader', 'Flarum\Tags\Handlers\TagSaver' ]), @@ -39,6 +38,14 @@ class TagsServiceProvider extends ServiceProvider new ApiInclude(['discussions.index', 'discussions.show'], 'tags', true), + new Relationship('Flarum\Core\Models\Forum', 'tags', function ($model) { + return Tag::query(); + }), + + new SerializeRelationship('Flarum\Api\Serializers\ForumSerializer', 'hasMany', 'tags', 'Flarum\Tags\TagSerializer'), + + new ApiInclude(['forum.show'], ['tags', 'tags.parent', 'tags.lastDiscussion'], true), + (new Permission('discussion.tag')) ->serialize() ->grant(function ($grant, $user) {