Skip to content

Commit

Permalink
kobo sync read progress
Browse files Browse the repository at this point in the history
  • Loading branch information
gotson committed Aug 23, 2024
1 parent 64ce165 commit cf489fd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,18 @@ class BookLifecycle(
// match progression with positions
val matchingPositions = extension.positions.filter { it.href == href }
val matchedPosition =
matchingPositions.firstOrNull { it.locations!!.progression == newProgression.locator.locations!!.progression }
?: run {
// no exact match
val before = matchingPositions.filter { it.locations!!.progression!! < newProgression.locator.locations!!.progression!! }.maxByOrNull { it.locations!!.position!! }
val after = matchingPositions.filter { it.locations!!.progression!! > newProgression.locator.locations!!.progression!! }.minByOrNull { it.locations!!.position!! }
if (before == null || after == null || before.locations!!.position!! > after.locations!!.position!!)
throw IllegalArgumentException("Invalid progression")
before
}
if (extension.isFixedLayout && matchingPositions.size == 1)
matchingPositions.first()
else
matchingPositions.firstOrNull { it.locations!!.progression == newProgression.locator.locations!!.progression }
?: run {
// no exact match
val before = matchingPositions.filter { it.locations!!.progression!! < newProgression.locator.locations!!.progression!! }.maxByOrNull { it.locations!!.position!! }
val after = matchingPositions.filter { it.locations!!.progression!! > newProgression.locator.locations!!.progression!! }.minByOrNull { it.locations!!.position!! }
if (before == null || after == null || before.locations!!.position!! > after.locations!!.position!!)
throw IllegalArgumentException("Invalid progression")
before
}

val totalProgression = matchedPosition.locations?.totalProgression
ReadProgress(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.gotson.komga.interfaces.api.kobo.dto.ChangedEntitlementDto
import org.gotson.komga.interfaces.api.kobo.dto.KoboBookMetadataDto
import org.gotson.komga.interfaces.api.kobo.dto.NewEntitlementDto
import org.gotson.komga.interfaces.api.kobo.dto.ReadingStateDto
import org.gotson.komga.interfaces.api.kobo.dto.ReadingStateStateUpdateDto
import org.gotson.komga.interfaces.api.kobo.dto.ReadingStateUpdateResultDto
import org.gotson.komga.interfaces.api.kobo.dto.RequestResultDto
import org.gotson.komga.interfaces.api.kobo.dto.ResourcesDto
Expand Down Expand Up @@ -382,17 +383,18 @@ class KoboController(
fun updateState(
@AuthenticationPrincipal principal: KomgaPrincipal,
@PathVariable bookId: String,
@RequestBody koboUpdate: ReadingStateDto,
@RequestBody body: ReadingStateStateUpdateDto,
@RequestHeader(name = X_KOBO_DEVICEID, required = false) koboDeviceId: String = "unknown",
): ResponseEntity<*> {
val book =
bookRepository.findByIdOrNull(bookId)
?: if (koboProxy.isEnabled())
return koboProxy.proxyCurrentRequest(koboUpdate)
return koboProxy.proxyCurrentRequest(body)
else
throw ResponseStatusException(HttpStatus.NOT_FOUND)

if (koboUpdate.currentBookmark.location == null) throw ResponseStatusException(HttpStatus.BAD_REQUEST)
val koboUpdate = body.readingStates.firstOrNull() ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST)
if (koboUpdate.currentBookmark.location == null || koboUpdate.currentBookmark.contentSourceProgressPercent == null) throw ResponseStatusException(HttpStatus.BAD_REQUEST)

// convert the Kobo update request to an R2Progression
val r2Progression =
Expand All @@ -411,7 +413,7 @@ class KoboController(
type = "application/xhtml+xml",
locations =
R2Locator.Location(
progression = koboUpdate.currentBookmark.progressPercent,
progression = koboUpdate.currentBookmark.contentSourceProgressPercent / 100,
),
),
)
Expand All @@ -433,6 +435,7 @@ class KoboController(
),
)
} catch (e: Exception) {
logger.error(e) { "Could not update progression" }
RequestResultDto(
requestResult = ResultDto.FAILURE,
updateResults =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import java.time.ZonedDateTime

@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy::class)
data class ReadingStateDto(
val created: ZonedDateTime,
val created: ZonedDateTime? = null,
val currentBookmark: BookmarkDto,
val entitlementId: String,
val lastModified: ZonedDateTime,
/**
* From CW: apparently always equals to lastModified
*/
val priorityTimestamp: ZonedDateTime,
val priorityTimestamp: ZonedDateTime? = null,
val statistics: StatisticsDto,
val statusInfo: StatusInfoDto,
)
Expand All @@ -29,8 +29,8 @@ fun ReadProgress.toDto() =
currentBookmark =
BookmarkDto(
lastModified = this.lastModifiedDate.toUTCZoned(),
progressPercent = this.locator?.locations?.totalProgression,
contentSourceProgressPercent = this.locator?.locations?.progression,
progressPercent = this.locator?.locations?.totalProgression?.times(100),
contentSourceProgressPercent = this.locator?.locations?.progression?.times(100),
location = this.locator?.let { LocationDto(source = it.href) },
),
statistics =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.gotson.komga.interfaces.api.kobo.dto

import com.fasterxml.jackson.databind.PropertyNamingStrategies
import com.fasterxml.jackson.databind.annotation.JsonNaming

@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy::class)
data class ReadingStateStateUpdateDto(
val readingStates: Collection<ReadingStateDto> = emptyList(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming
enum class StatusDto {
@JsonProperty("ReadyToRead")
READY_TO_READ,

@JsonProperty("Finished")
FINISHED,

@JsonProperty("Reading")
READING,
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.time.ZonedDateTime
data class StatusInfoDto(
val lastModified: ZonedDateTime,
val status: StatusDto,
val timesStartedReading: Int,
val timesStartedReading: Int? = null,
val lastTimeFinished: ZonedDateTime? = null,
val lastTimeStartedReading: ZonedDateTime? = null,
)

0 comments on commit cf489fd

Please sign in to comment.