some progress

This commit is contained in:
Doar Mihai
2026-03-07 13:29:50 +02:00
parent bf5c92c239
commit 72c2cd9485
15 changed files with 460 additions and 42 deletions
@@ -58,7 +58,12 @@ class FetchItemDataCommand extends Command
{
switch ($type){
case ItemToFetch::TYPE_MOVIE:
return MovieProcessingService::getBatch($batch, $job);
$items = MovieProcessingService::getBatch($batch, $job);
if ($items->count() == 0){
$items = MovieProcessingService::getMissingMoviesBatch($batch, $job);
}
return $items;
case ItemToFetch::TYPE_TV_SERIES:
return ShowProcessingService::getBatch($batch, $job, $tmdbId);
}
@@ -52,7 +52,7 @@ class FetchExportItemsCommand extends Command
if (null === $decodedLine) {
continue;
}
echo '.';
$this->processItem($decodedLine, $type);
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class CheckPermission
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
* @param string ...$permissions
*/
public function handle(Request $request, Closure $next, ...$permissions): Response
{
if (!$request->user()) {
abort(403, 'Unauthorized action.');
}
foreach ($permissions as $permission) {
if ($request->user()->hasPermission($permission)) {
return $next($request);
}
}
abort(403, 'Unauthorized action.');
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class CheckRole
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
* @param string ...$roles
*/
public function handle(Request $request, Closure $next, ...$roles): Response
{
if (!$request->user()) {
abort(403, 'Unauthorized action.');
}
foreach ($roles as $role) {
if ($request->user()->hasRole($role)) {
return $next($request);
}
}
abort(403, 'Unauthorized action.');
}
}
+3 -3
View File
@@ -71,9 +71,9 @@ class Movie extends Model
public function director()
{
return $this->belongsToMany(Person::class, 'movie_cast', 'movie_id', 'person_id')
->where('movie_cast.job', 'Director')
->orWhere('movie_cast.known_for_department', 'Directing')
return $this->belongsToMany(Person::class, 'movie_crew', 'movie_id', 'person_id')
->where('movie_crew.job', 'Director')
->orWhere('movie_crew.known_for_department', 'Directing')
;
}
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Permission extends Model
{
protected $fillable = [
'name',
'slug',
'description',
];
public function roles()
{
return $this->belongsToMany(Role::class, 'permission_role');
}
public function users()
{
return $this->belongsToMany(User::class, 'permission_user');
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $fillable = [
'name',
'slug',
'description',
];
public function users()
{
return $this->belongsToMany(User::class, 'role_user');
}
public function permissions()
{
return $this->belongsToMany(Permission::class, 'permission_role');
}
public function hasPermission($permission)
{
if (is_string($permission)) {
return $this->permissions->contains('slug', $permission);
}
return $this->permissions->contains('id', $permission->id);
}
public function givePermissionTo($permission)
{
return $this->permissions()->save($permission);
}
}
+65
View File
@@ -52,4 +52,69 @@ class User extends Authenticatable
{
return $this->belongsToMany(MediaList::class, 'user_media_lists', 'user_id', 'media_list_id');
}
public function roles()
{
return $this->belongsToMany(Role::class, 'role_user');
}
public function permissions()
{
return $this->belongsToMany(Permission::class, 'permission_user');
}
public function hasRole($role)
{
if (is_string($role)) {
return $this->roles->contains('slug', $role);
}
return $this->roles->contains('id', $role->id);
}
public function hasPermission($permission)
{
if (is_string($permission)) {
// Check direct permissions
if ($this->permissions->contains('slug', $permission)) {
return true;
}
// Check role permissions
foreach ($this->roles as $role) {
if ($role->hasPermission($permission)) {
return true;
}
}
return false;
}
return $this->permissions->contains('id', $permission->id);
}
public function hasAnyRole($roles)
{
if (is_string($roles)) {
return $this->roles->contains('slug', $roles);
}
foreach ($roles as $role) {
if ($this->hasRole($role)) {
return true;
}
}
return false;
}
public function assignRole($role)
{
return $this->roles()->save($role);
}
public function givePermissionTo($permission)
{
return $this->permissions()->save($permission);
}
}
@@ -58,6 +58,20 @@ class MovieProcessingService implements ProcessingInterface
->get();
}
public static function getMissingMoviesBatch(int $batch, string $job)
{
return ItemToFetchJobs::select('item_to_fetch_jobs.*')
->leftJoin('movies', function ($join) {
$join->on('movies.tmdb_id', '=', 'item_to_fetch_jobs.tmdb_id');
})
->whereRaw("FIND_IN_SET('".$job."', item_to_fetch_jobs.jobs)")
->where('item_to_fetch_jobs.type', 'movie')
->whereNull('movies.id')
->whereNotNull('item_to_fetch_jobs.tmdb_id')
->take($batch)
->get();
}
public function process($item, $job)
{
$methodName = "get".ucfirst($job);
@@ -66,7 +80,7 @@ class MovieProcessingService implements ProcessingInterface
// @todo maybe raise an exception here
return;
}
echo "Processing {$methodName} ".$item->tmdb_id." (".$item->id.") \n";
echo "Processing {$methodName} ".$item->tmdb_id." (".$item->tmdb_id.") \n";
$this->$methodName($item);
// maybe we should sleep for a while so we do not spam the tmdb api
@@ -78,7 +92,7 @@ class MovieProcessingService implements ProcessingInterface
$tmdbMovie = $this->moviesApi->getMovie($item->tmdb_id);
if (isset($tmdbMovie['status_code']) && $tmdbMovie['status_code'] == 34) {
echo "[ApiError-Could not be found]\n]";
echo "[ApiError-Could not be found]\n";
return;
}
@@ -204,12 +218,7 @@ class MovieProcessingService implements ProcessingInterface
$imageExists = Image::create($image);
}
if ($movie->whereHas('images', function ($query) use ($imageExists) {
$query->where('type', $imageExists->type)
->where('file_path', $imageExists->file_path);
})->exists()) {
return;
} else {
if (!$movie->images()->where('image_id', $imageExists->id)->exists()) {
$movie->images()->attach($imageExists);
}
}
@@ -238,11 +247,7 @@ class MovieProcessingService implements ProcessingInterface
]);
}
if ($movie->whereHas('keywords', function ($query) use ($existingKeyword) {
$query->where('tmdb_id', $existingKeyword->tmdb_id);
})->exists()) {
return;
} else {
if (!$movie->keywords()->where('keyword_id', $existingKeyword->id)->exists()) {
$movie->keywords()->attach($existingKeyword);
}
}
@@ -276,11 +281,7 @@ class MovieProcessingService implements ProcessingInterface
]);
}
if ($movie->whereHas('releaseDates', function ($query) use ($movie) {
$query->where('movie_id', $movie->id);
})->exists()) {
return;
} else {
if (!$movie->releaseDates()->where('release_date_id', $existingReleaseDate->id)->exists()) {
$movie->releaseDates()->attach($existingReleaseDate);
}
}
@@ -452,9 +453,7 @@ class MovieProcessingService implements ProcessingInterface
]);
}
if (!$movie->whereHas('watchProviders', function ($query) use ($movie) {
$query->where('movie_id', $movie->id);
})->exists()) {
if (!$movie->watchProviders()->where('watch_provider_id', $existingWatchProvider->id)->where('locale', $locale)->exists()) {
$movie->watchProviders()->attach($existingWatchProvider, [
'locale' => $locale,
'tmdb_url' => $tmdbWatchProvider['link'],
@@ -494,11 +493,7 @@ class MovieProcessingService implements ProcessingInterface
]);
}
if ($movie->whereHas('videos', function ($query) use ($movie) {
$query->where('movie_id', $movie->id);
})->exists()) {
return;
} else {
if (!$movie->videos()->where('video_id', $videoExists->id)->exists()) {
$movie->videos()->attach($videoExists);
}
}
@@ -718,8 +713,6 @@ class MovieProcessingService implements ProcessingInterface
private function processMovieCollection(int $movieId, $belongsToCollection)
{
dd($belongsToCollection);
$movieCollection = MovieCollection::where('tmdb_id', $belongsToCollection['id'])->first();
if (!$movieCollection) {
@@ -795,6 +788,7 @@ class MovieProcessingService implements ProcessingInterface
if (empty($remainingJobs)) {
$itemToFetchJobs->delete();
return;
}
// $itemToFetchJobs->remaining_jobs = $remainingJobs;
@@ -833,7 +827,7 @@ class MovieProcessingService implements ProcessingInterface
$personData = array_merge($personData, [
'biography' => $tmdbPerson['biography'],
'birtday' => $tmdbPerson['birthday'],
'birthday' => $tmdbPerson['birthday'],
'deathday' => $tmdbPerson['deathday'],
'homepage' => $tmdbPerson['homepage'],
'imdb_id' => $tmdbPerson['imdb_id'],
@@ -883,7 +877,7 @@ class MovieProcessingService implements ProcessingInterface
if (!empty($tmdbPerson)) {
$personData = array_merge($personData, [
'biography' => $tmdbPerson['biography'],
'birtday' => $tmdbPerson['birthday'],
'birthday' => $tmdbPerson['birthday'],
'deathday' => $tmdbPerson['deathday'],
'homepage' => $tmdbPerson['homepage'],
'imdb_id' => $tmdbPerson['imdb_id'],