@@ -146,13 +146,20 @@ def HandleSyncRequest():
146
146
if not current_user .role_download ():
147
147
log .info ("Users need download permissions for syncing library to Kobo reader" )
148
148
return abort (403 )
149
+
149
150
sync_token = SyncToken .SyncToken .from_headers (request .headers )
150
151
log .info ("Kobo library sync request received" )
151
152
log .debug ("SyncToken: {}" .format (sync_token ))
152
153
log .debug ("Download link format {}" .format (get_download_url_for_book ('[bookid]' , '[bookformat]' )))
153
154
if not current_app .wsgi_app .is_proxied :
154
155
log .debug ('Kobo: Received unproxied request, changed request port to external server port' )
155
156
157
+ # Produce an equivalent curl command to assist with debugging
158
+ #curl = f"curl -X{request.method} '{request.url}'"
159
+ #for key, value in request.headers.items():
160
+ # curl += f" -H'{key}: {value}'"
161
+ #log.debug(f"curl: {curl}")
162
+
156
163
# if no books synced don't respect sync_token
157
164
if not ub .session .query (ub .KoboSyncedBooks ).filter (ub .KoboSyncedBooks .user_id == current_user .id ).count ():
158
165
sync_token .books_last_modified = datetime .min
@@ -172,6 +179,8 @@ def HandleSyncRequest():
172
179
173
180
only_kobo_shelves = current_user .kobo_only_shelves_sync
174
181
182
+ log .debug ("Kobo Sync: books last modified: {}" .format (sync_token .books_last_modified ))
183
+
175
184
if only_kobo_shelves :
176
185
changed_entries = calibre_db .session .query (db .Books ,
177
186
ub .ArchivedBook .last_modified ,
@@ -182,11 +191,14 @@ def HandleSyncRequest():
182
191
ub .ArchivedBook .user_id == current_user .id ))
183
192
.filter (db .Books .id .notin_ (calibre_db .session .query (ub .KoboSyncedBooks .book_id )
184
193
.filter (ub .KoboSyncedBooks .user_id == current_user .id )))
185
- .filter (ub .BookShelf .date_added > sync_token .books_last_modified )
194
+ .filter (or_ (
195
+ ub .BookShelf .date_added > sync_token .books_last_modified ,
196
+ db .Books .last_modified > sync_token .books_last_modified ,
197
+ ))
186
198
.filter (db .Data .format .in_ (KOBO_FORMATS ))
187
199
.filter (calibre_db .common_filters (allow_show_archived = True ))
200
+ .order_by (db .Books .last_modified )
188
201
.order_by (db .Books .id )
189
- .order_by (ub .ArchivedBook .last_modified )
190
202
.join (ub .BookShelf , db .Books .id == ub .BookShelf .book_id )
191
203
.join (ub .Shelf )
192
204
.filter (ub .Shelf .user_id == current_user .id )
@@ -205,10 +217,11 @@ def HandleSyncRequest():
205
217
.filter (db .Data .format .in_ (KOBO_FORMATS ))
206
218
.order_by (db .Books .last_modified )
207
219
.order_by (db .Books .id ))
220
+ log .debug ("Kobo Sync: changed entries: {}" .format (changed_entries .count ()))
208
221
209
222
reading_states_in_new_entitlements = []
210
223
books = changed_entries .limit (SYNC_ITEM_LIMIT )
211
- log .debug ("Books to Sync : {}" .format (len (books .all ())))
224
+ log .debug ("Kobo Sync: selected to sync : {}" .format (len (books .all ())))
212
225
for book in books :
213
226
formats = [data .format for data in book .Books .data ]
214
227
if 'KEPUB' not in formats and config .config_kepubifypath and 'EPUB' in formats :
@@ -240,12 +253,6 @@ def HandleSyncRequest():
240
253
new_books_last_modified = max (
241
254
book .Books .last_modified .replace (tzinfo = None ), new_books_last_modified
242
255
)
243
- try :
244
- new_books_last_modified = max (
245
- new_books_last_modified , book .date_added
246
- )
247
- except AttributeError :
248
- pass
249
256
250
257
new_books_last_created = max (ts_created , new_books_last_created )
251
258
kobo_sync_status .add_synced_books (book .Books .id )
@@ -262,20 +269,19 @@ def HandleSyncRequest():
262
269
book_count = changed_entries .count ()
263
270
# last entry:
264
271
cont_sync = bool (book_count )
265
- log .debug ("Remaining books to Sync : {}" .format (book_count ))
272
+ log .debug ("Kobo Sync: remaining books to sync : {}" .format (book_count ))
266
273
# generate reading state data
267
274
changed_reading_states = ub .session .query (ub .KoboReadingState )
268
275
276
+ log .debug ("Kobo Sync: rstate last modified: {}" .format (sync_token .reading_state_last_modified ))
269
277
if only_kobo_shelves :
270
278
changed_reading_states = changed_reading_states .join (ub .BookShelf ,
271
279
ub .KoboReadingState .book_id == ub .BookShelf .book_id )\
272
280
.join (ub .Shelf )\
273
281
.filter (current_user .id == ub .Shelf .user_id )\
274
282
.filter (ub .Shelf .kobo_sync ,
275
- or_ (
276
- ub .KoboReadingState .last_modified > sync_token .reading_state_last_modified ,
277
- func .datetime (ub .BookShelf .date_added ) > sync_token .books_last_modified
278
- )).distinct ()
283
+ ub .KoboReadingState .last_modified > sync_token .reading_state_last_modified )\
284
+ .distinct ()
279
285
else :
280
286
changed_reading_states = changed_reading_states .filter (
281
287
ub .KoboReadingState .last_modified > sync_token .reading_state_last_modified )
@@ -284,6 +290,7 @@ def HandleSyncRequest():
284
290
and_ (ub .KoboReadingState .user_id == current_user .id ,
285
291
ub .KoboReadingState .book_id .notin_ (reading_states_in_new_entitlements )))\
286
292
.order_by (ub .KoboReadingState .last_modified )
293
+ log .debug ("Kobo Sync: changed states: {}" .format (changed_reading_states .count ()))
287
294
cont_sync |= bool (changed_reading_states .count () > SYNC_ITEM_LIMIT )
288
295
for kobo_reading_state in changed_reading_states .limit (SYNC_ITEM_LIMIT ).all ():
289
296
book = calibre_db .session .query (db .Books ).filter (db .Books .id == kobo_reading_state .book_id ).one_or_none ()
0 commit comments