From 86f5976ea53f987f347ea86590373f2008dc396c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 8 Jul 2020 08:55:00 +0530 Subject: [PATCH] Fix merging of last-read annotations --- src/calibre/db/annotations.py | 18 ++++++++++++++---- src/calibre/gui2/viewer/ui.py | 3 +-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/calibre/db/annotations.py b/src/calibre/db/annotations.py index 18cc81cc64..32f30297c6 100644 --- a/src/calibre/db/annotations.py +++ b/src/calibre/db/annotations.py @@ -53,6 +53,9 @@ def merge_annots_with_identical_field(a, b, field='title'): return changed, ans +merge_field_map = {'bookmark': 'title', 'highlight': 'uuid'} + + def merge_annot_lists(a, b, annot_type): if not a: return list(b) @@ -62,7 +65,7 @@ def merge_annot_lists(a, b, annot_type): ans = a + b ans.sort(key=itemgetter('timestamp'), reverse=True) return ans - merge_field = {'bookmark': 'title', 'highlight': 'uuid'}.get(annot_type) + merge_field = merge_field_map.get(annot_type) if merge_field is None: return a + b changed, c = merge_annots_with_identical_field(a, b, merge_field) @@ -77,10 +80,17 @@ def merge_annotations(annots, annots_map): amap = defaultdict(list) for annot in annots: amap[annot['type']].append(annot) - lr = annots_map.get('last-read') + + lr = amap.get('last-read') if lr: - lr.sort(key=itemgetter('timestamp'), reverse=True) - for annot_type, field in {'bookmark': 'title', 'highlight': 'uuid'}.items(): + existing = annots_map.get('last-read') + if existing: + lr = existing + lr + if lr: + lr.sort(key=itemgetter('timestamp'), reverse=True) + annots_map['last-read'] = [lr[0]] + + for annot_type, field in merge_field_map.items(): a = annots_map.get(annot_type) b = amap[annot_type] if not b: diff --git a/src/calibre/gui2/viewer/ui.py b/src/calibre/gui2/viewer/ui.py index 21eec2c370..fab78b8d02 100644 --- a/src/calibre/gui2/viewer/ui.py +++ b/src/calibre/gui2/viewer/ui.py @@ -45,7 +45,6 @@ from calibre.utils.date import utcnow from calibre.utils.img import image_from_path from calibre.utils.ipc.simple_worker import WorkerError from calibre.utils.monotonic import monotonic -from calibre.utils.serialize import json_loads from polyglot.builtins import as_bytes, as_unicode, iteritems, itervalues @@ -556,7 +555,7 @@ class EbookViewer(MainWindow): if os.path.exists(path): with open(path, 'rb') as f: raw = f.read() - merge_annotations(json_loads(raw), amap) + merge_annotations(parse_annotations(raw), amap) path = os.path.join(annotations_dir, self.current_book_data['annotations_path_key']) if os.path.exists(path): with open(path, 'rb') as f: