-
-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: migrate User Recent Posts to React.js #2664
Merged
wescopeland
merged 20 commits into
RetroAchievements:master
from
wescopeland:user-forum-posts
Sep 8, 2024
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4384849
refactor: improve type safety of forum Recent Posts page
wescopeland a3f8b87
chore: lint
wescopeland 9e10261
chore: lint
wescopeland a5f46d0
chore: rename method
wescopeland 2c3976b
feat: migrate User Recent Posts to React
wescopeland 7c28969
fix: add page meta
wescopeland 87d81a3
chore: fix route
wescopeland e0f6564
Merge branch 'master' into recent-posts-type-safety
wescopeland 1f3e563
chore: fix i18n
wescopeland 355af9c
Merge branch 'recent-posts-type-safety' into user-forum-posts
wescopeland 65ab4fb
Merge branch 'master' into recent-posts-type-safety
wescopeland 3cbc50e
Merge branch 'recent-posts-type-safety' into user-forum-posts
wescopeland 3d8c0f6
Merge branch 'master' into recent-posts-type-safety
wescopeland 83cbdde
Merge branch 'recent-posts-type-safety' into user-forum-posts
wescopeland 3c472bb
Merge branch 'master' into recent-posts-type-safety
wescopeland 4b3c084
chore: lint
wescopeland f94c034
Merge branch 'recent-posts-type-safety' into user-forum-posts
wescopeland 937de34
chore: lint
wescopeland fb6691f
Merge branch 'master' into user-forum-posts
wescopeland 8dc294b
Merge branch 'master' into user-forum-posts
wescopeland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
103 changes: 103 additions & 0 deletions
103
app/Community/Controllers/UserForumTopicCommentController.php
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,103 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Community\Controllers; | ||
|
||
use App\Community\Data\UserRecentPostsPagePropsData; | ||
use App\Data\ForumTopicData; | ||
use App\Data\PaginatedData; | ||
use App\Data\UserData; | ||
use App\Enums\Permissions; | ||
use App\Http\Controller; | ||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Pagination\LengthAwarePaginator; | ||
use Inertia\Inertia; | ||
use Inertia\Response as InertiaResponse; | ||
|
||
class UserForumTopicCommentController extends Controller | ||
{ | ||
public function index(Request $request, User $user): InertiaResponse | ||
{ | ||
$this->authorize('view', $user); | ||
|
||
$offset = $request->input('page', 1) - 1; | ||
$count = 25; | ||
|
||
/** @var User $me */ | ||
$me = auth()->user(); | ||
$permissions = Permissions::Unregistered; | ||
if ($me) { | ||
$permissions = (int) $me->getAttribute('Permissions'); | ||
} | ||
|
||
$posts = $this->getUserPosts( | ||
$user, | ||
page: (int) $request->input('page', 1), | ||
permissions: $permissions, | ||
); | ||
|
||
$transformedPosts = array_map( | ||
fn ($post) => ForumTopicData::fromUserPost($post)->include('latestComment'), | ||
$posts | ||
); | ||
|
||
$paginator = new LengthAwarePaginator( | ||
items: $transformedPosts, | ||
total: $user->forumPosts()->authorized()->viewable($me)->count(), | ||
perPage: $count, | ||
currentPage: $offset + 1, | ||
options: [ | ||
'path' => $request->url(), | ||
'query' => $request->query(), | ||
] | ||
); | ||
|
||
$paginatedPosts = PaginatedData::fromLengthAwarePaginator($paginator); | ||
|
||
$props = new UserRecentPostsPagePropsData( | ||
UserData::fromUser($user), | ||
$paginatedPosts | ||
); | ||
|
||
return Inertia::render('user/[user]/posts', $props); | ||
} | ||
|
||
private function getUserPosts(User $user, int $page = 1, int $permissions = Permissions::Unregistered): array | ||
{ | ||
$count = 25; | ||
$offset = ($page - 1) * $count; | ||
|
||
$query = " | ||
SELECT | ||
ft.ID AS ForumTopicID, | ||
ft.Title AS ForumTopicTitle, | ||
f.ID AS ForumID, | ||
f.Title AS ForumTitle, | ||
lftc.ID AS CommentID, | ||
lftc.DateCreated AS PostedAt, | ||
lftc.author_id, | ||
ua.User AS Author, | ||
ua.display_name AS AuthorDisplayName, | ||
LEFT(lftc.Payload, 260) AS ShortMsg, | ||
LENGTH(lftc.Payload) > 260 AS IsTruncated | ||
FROM ForumTopicComment AS lftc | ||
INNER JOIN ForumTopic AS ft ON ft.ID = lftc.ForumTopicID | ||
INNER JOIN Forum AS f ON f.ID = ft.ForumID | ||
LEFT JOIN UserAccounts AS ua ON ua.ID = lftc.author_id | ||
WHERE lftc.author_id = :author_id | ||
AND lftc.Authorised = 1 | ||
AND ft.RequiredPermissions <= :permissions | ||
AND ft.deleted_at IS NULL | ||
ORDER BY lftc.DateCreated DESC | ||
LIMIT :offset, :count"; | ||
|
||
return legacyDbFetchAll($query, [ | ||
'author_id' => $user->id, | ||
'offset' => $offset, | ||
'count' => $count, | ||
'permissions' => $permissions, | ||
])->toArray(); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Community\Data; | ||
|
||
use App\Data\PaginatedData; | ||
use App\Data\UserData; | ||
use Spatie\LaravelData\Data; | ||
use Spatie\TypeScriptTransformer\Attributes\TypeScript; | ||
|
||
#[TypeScript('UserRecentPostsPageProps<TItems = App.Data.ForumTopic>')] | ||
class UserRecentPostsPagePropsData extends Data | ||
{ | ||
public function __construct( | ||
public UserData $targetUser, | ||
public PaginatedData $paginatedTopics, | ||
) { | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change fixes a bug on the user profile. The current count of forum posts represents all posts made by the user, including the ones that can't be seen due to being unauthorized or locked behind a permissions gate.
The count now reflects the posts that are visible on the
/user/[user]/posts
page.