@@ -3,6 +3,7 @@ package org.gotson.komga.interfaces.api.kobo
3
3
import com.fasterxml.jackson.databind.JsonNode
4
4
import com.fasterxml.jackson.databind.ObjectMapper
5
5
import com.fasterxml.jackson.databind.node.ObjectNode
6
+ import com.fasterxml.jackson.module.kotlin.treeToValue
6
7
import io.github.oshai.kotlinlogging.KotlinLogging
7
8
import org.apache.commons.lang3.RandomStringUtils
8
9
import org.gotson.komga.domain.model.KomgaSyncToken
@@ -175,20 +176,20 @@ class KoboController(
175
176
@AuthenticationPrincipal principal : KomgaPrincipal ,
176
177
@PathVariable authToken : String ,
177
178
): ResponseEntity <Collection <SyncResultDto >> {
178
- val syncToken = komgaSyncTokenGenerator.fromRequestHeaders(getCurrentRequest()) ? : KomgaSyncToken ()
179
+ val syncTokenReceived = komgaSyncTokenGenerator.fromRequestHeaders(getCurrentRequest()) ? : KomgaSyncToken ()
179
180
180
181
// find the ongoing sync point, else create one
181
182
val toSyncPoint =
182
- getSyncPointVerified(syncToken .ongoingSyncPointId, principal.user.id)
183
- ? : syncPointLifecycle.createSyncPoint(principal.user, null ) // TODO: for now we sync all libraries
183
+ getSyncPointVerified(syncTokenReceived .ongoingSyncPointId, principal.user.id)
184
+ ? : syncPointLifecycle.createSyncPoint(principal.user, null ) // for now we sync all libraries
184
185
185
186
// find the last successful sync, if any
186
- val fromSyncPoint = getSyncPointVerified(syncToken .lastSuccessfulSyncPointId, principal.user.id)
187
+ val fromSyncPoint = getSyncPointVerified(syncTokenReceived .lastSuccessfulSyncPointId, principal.user.id)
187
188
188
189
logger.debug { " Library sync from SyncPoint $fromSyncPoint , to SyncPoint: $toSyncPoint " }
189
190
190
191
var shouldContinueSync: Boolean
191
- val syncResult : Collection <SyncResultDto > =
192
+ val syncResultKomga : Collection <SyncResultDto > =
192
193
if (fromSyncPoint != null ) {
193
194
// find books added/changed/removed and map to DTO
194
195
var maxRemainingCount = komgaProperties.kobo.syncItemLimit
@@ -272,27 +273,42 @@ class KoboController(
272
273
}
273
274
}
274
275
275
- // update synctoken to send back to Kobo
276
+ // merge Kobo store sync response
277
+ val (syncResultMerged, syncTokenMerged, shouldContinueSyncMerged) =
278
+ if (koboProxy.isEnabled()) {
279
+ try {
280
+ val koboStoreResponse = koboProxy.proxyCurrentRequest(includeSyncToken = true )
281
+ val syncResultsKobo = koboStoreResponse.body?.let { mapper.treeToValue<Collection <SyncResultDto >>(it) } ? : emptyList()
282
+ val syncTokenKobo = koboStoreResponse.headers[X_KOBO_SYNCTOKEN ]?.firstOrNull()?.let { komgaSyncTokenGenerator.fromBase64(it) }
283
+ val shouldContinueSyncKobo = koboStoreResponse.headers[X_KOBO_SYNC ]?.firstOrNull()?.lowercase() == " continue"
284
+
285
+ Triple (syncResultKomga + syncResultsKobo, syncTokenKobo ? : syncTokenReceived, shouldContinueSyncKobo || shouldContinueSync)
286
+ } catch (e: Exception ) {
287
+ logger.error(e) { " Kobo sync endpoint failure" }
288
+ Triple (syncResultKomga, syncTokenReceived, shouldContinueSync)
289
+ }
290
+ } else {
291
+ Triple (syncResultKomga, syncTokenReceived, shouldContinueSync)
292
+ }
293
+
294
+ // update synctoken to send back to Kobo device
276
295
val syncTokenUpdated =
277
- if (shouldContinueSync ) {
278
- syncToken .copy(ongoingSyncPointId = toSyncPoint.id)
296
+ if (shouldContinueSyncMerged ) {
297
+ syncTokenMerged .copy(ongoingSyncPointId = toSyncPoint.id)
279
298
} else {
280
299
// cleanup old syncpoint if it exists
281
300
fromSyncPoint?.let { syncPointRepository.deleteOne(it.id) }
282
301
283
- syncToken .copy(ongoingSyncPointId = null , lastSuccessfulSyncPointId = toSyncPoint.id)
302
+ syncTokenMerged .copy(ongoingSyncPointId = null , lastSuccessfulSyncPointId = toSyncPoint.id)
284
303
}
285
304
286
- // TODO: merge kobo store response
287
- // return koboProxy.proxyCurrentRequest(includeSyncToken = true)
288
-
289
305
return ResponseEntity
290
306
.ok()
291
307
.headers {
292
- if (shouldContinueSync ) it.set(X_KOBO_SYNC , " continue" )
308
+ if (shouldContinueSyncMerged ) it.set(X_KOBO_SYNC , " continue" )
293
309
it.set(X_KOBO_SYNCTOKEN , komgaSyncTokenGenerator.toBase64(syncTokenUpdated))
294
310
}
295
- .body(syncResult )
311
+ .body(syncResultMerged )
296
312
}
297
313
298
314
@GetMapping(" /v1/library/{bookId}/metadata" )
0 commit comments