Skip to content

Commit

Permalink
Merge branch 'master' into irfan-dahir-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
irfan-dahir authored Jun 27, 2024
2 parents 4672ae0 + 1b46890 commit 8d1e935
Show file tree
Hide file tree
Showing 31 changed files with 706 additions and 340 deletions.
2 changes: 1 addition & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ APP_DEBUG=false
APP_KEY=
APP_TIMEZONE=UTC
APP_URL=http://localhost
APP_VERSION="4.2.1"
APP_VERSION="4.2.2"

###
# Logging
Expand Down
13 changes: 9 additions & 4 deletions .github/workflows/container-image-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ jobs:
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Transform platform value
id: platform_transform
run: echo "platform=$(echo '${{ matrix.platform }}' | sed 's/\//_/g')" >> $GITHUB_OUTPUT

- name: Upload digest
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: digests
name: digests-${{ steps.platform_transform.outputs.platform }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
Expand All @@ -101,9 +105,10 @@ jobs:
- build-app-image
steps:
- name: Download digests
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: digests
pattern: digests-*
merge-multiple: true
path: /tmp/digests

- name: Set up Docker Buildx
Expand Down
49 changes: 29 additions & 20 deletions app/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Jikan\Jikan;
use Jikan\Request\Anime\AnimeRequest;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use MongoDB\Model\BSONDocument;

class Anime extends JikanApiSearchableModel
{
Expand Down Expand Up @@ -103,7 +104,6 @@ public function getYearAttribute()
|| !is_string($premiered)
|| !preg_match('~(Winter|Spring|Summer|Fall|)\s([\d+]{4})~', $premiered)
) {
Log::warning("Invalid premiered value in Anime model[$this->mal_id]: " . $premiered);
return null;
}

Expand Down Expand Up @@ -182,14 +182,11 @@ public function filterByProducer(\Laravel\Scout\Builder|\Illuminate\Database\Elo
}

$producer = (int)$value;
/** @noinspection PhpParamsInspection */
return $query->whereRaw([
'$or' => [
['producers.mal_id' => $producer],
['licensors.mal_id' => $producer],
['studios.mal_id' => $producer]
]
]);
return $query->where(function (\Jenssegers\Mongodb\Eloquent\Builder $query) use ($producer) {
return $query->where('producers.mal_id', $producer)
->orWhere('licensors.mal_id', $producer)
->orWhere('studios.mal_id', $producer);
});
}

/** @noinspection PhpUnused */
Expand All @@ -199,16 +196,24 @@ public function filterByProducers(\Laravel\Scout\Builder|\Illuminate\Database\El
return $query;
}

$producers = collect(explode(',', $value))->filter()->toArray();
$orFilters = [];
foreach ($producers as $producer) {
$producer = (int)$producer;
$orFilters[] = ['producers.mal_id' => $producer];
$orFilters[] = ['licensors.mal_id' => $producer];
$orFilters[] = ['studios.mal_id' => $producer];
}
/** @noinspection PhpParamsInspection */
return $query->whereRaw(['$or' => $orFilters]);
/* @var \Illuminate\Support\Collection $producers */
$producers = collect(explode(',', $value))->filter();

return $query->where(function (\Jenssegers\Mongodb\Eloquent\Builder $query) use ($producers) {
$firstProducer = (int)$producers->first();
$query = $query->where('producers.mal_id', $firstProducer)
->orWhere('licensors.mal_id', $firstProducer)
->orWhere('studios.mal_id', $firstProducer);

foreach ($producers->skip(1) as $producer) {
$producer = (int)$producer;
$query = $query->orWhere('producers.mal_id', $producer)
->orWhere('licensors.mal_id', $producer)
->orWhere('studios.mal_id', $producer);
}

return $query;
});
}

/** @noinspection PhpUnused */
Expand Down Expand Up @@ -403,7 +408,7 @@ public function getSearchIndexSortBy(): array|null
];
}

private function adaptBroadcastValue(array|string|null $broadcast): array
private function adaptBroadcastValue(array|string|null|BSONDocument $broadcast): array
{
$null_value = [
'day' => null,
Expand All @@ -419,6 +424,10 @@ private function adaptBroadcastValue(array|string|null $broadcast): array
return $broadcast;
}

if ($broadcast instanceof BSONDocument) {
return $broadcast->getArrayCopy();
}

if (!preg_match('~(.*) at (.*) \(~', $broadcast, $matches)) {
return [
'day' => null,
Expand Down
5 changes: 3 additions & 2 deletions app/Contracts/AnimeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ public function getCurrentlyAiring(
?AnimeScheduleFilterEnum $filter = null
): EloquentBuilder;

public function getAiredBetween(
public function getItemsBySeason(
Carbon $from,
Carbon $to,
?AnimeTypeEnum $type = null,
?string $premiered = null
?string $premiered = null,
bool $includeContinuingItems = false
): EloquentBuilder;

public function getUpcomingSeasonItems(?AnimeTypeEnum $type = null): EloquentBuilder;
Expand Down
26 changes: 26 additions & 0 deletions app/Dto/Concerns/HasContinuingParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Dto\Concerns;

use App\Casts\ContextualBooleanCast;
use OpenApi\Annotations as OA;
use Spatie\LaravelData\Attributes\Validation\BooleanType;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Optional;

/**
* @OA\Parameter(
* name="continuing",
* in="query",
* required=false,
* description="This is a flag. When supplied it will include entries which are continuing from previous seasons. MAL includes these items on the seasons view in the &#8243;TV (continuing)&#8243; section. (Example: https://myanimelist.net/anime/season/2024/winter) <br />Example usage: `?continuing`",
* @OA\Schema(type="boolean")
* ),
*/
trait HasContinuingParameter
{
use PreparesData;

#[BooleanType, WithCast(ContextualBooleanCast::class)]
public bool|Optional $continuing = false;
}
5 changes: 2 additions & 3 deletions app/Dto/Concerns/PreparesData.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public static function prepareForPipeline(Collection $properties): Collection
{
// let's always set the limit parameter to the globally configured default value
if (property_exists(static::class, "limit") && !$properties->has("limit")) {
/** @noinspection PhpUndefinedFieldInspection */
$properties->put("limit", max_results_per_page(
property_exists(static::class, "defaultLimit") ? static::$defaultLimit : null));
}
Expand All @@ -44,7 +43,7 @@ public static function prepareForPipeline(Collection $properties): Collection
}
}
// if the property is optional and the value is an empty string, we want to ignore it.
if ($property->type->isOptional && $propertyVal === "") {
if ($property->type->isOptional && $propertyVal === "" && !$property->type->acceptsType("bool")) {
$propertyVal = null;
}

Expand All @@ -53,7 +52,7 @@ public static function prepareForPipeline(Collection $properties): Collection
} else {
$properties->forget($propertyRawName);
}
}
}
}

return $properties;
Expand Down
9 changes: 8 additions & 1 deletion app/Dto/QueryAnimeSeasonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Dto\Concerns\HasPageParameter;
use App\Dto\Concerns\HasSfwParameter;
use App\Dto\Concerns\HasUnapprovedParameter;
use App\Dto\Concerns\HasContinuingParameter;
use App\Enums\AnimeTypeEnum;
use App\Rules\Attributes\EnumValidation;
use Spatie\LaravelData\Attributes\WithCast;
Expand All @@ -20,7 +21,13 @@

