feat(likes): Add likes tab to user profile (#3528)

This commit is contained in:
Ian Morland 2022-07-14 14:38:31 +01:00 committed by GitHub
parent bf6f63cfe1
commit 3246f5a8f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 0 deletions

View File

@ -15,8 +15,10 @@ use Flarum\Likes\Event\PostWasLiked;
use Flarum\Likes\Event\PostWasUnliked;
use Flarum\Likes\Listener;
use Flarum\Likes\Notification\PostLikedBlueprint;
use Flarum\Likes\Query\LikedByFilter;
use Flarum\Post\Event\Deleted;
use Flarum\Post\Event\Saving;
use Flarum\Post\Filter\PostFilterer;
use Flarum\Post\Post;
use Flarum\User\User;
@ -59,4 +61,7 @@ return [
->listen(PostWasUnliked::class, Listener\SendNotificationWhenPostIsUnliked::class)
->listen(Deleted::class, [Listener\SaveLikesToDatabase::class, 'whenPostIsDeleted'])
->listen(Saving::class, [Listener\SaveLikesToDatabase::class, 'whenPostIsSaving']),
(new Extend\Filter(PostFilterer::class))
->addFilter(LikedByFilter::class),
];

View File

@ -0,0 +1,21 @@
import { extend } from 'flarum/common/extend';
import app from 'flarum/forum/app';
import UserPage from 'flarum/forum/components/UserPage';
import LinkButton from 'flarum/common/components/LinkButton';
import LikesUserPage from './components/LikesUserPage';
import ItemList from 'flarum/common/utils/ItemList';
import type Mithril from 'mithril';
export default function addLikesTabToUserProfile() {
app.routes['user.likes'] = { path: '/u/:username/likes', component: LikesUserPage };
extend(UserPage.prototype, 'navItems', function (items: ItemList<Mithril.Children>) {
const user = this.user;
items.add(
'likes',
<LinkButton href={app.route('user.likes', { username: user.slug() })} icon="far fa-thumbs-up">
{app.translator.trans('flarum-likes.forum.user.likes_link')}
</LinkButton>,
88
);
});
}

View File

@ -0,0 +1,24 @@
import app from 'flarum/forum/app';
import PostsUserPage from 'flarum/forum/components/PostsUserPage';
/**
* The `LikesUserPage` component shows posts which user the user liked.
*/
export default class LikesUserPage extends PostsUserPage {
/**
* Load a new page of the user's activity feed.
*
* @param offset The position to start getting results from.
* @protected
*/
loadResults(offset: number) {
return app.store.find('posts', {
filter: {
type: 'comment',
likedBy: this.user.id(),
},
page: { offset, limit: this.loadLimit },
sort: '-createdAt',
});
}
}

View File

@ -7,6 +7,7 @@ import NotificationGrid from 'flarum/forum/components/NotificationGrid';
import addLikeAction from './addLikeAction';
import addLikesList from './addLikesList';
import PostLikedNotification from './components/PostLikedNotification';
import addLikesTabToUserProfile from './addLikesTabToUserProfile';
app.initializers.add('flarum-likes', () => {
app.notificationComponents.postLiked = PostLikedNotification;
@ -16,6 +17,7 @@ app.initializers.add('flarum-likes', () => {
addLikeAction();
addLikesList();
addLikesTabToUserProfile();
extend(NotificationGrid.prototype, 'notificationTypes', function (items) {
items.add('postLiked', {

View File

@ -35,3 +35,7 @@ flarum-likes:
# These translations are used in the Settings page.
settings:
notify_post_liked_label: Someone likes one of my posts
# These translations are used in the User profile page.
user:
likes_link: Likes

View File

@ -0,0 +1,34 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Likes\Query;
use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
class LikedByFilter implements FilterInterface
{
public function getFilterKey(): string
{
return 'likedBy';
}
public function filter(FilterState $filterState, string $filterValue, bool $negate)
{
$likedId = trim($filterValue, '"');
$filterState
->getQuery()
->whereIn('id', function ($query) use ($likedId, $negate) {
$query->select('post_id')
->from('post_likes')
->where('user_id', $negate ? '!=' : '=', $likedId);
});
}
}