Skip to content

Commit

Permalink
Content server: Fix newly added books on homepage not restricted to t…
Browse files Browse the repository at this point in the history
…he books the logged in user is allowed to access
  • Loading branch information
kovidgoyal committed Nov 18, 2023
1 parent 7fc0952 commit 8d67ae5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/calibre/db/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,8 +1405,8 @@ def format(self, book_id, fmt, as_file=False, as_path=False, preserve_filename=F
return ret

@read_api
def newly_added_book_ids(self, count=5) -> list[int]:
ids_to_sort = self._all_book_ids(list)
def newly_added_book_ids(self, count=5, book_ids=None) -> list[int]:
ids_to_sort = self._all_book_ids(list) if book_ids is None else list(book_ids)
ids_to_sort.sort(reverse=True)
return ids_to_sort[:count]

Expand Down
4 changes: 2 additions & 2 deletions src/calibre/srv/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ def newly_added(ctx, rd):
'''
db, library_id = get_library_data(ctx, rd)[:2]
count = int(rd.query.get('num', 3))
nbids = ctx.newest_book_ids(rd, db, count=count)
with db.safe_read_lock:
nbids = db._newly_added_book_ids(count)
titles = db._all_field_for('title', nbids)
authors = db._all_field_for('authors', nbids)
return {'library_id': library_id, 'books': nbids, 'titles': titles, 'authors': authors}
return {'library_id': library_id, 'books': nbids, 'titles': titles, 'authors': authors}


@endpoint('/interface-data/more-books', postprocess=json, methods=POSTABLE)
Expand Down
7 changes: 7 additions & 0 deletions src/calibre/srv/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ def has_id(self, request_data, db, book_id):
return False
return db.has_id(book_id)

def newest_book_ids(self, request_data, db, count=5):
restriction = self.restriction_for(request_data, db)
allowed_book_ids = None
if restriction:
allowed_book_ids = db.search('', restriction=restriction)
return db.newly_added_book_ids(count=count, book_ids=allowed_book_ids)

def get_allowed_book_ids_from_restriction(self, request_data, db):
restriction = self.restriction_for(request_data, db)
return frozenset(db.search('', restriction=restriction)) if restriction else None
Expand Down

0 comments on commit 8d67ae5

Please sign in to comment.