A lot of things.

This commit is contained in:
2026-06-08 16:25:52 +02:00
parent 6f6d6b9b84
commit f529f74823
94 changed files with 9178 additions and 107 deletions

View File

@@ -17,6 +17,7 @@ use App\Models\Genre;
use App\Models\Language;
use App\Models\Modification;
use App\Models\Platform;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
@@ -28,9 +29,9 @@ class SubmissionsService {
/**
* Request for store/edit.
* @var StoreEntryRequest|null
* @var Request|null
*/
private ?StoreEntryRequest $request = null;
private ?Request $request = null;
/**
* Section for store/edit.
@@ -72,7 +73,9 @@ class SubmissionsService {
'done' => true,
'error' => null,
'uuid' => $uuid,
'state' => $file->state
'state' => $file->state,
'meta_online_patcher' => $file->online_patcher,
'meta_secondary_online_patcher' => $file->secondary_online_patcher,
];
$file = Cache::get("uploaded_file_{$uuid}");
@@ -86,7 +89,9 @@ class SubmissionsService {
'done' => true,
'error' => null,
'uuid' => $uuid,
'state' => $file['state']
'state' => $file['state'],
'meta_online_patcher' => false,
'meta_secondary_online_patcher' => false,
];
return null;
@@ -102,7 +107,7 @@ class SubmissionsService {
* @throws SubmissionException
* @throws \Throwable
*/
public function storeEntry( StoreEntryRequest $request, string $section ){
public function storeEntry( Request $request, string $section ){
// STEP 1 : Prepare basic fields.
@@ -188,23 +193,30 @@ class SubmissionsService {
$this->Step13_CreateCommentsThread( $entry );
// Step 14: Refresh XF count.
XenForoHelpers::updateEntriesCount( $entry->user_id );
if( $entry->state !== 'draft')
XenForoHelpers::updateEntriesCount( $entry->user_id );
return $entry;
}
/**
* @return int
* @return null|int
*
* @throws SubmissionException
*/
private function Step2_CreateAndReturnGameId(): int {
private function Step2_CreateAndReturnGameId(): ?int {
// Already existing game.
if( $this->request->input('game_id') )
return $this->request->input('game_id');
// No fields like a draft.
if( !$this->request->input('new-game-title') &&
!$this->request->input('new-game-platform') &&
!$this->request->input('new-game-genre') )
return null;
// Need to create a game.
$game = $this->createGameFromFormFields();
@@ -244,14 +256,14 @@ class SubmissionsService {
$fields['entry_title'] = $this->request->input('entry_title') ?? null;
if( section_must_be( [ 'homebrew', 'translations' ], $this->section ) && $gameId ){
$fields['game_name'] = Game::find( $gameId )->name;
$fields['game_name'] = $gameId ? Game::find( $gameId )->name : null;
}
if( section_must_be( 'translations', $this->section ) ) {
$fields['languages_string'] = Language::whereIn('id', $this->request->input('languages', []))->pluck('name')->implode(', ');
}
if( section_must_be(['romhacks', 'translations', 'homebrew', 'lua-scripts', 'tutorials'], $this->section ) ) {
// TODO: Add single platform ID compatibility.
$fields['platform_name'] = Game::find( $gameId )->platform->name;
$fields['platform_name'] = $gameId ? Game::find( $gameId )->platform->name : null;
}
return EntryHelpers::buildCompleteTitle( $this->section, $fields );
@@ -268,7 +280,7 @@ class SubmissionsService {
if( !$uuidData )
$uuidData = $this->request->input('files_uuid', [] );
foreach ( $uuidData as $uuid ) {
foreach ( $uuidData ?? [] as $uuid ) {
$fileData = Cache::pull("uploaded_file_{$uuid}");
if( !$fileData )
throw new SubmissionException( "File {$uuid} has expired. Please delete all your files and retry. If it's an edition, delete all your new files and retry." );
@@ -294,7 +306,7 @@ class SubmissionsService {
*/
private function Step8_SaveHashes( int $entryId ): void
{
foreach ( $this->request->input('hashes', [] ) as $hash ) {
foreach ( $this->request->input('hashes', [] ) ?? [] as $hash ) {
if( !isset($hash['filename'], $hash['hash_crc32'], $hash['hash_sha1'], $hash['verified']) ) {
continue;
}
@@ -320,7 +332,7 @@ class SubmissionsService {
// TODO: Code fragment to be replaced by edit version.
// Existing authors.
foreach ( $this->request->input('authors', [] ) as $authorId ) {
foreach ( $this->request->input('authors', [] ) ?? [] as $authorId ) {
$author = Author::find( $authorId );
if( !$author )
throw new SubmissionException( "Author {$authorId} does not exist." );
@@ -328,7 +340,7 @@ class SubmissionsService {
}
// New Authors
foreach ( $this->request->input('new-authors', [] ) as $authorName ) {
foreach ( $this->request->input('new-authors', [] ) ?? [] as $authorName ) {
$authorName = trim( $authorName );
if( $authorName === '' )
continue;
@@ -352,7 +364,7 @@ class SubmissionsService {
// TODO: Replace by edit version
foreach ( $this->request->input('modifications', [] ) as $modificationId ) {
foreach ( $this->request->input('modifications', [] ) ?? [] as $modificationId ) {
$modification = Modification::find( $modificationId );
if( !$modification )
throw new SubmissionException( "Modification {$modificationId} does not exist." );
@@ -370,7 +382,7 @@ class SubmissionsService {
{
// TODO: Replace by edit version.
foreach ( $this->request->input('languages', [] ) as $languageId ) {
foreach ( $this->request->input('languages', [] ) ?? [] as $languageId ) {
$language = Language::find( $languageId );
if( !$language )
throw new SubmissionException( "Language {$languageId} does not exist." );
@@ -381,7 +393,7 @@ class SubmissionsService {
private function Step12a_PrepareGalleryImages( Entry $entry ): void
{
foreach ( $this->request->input('gallery', [] ) as $imagePath ) {
foreach ( $this->request->input('gallery', [] ) ?? [] as $imagePath ) {
EntryGallery::create([
'entry_id' => $entry->id,
'image' => $imagePath,
@@ -396,6 +408,10 @@ class SubmissionsService {
*/
private function Step12b_MoveMainImage( Entry $entry ): void {
$mainImage = $entry->main_image;
if( !$mainImage )
return;
$newPath = 'entries/main-images/' . basename($mainImage);
if( !Storage::disk('public')->move($mainImage, $newPath) )
@@ -406,7 +422,7 @@ class SubmissionsService {
private function Step12c_SaveGalleryImages( Entry $entry ): void
{
foreach ( $entry->gallery as $galleryItem ) {
foreach ( $entry->gallery ?? [] as $galleryItem ) {
$newPath = 'entries/gallery-images/' . $entry->id . '/' . basename($galleryItem->image);
if( !Storage::disk('public')->move($galleryItem->image, $newPath) )
@@ -416,7 +432,7 @@ class SubmissionsService {
}
}
public function editEntry( StoreEntryRequest $request, string $section, Entry $entry ): Entry
public function editEntry(Request $request, string $section, Entry $entry ): Entry
{
// STEP 1: Prepare basic fields and keep in save some others fields.
@@ -477,6 +493,8 @@ class SubmissionsService {
'youtube_link' => $this->request->input('youtube_video'),
'user_id' => $user_id,
'complete_title' => $completeTitle,
'comments_thread_id' => $this->request->input('comments_thread_id'),
'featured' => $this->request->input('featured'),
];
if( \Auth::user()->can('moderate', $this->entry) ){
@@ -505,9 +523,6 @@ class SubmissionsService {
// STEP 11: Prepare new gallery images and prepare deletion of others ones.
$galleryPaths = $this->eStep11a_UpdateGalleryImages();
// STEP 13: Try to create comments area if it doesn't exist.
$this->Step13_CreateCommentsThread( $this->entry );
return $this->entry;
});
@@ -519,9 +534,14 @@ class SubmissionsService {
$this->eStep11c_UpdateGalleryImages( $galleryPaths );
// STEP 12: Refresh XF count.
if( $oldUserId )
XenForoHelpers::updateEntriesCount( $oldUserId );
XenForoHelpers::updateEntriesCount( $entry->user_id );
if( $entry->state !== 'draft' ) {
if ($oldUserId)
XenForoHelpers::updateEntriesCount($oldUserId);
XenForoHelpers::updateEntriesCount($entry->user_id);
}
// STEP 13: Try to create comments area if it doesn't exist.
$this->Step13_CreateCommentsThread( $this->entry );
return $entry;
}
@@ -550,6 +570,12 @@ class SubmissionsService {
}
// In draft.
if( !$this->request->input('new-game-title') &&
!$this->request->input('new-game-platform') &&
!$this->request->input('new-game-genre') )
return $this->entry->game_id;
// Need to create a game.
$game = $this->createGameFromFormFields();
@@ -562,8 +588,8 @@ class SubmissionsService {
*/
private function eStep6_UpdateEntryFiles(int $entryId ): void
{
$requestUuids = $this->request->input('files_uuid', []);
$requestStates = $this->request->input('files_state', []);
$requestUuids = $this->request->input('files_uuid', []) ?? [];
$requestStates = $this->request->input('files_state', []) ?? [];
$existingUuids = EntryFile::where( 'entry_id', $entryId )->pluck('file_uuid')->toArray();
$needDeletion = array_diff( $existingUuids, $requestUuids );
@@ -626,7 +652,7 @@ class SubmissionsService {
private function eStep8_UpdateAuthors(): void
{
$syncAuthorsId = [];
$requestAuthorsId = $this->request->input('authors', [] );
$requestAuthorsId = $this->request->input('authors', [] ) ?? [];
if( !empty( $requestAuthorsId ) ){
$valid = Author::whereIn( 'id', $requestAuthorsId )->pluck('id')->toArray();
@@ -638,7 +664,7 @@ class SubmissionsService {
$syncAuthorsId = array_merge( $syncAuthorsId, $requestAuthorsId );
}
foreach ( $this->request->input('new-authors', [] ) as $authorName ) {
foreach ( $this->request->input('new-authors', [] ) ?? [] as $authorName ) {
$authorName = trim($authorName);
if ($authorName === '')
continue;
@@ -660,7 +686,7 @@ class SubmissionsService {
*/
private function eStep9_UpdateRomhacksModifications(): void
{
$requestModifications = $this->request->input('modifications', [] );
$requestModifications = $this->request->input('modifications', [] ) ?? [];
if( !empty( $requestModifications ) ){
$valid = Modification::whereIn( 'id', $requestModifications )->pluck('id')->toArray();
@@ -681,7 +707,7 @@ class SubmissionsService {
*/
private function eStep10_UpdateLanguages(): void
{
$requestLanguages = $this->request->input('languages', [] );
$requestLanguages = $this->request->input('languages', [] ) ?? [];
if( !empty( $requestLanguages ) ){
$valid = Language::whereIn( 'id', $requestLanguages )->pluck('id')->toArray();
if( count( $valid ) !== count( $requestLanguages ) ){
@@ -695,7 +721,7 @@ class SubmissionsService {
private function eStep11a_UpdateGalleryImages(): array
{
$requestGallery = $this->request->input('gallery', [] );
$requestGallery = $this->request->input('gallery', [] ) ?? [];
$existingGalleryPaths = $this->entry->gallery->pluck('image')->toArray();
$needDeletion = array_diff( $existingGalleryPaths, $requestGallery );
@@ -723,6 +749,12 @@ class SubmissionsService {
if( $currentMainImagePath === $oldMainImagePath )
return;
if( !$currentMainImagePath ) {
if( $oldMainImagePath && Storage::disk('public')->exists($oldMainImagePath) )
Storage::disk('public')->delete($oldMainImagePath);
return;
}
$newPath = 'entries/main-images/' . basename( $currentMainImagePath );
if( !Storage::disk('public')->move( $currentMainImagePath, $newPath ) ){
@@ -755,6 +787,9 @@ class SubmissionsService {
private function Step13_CreateCommentsThread( Entry $entry ): void
{
if( $entry->state !== 'published' )
return;
if( !$entry->comments_thread_id )
CreateXenForoCommentsThread::dispatch( $entry );
// app(XenforoApiService::class)->createCommentsThread( $entry );

View File

@@ -48,6 +48,19 @@ class XenforoApiService {
return $response->json();
}
private function delete(string $endpoint, ?int $customUserId = null, array $data = [] ): mixed
{
$response = Http::withHeaders([
'XF-Api-Key' => $this->apiKey,
'XF-Api-User' => $customUserId ?? $this->superUserId,
])->delete("{$this->apiUrl}/{$endpoint}", $data);
if( !$response->ok() )
return null;
return $response->json();
}
public function getUserAlerts(int $userId): mixed
{
if( app(XenforoService::class)->getXfUser($userId)?->alerts_unviewed > 0 ) {
@@ -121,4 +134,14 @@ class XenforoApiService {
return $response['success'] ?? false;
}
public function deleteThreadWithEntry(int $threadId): bool
{
return (bool) $this->delete( "threads/{$threadId}", data: ['reason' => "Deletion with entry." ] );
}
public function restoreThreadWithEntry(int $threadId): bool
{
return (bool) $this->post("threads/{$threadId}/undelete" );
}
}