Skip to content

Commit 603a0e4

Browse files
committed
Fix downloads not counted due to Laravel's dumb way of handling relations
Essentially it DID NOT wrap the relations conditions which meant that single orWhere check made sure to make the relation useless and return true as long as you have a single download on said IP
1 parent c072740 commit 603a0e4

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

backend/app/Http/Controllers/FileController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public function getVersions(Request $request) {
376376
*/
377377
public function registerDownload(Request $request, File $file)
378378
{
379-
ModService::registerDownload($file);
379+
return ModService::registerDownload($file);
380380
}
381381

382382
/**

backend/app/Services/ModService.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,13 @@ public static function registerDownload(File|Link $downloadable)
396396
* @var Builder
397397
*/
398398
$downloads = $downloadable->downloadsRelation();
399-
$alreadyDownloadedFile = $downloads->when(isset($user), fn($q) => $q->where('user_id', $user->id))
400-
->orWhere('ip_address', $ip)
401-
->exists();
399+
$alreadyDownloadedFile = $downloads->where(function($q) use ($user, $ip) {
400+
$q->when(isset($user), fn($q) => $q->where('user_id', $user->id))
401+
->orWhere('ip_address', $ip);
402+
})->exists();
402403

403404
if ($alreadyDownloadedFile) {
404-
return response()->noContent(201);
405+
return response()->noContent(200);
405406
}
406407

407408
// Create download for file or link
@@ -420,7 +421,7 @@ public static function registerDownload(File|Link $downloadable)
420421
})->where('mod_id', $mod->id)->exists();
421422

422423
if ($alreadyDownloadedMod) {
423-
return response()->noContent(201);
424+
return response()->noContent(200);
424425
}
425426

426427
$download = new ModDownload([

0 commit comments

Comments
 (0)