-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate forum post Edit page to React (#3021)
- Loading branch information
1 parent
82e048b
commit 229f3c0
Showing
121 changed files
with
4,984 additions
and
69 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
app/Community/Actions/FetchDynamicShortcodeContentAction.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,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Community\Actions; | ||
|
||
use App\Data\UserData; | ||
use App\Models\Achievement; | ||
use App\Models\Game; | ||
use App\Models\Ticket; | ||
use App\Models\User; | ||
use App\Platform\Data\AchievementData; | ||
use App\Platform\Data\GameData; | ||
use App\Platform\Data\TicketData; | ||
use Illuminate\Support\Collection; | ||
|
||
class FetchDynamicShortcodeContentAction | ||
{ | ||
public function execute( | ||
array $usernames = [], | ||
array $ticketIds = [], | ||
array $achievementIds = [], | ||
array $gameIds = [], | ||
): array { | ||
$results = collect([ | ||
'users' => $this->fetchUsers($usernames), | ||
'tickets' => $this->fetchTickets($ticketIds), | ||
'achievements' => $this->fetchAchievements($achievementIds), | ||
'games' => $this->fetchGames($gameIds), | ||
]); | ||
|
||
return $results->toArray(); | ||
} | ||
|
||
/** | ||
* @return Collection<int, UserData> | ||
*/ | ||
private function fetchUsers(array $usernames): Collection | ||
{ | ||
if (empty($usernames)) { | ||
return collect(); | ||
} | ||
|
||
$users = User::query() | ||
->where(function ($query) use ($usernames) { | ||
$query->whereIn('User', $usernames) | ||
->orWhereIn('display_name', $usernames); | ||
}) | ||
->get(); | ||
|
||
return $users->map(fn (User $user) => UserData::fromUser($user)); | ||
} | ||
|
||
/** | ||
* @return Collection<int, TicketData> | ||
*/ | ||
private function fetchTickets(array $ticketIds): Collection | ||
{ | ||
if (empty($ticketIds)) { | ||
return collect(); | ||
} | ||
|
||
return Ticket::with('achievement') | ||
->whereIn('ID', $ticketIds) | ||
->get() | ||
->map(fn (Ticket $ticket) => TicketData::fromTicket($ticket)->include('state', 'ticketable')); | ||
} | ||
|
||
/** | ||
* @return Collection<int, AchievementData> | ||
*/ | ||
private function fetchAchievements(array $achievementIds): Collection | ||
{ | ||
if (empty($achievementIds)) { | ||
return collect(); | ||
} | ||
|
||
return Achievement::whereIn('ID', $achievementIds) | ||
->get() | ||
->map(fn (Achievement $achievement) => AchievementData::fromAchievement($achievement)->include( | ||
'badgeUnlockedUrl', | ||
'points' | ||
)); | ||
} | ||
|
||
/** | ||
* @return Collection<int, GameData> | ||
*/ | ||
private function fetchGames(array $gameIds): Collection | ||
{ | ||
if (empty($gameIds)) { | ||
return collect(); | ||
} | ||
|
||
return Game::with('system') | ||
->whereIn('ID', $gameIds) | ||
->get() | ||
->map(fn (Game $game) => GameData::fromGame($game)->include('badgeUrl', 'system.name')); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
app/Community/Controllers/Api/ForumTopicCommentApiController.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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Community\Controllers\Api; | ||
|
||
use App\Community\Actions\FetchDynamicShortcodeContentAction; | ||
use App\Community\Requests\PreviewForumPostRequest; | ||
use App\Community\Requests\UpdateForumTopicCommentRequest; | ||
use App\Http\Controller; | ||
use App\Models\ForumTopicComment; | ||
use App\Support\Shortcode\Shortcode; | ||
use Illuminate\Http\JsonResponse; | ||
|
||
class ForumTopicCommentApiController extends Controller | ||
{ | ||
public function store(): void | ||
{ | ||
} | ||
|
||
public function update( | ||
UpdateForumTopicCommentRequest $request, | ||
ForumTopicComment $comment | ||
): JsonResponse { | ||
$this->authorize('update', $comment); | ||
|
||
// Take any RA links and convert them to relevant shortcodes. | ||
// eg: "https://retroachievements.org/game/1" --> "[game=1]" | ||
$newPayload = normalize_shortcodes($request->input('body')); | ||
|
||
// Convert [user=$user->username] to [user=$user->id]. | ||
$newPayload = Shortcode::convertUserShortcodesToUseIds($newPayload); | ||
|
||
$comment->body = $newPayload; | ||
$comment->save(); | ||
|
||
return response()->json(['success' => true]); | ||
} | ||
|
||
public function destroy(): void | ||
{ | ||
} | ||
|
||
public function preview( | ||
PreviewForumPostRequest $request, | ||
FetchDynamicShortcodeContentAction $action | ||
): JsonResponse { | ||
$entities = $action->execute( | ||
usernames: $request->input('usernames'), | ||
ticketIds: $request->input('ticketIds'), | ||
achievementIds: $request->input('achievementIds'), | ||
gameIds: $request->input('gameIds'), | ||
); | ||
|
||
return response()->json($entities); | ||
} | ||
} |
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
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Community\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class PreviewForumPostRequest extends FormRequest | ||
{ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'usernames' => 'present|array', | ||
'usernames.*' => 'string', | ||
'ticketIds' => 'present|array', | ||
'ticketIds.*' => 'integer', | ||
'achievementIds' => 'present|array', | ||
'achievementIds.*' => 'integer', | ||
'gameIds' => 'present|array', | ||
'gameIds.*' => 'integer', | ||
]; | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Community\Requests; | ||
|
||
use App\Support\Rules\ContainsRegularCharacter; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class UpdateForumTopicCommentRequest extends FormRequest | ||
{ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'body' => [ | ||
'required', | ||
'string', | ||
'max:60000', | ||
new ContainsRegularCharacter(), | ||
], | ||
]; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Data; | ||
|
||
use Spatie\LaravelData\Data; | ||
use Spatie\TypeScriptTransformer\Attributes\TypeScript; | ||
|
||
#[TypeScript('EditForumTopicCommentPageProps')] | ||
class EditForumTopicCommentPagePropsData extends Data | ||
{ | ||
public function __construct( | ||
public ForumTopicCommentData $forumTopicComment, | ||
) { | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Data; | ||
|
||
use App\Models\ForumCategory; | ||
use Spatie\LaravelData\Data; | ||
use Spatie\LaravelData\Lazy; | ||
use Spatie\TypeScriptTransformer\Attributes\TypeScript; | ||
|
||
#[TypeScript('ForumCategory')] | ||
class ForumCategoryData extends Data | ||
{ | ||
public function __construct( | ||
public int $id, | ||
public string $title, | ||
public Lazy|string $description, | ||
public Lazy|int $orderColumn, | ||
) { | ||
} | ||
|
||
public static function fromForumCategory(ForumCategory $category): self | ||
{ | ||
return new self( | ||
id: $category->id, | ||
title: $category->title, | ||
description: Lazy::create(fn () => $category->description), | ||
orderColumn: Lazy::create(fn () => $category->order_column), | ||
); | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Data; | ||
|
||
use App\Models\Forum; | ||
use Spatie\LaravelData\Data; | ||
use Spatie\LaravelData\Lazy; | ||
use Spatie\TypeScriptTransformer\Attributes\TypeScript; | ||
|
||
#[TypeScript('Forum')] | ||
class ForumData extends Data | ||
{ | ||
public function __construct( | ||
public int $id, | ||
public string $title, | ||
public Lazy|string $description, | ||
public Lazy|int $orderColumn, | ||
public Lazy|ForumCategoryData $category, | ||
) { | ||
} | ||
|
||
public static function fromForum(Forum $forum): self | ||
{ | ||
return new self( | ||
id: $forum->id, | ||
title: $forum->title, | ||
description: Lazy::create(fn () => $forum->description), | ||
orderColumn: Lazy::create(fn () => $forum->order_column), | ||
category: Lazy::create(fn () => ForumCategoryData::fromForumCategory($forum->category)), | ||
); | ||
} | ||
} |
Oops, something went wrong.