Skip to content

Commit 3e2c1ae

Browse files
committed
Better handling of read status sync to prevent multiple reads being created.
1 parent a83b664 commit 3e2c1ae

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

root/app/calibre-web/cps/services/hardcover.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
id
3939
pages
4040
}
41-
user_book_reads(order_by: {started_at: desc}, limit: 1) {
41+
user_book_reads(order_by: {started_at: desc}, where: {finished_at: {_is_null: true}}) {
4242
id
4343
started_at
4444
finished_at
@@ -108,37 +108,40 @@ def get_user_book(self, ids):
108108
def update_reading_progress(self, identifiers, progress_percent):
109109
ids = self.parse_identifiers(identifiers)
110110
book = self.get_user_book(ids)
111-
if not book:
111+
if not book: # Book doesn't exist, add it in Reading status
112112
book = self.add_book(ids, status=2)
113-
if book.get("status_id") is not 2:
113+
if book.get("status_id") != 2: # Book is either WTR or Read
114114
book = self.change_book_status(book, 2)
115115
pages = book.get("edition",{}).get("pages",0)
116116
if pages:
117117
pages_read = round(pages * (progress_percent / 100))
118118
read = next(iter(book.get("user_book_reads")),None)
119119
if not read:
120-
read = self.add_read(book, pages_read)
121-
mutation = """
122-
mutation ($readId: Int!, $pages: Int, $editionId: Int, $startedAt: date, $finishedAt: date) {
123-
update_user_book_read(id: $readId, object: {
124-
progress_pages: $pages,
125-
edition_id: $editionId,
126-
started_at: $startedAt,
127-
finished_at: $finishedAt
128-
}) {
129-
id
120+
# read = self.add_read(book, pages_read)
121+
# No read exists for some reason, return since we can't update anything.
122+
return
123+
else:
124+
mutation = """
125+
mutation ($readId: Int!, $pages: Int, $editionId: Int, $startedAt: date, $finishedAt: date) {
126+
update_user_book_read(id: $readId, object: {
127+
progress_pages: $pages,
128+
edition_id: $editionId,
129+
started_at: $startedAt,
130+
finished_at: $finishedAt
131+
}) {
132+
id
133+
}
134+
}"""
135+
variables = {
136+
"readId": int(read.get("id")),
137+
"pages": pages_read,
138+
"editionId": int(book.get("edition").get("id")),
139+
"startedAt":read.get("started_at",datetime.now().strftime("%Y-%m-%d")),
140+
"finishedAt": datetime.now().strftime("%Y-%m-%d") if progress_percent is 100 else None
130141
}
131-
}"""
132-
variables = {
133-
"readId": int(read.get("id")),
134-
"pages": pages_read,
135-
"editionId": int(book.get("edition").get("id")),
136-
"startedAt":read.get("started_at",datetime.now().strftime("%Y-%m-%d")),
137-
"finishedAt": datetime.now().strftime("%Y-%m-%d") if progress_percent is 100 else None
138-
}
139-
if progress_percent is 100:
140-
self.change_book_status(book, 3)
141-
return self.execute(query=mutation, variables=variables)
142+
if progress_percent is 100:
143+
self.change_book_status(book, 3)
144+
self.execute(query=mutation, variables=variables)
142145
return
143146

144147
def change_book_status(self, book, status):

0 commit comments

Comments
 (0)