abstract class QueryAnimeSeasonCommand extends Data implements DataRequest
{
use HasSfwParameter, HasKidsParameter, HasUnapprovedParameter, HasLimitParameter, HasRequestFingerprint, HasPageParameter;
use HasSfwParameter,
HasKidsParameter,
HasUnapprovedParameter,
HasLimitParameter,
HasRequestFingerprint,
HasPageParameter,
HasContinuingParameter;

#[WithCast(EnumCast::class, AnimeTypeEnum::class), EnumValidation(AnimeTypeEnum::class)]
public AnimeTypeEnum|Optional $filter;
Expand Down
2 changes: 0 additions & 2 deletions app/Features/QueryAnimeSeasonHandlerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public function handle($request): JsonResponse
{
$requestParams = collect($request->all());
$type = $requestParams->has("filter") ? $request->filter : null;
$season = $requestParams->has("season") ? $request->season : null;
$year = $requestParams->has("year") ? $request->year : null;
$results = $this->getSeasonItems($request, $type);
// apply sfw, kids and unapproved filters
/** @noinspection PhpUndefinedMethodInspection */
Expand Down
3 changes: 2 additions & 1 deletion app/Features/QueryCurrentAnimeSeasonHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ protected function getSeasonItems($request, ?AnimeTypeEnum $type): Builder
*/
[$from, $to] = $this->getSeasonRange($year, $season);
$premiered = ucfirst($season)." {$year}";
$includeContinuingItems = $request->continuing;

return $this->repository->getAiredBetween($from, $to, $type, $premiered);
return $this->repository->getItemsBySeason($from, $to, $type, $premiered, $includeContinuingItems);
}
}
10 changes: 6 additions & 4 deletions app/Features/QueryRandomAnimeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Contracts\RequestHandler;
use App\Dto\QueryRandomAnimeCommand;
use App\Http\Resources\V4\AnimeResource;
use Spatie\LaravelData\Optional;

/**
* @implements RequestHandler<QueryRandomAnimeCommand, AnimeResource>
Expand All @@ -18,12 +19,13 @@ final class QueryRandomAnimeHandler implements RequestHandler
public function handle($request): AnimeResource
{
$queryable = Anime::query();
// apply sfw, kids and unapproved filters
/** @noinspection PhpUndefinedMethodInspection */
$queryable = $queryable->filter(collect($request->all()));

$o = Optional::create();
$sfwParam = $request->sfw === $o ? false : $request->sfw;
$unapprovedParam = $request->unapproved === $o ? false : $request->unapproved;

return new AnimeResource(
$queryable->random()->first()
$queryable->random(1, $sfwParam, $unapprovedParam)->first()
);
}

Expand Down
10 changes: 6 additions & 4 deletions app/Features/QueryRandomMangaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Dto\QueryRandomMangaCommand;
use App\Http\Resources\V4\MangaResource;
use App\Manga;
use Spatie\LaravelData\Optional;

/**
* @implements RequestHandler<QueryRandomMangaCommand, MangaResource>
Expand All @@ -18,12 +19,13 @@ final class QueryRandomMangaHandler implements RequestHandler
public function handle($request)
{
$queryable = Manga::query();
// apply sfw, kids and unapproved filters
/** @noinspection PhpUndefinedMethodInspection */
$queryable = $queryable->filter(collect($request->all()));

$o = Optional::create();
$sfwParam = $request->sfw === $o ? false : $request->sfw;
$unapprovedParam = $request->unapproved === $o ? false : $request->unapproved;

return new MangaResource(
$queryable->random()->first()
$queryable->random(1, $sfwParam, $unapprovedParam)->first()
);
}

Expand Down
4 changes: 2 additions & 2 deletions app/Features/QuerySpecificAnimeSeasonHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ protected function getSeasonItems($request, ?AnimeTypeEnum $type): Builder

[$from, $to] = $this->getSeasonRange($request->year, $request->season);
$premiered = ucfirst($request->season)." {$request->year}";
$includeContinuingItems = $request->continuing;

