From b4d7c18eb95e839630ff4a3f035f5bd7c608ada6 Mon Sep 17 00:00:00 2001 From: Sajjad Hasehmian Date: Thu, 4 Feb 2016 01:04:08 +0330 Subject: [PATCH] Add Mentions section to user profile (fixes flarum/core#319) --- extensions/mentions/bootstrap.php | 1 + .../mentions/js/forum/dist/extension.js | 71 ++++++++++++++++++- .../forum/src/components/MentionsUserPage.js | 24 +++++++ extensions/mentions/js/forum/src/main.js | 18 +++++ .../src/Listener/AddFilterByMentions.php | 36 ++++++++++ 5 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 extensions/mentions/js/forum/src/components/MentionsUserPage.js create mode 100755 extensions/mentions/src/Listener/AddFilterByMentions.php diff --git a/extensions/mentions/bootstrap.php b/extensions/mentions/bootstrap.php index f4728df85..8297197de 100644 --- a/extensions/mentions/bootstrap.php +++ b/extensions/mentions/bootstrap.php @@ -20,6 +20,7 @@ return function (Dispatcher $events, Factory $views) { $events->subscribe(Listener\FormatUserMentions::class); $events->subscribe(Listener\UpdatePostMentionsMetadata::class); $events->subscribe(Listener\UpdateUserMentionsMetadata::class); + $events->subscribe(Listener\AddFilterByMentions::class); $views->addNamespace('flarum-mentions', __DIR__.'/views'); }; diff --git a/extensions/mentions/js/forum/dist/extension.js b/extensions/mentions/js/forum/dist/extension.js index 0a05ee713..779d5d845 100644 --- a/extensions/mentions/js/forum/dist/extension.js +++ b/extensions/mentions/js/forum/dist/extension.js @@ -924,10 +924,10 @@ System.register('flarum/mentions/components/UserMentionedNotification', ['flarum } }; });; -System.register('flarum/mentions/main', ['flarum/extend', 'flarum/app', 'flarum/components/NotificationGrid', 'flarum/utils/string', 'flarum/mentions/addPostMentionPreviews', 'flarum/mentions/addMentionedByList', 'flarum/mentions/addPostReplyAction', 'flarum/mentions/addComposerAutocomplete', 'flarum/mentions/components/PostMentionedNotification', 'flarum/mentions/components/UserMentionedNotification'], function (_export) { +System.register('flarum/mentions/main', ['flarum/extend', 'flarum/app', 'flarum/components/NotificationGrid', 'flarum/utils/string', 'flarum/mentions/addPostMentionPreviews', 'flarum/mentions/addMentionedByList', 'flarum/mentions/addPostReplyAction', 'flarum/mentions/addComposerAutocomplete', 'flarum/mentions/components/PostMentionedNotification', 'flarum/mentions/components/UserMentionedNotification', 'flarum/components/UserPage', 'flarum/components/LinkButton', 'flarum/mentions/components/MentionsUserPage'], function (_export) { 'use strict'; - var extend, app, NotificationGrid, getPlainContent, addPostMentionPreviews, addMentionedByList, addPostReplyAction, addComposerAutocomplete, PostMentionedNotification, UserMentionedNotification; + var extend, app, NotificationGrid, getPlainContent, addPostMentionPreviews, addMentionedByList, addPostReplyAction, addComposerAutocomplete, PostMentionedNotification, UserMentionedNotification, UserPage, LinkButton, MentionsUserPage; return { setters: [function (_flarumExtend) { extend = _flarumExtend.extend; @@ -949,6 +949,12 @@ System.register('flarum/mentions/main', ['flarum/extend', 'flarum/app', 'flarum/ PostMentionedNotification = _flarumMentionsComponentsPostMentionedNotification['default']; }, function (_flarumMentionsComponentsUserMentionedNotification) { UserMentionedNotification = _flarumMentionsComponentsUserMentionedNotification['default']; + }, function (_flarumComponentsUserPage) { + UserPage = _flarumComponentsUserPage['default']; + }, function (_flarumComponentsLinkButton) { + LinkButton = _flarumComponentsLinkButton['default']; + }, function (_flarumMentionsComponentsMentionsUserPage) { + MentionsUserPage = _flarumMentionsComponentsMentionsUserPage['default']; }], execute: function () { @@ -987,8 +993,69 @@ System.register('flarum/mentions/main', ['flarum/extend', 'flarum/app', 'flarum/ }); }); + // Add add mentions tab in user profile + app.routes['user.mentions'] = { path: '/u/:username/mentions', component: MentionsUserPage.component() }; + extend(UserPage.prototype, 'navItems', function (items) { + var user = this.user; + items.add('mentions', LinkButton.component({ + href: app.route('user.mentions', { username: user.username() }), + name: 'mentions', + children: [app.translator.trans('flarum-mentions.forum.user.mentions_link')], + icon: 'at' + }), 80); + }); + getPlainContent.removeSelectors.push('a.PostMention'); }); } }; +});; +System.register('flarum/mentions/components/MentionsUserPage', ['flarum/components/PostsUserPage'], function (_export) { + + /** + * The `MentionsUserPage` component shows post which user Mentioned at + */ + 'use strict'; + + var PostsUserPage, MentionsUserPage; + return { + setters: [function (_flarumComponentsPostsUserPage) { + PostsUserPage = _flarumComponentsPostsUserPage['default']; + }], + execute: function () { + MentionsUserPage = (function (_PostsUserPage) { + babelHelpers.inherits(MentionsUserPage, _PostsUserPage); + + function MentionsUserPage() { + babelHelpers.classCallCheck(this, MentionsUserPage); + babelHelpers.get(Object.getPrototypeOf(MentionsUserPage.prototype), 'constructor', this).apply(this, arguments); + } + + babelHelpers.createClass(MentionsUserPage, [{ + key: 'loadResults', + + /** + * Load a new page of the user's activity feed. + * + * @param {Integer} [offset] The position to start getting results from. + * @return {Promise} + * @protected + */ + value: function loadResults(offset) { + return app.store.find('posts', { + filter: { + type: 'comment', + mentioned: this.user.id() + }, + page: { offset: offset, limit: this.loadLimit }, + sort: '-time' + }); + } + }]); + return MentionsUserPage; + })(PostsUserPage); + + _export('default', MentionsUserPage); + } + }; }); \ No newline at end of file diff --git a/extensions/mentions/js/forum/src/components/MentionsUserPage.js b/extensions/mentions/js/forum/src/components/MentionsUserPage.js new file mode 100644 index 000000000..f7370eeef --- /dev/null +++ b/extensions/mentions/js/forum/src/components/MentionsUserPage.js @@ -0,0 +1,24 @@ +import PostsUserPage from 'flarum/components/PostsUserPage'; + +/** + * The `MentionsUserPage` component shows post which user Mentioned at + */ +export default class MentionsUserPage extends PostsUserPage { + /** + * Load a new page of the user's activity feed. + * + * @param {Integer} [offset] The position to start getting results from. + * @return {Promise} + * @protected + */ + loadResults(offset) { + return app.store.find('posts', { + filter: { + type: 'comment', + mentioned: this.user.id() + }, + page: {offset, limit: this.loadLimit}, + sort: '-time' + }); + } +} diff --git a/extensions/mentions/js/forum/src/main.js b/extensions/mentions/js/forum/src/main.js index 43c81a9da..d0c4f0a57 100644 --- a/extensions/mentions/js/forum/src/main.js +++ b/extensions/mentions/js/forum/src/main.js @@ -9,6 +9,9 @@ import addPostReplyAction from 'flarum/mentions/addPostReplyAction'; import addComposerAutocomplete from 'flarum/mentions/addComposerAutocomplete'; import PostMentionedNotification from 'flarum/mentions/components/PostMentionedNotification'; import UserMentionedNotification from 'flarum/mentions/components/UserMentionedNotification'; +import UserPage from 'flarum/components/UserPage' +import LinkButton from 'flarum/components/LinkButton'; +import MentionsUserPage from 'flarum/mentions/components/MentionsUserPage'; app.initializers.add('flarum-mentions', function() { // For every mention of a post inside a post's content, set up a hover handler @@ -45,5 +48,20 @@ app.initializers.add('flarum-mentions', function() { }); }); + // Add add mentions tab in user profile + app.routes['user.mentions'] = {path: '/u/:username/mentions', component: MentionsUserPage.component()}; + extend(UserPage.prototype, 'navItems', function(items) { + const user = this.user; + items.add('mentions', + LinkButton.component({ + href: app.route('user.mentions', {username: user.username()}), + name: 'mentions', + children: [app.translator.trans('flarum-mentions.forum.user.mentions_link')], + icon: 'at' + }), + 80 + ); + }); + getPlainContent.removeSelectors.push('a.PostMention'); }); diff --git a/extensions/mentions/src/Listener/AddFilterByMentions.php b/extensions/mentions/src/Listener/AddFilterByMentions.php new file mode 100755 index 000000000..757c86e0e --- /dev/null +++ b/extensions/mentions/src/Listener/AddFilterByMentions.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Mentions\Listener; + +use Flarum\Event\ConfigurePostsQuery; +use Illuminate\Contracts\Events\Dispatcher; + +class AddFilterByMentions +{ + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + $events->listen(ConfigurePostsQuery::class, [$this, 'addFilter']); + } + + /** + * @param ConfigurePostsQuery $event + */ + public function addFilter(ConfigurePostsQuery $event) + { + if ($mentionedId = array_get($event->filter, 'mentioned')) { + $event->query->join('mentions_users', 'posts.id', '=', 'mentions_users.post_id') + ->where('mentions_users.mentions_id', '=', $mentionedId); + } + } +}