Pass anilist ids more directly, see #39
Some checks failed
timw4mail/HummingBirdAnimeClient/pipeline/head There was a failure building this commit

This commit is contained in:
Timothy Warren 2022-09-21 15:36:38 -04:00
parent 29d5b9f4ae
commit 8835aa4e69
13 changed files with 83 additions and 18 deletions

View File

@ -1,6 +1,7 @@
<article
class="media"
data-kitsu-id="<?= $item['id'] ?>"
data-anilist-id="<?= $item['anilist_id'] ?>"
data-mal-id="<?= $item['mal_id'] ?>"
>
<?php if ($auth->isAuthenticated()): ?>

View File

@ -63,6 +63,7 @@ _.on('body.anime.list', 'click', '.plus-one', (e) => {
// Setup the update data
let data = {
id: parentSel.dataset.kitsuId,
anilist_id: parentSel.dataset.anilistId,
mal_id: parentSel.dataset.malId,
data: {
progress: watchedCount + 1

View File

@ -5,6 +5,7 @@ import _ from './anime-client.js';
_.on('main', 'change', '.big-check', (e) => {
const id = e.target.id;
document.getElementById(`mal_${id}`).checked = true;
document.getElementById(`anilist_${id}`).checked = true;
});
/**
@ -55,6 +56,7 @@ export function renderSearchResults (type, data, isCollection = false) {
return `
<article class="media search ${disabled}">
<div class="name">
<input type="radio" class="mal-check" id="anilist_${item.slug}" name="anilist_id" value="${item.anilist_id}" ${disabled} />
<input type="radio" class="mal-check" id="mal_${item.slug}" name="mal_id" value="${item.mal_id}" ${disabled} />
<input type="radio" class="big-check" id="${item.slug}" name="id" value="${item.id}" ${disabled} />
<label for="${item.slug}">

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -107,13 +107,7 @@ final class Model
*/
public function createListItem(array $data, string $type = 'anime'): ?Request
{
if ($data['mal_id'] === NULL)
{
return NULL;
}
$mediaId = $this->getMediaIdFromMalId($data['mal_id'], mb_strtoupper($type));
$mediaId = $this->getMediaId($data, $type);
if ($mediaId === NULL)
{
return NULL;
@ -145,7 +139,7 @@ final class Model
public function createFullListItem(array $data, string $type): Request
{
$createData = $data['data'];
$mediaId = $this->getMediaIdFromMalId($data['mal_id'], strtoupper($type));
$mediaId = $this->getMediaId($data, $type);
if (empty($mediaId))
{
@ -230,6 +224,13 @@ final class Model
return $this->listItem->delete($id);
}
public function deleteItem(FormItem $data, string $type): ?Request
{
$mediaId = $this->getMediaId((array)$data, $type);
return $this->listItem->delete($mediaId);
}
/**
* Get the id of the specific list entry from the malId
*
@ -269,6 +270,28 @@ final class Model
return (string) $info['data']['MediaList']['id'];
}
/**
* Find the id to update by
*
* @param array $data
* @param string $type
* @return string|null
*/
private function getMediaId (array $data, string $type = 'ANIME'): ?string
{
if ($data['anilist_id'] !== NULL)
{
return $data['anilist_id'];
}
if ($data['mal_id'] !== NULL)
{
return $this->getMediaIdFromMalId($data['mal_id'], mb_strtoupper($type));
}
return NULL;
}
/**
* Get the Anilist media id from the malId
*/

View File

@ -64,4 +64,15 @@ trait MutationTrait
{
return $this->listItem->delete($id);
}
/**
* Remove a list item
*
* @param FormItem $data
* @return Request
*/
public function deleteItem(FormItem $data): Request
{
return $this->listItem->delete($data['id']);
}
}

View File

@ -49,6 +49,7 @@ final class AnimeListTransformer extends AbstractTransformer
? (int) $anime['episodeCount']
: '-';
$AnilistId = NULL;
$MALid = NULL;
$mappings = $anime['mappings']['nodes'] ?? [];
@ -59,7 +60,11 @@ final class AnimeListTransformer extends AbstractTransformer
if ($mapping['externalSite'] === 'MYANIMELIST_ANIME')
{
$MALid = $mapping['externalId'];
break;
}
if ($mapping['externalSite'] === 'ANILIST_ANIME')
{
$AnilistId = $mapping['externalId'];
}
}
}
@ -73,6 +78,7 @@ final class AnimeListTransformer extends AbstractTransformer
return AnimeListItem::from([
'id' => $item['id'],
'anilist_id' => $AnilistId,
'mal_id' => $MALid,
'episodes' => [
'watched' => (int) $item['progress'] !== 0
@ -120,6 +126,7 @@ final class AnimeListTransformer extends AbstractTransformer
$untransformed = FormItem::from([
'id' => $item['id'],
'anilist_id' => $item['anilist_id'] ?? NULL,
'mal_id' => $item['mal_id'] ?? NULL,
'data' => [
'status' => $item['watching_status'],

View File

@ -266,7 +266,7 @@ final class Anime extends BaseController
$this->checkAuth();
$body = (array) $this->request->getParsedBody();
$response = $this->model->deleteLibraryItem($body['id'], $body['mal_id']);
$response = $this->model->deleteItem(FormItem::from($body));
if ($response === TRUE)
{

View File

@ -95,7 +95,7 @@ trait MediaTrait
$requester->addRequest($kitsuRequest, 'kitsu');
if ($this->anilistEnabled && $data['mal_id'] !== NULL)
if ($this->anilistEnabled && (isset($data['anilist_id']) || isset($data['mal_id'])))
{
// If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->createListItem($data, strtoupper($this->type));
@ -121,7 +121,7 @@ trait MediaTrait
$requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->incrementListItem($data), 'kitsu');
if (( ! empty($data['mal_id'])) && $this->anilistEnabled)
if ($this->anilistEnabled && (isset($data['anilist_id']) || isset($data['mal_id'])))
{
// If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->incrementListItem($data, strtoupper($this->type));
@ -153,7 +153,7 @@ trait MediaTrait
$requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu');
if (( ! empty($data['mal_id'])) && $this->anilistEnabled)
if ($this->anilistEnabled && (isset($data['anilist_id']) || isset($data['mal_id'])))
{
// If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->updateListItem($data, strtoupper($this->type));
@ -174,6 +174,26 @@ trait MediaTrait
];
}
public function deleteItem(FormItem $data): bool
{
$requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->deleteItem($data), 'kitsu');
if ($this->anilistEnabled && (isset($data['anilist_id']) || isset($data['mal_id'])))
{
// If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->deleteItem($data, strtoupper($this->type));
if ($maybeRequest !== NULL)
{
$requester->addRequest($maybeRequest, 'anilist');
}
}
$results = $requester->makeRequests();
return $results !== [];
}
/**
* Delete a list entry
*