-
-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(likes): limit
likes
relationship results (#3781)
* perf(core,mentions): limit `mentionedBy` post relation results Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * Apply fixes from StyleCI * chore: use a static property to allow customization Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * chore: use a static property to allow customization Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * chore: include count in show post endpoint Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * chore: consistent locale key format Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * chore: forgot to delete `FilterVisiblePosts` Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * test: `mentionedByCount` must not include invisible posts to actor Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * fix: visibility scoping on `mentionedByCount` Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * fix: `loadAggregates` conflicts with visibility scopers Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * Apply fixes from StyleCI * chore: phpstan Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * perf(likes): limit `likes` relationship results Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * Apply fixes from StyleCI * chore: simplify Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * test: `likesCount` is as expected Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * Apply fixes from StyleCI --------- Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> Co-authored-by: StyleCI Bot <bot@styleci.io> Co-authored-by: IanM <16573496+imorland@users.noreply.github.com>
- Loading branch information
1 parent
6b8e9ce
commit d0669b0
Showing
15 changed files
with
468 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Post from 'flarum/common/models/Post'; | ||
import User from 'flarum/common/models/User'; | ||
|
||
declare module 'flarum/common/models/Post' { | ||
export default interface Post { | ||
likes(): User[]; | ||
likesCount(): number; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 0 additions & 31 deletions
31
extensions/likes/js/src/forum/components/PostLikesModal.js
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
extensions/likes/js/src/forum/components/PostLikesModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import app from 'flarum/forum/app'; | ||
import Modal from 'flarum/common/components/Modal'; | ||
import Link from 'flarum/common/components/Link'; | ||
import avatar from 'flarum/common/helpers/avatar'; | ||
import username from 'flarum/common/helpers/username'; | ||
import type { IInternalModalAttrs } from 'flarum/common/components/Modal'; | ||
import type Post from 'flarum/common/models/Post'; | ||
import type Mithril from 'mithril'; | ||
import PostLikesModalState from '../states/PostLikesModalState'; | ||
import Button from '@flarum/core/src/common/components/Button'; | ||
import LoadingIndicator from '@flarum/core/src/common/components/LoadingIndicator'; | ||
|
||
export interface IPostLikesModalAttrs extends IInternalModalAttrs { | ||
post: Post; | ||
} | ||
|
||
export default class PostLikesModal<CustomAttrs extends IPostLikesModalAttrs = IPostLikesModalAttrs> extends Modal<CustomAttrs, PostLikesModalState> { | ||
oninit(vnode: Mithril.VnodeDOM<CustomAttrs, this>) { | ||
super.oninit(vnode); | ||
|
||
this.state = new PostLikesModalState({ | ||
filter: { | ||
liked: this.attrs.post.id()!, | ||
}, | ||
}); | ||
|
||
this.state.refresh(); | ||
} | ||
|
||
className() { | ||
return 'PostLikesModal Modal--small'; | ||
} | ||
|
||
title() { | ||
return app.translator.trans('flarum-likes.forum.post_likes.title'); | ||
} | ||
|
||
content() { | ||
return ( | ||
<> | ||
<div className="Modal-body"> | ||
{this.state.isInitialLoading() ? ( | ||
<LoadingIndicator /> | ||
) : ( | ||
<ul className="PostLikesModal-list"> | ||
{this.state.getPages().map((page) => | ||
page.items.map((user) => ( | ||
<li> | ||
<Link href={app.route.user(user)}> | ||
{avatar(user)} {username(user)} | ||
</Link> | ||
</li> | ||
)) | ||
)} | ||
</ul> | ||
)} | ||
</div> | ||
{this.state.hasNext() ? ( | ||
<div className="Modal-footer"> | ||
<div className="Form Form--centered"> | ||
<div className="Form-group"> | ||
<Button className="Button Button--block" onclick={() => this.state.loadNext()} loading={this.state.isLoadingNext()}> | ||
{app.translator.trans('flarum-likes.forum.post_likes.load_more_button')} | ||
</Button> | ||
</div> | ||
</div> | ||
</div> | ||
) : null} | ||
</> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
extensions/likes/js/src/forum/states/PostLikesModalState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import PaginatedListState, { PaginatedListParams } from '@flarum/core/src/common/states/PaginatedListState'; | ||
import User from 'flarum/common/models/User'; | ||
|
||
export interface PostLikesModalListParams extends PaginatedListParams { | ||
filter: { | ||
liked: string; | ||
}; | ||
page?: { | ||
offset?: number; | ||
limit: number; | ||
}; | ||
} | ||
|
||
export default class PostLikesModalState<P extends PostLikesModalListParams = PostLikesModalListParams> extends PaginatedListState<User, P> { | ||
constructor(params: P, page: number = 1) { | ||
const limit = 10; | ||
|
||
params.page = { ...(params.page || {}), limit }; | ||
|
||
super(params, page, limit); | ||
} | ||
|
||
get type(): string { | ||
return 'users'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?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\Api; | ||
|
||
use Flarum\Discussion\Discussion; | ||
use Flarum\Http\RequestUtil; | ||
use Flarum\Post\Post; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
use Illuminate\Database\Query\Expression; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class LoadLikesRelationship | ||
{ | ||
public static $maxLikes = 4; | ||
|
||
public static function mutateRelation(BelongsToMany $query, ServerRequestInterface $request): BelongsToMany | ||
{ | ||
$actor = RequestUtil::getActor($request); | ||
|
||
$grammar = $query->getQuery()->getGrammar(); | ||
|
||
return $query | ||
// So that we can tell if the current user has liked the post. | ||
->orderBy(new Expression($grammar->wrap('user_id').' = '.$actor->id), 'desc') | ||
// Limiting a relationship results is only possible because | ||
// the Post model uses the \Staudenmeir\EloquentEagerLimit\HasEagerLimit | ||
// trait. | ||
->limit(self::$maxLikes); | ||
} | ||
|
||
/** | ||
* Called using the @see ApiController::prepareDataForSerialization extender. | ||
*/ | ||
public static function countRelation($controller, $data): void | ||
{ | ||
$loadable = null; | ||
|
||
if ($data instanceof Discussion) { | ||
// @phpstan-ignore-next-line | ||
$loadable = $data->newCollection($data->posts)->filter(function ($post) { | ||
return $post instanceof Post; | ||
}); | ||
} elseif ($data instanceof Collection) { | ||
$loadable = $data; | ||
} elseif ($data instanceof Post) { | ||
$loadable = $data->newCollection([$data]); | ||
} | ||
|
||
if ($loadable) { | ||
$loadable->loadCount('likes'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 LikedFilter implements FilterInterface | ||
{ | ||
public function getFilterKey(): string | ||
{ | ||
return 'liked'; | ||
} | ||
|
||
public function filter(FilterState $filterState, string $filterValue, bool $negate) | ||
{ | ||
$likedId = trim($filterValue, '"'); | ||
|
||
$filterState | ||
->getQuery() | ||
->whereIn('id', function ($query) use ($likedId) { | ||
$query->select('user_id') | ||
->from('post_likes') | ||
->where('post_id', $likedId); | ||
}, 'and', $negate); | ||
} | ||
} |
Oops, something went wrong.