Pass anilist ids more directly, see #39

This commit is contained in:
Timothy Warren 2022-09-21 15:36:38 -04:00
parent ec280cd76d
commit 2139f031f4
13 changed files with 83 additions and 18 deletions

View File

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

View File

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

View File

@ -5,6 +5,7 @@ import _ from './anime-client.js';
_.on('main', 'change', '.big-check', (e) => { _.on('main', 'change', '.big-check', (e) => {
const id = e.target.id; const id = e.target.id;
document.getElementById(`mal_${id}`).checked = true; document.getElementById(`mal_${id}`).checked = true;
document.getElementById(`anilist_${id}`).checked = true;
}); });
/** /**
@ -55,6 +56,7 @@ export function renderSearchResults (type, data, isCollection = false) {
return ` return `
<article class="media search ${disabled}"> <article class="media search ${disabled}">
<div class="name"> <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="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} /> <input type="radio" class="big-check" id="${item.slug}" name="id" value="${item.id}" ${disabled} />
<label for="${item.slug}"> <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 public function createListItem(array $data, string $type = 'anime'): ?Request
{ {
if ($data['mal_id'] === NULL) $mediaId = $this->getMediaId($data, $type);
{
return NULL;
}
$mediaId = $this->getMediaIdFromMalId($data['mal_id'], mb_strtoupper($type));
if ($mediaId === NULL) if ($mediaId === NULL)
{ {
return NULL; return NULL;
@ -145,7 +139,7 @@ final class Model
public function createFullListItem(array $data, string $type): Request public function createFullListItem(array $data, string $type): Request
{ {
$createData = $data['data']; $createData = $data['data'];
$mediaId = $this->getMediaIdFromMalId($data['mal_id'], strtoupper($type)); $mediaId = $this->getMediaId($data, $type);
if (empty($mediaId)) if (empty($mediaId))
{ {
@ -230,6 +224,13 @@ final class Model
return $this->listItem->delete($id); 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 * Get the id of the specific list entry from the malId
* *
@ -269,6 +270,28 @@ final class Model
return (string) $info['data']['MediaList']['id']; 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 * Get the Anilist media id from the malId
*/ */

View File

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

View File

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

View File

@ -95,7 +95,7 @@ trait MediaTrait
$requester->addRequest($kitsuRequest, 'kitsu'); $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 // If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->createListItem($data, strtoupper($this->type)); $maybeRequest = $this->anilistModel->createListItem($data, strtoupper($this->type));
@ -121,7 +121,7 @@ trait MediaTrait
$requester = new ParallelAPIRequest(); $requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->incrementListItem($data), 'kitsu'); $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 // If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->incrementListItem($data, strtoupper($this->type)); $maybeRequest = $this->anilistModel->incrementListItem($data, strtoupper($this->type));
@ -153,7 +153,7 @@ trait MediaTrait
$requester = new ParallelAPIRequest(); $requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu'); $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 // If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->updateListItem($data, strtoupper($this->type)); $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 * Delete a list entry
* *