Fix merging of last-read annotations

This commit is contained in:
Kovid Goyal 2020-07-08 08:55:00 +05:30
parent b37ce63c86
commit 86f5976ea5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 15 additions and 6 deletions

View File

@ -53,6 +53,9 @@ def merge_annots_with_identical_field(a, b, field='title'):
return changed, ans return changed, ans
merge_field_map = {'bookmark': 'title', 'highlight': 'uuid'}
def merge_annot_lists(a, b, annot_type): def merge_annot_lists(a, b, annot_type):
if not a: if not a:
return list(b) return list(b)
@ -62,7 +65,7 @@ def merge_annot_lists(a, b, annot_type):
ans = a + b ans = a + b
ans.sort(key=itemgetter('timestamp'), reverse=True) ans.sort(key=itemgetter('timestamp'), reverse=True)
return ans return ans
merge_field = {'bookmark': 'title', 'highlight': 'uuid'}.get(annot_type) merge_field = merge_field_map.get(annot_type)
if merge_field is None: if merge_field is None:
return a + b return a + b
changed, c = merge_annots_with_identical_field(a, b, merge_field) changed, c = merge_annots_with_identical_field(a, b, merge_field)
@ -77,10 +80,17 @@ def merge_annotations(annots, annots_map):
amap = defaultdict(list) amap = defaultdict(list)
for annot in annots: for annot in annots:
amap[annot['type']].append(annot) amap[annot['type']].append(annot)
lr = annots_map.get('last-read')
lr = amap.get('last-read')
if lr: if lr:
lr.sort(key=itemgetter('timestamp'), reverse=True) existing = annots_map.get('last-read')
for annot_type, field in {'bookmark': 'title', 'highlight': 'uuid'}.items(): 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) a = annots_map.get(annot_type)
b = amap[annot_type] b = amap[annot_type]
if not b: if not b:

View File

@ -45,7 +45,6 @@ from calibre.utils.date import utcnow
from calibre.utils.img import image_from_path from calibre.utils.img import image_from_path
from calibre.utils.ipc.simple_worker import WorkerError from calibre.utils.ipc.simple_worker import WorkerError
from calibre.utils.monotonic import monotonic from calibre.utils.monotonic import monotonic
from calibre.utils.serialize import json_loads
from polyglot.builtins import as_bytes, as_unicode, iteritems, itervalues from polyglot.builtins import as_bytes, as_unicode, iteritems, itervalues
@ -556,7 +555,7 @@ class EbookViewer(MainWindow):
if os.path.exists(path): if os.path.exists(path):
with open(path, 'rb') as f: with open(path, 'rb') as f:
raw = f.read() 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']) path = os.path.join(annotations_dir, self.current_book_data['annotations_path_key'])
if os.path.exists(path): if os.path.exists(path):
with open(path, 'rb') as f: with open(path, 'rb') as f: