|
40 | 40 | from datetime import datetime, timezone |
41 | 41 | from typing import Dict, Optional, Any, Tuple |
42 | 42 |
|
| 43 | +from ...services import SyncToken as SyncToken, hardcover |
| 44 | +from ...kobo import push_reading_state_to_hardcover |
| 45 | + |
43 | 46 | from flask import Blueprint, request, jsonify |
44 | 47 | from flask_babel import gettext as _ |
45 | 48 | from werkzeug.security import check_password_hash |
@@ -190,7 +193,7 @@ def authenticate_user() -> Optional[ub.User]: |
190 | 193 | if login_result: |
191 | 194 | log.info(f"authenticate_user: Successfully authenticated user via LDAP: {user.name}") |
192 | 195 | return user |
193 | | - |
| 196 | + |
194 | 197 | # Log LDAP failure but continue to local check (fallback) |
195 | 198 | # We use debug level here because failure is expected if the user is using a local password |
196 | 199 | if error: |
@@ -326,7 +329,7 @@ def enrich_response_with_book_info(response_data: Dict[str, Any], document_check |
326 | 329 | return response_data, book_id, book_format, book_title, checksum_version |
327 | 330 |
|
328 | 331 |
|
329 | | -def update_book_read_status(user_id: int, book_id: int, percentage: float) -> None: |
| 332 | +def update_book_read_status(user, book_id: int, percentage: float) -> None: |
330 | 333 | """ |
331 | 334 | Update the user's ReadBook status based on reading progress percentage. |
332 | 335 |
|
@@ -360,6 +363,8 @@ def update_book_read_status(user_id: int, book_id: int, percentage: float) -> No |
360 | 363 | else: |
361 | 364 | new_status = ub.ReadBook.STATUS_UNREAD |
362 | 365 |
|
| 366 | + user_id = user.id |
| 367 | + |
363 | 368 | log.debug(f"update_book_read_status: user {user_id}, book {book_id}, " |
364 | 369 | f"percentage {percentage:.2f}% -> status {new_status}") |
365 | 370 |
|
@@ -717,10 +722,21 @@ def update_progress(): |
717 | 722 | # This is done AFTER kosync_progress is committed, so sync location is always safe |
718 | 723 | if book_id: |
719 | 724 | try: |
720 | | - update_book_read_status(user.id, book_id, percentage_float) |
| 725 | + update_book_read_status(user, book_id, percentage_float) |
721 | 726 | ub.session.commit() |
722 | 727 | log.info(f"Updated ReadBook status: user={user.id}, book={book_id} " |
723 | 728 | f"({book_title}), status based on {percentage_float:.1f}%") |
| 729 | + |
| 730 | + # Push to Hardcover |
| 731 | + from ... import calibre_db |
| 732 | + book = calibre_db.get_book(book_id) |
| 733 | + |
| 734 | + if user is not None: |
| 735 | + log.debug(f"Going to sync book {book_id} to Hardcover.") |
| 736 | + push_reading_state_to_hardcover(user, book, int(percentage_float)) |
| 737 | + else: |
| 738 | + log.debug(f"Book {book_id} not syncing to Hardcover, no matched user.") |
| 739 | + |
724 | 740 | except SQLAlchemyError as e: |
725 | 741 | log.error(f"Failed to update ReadBook status for book {book_id}: {e}") |
726 | 742 | # Rollback only affects the failed ReadBook update |
|
0 commit comments