-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added sync external profile job (#748)
- Loading branch information
Showing
5 changed files
with
203 additions
and
32 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
app/Actions/Models/List/ExternalProfile/SyncExternalProfileAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Actions\Models\List\ExternalProfile; | ||
|
||
use App\Actions\Models\List\BaseStoreExternalProfileAction; | ||
use App\Actions\Models\List\ExternalProfile\ExternalEntry\BaseExternalEntryAction; | ||
use App\Actions\Models\List\ExternalProfile\ExternalEntry\BaseExternalEntryTokenAction; | ||
use App\Actions\Models\List\ExternalProfile\ExternalEntry\Site\AnilistExternalEntryAction; | ||
use App\Actions\Models\List\ExternalProfile\ExternalEntry\Site\AnilistExternalEntryTokenAction; | ||
use App\Enums\Models\List\ExternalProfileSite; | ||
use App\Models\List\External\ExternalEntry; | ||
use App\Models\List\ExternalProfile; | ||
use Exception; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
/** | ||
* Class SyncExternalProfileAction. | ||
*/ | ||
class SyncExternalProfileAction extends BaseStoreExternalProfileAction | ||
{ | ||
/** | ||
* Sync the profile. | ||
* | ||
* @param ExternalProfile $profile | ||
* @return ExternalProfile|null | ||
*/ | ||
public function handle(ExternalProfile $profile): ?ExternalProfile | ||
{ | ||
try { | ||
DB::beginTransaction(); | ||
|
||
$action = $profile->isClaimed() | ||
? $this->getClaimedActionClass($profile) | ||
: $this->getUnclaimedActionClass($profile); | ||
|
||
$entries = $action->getEntries(); | ||
|
||
$this->preloadResources($profile->site, $entries); | ||
|
||
$externalEntries = []; | ||
foreach ($entries as $entry) { | ||
$externalId = Arr::get($entry, 'external_id'); | ||
|
||
foreach ($this->getAnimesByExternalId($externalId) as $anime) { | ||
$externalEntries[] = [ | ||
ExternalEntry::ATTRIBUTE_SCORE => Arr::get($entry, ExternalEntry::ATTRIBUTE_SCORE), | ||
ExternalEntry::ATTRIBUTE_IS_FAVORITE => Arr::get($entry, ExternalEntry::ATTRIBUTE_IS_FAVORITE), | ||
ExternalEntry::ATTRIBUTE_WATCH_STATUS => Arr::get($entry, ExternalEntry::ATTRIBUTE_WATCH_STATUS), | ||
ExternalEntry::ATTRIBUTE_ANIME => $anime->getKey(), | ||
ExternalEntry::ATTRIBUTE_PROFILE => $profile->getKey(), | ||
]; | ||
} | ||
} | ||
|
||
// Delete the old entries before creating new ones. | ||
$profile->externalentries->each(function (ExternalEntry $entry) { | ||
ExternalEntry::withoutEvents(function () use ($entry) { | ||
$entry->delete(); | ||
}); | ||
}); | ||
|
||
ExternalEntry::insert($externalEntries); | ||
|
||
return $profile; | ||
} catch (Exception $e) { | ||
Log::error($e->getMessage()); | ||
|
||
DB::rollBack(); | ||
|
||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Get the mapping for the entries token class. | ||
* | ||
* @param ExternalProfile $profile | ||
* @return BaseExternalEntryTokenAction|null | ||
*/ | ||
protected function getClaimedActionClass(ExternalProfile $profile): ?BaseExternalEntryTokenAction | ||
{ | ||
return match ($profile->site) { | ||
ExternalProfileSite::ANILIST => new AnilistExternalEntryTokenAction($profile->externaltoken), | ||
default => null, | ||
}; | ||
} | ||
|
||
/** | ||
* Get the mapping for the entries class. | ||
* | ||
* @param ExternalProfile $profile | ||
* @return BaseExternalEntryAction|null | ||
*/ | ||
protected function getUnclaimedActionClass(ExternalProfile $profile): ?BaseExternalEntryAction | ||
{ | ||
return match ($profile->site) { | ||
ExternalProfileSite::ANILIST => new AnilistExternalEntryAction($profile->toArray()), | ||
default => null, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Jobs\List; | ||
|
||
use App\Actions\Models\List\ExternalProfile\SyncExternalProfileAction; | ||
use App\Features\AllowExternalProfileManagement; | ||
use App\Jobs\Middleware\RateLimited; | ||
use App\Models\List\ExternalProfile; | ||
use DateTime; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Laravel\Pennant\Feature; | ||
|
||
/** | ||
* Class SyncExternalProfileJob. | ||
*/ | ||
class SyncExternalProfileJob implements ShouldQueue | ||
{ | ||
use Dispatchable; | ||
use InteractsWithQueue; | ||
use Queueable; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param ExternalProfile $profile | ||
* @return void | ||
*/ | ||
public function __construct(protected readonly ExternalProfile $profile) | ||
{ | ||
$this->onQueue('sync-external-profile'); | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle(): void | ||
{ | ||
if (Feature::for(null)->active(AllowExternalProfileManagement::class)) { | ||
$action = new SyncExternalProfileAction(); | ||
|
||
$action->handle($this->profile); | ||
} | ||
} | ||
|
||
/** | ||
* Get the middleware the job should pass through. | ||
* | ||
* @return array | ||
*/ | ||
public function middleware(): array | ||
{ | ||
return [new RateLimited()]; | ||
} | ||
|
||
/** | ||
* Determine the time at which the job should time out. | ||
* | ||
* @return DateTime | ||
*/ | ||
public function retryUntil(): DateTime | ||
{ | ||
return now()->addMinutes(15); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.