This commit is contained in:
User
2025-01-18 19:05:23 -05:00
parent 3f282d6ecd
commit 14938f63df
23 changed files with 652 additions and 177 deletions

View File

@@ -3,11 +3,14 @@
namespace App\Http\Controllers;
use App\Helper\ZhConversion;
use App\Jobs\ComicInsert;
use App\Jobs\ComicUpsert;
use App\Jobs\ImageUpsert;
use App\Jobs\RemotePrefetch;
use App\Models\Author;
use App\Models\Chapter;
use App\Models\Comic;
use App\Models\ReadingHistory;
use App\Remote\CopyManga;
use App\Remote\ImageFetcher;
use GuzzleHttp\Exception\GuzzleException;
@@ -19,6 +22,7 @@ use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response as IlluminateHttpResponse;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Inertia\Inertia;
use Inertia\Response;
@@ -30,12 +34,15 @@ class ComicController extends Controller
{
/**
* Autoload classes
*
* @param CopyManga $copyManga
* @param ZhConversion $zhConversion
*/
public function __construct(private readonly CopyManga $copyManga, private readonly ZhConversion $zhConversion)
{
}
public function __construct(
private readonly CopyManga $copyManga,
private readonly ZhConversion $zhConversion
) {}
/**
* Convert SC to ZH
@@ -61,10 +68,8 @@ class ComicController extends Controller
*/
public function favourites(Request $request): Response
{
$favourites = $this->scToZh($request->user()->favourites()->with(['authors'])->orderBy('upstream_updated_at', 'desc')->get());
return Inertia::render('Comic/Favourites', [
'favourites' => $favourites,
'favourites' => $this->scToZh($request->user()->favourites()->with(['authors'])->orderBy('upstream_updated_at', 'desc')->get()),
]);
}
@@ -84,16 +89,16 @@ class ComicController extends Controller
// Fetch from remote
$remoteComic = $this->copyManga->comic($request->pathword);
$comic = new Comic;
$comic->pathword = $remoteComic['comic']['path_word'];
$comic->name = $remoteComic['comic']['name'];
$comic->cover = $remoteComic['comic']['cover'];
$comic->upstream_updated_at = $remoteComic['comic']['datetime_updated'];
$comic->uuid = $remoteComic['comic']['uuid'];
$comic->alias = explode(',', $remoteComic['comic']['alias']);
$comic->description = $remoteComic['comic']['brief'];
$comic->metadata = $remoteComic;
$comic->save();
$comic = Comic::create([
'pathword' => $remoteComic['comic']['path_word'],
'name' => $remoteComic['comic']['name'],
'cover' => $remoteComic['comic']['cover'],
'upstream_updated_at' => $remoteComic['comic']['datetime_updated'],
'uuid' => $remoteComic['comic']['uuid'],
'alias' => explode(',', $remoteComic['comic']['alias']),
'description' => $remoteComic['comic']['brief'],
'metadata' => $remoteComic,
]);
}
// Set favourite
@@ -125,47 +130,6 @@ class ComicController extends Controller
]);
}
// Internal function for Upsert comics to db
protected function comicsUpsert($comics): void
{
// Prep the array for upsert
$comicsUpsertArray = [];
$authorsUpsertArray = [];
foreach ($comics['list'] as $comic) {
$comicsUpsertArray[] = [
'pathword' => $comic['path_word'],
'uuid' => '',
'name' => $comic['name'],
'alias' => '{}',
'description' => '',
'cover' => $comic['cover'],
'upstream_updated_at' => $comic['datetime_updated'] ?? null,
];
foreach ($comic['author'] as $author) {
$authorsUpsertArray[] = [
'name' => $author['name']
];
}
}
// Do an upsert for comics
Comic::upsert($comicsUpsertArray, uniqueBy: 'pathword', update: ['upstream_updated_at']);
Author::upsert($authorsUpsertArray, uniqueBy: 'name');
// Had to do a second pass to insert the relationships
foreach ($comics['list'] as $comic) {
// Get the comic id
$comicObj = Comic::where('pathword', $comic['path_word'])->first();
foreach ($comic['author'] as $author) {
$authorObj = Author::where('name', $author['name'])->first();
$comicObj->authors()->sync($authorObj);
}
}
}
/**
* Index / Tags for comic listing
*
@@ -180,12 +144,23 @@ class ComicController extends Controller
$params['theme'] = $request->get('tag');
}
$comics = $this->copyManga->comics(30, $request->header('offset', 0), $request->get('top', 'all'), $params);
$this->comicsUpsert($comics);
$offset = $request->header('offset', 0);
$comics = $this->copyManga->comics(30, $offset, $request->get('top', 'all'), $params);
// Upsert into DB
ComicUpsert::dispatch($comics);
// Prefetch next page
RemotePrefetch::dispatch('comics', [
'limit' => 30,
'offset' => $offset + 30,
'top' => $request->get('top', 'all'),
'params' => $params
])->delay(now()->addSecond());
return Inertia::render('Comic/Index', [
'comics' => $this->scToZh($comics),
'offset' => $request->header('offset', 0)
'offset' => $offset
]);
}
@@ -199,15 +174,15 @@ class ComicController extends Controller
*/
public function author(Request $request, string $author): Response
{
$params = [];
$params['author'] = $author;
$offset = $request->header('offset', 0);
$comics = $this->copyManga->comics(30, $offset, $request->get('top', 'all'), ['author' => $author]);
$comics = $this->copyManga->comics(30, $request->header('offset', 0), $request->get('top', 'all'), $params);
$this->comicsUpsert($comics);
// Upsert into DB
ComicUpsert::dispatch($comics);
return Inertia::render('Comic/Index', [
'comics' => $this->scToZh($comics),
'offset' => $request->header('offset', 0)
'offset' => $offset
]);
}
@@ -221,13 +196,14 @@ class ComicController extends Controller
*/
public function search(Request $request, string $search): Response
{
$comics = $this->copyManga->search($search, 30, $request->header('offset', 0));
$offset = $request->header('offset', 0);
$comics = $this->copyManga->search($search, 30, $offset);
// Search API is limited, no upsert
return Inertia::render('Comic/Index', [
'comics' => $this->scToZh($comics),
'offset' => $request->header('offset', 0)
'offset' => $offset
]);
}
@@ -245,8 +221,10 @@ class ComicController extends Controller
return to_route('comics.index');
}
$offset = $request->header('offset', 0);
$comic = $this->copyManga->comic($pathword);
$chapters = $this->copyManga->chapters($pathword, 200, $request->header('offset', 0), [], $request->get('group', 'default'));
$chapters = $this->copyManga->chapters($pathword, 200, $offset, [], $request->get('group', 'default'));
// Get the comic object and fill other parameters
try {
@@ -297,15 +275,20 @@ class ComicController extends Controller
// Do an upsert
Chapter::upsert($arrayForUpsert, uniqueBy: 'chapter_uuid');
// Get history
// Get user history
$histories = $request->user()->readingHistories()->where('reading_histories.comic_id', $comicObject->id)
->distinct()->select('chapter_uuid')->get()->pluck('chapter_uuid');
// Check if the first chapter is already fetched or not, if not, do a prefetch
if (!$histories->contains($chapters['list'][0]['uuid'])) {
RemotePrefetch::dispatch('chapter', ['pathword' => $pathword, 'uuid' => $chapters['list'][0]['uuid']]);
}
return Inertia::render('Comic/Chapters', [
'comic' => $this->scToZh($comic),
'chapters' => $this->scToZh($chapters),
'histories' => $histories,
'offset' => $request->header('offset', 0)
'offset' => $offset
]);
}
@@ -351,7 +334,7 @@ class ComicController extends Controller
try {
Chapter::where('chapter_uuid', $chapter['chapter']['next'])->firstOrFail()->images()->firstOrFail();
} catch (ModelNotFoundException $e) {
RemotePrefetch::dispatch('chapter', ['pathword' => $pathword, 'uuid' => $chapter['chapter']['next']]);
RemotePrefetch::dispatch('chapter', ['pathword' => $pathword, 'uuid' => $chapter['chapter']['next']]);
}
}
@@ -370,7 +353,48 @@ class ComicController extends Controller
*/
public function histories(Request $request): Response
{
// Get history
// Group by comic
if ($request->get('group', '') === 'comic') {
$readingHistory = ReadingHistory::query()
->join('comics', 'reading_histories.comic_id', '=', 'comics.id')
->join('chapters', 'reading_histories.chapter_id', '=', 'chapters.id')
->select(
'comics.id as comic_id',
'comics.name as comic_name',
'comics.pathword as comic_pathword',
'comics.upstream_updated_at as comic_upstream_updated_at',
'chapters.name as chapter_name',
'chapters.chapter_uuid as chapter_uuid',
'reading_histories.created_at as read_at'
)
->where('reading_histories.user_id', $request->user()->id)
->orderBy('comics.name')->orderByDesc('reading_histories.created_at')->paginate(2000);
// Transform the paginated data into a 2D array grouped by comic_id, including comic data and histories
$groupedData = $readingHistory->getCollection()
->groupBy('comic_id')->map(function (Collection $records, $comicId) {
$firstRecord = $records->first();
return [
'comic' => [
'comic_id' => $comicId,
'comic_name' => $firstRecord->comic_name,
'comic_pathword' => $firstRecord->comic_pathword,
'comic_upstream_updated_at' => $firstRecord->comic_upstream_updated_at,
],
'histories' => $records->map(fn($record) => [
'chapter_name' => $record->chapter_name,
'chapter_uuid' => $record->chapter_uuid,
'read_at' => $record->read_at,
])->toArray(),
];
})->values()->toArray(); // Use values() to reset numeric keys after grouping
return Inertia::render('Comic/HistoriesByComic', [
'histories' => $this->scToZh($groupedData)
]);
}
// Only order by chapter
$histories = $request->user()->readingHistories()->with(['comic:id,name,pathword'])->orderByDesc('reading_histories.created_at')
->select(['reading_histories.id as hid', 'chapters.comic_id', 'chapters.name'])->paginate(50)->toArray();
@@ -425,7 +449,7 @@ class ComicController extends Controller
*/
public function destroyHistories(Request $request): RedirectResponse
{
$result = $request->user()->cleanUpReadingHistories();
$request->user()->cleanUpReadingHistories();
return redirect()->route('comics.histories');
}