Skip to content

Commit

Permalink
feat: added feature flag to enable only localhost middleware (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch committed Jul 3, 2024
1 parent 9238fd3 commit 50e0bab
Show file tree
Hide file tree
Showing 32 changed files with 556 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function store(Builder $builder, array $profileParameters): Model
$entryParameters,
[
ExternalEntry::ATTRIBUTE_ANIME => $anime->getKey(),
ExternalEntry::ATTRIBUTE_EXTERNAL_PROFILE => $profile->getKey(),
ExternalEntry::ATTRIBUTE_PROFILE => $profile->getKey(),
]
));
}
Expand Down
2 changes: 2 additions & 0 deletions app/Constants/FeatureConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class FeatureConstants

final public const AUDIO_BITRATE_RESTRICTION = 'audio_bitrate_restriction';

final public const ENABLED_ONLY_ON_LOCALHOST = 'enabled_only_on_localhost';

final public const IGNORE_ALL_FILE_VALIDATIONS = 'ignore_all_file_validations';

final public const NULL_SCOPE = '__laravel_null';
Expand Down
1 change: 1 addition & 0 deletions app/Enums/Models/List/AnimeWatchStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ enum AnimeWatchStatus: int
case PAUSED = 2;
case DROPPED = 3;
case PLAN_TO_WATCH = 4;
case REWATCHING = 5;
}
12 changes: 12 additions & 0 deletions app/Events/List/Playlist/PlaylistUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public function __construct(Playlist $playlist)
$this->initializeEmbedFields($playlist);
}

/**
* Determine if the message should be sent.
*
* @return bool
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public function shouldSendDiscordMessage(): bool
{
return false;
}

/**
* Get the model that has fired this event.
*
Expand Down
10 changes: 5 additions & 5 deletions app/Filament/Resources/List/External/ExternalEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static function form(Form $form): Form
{
return $form
->schema([
Select::make(ExternalEntryModel::ATTRIBUTE_EXTERNAL_PROFILE)
Select::make(ExternalEntryModel::ATTRIBUTE_PROFILE)
->label(__('filament.resources.singularLabel.external_profile'))
->useScout(ExternalProfileModel::class)
->required()
Expand Down Expand Up @@ -171,9 +171,9 @@ public static function table(Table $table): Table
{
return parent::table($table)
->columns([
TextColumn::make(ExternalEntryModel::ATTRIBUTE_EXTERNAL_PROFILE)
TextColumn::make(ExternalEntryModel::ATTRIBUTE_PROFILE)
->label(__('filament.resources.singularLabel.external_profile'))
->urlToRelated(ExternalProfileResource::class, ExternalEntryModel::RELATION_EXTERNAL_PROFILE, true)
->urlToRelated(ExternalProfileResource::class, ExternalEntryModel::RELATION_PROFILE, true)
->sortable()
->toggleable(),

Expand Down Expand Up @@ -221,9 +221,9 @@ public static function infolist(Infolist $infolist): Infolist
->schema([
Section::make(static::getRecordTitle($infolist->getRecord()))
->schema([
TextEntry::make(ExternalEntryModel::ATTRIBUTE_EXTERNAL_PROFILE)
TextEntry::make(ExternalEntryModel::ATTRIBUTE_PROFILE)
->label(__('filament.resources.singularLabel.external_profile'))
->urlToRelated(ExternalProfileResource::class, ExternalEntryModel::RELATION_EXTERNAL_PROFILE, true),
->urlToRelated(ExternalProfileResource::class, ExternalEntryModel::RELATION_PROFILE, true),

TextEntry::make(ExternalEntryModel::ATTRIBUTE_ANIME)
->label(__('filament.resources.singularLabel.anime'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function table(Table $table): Table
{
return parent::table(
$table
->inverseRelationship(ExternalEntry::RELATION_EXTERNAL_PROFILE)
->inverseRelationship(ExternalEntry::RELATION_PROFILE)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(Schema $schema)
*/
public function shouldSelect(Query $query, Schema $schema): bool
{
// Needed to match playlist relation.
// Needed to match profile relation.
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ExternalEntryExternalProfileIdField extends Field implements SelectableFie
*/
public function __construct(Schema $schema)
{
parent::__construct($schema, ExternalEntry::ATTRIBUTE_EXTERNAL_PROFILE);
parent::__construct($schema, ExternalEntry::ATTRIBUTE_PROFILE);
}

/**
Expand All @@ -34,7 +34,7 @@ public function __construct(Schema $schema)
*/
public function shouldSelect(Query $query, Schema $schema): bool
{
// Needed to match playlist relation.
// Needed to match profile relation.
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(Schema $schema)
parent::__construct($schema, ExternalEntry::ATTRIBUTE_ID);
}

/**
/**
* Determine if the field should be included in the select clause of our query.
*
* @param Query $query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct(Schema $schema)
public function getCreationRules(Request $request): array
{
return [
'sometimes',
'required',
'boolean',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function __construct(Schema $schema)
public function getCreationRules(Request $request): array
{
return [
'sometimes',
'required',
'decimal:0,2',
];
Expand All @@ -51,6 +52,7 @@ public function getUpdateRules(Request $request): array
{
return [
'sometimes',
'required',
'decimal:0,2',
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function getUpdateRules(Request $request): array
{
return [
'sometimes',
'required',
new Enum(AnimeWatchStatus::class),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function __construct(Schema $schema)
public function getCreationRules(Request $request): array
{
return [
'sometimes',
'required',
new Enum(ExternalProfileVisibility::class),
];
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Api/Field/List/Playlist/Track/TrackEntryIdField.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ public function shouldSelect(Query $query, Schema $schema): bool
*/
public function getUpdateRules(Request $request): array
{
$videoId = $this->resolveVideoId($request);

return [
'sometimes',
'required',
'integer',
Rule::exists(AnimeThemeEntry::class, AnimeThemeEntry::ATTRIBUTE_ID),
Rule::exists(AnimeThemeEntryVideo::class, AnimeThemeEntryVideo::ATTRIBUTE_ENTRY)
->where(AnimeThemeEntryVideo::ATTRIBUTE_VIDEO, $this->resolveVideoId($request)),
];
}

Expand Down
9 changes: 9 additions & 0 deletions app/Http/Api/Field/List/Playlist/Track/TrackVideoIdField.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,20 @@ public function shouldSelect(Query $query, Schema $schema): bool
*/
public function getUpdateRules(Request $request): array
{
$entryId = $this->resolveEntryId($request);

return [
'sometimes',
'required',
'integer',
Rule::exists(Video::class, Video::ATTRIBUTE_ID),
Rule::when(
! empty($entryId),
[
Rule::exists(AnimeThemeEntryVideo::class, AnimeThemeEntryVideo::ATTRIBUTE_VIDEO)
->where(AnimeThemeEntryVideo::ATTRIBUTE_ENTRY, $this->resolveEntryId($request)),
]
),
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Api/Schema/List/External/ExternalEntrySchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function allowedIncludes(): array
{
return [
new AllowedInclude(new AnimeSchema(), ExternalEntry::RELATION_ANIME),
new AllowedInclude(new ExternalProfileSchema(), ExternalEntry::RELATION_EXTERNAL_PROFILE),
new AllowedInclude(new ExternalProfileSchema(), ExternalEntry::RELATION_PROFILE),
new AllowedInclude(new UserSchema(), ExternalEntry::RELATION_USER),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function index(IndexRequest $request, ExternalProfile $externalprofile, I
{
$query = new Query($request->validated());

$builder = ExternalEntry::query()->where(ExternalEntry::ATTRIBUTE_EXTERNAL_PROFILE, $externalprofile->getKey());
$builder = ExternalEntry::query()->where(ExternalEntry::ATTRIBUTE_PROFILE, $externalprofile->getKey());

$resources = $action->index($builder, $query, $request->schema());

Expand All @@ -81,7 +81,7 @@ public function store(StoreRequest $request, ExternalProfile $externalprofile, S
{
$validated = array_merge(
$request->validated(),
[ExternalEntry::ATTRIBUTE_EXTERNAL_PROFILE => $externalprofile->getKey()]
[ExternalEntry::ATTRIBUTE_PROFILE => $externalprofile->getKey()]
);

$externalentry = $action->store(ExternalEntry::query(), $validated);
Expand Down
10 changes: 7 additions & 3 deletions app/Http/Middleware/Api/EnabledOnlyOnLocalhost.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace App\Http\Middleware\Api;

use App\Constants\FeatureConstants;
use Closure;
use Illuminate\Http\Request;
use Laravel\Pennant\Feature;

/**
* Class EnabledOnlyOnLocalhost.
Expand All @@ -21,10 +23,12 @@ class EnabledOnlyOnLocalhost
*/
public function handle(Request $request, Closure $next): mixed
{
$ip = $request->ip();
if (Feature::for(null)->active(FeatureConstants::ENABLED_ONLY_ON_LOCALHOST)) {
$ip = $request->ip();

if ($ip !== '127.0.0.1') {
abort(403, "Route only available for localhost");
if ($ip !== '127.0.0.1') {
abort(403, "Route only available for localhost");
}
}

return $next($request);
Expand Down
16 changes: 8 additions & 8 deletions app/Models/List/External/ExternalEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
* Class ExternalEntry.
*
* @property int $entry_id
* @property int $anime_id
* @property Anime $anime
* @property int $external_profile_id
* @property int|null $anime_id
* @property Anime|null $anime
* @property int $profile_id
* @property ExternalProfile $externalprofile
* @property bool $is_favorite
* @property float|null $score
* @property AnimeWatchStatus|null $watch_status
* @property AnimeWatchStatus $watch_status
*
* @method static ExternalEntryFactory factory(...$parameters)
*/
Expand All @@ -38,13 +38,13 @@ class ExternalEntry extends BaseModel
final public const ATTRIBUTE_ID = 'entry_id';
final public const ATTRIBUTE_ANIME = 'anime_id';
final public const ATTRIBUTE_EXTERNAL_PROFILE = 'external_profile_id';
final public const ATTRIBUTE_PROFILE = 'profile_id';
final public const ATTRIBUTE_IS_FAVORITE = 'is_favorite';
final public const ATTRIBUTE_SCORE = 'score';
final public const ATTRIBUTE_WATCH_STATUS = 'watch_status';

final public const RELATION_ANIME = 'anime';
final public const RELATION_EXTERNAL_PROFILE = 'externalprofile';
final public const RELATION_PROFILE = 'externalprofile';
final public const RELATION_USER = 'externalprofile.user';

/**
Expand All @@ -54,7 +54,7 @@ class ExternalEntry extends BaseModel
*/
protected $fillable = [
ExternalEntry::ATTRIBUTE_ANIME,
ExternalEntry::ATTRIBUTE_EXTERNAL_PROFILE,
ExternalEntry::ATTRIBUTE_PROFILE,
ExternalEntry::ATTRIBUTE_IS_FAVORITE,
ExternalEntry::ATTRIBUTE_SCORE,
ExternalEntry::ATTRIBUTE_WATCH_STATUS,
Expand Down Expand Up @@ -147,6 +147,6 @@ public function anime(): BelongsTo
*/
public function externalprofile(): BelongsTo
{
return $this->belongsTo(ExternalProfile::class, ExternalEntry::ATTRIBUTE_EXTERNAL_PROFILE);
return $this->belongsTo(ExternalProfile::class, ExternalEntry::ATTRIBUTE_PROFILE);
}
}
2 changes: 1 addition & 1 deletion app/Models/List/ExternalProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function shouldBeSearchable(): bool
*/
public function externalentries(): HasMany
{
return $this->hasMany(ExternalEntry::class, ExternalEntry::ATTRIBUTE_EXTERNAL_PROFILE);
return $this->hasMany(ExternalEntry::class, ExternalEntry::ATTRIBUTE_PROFILE);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Nova/Resources/List/External/ExternalEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function fields(NovaRequest $request): array
->withSubtitles()
->showOnPreview(),

BelongsTo::make(__('nova.resources.singularLabel.externalprofiles'), ExternalEntryModel::RELATION_EXTERNAL_PROFILE, ExternalProfile::class)
BelongsTo::make(__('nova.resources.singularLabel.externalprofiles'), ExternalEntryModel::RELATION_PROFILE, ExternalProfile::class)
->sortable()
->filterable()
->searchable()
Expand Down
6 changes: 5 additions & 1 deletion database/factories/List/External/ExternalEntryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Database\Factories\List\External;

use App\Enums\Models\List\AnimeWatchStatus;
use App\Models\List\External\ExternalEntry;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Arr;

/**
* Class ExternalEntryFactory.
Expand All @@ -31,8 +33,10 @@ class ExternalEntryFactory extends Factory
*/
public function definition(): array
{
$watchStatus = Arr::random(AnimeWatchStatus::cases());

return [
//
ExternalEntry::ATTRIBUTE_WATCH_STATUS => $watchStatus->value,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function up(): void
$table->softDeletes(BaseModel::ATTRIBUTE_DELETED_AT, 6);
$table->id(ExternalProfile::ATTRIBUTE_ID);
$table->string(ExternalProfile::ATTRIBUTE_NAME);
$table->integer(ExternalProfile::ATTRIBUTE_SITE)->nullable();
$table->integer(ExternalProfile::ATTRIBUTE_SITE);
$table->integer(ExternalProfile::ATTRIBUTE_VISIBILITY)->default(ExternalProfileVisibility::PRIVATE->value);

$table->unsignedBigInteger(ExternalProfile::ATTRIBUTE_USER)->nullable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public function up(): void
$table->softDeletes(BaseModel::ATTRIBUTE_DELETED_AT, 6);
$table->id(ExternalEntry::ATTRIBUTE_ID);
$table->float(ExternalEntry::ATTRIBUTE_SCORE)->nullable();
$table->integer(ExternalEntry::ATTRIBUTE_WATCH_STATUS)->nullable();
$table->integer(ExternalEntry::ATTRIBUTE_WATCH_STATUS);
$table->boolean(ExternalEntry::ATTRIBUTE_IS_FAVORITE)->default(false);

$table->unsignedBigInteger(ExternalEntry::ATTRIBUTE_ANIME)->nullable();
$table->foreign(ExternalEntry::ATTRIBUTE_ANIME)->references(Anime::ATTRIBUTE_ID)->on(Anime::TABLE)->nullOnDelete();

$table->unsignedBigInteger(ExternalEntry::ATTRIBUTE_EXTERNAL_PROFILE)->nullable();
$table->foreign(ExternalEntry::ATTRIBUTE_EXTERNAL_PROFILE)->references(ExternalProfile::ATTRIBUTE_ID)->on(ExternalProfile::TABLE)->cascadeOnDelete();
$table->unsignedBigInteger(ExternalEntry::ATTRIBUTE_PROFILE);
$table->foreign(ExternalEntry::ATTRIBUTE_PROFILE)->references(ExternalProfile::ATTRIBUTE_ID)->on(ExternalProfile::TABLE)->cascadeOnDelete();
});
}
}
Expand Down
13 changes: 7 additions & 6 deletions database/seeders/Admin/Feature/FeatureSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ public function run(): void
Feature::deactivate(FeatureConstants::ALLOW_DISCORD_NOTIFICATIONS);
Feature::deactivate(FeatureConstants::ALLOW_VIEW_RECORDING);
Feature::activate(FeatureConstants::AUDIO_BITRATE_RESTRICTION);
Feature::activate(FeatureConstants::ENABLED_ONLY_ON_LOCALHOST);
Feature::deactivate(FeatureConstants::IGNORE_ALL_FILE_VALIDATIONS);
Feature::deactivate(FeatureConstants::REQUIRED_ENCODER_VERSION);
Feature::activate(FeatureConstants::REQUIRED_ENCODER_VERSION, 'Lavf59.27.100');
Feature::activate(FeatureConstants::VIDEO_BITRATE_RESTRICTION);
Feature::deactivate(FeatureConstants::VIDEO_CODEC_STREAM);
Feature::deactivate(FeatureConstants::VIDEO_COLOR_PRIMARIES_STREAM);
Feature::deactivate(FeatureConstants::VIDEO_COLOR_SPACE_STREAM);
Feature::deactivate(FeatureConstants::VIDEO_COLOR_TRANSFER_STREAM);
Feature::deactivate(FeatureConstants::VIDEO_PIXEL_FORMAT_STREAM);
Feature::activate(FeatureConstants::VIDEO_CODEC_STREAM, 'vp9');
Feature::activate(FeatureConstants::VIDEO_COLOR_PRIMARIES_STREAM, 'bt709,smpte170m,bt470bg');
Feature::activate(FeatureConstants::VIDEO_COLOR_SPACE_STREAM, 'bt709,smpte170m,bt470bg');
Feature::activate(FeatureConstants::VIDEO_COLOR_TRANSFER_STREAM, 'bt709,smpte170m,bt470bg');
Feature::activate(FeatureConstants::VIDEO_PIXEL_FORMAT_STREAM, 'yuv420p');
}
}
1 change: 1 addition & 0 deletions lang/en/enums.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
AnimeWatchStatus::PAUSED->name => 'Paused',
AnimeWatchStatus::DROPPED->name => 'Dropped',
AnimeWatchStatus::PLAN_TO_WATCH->name => 'Plan to Watch',
AnimeWatchStatus::REWATCHING->name => 'Rewatching',
],
ExternalProfileSite::class => [
ExternalProfileSite::MAL->name => 'MyAnimeList',
Expand Down
Loading

0 comments on commit 50e0bab

Please sign in to comment.