-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate User Recent Posts to React.js (#2664)
- Loading branch information
1 parent
753173d
commit c5a9891
Showing
44 changed files
with
507 additions
and
492 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
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.