Skip to content

Commit

Permalink
feat: added a proper function to the token callback controller (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Aug 29, 2024
1 parent 8d4f27f commit a27a463
Show file tree
Hide file tree
Showing 23 changed files with 607 additions and 127 deletions.
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,13 @@ DISCORD_BOT_API_URL=
DISCORD_BOT_API_KEY=
DB_UPDATES_DISCORD_CHANNEL=
ADMIN_DISCORD_CHANNEL=
MAL_CLIENT_ID=null
OPENAI_BEARER_TOKEN=null
MAL_CLIENT_ID=null
MAL_CLIENT_SECRET=null
MAL_REDIRECT_URI=null
ANILIST_CLIENT_ID=null
ANILIST_CLIENT_SECRET=null
ANILIST_REDIRECT_URI=null

# session
SESSION_DRIVER=database
Expand Down Expand Up @@ -294,3 +299,4 @@ WEB_PATH=
# wiki
WIKI_LOGIN=http://localhost/login
WIKI_RESET_PASSWORD=http://localhost/reset-password
WIKI_EXTERNAL_PROFILE=http://localhost/external
8 changes: 7 additions & 1 deletion .env.example-sail
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,13 @@ DISCORD_BOT_API_URL=
DISCORD_BOT_API_KEY=
DB_UPDATES_DISCORD_CHANNEL=
ADMIN_DISCORD_CHANNEL=
MAL_CLIENT_ID=null
OPENAI_BEARER_TOKEN=null
MAL_CLIENT_ID=null
MAL_CLIENT_SECRET=null
MAL_REDIRECT_URI=null
ANILIST_CLIENT_ID=null
ANILIST_CLIENT_SECRET=null
ANILIST_REDIRECT_URI=null

# session
SESSION_DRIVER=database
Expand Down Expand Up @@ -292,3 +297,4 @@ WEB_PATH=
# wiki
WIKI_LOGIN=http://localhost/login
WIKI_RESET_PASSWORD=http://localhost/reset-password
WIKI_EXTERNAL_PROFILE=http://localhost/external
49 changes: 49 additions & 0 deletions app/Actions/Models/List/BaseStoreExternalProfileAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace App\Actions\Models\List;

use App\Enums\Models\List\ExternalProfileSite;
use App\Models\Wiki\Anime;
use App\Models\Wiki\ExternalResource;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

/**
* Class BaseStoreExternalProfileAction.
*/
abstract class BaseStoreExternalProfileAction
{
protected Collection $resources;

/**
* Preload the resources for performance proposals.
*
* @param ExternalProfileSite $profileSite
* @param array $entries
* @return void
*/
protected function preloadResources(ExternalProfileSite $profileSite, array $entries): void
{
$externalResources = ExternalResource::query()
->where(ExternalResource::ATTRIBUTE_SITE, $profileSite->getResourceSite()->value)
->whereIn(ExternalResource::ATTRIBUTE_EXTERNAL_ID, Arr::pluck($entries, 'external_id'))
->with(ExternalResource::RELATION_ANIME)
->get()
->mapWithKeys(fn (ExternalResource $resource) => [$resource->external_id => $resource->anime]);

$this->resources = $externalResources;
}

/**
* Get the animes by the external id.
*
* @param int $externalId
* @return Collection<int, Anime>
*/
protected function getAnimesByExternalId(int $externalId): Collection
{
return $this->resources[$externalId] ?? new Collection();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
abstract class BaseExternalEntryAction
{
protected ?array $response = null;

/**
* Create a new action instance.
*
Expand Down Expand Up @@ -54,6 +56,13 @@ public function getUsername(): string
return Arr::get($this->profileParameters, 'name');
}

/**
* Get the id of the external user.
*
* @return int|null
*/
abstract public function getId(): ?int;

/**
* Get the entries of the response.
*
Expand All @@ -64,7 +73,7 @@ abstract public function getEntries(): array;
/**
* Make the request to the external api.
*
* @return array|null
* @return static
*/
abstract public function makeRequest(): ?array;
abstract protected function makeRequest(): static;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,36 @@

namespace App\Actions\Models\List\ExternalProfile\ExternalEntry;

use App\Models\List\External\ExternalToken;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Illuminate\Support\Facades\Config;

/**
* Class BaseExternalEntryTokenAction
*
* This action will create the entries through the authentication method.
*/
abstract class BaseExternalEntryTokenAction
{
protected ?array $response = null;
protected ?int $id = null;

/**
* Create a new action instance.
*
* @param array $parameters
*/
public function __construct(protected array $parameters)
public function __construct(protected ExternalToken $token)
{
}

/**
* Get the username of the profile.
* Get the id of the external user.
*
* @return string
* @return int|null
*/
public function getUsername(): string
public function getId(): ?int
{
return ''; // TODO
return $this->id;
}

/**
Expand All @@ -37,7 +43,17 @@ public function getUsername(): string
*/
public function getToken(): string
{
return ''; // TODO
return $this->token->access_token;
}

/**
* Get the username.
*
* @return string|null
*/
public function getUsername(): ?string
{
return null;
}

/**
Expand All @@ -50,7 +66,7 @@ abstract public function getEntries(): array;
/**
* Make the request to the external api.
*
* @return array|null
* @return static
*/
abstract public function makeRequest(): ?array;
abstract protected function makeRequest(): static;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,55 @@ class AnilistExternalEntryAction extends BaseExternalEntryAction
public function getEntries(): array
{
$entries = [];
$response = $this->makeRequest();

if ($response !== null) {
$favorites = Arr::map(Arr::get($response, 'data.User.favourites.anime.nodes'), fn ($value) => $value['id']);
$lists = Arr::where(Arr::get($response, 'data.MediaListCollection.lists'), fn ($value) => $value['isCustomList'] === false);

foreach ($lists as $list) {
foreach (Arr::get($list, 'entries') as $entry) {
$entryId = intval(Arr::get($entry, 'media.id'));
$entries[] = [
ExternalResource::ATTRIBUTE_EXTERNAL_ID => $entryId,
ExternalEntry::ATTRIBUTE_SCORE => Arr::get($entry, 'score'),
ExternalEntry::ATTRIBUTE_WATCH_STATUS => ExternalEntryWatchStatus::getAnilistMapping(Arr::get($entry, 'status'))->value,
ExternalEntry::ATTRIBUTE_IS_FAVORITE => in_array($entryId, $favorites),
];
}

if ($this->response === null) {
$this->makeRequest();
}

$favorites = Arr::map(Arr::get($this->response, 'data.User.favourites.anime.nodes'), fn ($value) => $value['id']);
$lists = Arr::where(Arr::get($this->response, 'data.MediaListCollection.lists'), fn ($value) => $value['isCustomList'] === false);

foreach ($lists as $list) {
foreach (Arr::get($list, 'entries') as $entry) {
$entryId = intval(Arr::get($entry, 'media.id'));
$entries[] = [
ExternalResource::ATTRIBUTE_EXTERNAL_ID => $entryId,
ExternalEntry::ATTRIBUTE_SCORE => Arr::get($entry, 'score'),
ExternalEntry::ATTRIBUTE_WATCH_STATUS => ExternalEntryWatchStatus::getAnilistMapping(Arr::get($entry, 'status'))->value,
ExternalEntry::ATTRIBUTE_IS_FAVORITE => in_array($entryId, $favorites),
];
}
}

return $entries;
}

/**
* Get the id of the external user.
*
* @return int|null
*/
public function getId(): ?int
{
if ($this->response === null) {
$this->makeRequest();
}

return Arr::get($this->response, 'data.User.id');
}

/**
* Make the request to the external api.
*
* @return array|null
* @return static
*/
public function makeRequest(): ?array
protected function makeRequest(): static
{
try {
$query = '
query($userName: String) {
User(name: $userName) {
id
favourites {
anime {
nodes {
Expand Down Expand Up @@ -88,14 +104,14 @@ public function makeRequest(): ?array
'userName' => $this->getUsername(),
];

$response = Http::post('https://graphql.anilist.co', [
$this->response = Http::post('https://graphql.anilist.co', [
'query' => $query,
'variables' => $variables,
])
->throw()
->json();

return $response;
return $this;

} catch (RequestException $e) {
Log::error($e->getMessage());
Expand Down
Loading

0 comments on commit a27a463

Please sign in to comment.