2024-05-15 00:31:24 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-08-08 16:11:50 -04:00
|
|
|
use App\Http\Filters\V1\QueryFilter;
|
2024-05-31 17:44:52 -04:00
|
|
|
use App\Models\Scopes\DisabledScope;
|
2024-07-26 09:35:09 -04:00
|
|
|
use App\Models\Scopes\PublishedScope;
|
2024-09-12 13:19:52 -04:00
|
|
|
use Database\Factories\ModFactory;
|
2024-08-08 16:11:50 -04:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2024-07-04 22:10:48 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2024-07-15 23:13:51 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2024-09-08 22:06:52 -04:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2024-06-23 08:34:22 -04:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Support\Str;
|
2024-06-02 22:03:59 -04:00
|
|
|
use Laravel\Scout\Searchable;
|
2024-05-15 00:31:24 -04:00
|
|
|
|
|
|
|
class Mod extends Model
|
|
|
|
{
|
2024-09-12 13:19:52 -04:00
|
|
|
/** @use HasFactory<ModFactory> */
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
use Searchable;
|
|
|
|
use SoftDeletes;
|
2024-05-15 00:31:24 -04:00
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* Post boot method to configure the model.
|
|
|
|
*/
|
2024-05-31 17:44:52 -04:00
|
|
|
protected static function booted(): void
|
|
|
|
{
|
|
|
|
static::addGlobalScope(new DisabledScope);
|
2024-07-26 09:35:09 -04:00
|
|
|
static::addGlobalScope(new PublishedScope);
|
2024-05-31 17:44:52 -04:00
|
|
|
}
|
|
|
|
|
2024-08-31 01:19:22 -04:00
|
|
|
/**
|
|
|
|
* Calculate the total number of downloads for the mod.
|
|
|
|
*/
|
|
|
|
public function calculateDownloads(): void
|
|
|
|
{
|
|
|
|
$this->downloads = $this->versions->sum('downloads');
|
|
|
|
$this->saveQuietly();
|
|
|
|
}
|
|
|
|
|
2024-07-04 22:10:48 -04:00
|
|
|
/**
|
2024-07-20 19:52:36 -04:00
|
|
|
* The relationship between a mod and its users.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
|
|
|
* @return BelongsToMany<User>
|
2024-07-04 22:10:48 -04:00
|
|
|
*/
|
|
|
|
public function users(): BelongsToMany
|
2024-05-15 00:31:24 -04:00
|
|
|
{
|
2024-07-04 22:10:48 -04:00
|
|
|
return $this->belongsToMany(User::class);
|
2024-05-15 00:31:24 -04:00
|
|
|
}
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* The relationship between a mod and its license.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
|
|
|
* @return BelongsTo<License, Mod>
|
2024-07-20 19:52:36 -04:00
|
|
|
*/
|
2024-05-15 00:31:24 -04:00
|
|
|
public function license(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(License::class);
|
|
|
|
}
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
2024-09-15 23:05:38 -04:00
|
|
|
* The relationship between a mod and its last updated version.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
2024-09-15 23:05:38 -04:00
|
|
|
* @return HasOne<ModVersion>
|
2024-07-20 19:52:36 -04:00
|
|
|
*/
|
2024-09-15 23:05:38 -04:00
|
|
|
public function latestUpdatedVersion(): HasOne
|
2024-05-15 00:31:24 -04:00
|
|
|
{
|
2024-09-15 23:05:38 -04:00
|
|
|
return $this->versions()
|
|
|
|
->one()
|
|
|
|
->ofMany('updated_at', 'max')
|
2024-09-11 15:09:27 -04:00
|
|
|
->chaperone();
|
2024-05-15 00:31:24 -04:00
|
|
|
}
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
2024-09-15 23:05:38 -04:00
|
|
|
* The relationship between a mod and its versions.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
2024-09-15 23:05:38 -04:00
|
|
|
* @return HasMany<ModVersion>
|
2024-07-20 19:52:36 -04:00
|
|
|
*/
|
2024-09-15 23:05:38 -04:00
|
|
|
public function versions(): HasMany
|
2024-05-17 23:54:03 -04:00
|
|
|
{
|
2024-09-15 23:05:38 -04:00
|
|
|
return $this->hasMany(ModVersion::class)
|
|
|
|
->orderByDesc('version_major')
|
|
|
|
->orderByDesc('version_minor')
|
|
|
|
->orderByDesc('version_patch')
|
|
|
|
->orderByDesc('version_pre_release')
|
2024-09-11 15:09:27 -04:00
|
|
|
->chaperone();
|
2024-05-15 00:31:24 -04:00
|
|
|
}
|
2024-06-02 22:03:59 -04:00
|
|
|
|
2024-06-23 08:34:22 -04:00
|
|
|
/**
|
2024-07-20 19:52:36 -04:00
|
|
|
* The data that is searchable by Scout.
|
2024-06-23 08:34:22 -04:00
|
|
|
*/
|
2024-06-02 22:03:59 -04:00
|
|
|
public function toSearchableArray(): array
|
|
|
|
{
|
|
|
|
return [
|
2024-08-29 15:46:10 -04:00
|
|
|
'id' => $this->id,
|
2024-06-02 22:03:59 -04:00
|
|
|
'name' => $this->name,
|
|
|
|
'slug' => $this->slug,
|
|
|
|
'description' => $this->description,
|
|
|
|
'thumbnail' => $this->thumbnail,
|
|
|
|
'featured' => $this->featured,
|
|
|
|
'created_at' => strtotime($this->created_at),
|
|
|
|
'updated_at' => strtotime($this->updated_at),
|
2024-07-26 09:34:57 -04:00
|
|
|
'published_at' => strtotime($this->published_at),
|
2024-09-15 23:05:38 -04:00
|
|
|
'latestVersion' => $this->latestVersion->latestSptVersion->version_formatted,
|
|
|
|
'latestVersionColorClass' => $this->latestVersion->latestSptVersion->color_class,
|
2024-06-02 22:03:59 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-06-23 08:34:22 -04:00
|
|
|
/**
|
2024-07-20 19:52:36 -04:00
|
|
|
* Determine if the model instance should be searchable.
|
2024-06-23 08:34:22 -04:00
|
|
|
*/
|
2024-06-02 22:03:59 -04:00
|
|
|
public function shouldBeSearchable(): bool
|
|
|
|
{
|
2024-08-29 15:46:10 -04:00
|
|
|
// Ensure the mod is not disabled.
|
|
|
|
if ($this->disabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the mod has a publish date.
|
|
|
|
if (is_null($this->published_at)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the mod has a latest version.
|
2024-09-15 23:05:38 -04:00
|
|
|
if ($this->latestVersion()->doesntExist()) {
|
2024-08-29 15:46:10 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the latest version has a latest SPT version.
|
2024-09-15 23:05:38 -04:00
|
|
|
if ($this->latestVersion->latestSptVersion()->doesntExist()) {
|
2024-08-29 15:46:10 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-09-08 22:06:52 -04:00
|
|
|
// Ensure the latest SPT version is within the last three minor versions.
|
|
|
|
$activeSptVersions = Cache::remember('active-spt-versions', 60 * 60, function () {
|
|
|
|
return SptVersion::getVersionsForLastThreeMinors();
|
|
|
|
});
|
2024-09-15 23:05:38 -04:00
|
|
|
if (! in_array($this->latestVersion->latestSptVersion->version, $activeSptVersions->pluck('version')->toArray())) {
|
2024-09-08 22:06:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-29 15:46:10 -04:00
|
|
|
// All conditions are met; the mod should be searchable.
|
|
|
|
return true;
|
2024-06-02 22:03:59 -04:00
|
|
|
}
|
2024-06-23 08:34:22 -04:00
|
|
|
|
2024-09-15 23:05:38 -04:00
|
|
|
/**
|
|
|
|
* The relationship between a mod and its latest version.
|
|
|
|
*
|
|
|
|
* @return HasOne<ModVersion>
|
|
|
|
*/
|
|
|
|
public function latestVersion(string $sort = 'version'): HasOne
|
|
|
|
{
|
|
|
|
return $this->versions()
|
|
|
|
->one()
|
|
|
|
->ofMany([
|
|
|
|
'version_major' => 'max',
|
|
|
|
'version_minor' => 'max',
|
|
|
|
'version_patch' => 'max',
|
|
|
|
'version_pre_release' => 'max',
|
|
|
|
])
|
|
|
|
->chaperone();
|
|
|
|
}
|
|
|
|
|
2024-06-23 08:34:22 -04:00
|
|
|
/**
|
2024-07-20 19:52:36 -04:00
|
|
|
* Build the URL to the mod's thumbnail.
|
2024-06-23 08:34:22 -04:00
|
|
|
*/
|
|
|
|
public function thumbnailUrl(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::get(function (): string {
|
|
|
|
return $this->thumbnail
|
|
|
|
? Storage::disk($this->thumbnailDisk())->url($this->thumbnail)
|
|
|
|
: '';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-07-20 19:52:36 -04:00
|
|
|
* Get the disk where the thumbnail is stored based on the current environment.
|
2024-06-23 08:34:22 -04:00
|
|
|
*/
|
|
|
|
protected function thumbnailDisk(): string
|
|
|
|
{
|
|
|
|
return match (config('app.env')) {
|
|
|
|
'production' => 'r2', // Cloudflare R2 Storage
|
|
|
|
default => 'public', // Local
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-08-08 16:11:50 -04:00
|
|
|
/**
|
|
|
|
* Scope a query by applying QueryFilter filters.
|
|
|
|
*/
|
|
|
|
public function scopeFilter(Builder $builder, QueryFilter $filters): Builder
|
|
|
|
{
|
|
|
|
return $filters->apply($builder);
|
|
|
|
}
|
|
|
|
|
2024-08-09 12:35:46 -04:00
|
|
|
/**
|
|
|
|
* Build the URL to the mod's detail page.
|
|
|
|
*/
|
|
|
|
public function detailUrl(): string
|
|
|
|
{
|
|
|
|
return route('mod.show', [$this->id, $this->slug]);
|
|
|
|
}
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* The attributes that should be cast to native types.
|
|
|
|
*/
|
2024-07-04 22:10:48 -04:00
|
|
|
protected function casts(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'featured' => 'boolean',
|
|
|
|
'contains_ai_content' => 'boolean',
|
|
|
|
'contains_ads' => 'boolean',
|
|
|
|
'disabled' => 'boolean',
|
2024-09-13 00:08:00 -04:00
|
|
|
'created_at' => 'datetime',
|
|
|
|
'updated_at' => 'datetime',
|
|
|
|
'deleted_at' => 'datetime',
|
2024-07-04 22:10:48 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-06-23 08:34:22 -04:00
|
|
|
/**
|
2024-07-20 19:52:36 -04:00
|
|
|
* Mutate the slug attribute to always be lower case on get and slugified on set.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
|
|
|
* @return Attribute<string, string>
|
2024-06-23 08:34:22 -04:00
|
|
|
*/
|
|
|
|
protected function slug(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
get: fn (string $value) => Str::lower($value),
|
|
|
|
set: fn (string $value) => Str::slug($value),
|
|
|
|
);
|
|
|
|
}
|
2024-05-15 00:31:24 -04:00
|
|
|
}
|