Skip to content

Commit cf489fd

Browse files
committed
kobo sync read progress
1 parent 64ce165 commit cf489fd

File tree

6 files changed

+37
-18
lines changed

6 files changed

+37
-18
lines changed

komga/src/main/kotlin/org/gotson/komga/domain/service/BookLifecycle.kt

+12-9
Original file line numberDiff line numberDiff line change
@@ -459,15 +459,18 @@ class BookLifecycle(
459459
// match progression with positions
460460
val matchingPositions = extension.positions.filter { it.href == href }
461461
val matchedPosition =
462-
matchingPositions.firstOrNull { it.locations!!.progression == newProgression.locator.locations!!.progression }
463-
?: run {
464-
// no exact match
465-
val before = matchingPositions.filter { it.locations!!.progression!! < newProgression.locator.locations!!.progression!! }.maxByOrNull { it.locations!!.position!! }
466-
val after = matchingPositions.filter { it.locations!!.progression!! > newProgression.locator.locations!!.progression!! }.minByOrNull { it.locations!!.position!! }
467-
if (before == null || after == null || before.locations!!.position!! > after.locations!!.position!!)
468-
throw IllegalArgumentException("Invalid progression")
469-
before
470-
}
462+
if (extension.isFixedLayout && matchingPositions.size == 1)
463+
matchingPositions.first()
464+
else
465+
matchingPositions.firstOrNull { it.locations!!.progression == newProgression.locator.locations!!.progression }
466+
?: run {
467+
// no exact match
468+
val before = matchingPositions.filter { it.locations!!.progression!! < newProgression.locator.locations!!.progression!! }.maxByOrNull { it.locations!!.position!! }
469+
val after = matchingPositions.filter { it.locations!!.progression!! > newProgression.locator.locations!!.progression!! }.minByOrNull { it.locations!!.position!! }
470+
if (before == null || after == null || before.locations!!.position!! > after.locations!!.position!!)
471+
throw IllegalArgumentException("Invalid progression")
472+
before
473+
}
471474

472475
val totalProgression = matchedPosition.locations?.totalProgression
473476
ReadProgress(

komga/src/main/kotlin/org/gotson/komga/interfaces/api/kobo/KoboController.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import org.gotson.komga.interfaces.api.kobo.dto.ChangedEntitlementDto
3535
import org.gotson.komga.interfaces.api.kobo.dto.KoboBookMetadataDto
3636
import org.gotson.komga.interfaces.api.kobo.dto.NewEntitlementDto
3737
import org.gotson.komga.interfaces.api.kobo.dto.ReadingStateDto
38+
import org.gotson.komga.interfaces.api.kobo.dto.ReadingStateStateUpdateDto
3839
import org.gotson.komga.interfaces.api.kobo.dto.ReadingStateUpdateResultDto
3940
import org.gotson.komga.interfaces.api.kobo.dto.RequestResultDto
4041
import org.gotson.komga.interfaces.api.kobo.dto.ResourcesDto
@@ -382,17 +383,18 @@ class KoboController(
382383
fun updateState(
383384
@AuthenticationPrincipal principal: KomgaPrincipal,
384385
@PathVariable bookId: String,
385-
@RequestBody koboUpdate: ReadingStateDto,
386+
@RequestBody body: ReadingStateStateUpdateDto,
386387
@RequestHeader(name = X_KOBO_DEVICEID, required = false) koboDeviceId: String = "unknown",
387388
): ResponseEntity<*> {
388389
val book =
389390
bookRepository.findByIdOrNull(bookId)
390391
?: if (koboProxy.isEnabled())
391-
return koboProxy.proxyCurrentRequest(koboUpdate)
392+
return koboProxy.proxyCurrentRequest(body)
392393
else
393394
throw ResponseStatusException(HttpStatus.NOT_FOUND)
394395

395-
if (koboUpdate.currentBookmark.location == null) throw ResponseStatusException(HttpStatus.BAD_REQUEST)
396+
val koboUpdate = body.readingStates.firstOrNull() ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST)
397+
if (koboUpdate.currentBookmark.location == null || koboUpdate.currentBookmark.contentSourceProgressPercent == null) throw ResponseStatusException(HttpStatus.BAD_REQUEST)
396398

397399
// convert the Kobo update request to an R2Progression
398400
val r2Progression =
@@ -411,7 +413,7 @@ class KoboController(
411413
type = "application/xhtml+xml",
412414
locations =
413415
R2Locator.Location(
414-
progression = koboUpdate.currentBookmark.progressPercent,
416+
progression = koboUpdate.currentBookmark.contentSourceProgressPercent / 100,
415417
),
416418
),
417419
)
@@ -433,6 +435,7 @@ class KoboController(
433435
),
434436
)
435437
} catch (e: Exception) {
438+
logger.error(e) { "Could not update progression" }
436439
RequestResultDto(
437440
requestResult = ResultDto.FAILURE,
438441
updateResults =

komga/src/main/kotlin/org/gotson/komga/interfaces/api/kobo/dto/ReadingStateDto.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import java.time.ZonedDateTime
88

99
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy::class)
1010
data class ReadingStateDto(
11-
val created: ZonedDateTime,
11+
val created: ZonedDateTime? = null,
1212
val currentBookmark: BookmarkDto,
1313
val entitlementId: String,
1414
val lastModified: ZonedDateTime,
1515
/**
1616
* From CW: apparently always equals to lastModified
1717
*/
18-
val priorityTimestamp: ZonedDateTime,
18+
val priorityTimestamp: ZonedDateTime? = null,
1919
val statistics: StatisticsDto,
2020
val statusInfo: StatusInfoDto,
2121
)
@@ -29,8 +29,8 @@ fun ReadProgress.toDto() =
2929
currentBookmark =
3030
BookmarkDto(
3131
lastModified = this.lastModifiedDate.toUTCZoned(),
32-
progressPercent = this.locator?.locations?.totalProgression,
33-
contentSourceProgressPercent = this.locator?.locations?.progression,
32+
progressPercent = this.locator?.locations?.totalProgression?.times(100),
33+
contentSourceProgressPercent = this.locator?.locations?.progression?.times(100),
3434
location = this.locator?.let { LocationDto(source = it.href) },
3535
),
3636
statistics =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.gotson.komga.interfaces.api.kobo.dto
2+
3+
import com.fasterxml.jackson.databind.PropertyNamingStrategies
4+
import com.fasterxml.jackson.databind.annotation.JsonNaming
5+
6+
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy::class)
7+
data class ReadingStateStateUpdateDto(
8+
val readingStates: Collection<ReadingStateDto> = emptyList(),
9+
)

komga/src/main/kotlin/org/gotson/komga/interfaces/api/kobo/dto/StatusDto.kt

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming
88
enum class StatusDto {
99
@JsonProperty("ReadyToRead")
1010
READY_TO_READ,
11+
12+
@JsonProperty("Finished")
1113
FINISHED,
14+
15+
@JsonProperty("Reading")
1216
READING,
1317
}

komga/src/main/kotlin/org/gotson/komga/interfaces/api/kobo/dto/StatusInfoDto.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import java.time.ZonedDateTime
1010
data class StatusInfoDto(
1111
val lastModified: ZonedDateTime,
1212
val status: StatusDto,
13-
val timesStartedReading: Int,
13+
val timesStartedReading: Int? = null,
1414
val lastTimeFinished: ZonedDateTime? = null,
1515
val lastTimeStartedReading: ZonedDateTime? = null,
1616
)

0 commit comments

Comments
 (0)