mirror of
https://github.com/sp-tarkov/forge.git
synced 2025-02-13 04:30:41 -05:00
Refringe
7e1c66f250
Reviewed the download count PR work and made some changes: - Updated the download link route to include the mod's slug for easier identification. - Moved rate limiter from the route middleware (the entire controller) to just the show method in the controller. - Created a ModVersionPolicy that the controller can check against. - Moves download increment logic into the model. - Defers the call to the download increment logic (now run in the background) - Updated the route to have a name, and the downloadUrl methods to build the URL dynamically using the route name. - Wrote some tests to check URL building, download counting, and rate limiting. # Conflicts: # app/Http/Controllers/ModVersionController.php # app/Providers/AppServiceProvider.php
156 lines
4.9 KiB
PHP
156 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Exceptions\InvalidVersionNumberException;
|
|
use App\Models\Scopes\DisabledScope;
|
|
use App\Models\Scopes\PublishedScope;
|
|
use App\Support\Version;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class ModVersion extends Model
|
|
{
|
|
use HasFactory;
|
|
use SoftDeletes;
|
|
|
|
/**
|
|
* Update the parent mod's updated_at timestamp when the mod version is updated.
|
|
*/
|
|
protected $touches = ['mod'];
|
|
|
|
/**
|
|
* Post boot method to configure the model.
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::addGlobalScope(new DisabledScope);
|
|
|
|
static::addGlobalScope(new PublishedScope);
|
|
|
|
static::saving(function (ModVersion $model) {
|
|
// Extract the version sections from the version string.
|
|
try {
|
|
$version = new Version($model->version);
|
|
|
|
$model->version_major = $version->getMajor();
|
|
$model->version_minor = $version->getMinor();
|
|
$model->version_patch = $version->getPatch();
|
|
$model->version_pre_release = $version->getPreRelease();
|
|
} catch (InvalidVersionNumberException $e) {
|
|
$model->version_major = 0;
|
|
$model->version_minor = 0;
|
|
$model->version_patch = 0;
|
|
$model->version_pre_release = '';
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* The relationship between a mod version and mod.
|
|
*
|
|
* @return BelongsTo<Mod, ModVersion>
|
|
*/
|
|
public function mod(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Mod::class);
|
|
}
|
|
|
|
/**
|
|
* The relationship between a mod version and its dependencies.
|
|
*
|
|
* @return HasMany<ModDependency>
|
|
*/
|
|
public function dependencies(): HasMany
|
|
{
|
|
return $this->hasMany(ModDependency::class)
|
|
->chaperone();
|
|
}
|
|
|
|
/**
|
|
* The relationship between a mod version and its resolved dependencies.
|
|
*
|
|
* @return BelongsToMany<ModVersion>
|
|
*/
|
|
public function resolvedDependencies(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(ModVersion::class, 'mod_resolved_dependencies', 'mod_version_id', 'resolved_mod_version_id')
|
|
->withPivot('dependency_id')
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* The relationship between a mod version and its each of it's resolved dependencies' latest versions.
|
|
*
|
|
* @return BelongsToMany<ModVersion>
|
|
*/
|
|
public function latestResolvedDependencies(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(ModVersion::class, 'mod_resolved_dependencies', 'mod_version_id', 'resolved_mod_version_id')
|
|
->withPivot('dependency_id')
|
|
->join('mod_versions as latest_versions', function ($join) {
|
|
$join->on('latest_versions.id', '=', 'mod_versions.id')
|
|
->whereRaw('latest_versions.version = (SELECT MAX(mv.version) FROM mod_versions mv WHERE mv.mod_id = mod_versions.mod_id)');
|
|
})
|
|
->with('mod:id,name,slug')
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* The relationship between a mod version and its latest SPT version.
|
|
*
|
|
* @return HasOneThrough<SptVersion>
|
|
*/
|
|
public function latestSptVersion(): HasOneThrough
|
|
{
|
|
return $this->hasOneThrough(SptVersion::class, ModVersionSptVersion::class, 'mod_version_id', 'id', 'id', 'spt_version_id')
|
|
->orderByDesc('spt_versions.version_major')
|
|
->orderByDesc('spt_versions.version_minor')
|
|
->orderByDesc('spt_versions.version_patch')
|
|
->orderByDesc('spt_versions.version_pre_release')
|
|
->limit(1);
|
|
}
|
|
|
|
/**
|
|
* The relationship between a mod version and its SPT versions.
|
|
*
|
|
* @return BelongsToMany<SptVersion>
|
|
*/
|
|
public function sptVersions(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(SptVersion::class)
|
|
->using(ModVersionSptVersion::class)
|
|
->orderByDesc('version_major')
|
|
->orderByDesc('version_minor')
|
|
->orderByDesc('version_patch')
|
|
->orderByDesc('version_pre_release');
|
|
}
|
|
|
|
/**
|
|
* Build the download URL for this mod version.
|
|
*/
|
|
public function downloadUrl(bool $absolute = false): string
|
|
{
|
|
return route('mod.version.download', [$this->mod->id, $this->mod->slug, $this->version], absolute: $absolute);
|
|
}
|
|
|
|
/**
|
|
* Increment the download count for this mod version.
|
|
*/
|
|
public function incrementDownloads(): int
|
|
{
|
|
$this->downloads++;
|
|
$this->save();
|
|
|
|
// Recalculate the total download count for this mod.
|
|
$this->mod->calculateDownloads();
|
|
|
|
return $this->downloads;
|
|
}
|
|
}
|