Skip to content

Commit

Permalink
Update bookmark when reading online
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusa87 committed Aug 17, 2024
1 parent 3dc8aaa commit 7c86f5c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Controller/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,18 @@ public function relocate(Book $book, BookFileSystemManager $fileSystemManager, E
private function updateProgression(Request $request, Book $book, User $user): JsonResponse
{
try {
/** @var array{percent?:string|float, cfi?:string} $json */
/** @var array{percent?:string|float, cpi?:string} $json */
$json = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new BadRequestException('Invalid percent in json', 0, $e);
}
$percent = $json['percent'] ?? null;
$cpi = $json['cpi'] ?? null;
$percent = $percent === null ? null : floatval((string) $percent);

if ($percent !== null && $percent <= 1.0 && $percent >= 0) {
$this->bookProgressionService->setProgression($book, $user, $percent)
->updateBookMarkFromProgressionAndCpi($book, $user, $percent, $cpi)
->flush();

return new JsonResponse([
Expand Down
28 changes: 28 additions & 0 deletions src/Service/BookProgressionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Entity\Book;
use App\Entity\BookInteraction;
use App\Entity\BookmarkUser;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Kiwilan\Ebook\Ebook;
Expand All @@ -18,6 +19,33 @@ public function __construct(
) {
}

public function updateBookMarkFromProgressionAndCpi(Book $book, User $user, float $progress, ?string $cpi, ?float $sourcePercent = null): self
{
if ($cpi === null) {
return $this;
}

$nbPages = $this->processPageNumber($book);
if ($nbPages === null || $nbPages === 0) {
return $this;
}

$bookMark = $user->getBookmarkForBook($book);
if (!$bookMark instanceof BookmarkUser) {
$bookMark = new BookmarkUser($book, $user);
$this->em->persist($bookMark);
$user->addBookmarkUser($bookMark);
}

$bookMark->setPercent($progress);
$bookMark->setSourcePercent($sourcePercent);
$bookMark->setLocationValue('kobo.1.1');
$bookMark->setLocationType('KoboSpan');
$bookMark->setLocationSource($cpi);

return $this;
}

/**
* @return float|null Progression between 0 and 1 or null if the total number of pages of a book is unknown
*/
Expand Down

0 comments on commit 7c86f5c

Please sign in to comment.