return $this->repository->getAiredBetween($from, $to, $type, $premiered);
// ->where("status", "!=", AnimeStatusEnum::upcoming()->label);
return $this->repository->getItemsBySeason($from, $to, $type, $premiered, $includeContinuingItems);
}
}
3 changes: 0 additions & 3 deletions app/Http/Controllers/V4DB/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class ScheduleController extends Controller
* operationId="getSchedules",
* tags={"schedules"},
*
* @OA\Parameter(ref="#/components/parameters/page"),
*
* @OA\Parameter(
* name="filter",
* in="query",
Expand All @@ -39,7 +37,6 @@ class ScheduleController extends Controller
* @OA\Schema(type="string",enum={"true", "false"})
* ),
*
* @OA\Parameter(ref="#/components/parameters/sfw"),
* @OA\Parameter(ref="#/components/parameters/unapproved"),
* @OA\Parameter(ref="#/components/parameters/page"),
* @OA\Parameter(ref="#/components/parameters/limit"),
Expand Down
2 changes: 0 additions & 2 deletions app/Http/Controllers/V4DB/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class SearchController extends Controller
* operationId="getAnimeSearch",
* tags={"anime"},
*
* @OA\Parameter(ref="#/components/parameters/sfw"),
* @OA\Parameter(ref="#/components/parameters/unapproved"),
* @OA\Parameter(ref="#/components/parameters/page"),
* @OA\Parameter(ref="#/components/parameters/limit"),
Expand Down Expand Up @@ -159,7 +158,6 @@ public function anime(AnimeSearchCommand $request)
* operationId="getMangaSearch",
* tags={"manga"},
*
* @OA\Parameter(ref="#/components/parameters/sfw"),
* @OA\Parameter(ref="#/components/parameters/unapproved"),
* @OA\Parameter(ref="#/components/parameters/page"),
* @OA\Parameter(ref="#/components/parameters/limit"),
Expand Down
14 changes: 4 additions & 10 deletions app/Http/Controllers/V4DB/SeasonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@

namespace App\Http\Controllers\V4DB;

use App\Anime;
use App\Dto\QueryAnimeSeasonListCommand;
use App\Dto\QueryCurrentAnimeSeasonCommand;
use App\Dto\QuerySpecificAnimeSeasonCommand;
use App\Dto\QueryUpcomingAnimeSeasonCommand;
use App\Http\HttpResponse;
use App\Http\QueryBuilder\AnimeSearchQueryBuilder;
use App\Http\Resources\V4\AnimeCollection;
use App\Http\Resources\V4\ResultsResource;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Jikan\Request\SeasonList\SeasonListRequest;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use OpenApi\Annotations as OA;

/**
*
Expand All @@ -38,6 +29,7 @@ class SeasonController extends Controller
*
* @OA\Parameter(ref="#/components/parameters/sfw"),
* @OA\Parameter(ref="#/components/parameters/unapproved"),
* @OA\Parameter(ref="#/components/parameters/continuing"),
* @OA\Parameter(ref="#/components/parameters/page"),
* @OA\Parameter(ref="#/components/parameters/limit"),
*
Expand Down Expand Up @@ -89,6 +81,7 @@ public function now(QueryCurrentAnimeSeasonCommand $command)
*
* @OA\Parameter(ref="#/components/parameters/sfw"),
* @OA\Parameter(ref="#/components/parameters/unapproved"),
* @OA\Parameter(ref="#/components/parameters/continuing"),
* @OA\Parameter(ref="#/components/parameters/page"),
* @OA\Parameter(ref="#/components/parameters/limit"),
*
Expand Down Expand Up @@ -177,6 +170,7 @@ public function archive(QueryAnimeSeasonListCommand $command)
*
* @OA\Parameter(ref="#/components/parameters/sfw"),
* @OA\Parameter(ref="#/components/parameters/unapproved"),
* @OA\Parameter(ref="#/components/parameters/continuing"),
* @OA\Parameter(ref="#/components/parameters/page"),
* @OA\Parameter(ref="#/components/parameters/limit"),
*
Expand Down
Loading

0 comments on commit 8d1e935

Please sign in to comment.