From bec1f73c363032e364607e1fdd4970b025cb94ef Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 15 Jun 2015 09:00:30 +0930 Subject: [PATCH] Add event post when a discussion's tags are changed --- extensions/tags/js/bootstrap.js | 3 ++ .../discussion-tagged-notification.js | 16 ------- .../src/components/discussion-tagged-post.js | 23 +++++++--- extensions/tags/less/extension.less | 7 ++- .../tags/src/DiscussionMovedNotification.php | 38 ---------------- ...MovedPost.php => DiscussionTaggedPost.php} | 24 +++++----- .../src/Handlers/DiscussionMovedNotifier.php | 45 ------------------- .../src/Handlers/DiscussionTaggedNotifier.php | 31 +++++++++++++ extensions/tags/src/TagsServiceProvider.php | 11 +++-- 9 files changed, 75 insertions(+), 123 deletions(-) delete mode 100644 extensions/tags/js/src/components/discussion-tagged-notification.js delete mode 100644 extensions/tags/src/DiscussionMovedNotification.php rename extensions/tags/src/{DiscussionMovedPost.php => DiscussionTaggedPost.php} (67%) delete mode 100755 extensions/tags/src/Handlers/DiscussionMovedNotifier.php create mode 100755 extensions/tags/src/Handlers/DiscussionTaggedNotifier.php diff --git a/extensions/tags/js/bootstrap.js b/extensions/tags/js/bootstrap.js index 0a6e0e70f..4d6317826 100644 --- a/extensions/tags/js/bootstrap.js +++ b/extensions/tags/js/bootstrap.js @@ -5,6 +5,7 @@ import IndexPage from 'flarum/components/index-page'; import Tag from 'flarum-tags/models/tag'; import TagsPage from 'flarum-tags/components/tags-page'; +import DiscussionTaggedPost from 'flarum-tags/components/discussion-tagged-post'; import addTagList from 'flarum-tags/add-tag-list'; import addTagFilter from 'flarum-tags/add-tag-filter'; import addTagLabels from 'flarum-tags/add-tag-labels'; @@ -21,6 +22,8 @@ app.initializers.add('flarum-tags', function() { Discussion.prototype.tags = Model.many('tags'); Discussion.prototype.canTag = Model.prop('canTag'); + app.postComponentRegistry['discussionTagged'] = DiscussionTaggedPost; + // Add a list of tags to the index navigation. addTagList(); diff --git a/extensions/tags/js/src/components/discussion-tagged-notification.js b/extensions/tags/js/src/components/discussion-tagged-notification.js deleted file mode 100644 index fc77d7bcb..000000000 --- a/extensions/tags/js/src/components/discussion-tagged-notification.js +++ /dev/null @@ -1,16 +0,0 @@ -import Notification from 'flarum/components/notification'; -import username from 'flarum/helpers/username'; -import categoryLabel from 'flarum-categories/helpers/category-label'; - -export default class DiscussionMovedNotification extends Notification { - view() { - var notification = this.props.notification; - var discussion = notification.subject(); - - return super.view({ - href: app.route.discussion(discussion, notification.content().postNumber), - icon: 'arrow-right', - content: [username(notification.sender()), ' moved to ', categoryLabel(discussion.category())] - }); - } -} diff --git a/extensions/tags/js/src/components/discussion-tagged-post.js b/extensions/tags/js/src/components/discussion-tagged-post.js index 54870f5e1..f67954051 100644 --- a/extensions/tags/js/src/components/discussion-tagged-post.js +++ b/extensions/tags/js/src/components/discussion-tagged-post.js @@ -1,12 +1,25 @@ import EventPost from 'flarum/components/event-post'; -import categoryLabel from 'flarum-categories/helpers/category-label'; +import tagsLabel from 'flarum-tags/helpers/tags-label'; -export default class DiscussionMovedPost extends EventPost { +export default class DiscussionTaggedPost extends EventPost { view() { var post = this.props.post; - var oldCategory = app.store.getById('categories', post.content()[0]); - var newCategory = app.store.getById('categories', post.content()[1]); + var oldTags = post.content()[0]; + var newTags = post.content()[1]; - return super.view('arrow-right', ['moved the discussion from ', categoryLabel(oldCategory), ' to ', categoryLabel(newCategory), '.']); + var added = newTags.filter(tag => oldTags.indexOf(tag) === -1).map(id => app.store.getById('tags', id)); + var removed = oldTags.filter(tag => newTags.indexOf(tag) === -1).map(id => app.store.getById('tags', id)); + var total = added.concat(removed); + + var build = function(verb, tags, only) { + return tags.length ? [verb, ' ', only && tags.length == 1 ? 'the ' : '', tagsLabel(tags)] : ''; + }; + + return super.view('tag', [ + build('added', added, !removed.length), + added.length && removed.length ? ' and ' : '', + build('removed', removed, !added.length), + total.length ? (total.length == 1 ? ' tag.' : ' tags.') : '' + ]); } } diff --git a/extensions/tags/less/extension.less b/extensions/tags/less/extension.less index bc14a3d5b..7842c72a4 100644 --- a/extensions/tags/less/extension.less +++ b/extensions/tags/less/extension.less @@ -33,15 +33,14 @@ } } } - - .discussion-moved-post & { - margin: 0 2px; - } } .tags-label { .discussion-summary & { margin-right: 10px; } + .discussion-tagged-post & { + margin: 0 2px; + } & .tag-label { border-radius: 0; diff --git a/extensions/tags/src/DiscussionMovedNotification.php b/extensions/tags/src/DiscussionMovedNotification.php deleted file mode 100644 index 10a82ba84..000000000 --- a/extensions/tags/src/DiscussionMovedNotification.php +++ /dev/null @@ -1,38 +0,0 @@ -post = $post; - } - - public function getSubject() - { - return $this->post->discussion; - } - - public function getSender() - { - return $this->post->user; - } - - public function getData() - { - return ['postNumber' => (int) $this->post->number]; - } - - public static function getType() - { - return 'discussionMoved'; - } - - public static function getSubjectModel() - { - return 'Flarum\Core\Models\Discussion'; - } -} diff --git a/extensions/tags/src/DiscussionMovedPost.php b/extensions/tags/src/DiscussionTaggedPost.php similarity index 67% rename from extensions/tags/src/DiscussionMovedPost.php rename to extensions/tags/src/DiscussionTaggedPost.php index 01b8a87da..987f4676f 100755 --- a/extensions/tags/src/DiscussionMovedPost.php +++ b/extensions/tags/src/DiscussionTaggedPost.php @@ -1,16 +1,16 @@ -content = static::buildContent($previous->content[0], $this->content[1]); + $previous->time = $this->time; + return $previous; } @@ -38,15 +40,15 @@ class DiscussionMovedPost extends EventPost * * @param integer $discussionId * @param integer $userId - * @param integer $oldCategoryId - * @param integer $newCategoryId + * @param array $oldTagIds + * @param array $newTagIds * @return static */ - public static function reply($discussionId, $userId, $oldCategoryId, $newCategoryId) + public static function reply($discussionId, $userId, array $oldTagIds, array $newTagIds) { $post = new static; - $post->content = static::buildContent($oldCategoryId, $newCategoryId); + $post->content = static::buildContent($oldTagIds, $newTagIds); $post->time = time(); $post->discussion_id = $discussionId; $post->user_id = $userId; @@ -57,12 +59,12 @@ class DiscussionMovedPost extends EventPost /** * Build the content attribute. * - * @param boolean $oldCategoryId The old category ID. - * @param boolean $newCategoryId The new category ID. + * @param array $oldTagIds + * @param array $newTagIds * @return array */ - public static function buildContent($oldCategoryId, $newCategoryId) + public static function buildContent(array $oldTagIds, array $newTagIds) { - return [$oldCategoryId, $newCategoryId]; + return [$oldTagIds, $newTagIds]; } } diff --git a/extensions/tags/src/Handlers/DiscussionMovedNotifier.php b/extensions/tags/src/Handlers/DiscussionMovedNotifier.php deleted file mode 100755 index cb7723902..000000000 --- a/extensions/tags/src/Handlers/DiscussionMovedNotifier.php +++ /dev/null @@ -1,45 +0,0 @@ -notifications = $notifications; - } - - /** - * Register the listeners for the subscriber. - * - * @param \Illuminate\Contracts\Events\Dispatcher $events - */ - public function subscribe(Dispatcher $events) - { - $events->listen('Flarum\Categories\Events\DiscussionWasMoved', __CLASS__.'@whenDiscussionWasMoved'); - } - - public function whenDiscussionWasMoved(DiscussionWasMoved $event) - { - $post = DiscussionMovedPost::reply( - $event->discussion->id, - $event->user->id, - $event->oldCategoryId, - $event->discussion->category_id - ); - - $post = $event->discussion->addPost($post); - - if ($event->discussion->start_user_id !== $event->user->id) { - $notification = new DiscussionMovedNotification($post); - - $this->notifications->sync($notification, $post->exists ? [$event->discussion->startUser] : []); - } - } -} diff --git a/extensions/tags/src/Handlers/DiscussionTaggedNotifier.php b/extensions/tags/src/Handlers/DiscussionTaggedNotifier.php new file mode 100755 index 000000000..2c915d548 --- /dev/null +++ b/extensions/tags/src/Handlers/DiscussionTaggedNotifier.php @@ -0,0 +1,31 @@ +listen('Flarum\Tags\Events\DiscussionWasTagged', __CLASS__.'@whenDiscussionWasTagged'); + } + + public function whenDiscussionWasTagged(DiscussionWasTagged $event) + { + $post = DiscussionTaggedPost::reply( + $event->discussion->id, + $event->user->id, + array_pluck($event->oldTags, 'id'), + $event->discussion->tags()->lists('id') + ); + + $post = $event->discussion->addPost($post); + } +} diff --git a/extensions/tags/src/TagsServiceProvider.php b/extensions/tags/src/TagsServiceProvider.php index 328149a45..2a43f0c09 100644 --- a/extensions/tags/src/TagsServiceProvider.php +++ b/extensions/tags/src/TagsServiceProvider.php @@ -8,6 +8,7 @@ use Flarum\Extend\SerializeRelationship; use Flarum\Extend\ApiInclude; use Flarum\Extend\Permission; use Flarum\Extend\DiscussionGambit; +use Flarum\Extend\PostType; class TagsServiceProvider extends ServiceProvider { @@ -18,14 +19,14 @@ class TagsServiceProvider extends ServiceProvider */ public function boot() { - $this->extend( + $this->extend([ new ForumAssets([ __DIR__.'/../js/dist/extension.js', __DIR__.'/../less/extension.less' ]), new EventSubscribers([ - // 'Flarum\Tags\Handlers\DiscussionTaggedNotifier', + 'Flarum\Tags\Handlers\DiscussionTaggedNotifier', 'Flarum\Tags\Handlers\TagPreloader', 'Flarum\Tags\Handlers\TagSaver' ]), @@ -45,8 +46,10 @@ class TagsServiceProvider extends ServiceProvider // @todo add limitations to time etc. according to a config setting }), - new DiscussionGambit('Flarum\Tags\TagGambit') - ); + new DiscussionGambit('Flarum\Tags\TagGambit'), + + new PostType('Flarum\Tags\DiscussionTaggedPost') + ]); } /**