fix(api): fix content restrictions for api/v1/books/page (#1706)
This commit is contained in:
+129
@@ -0,0 +1,129 @@
|
||||
package org.booklore.security.policy;
|
||||
|
||||
import jakarta.persistence.criteria.Join;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import jakarta.persistence.criteria.Subquery;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.model.entity.UserContentRestrictionEntity;
|
||||
import org.booklore.model.enums.ContentRestrictionMode;
|
||||
import org.booklore.model.enums.ContentRestrictionType;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class ContentRestrictionSpecification {
|
||||
|
||||
private ContentRestrictionSpecification() {
|
||||
}
|
||||
|
||||
public static Specification<BookEntity> from(List<UserContentRestrictionEntity> restrictions) {
|
||||
if (restrictions == null || restrictions.isEmpty()) {
|
||||
return (root, query, cb) -> cb.conjunction();
|
||||
}
|
||||
|
||||
Specification<BookEntity> spec = (root, query, cb) -> cb.conjunction();
|
||||
|
||||
for (CollectionType type : CollectionType.values()) {
|
||||
Set<String> excluded = values(restrictions, type.restrictionType, ContentRestrictionMode.EXCLUDE);
|
||||
if (!excluded.isEmpty()) {
|
||||
spec = spec.and(Specification.not(hasAnyCollectionValue(type.attribute, excluded)));
|
||||
}
|
||||
Set<String> allowed = values(restrictions, type.restrictionType, ContentRestrictionMode.ALLOW_ONLY);
|
||||
if (!allowed.isEmpty()) {
|
||||
spec = spec.and(hasAnyCollectionValue(type.attribute, allowed));
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> excludedRatings = values(restrictions, ContentRestrictionType.CONTENT_RATING, ContentRestrictionMode.EXCLUDE);
|
||||
if (!excludedRatings.isEmpty()) {
|
||||
spec = spec.and(Specification.not(hasContentRatingIn(excludedRatings)));
|
||||
}
|
||||
Set<String> allowedRatings = values(restrictions, ContentRestrictionType.CONTENT_RATING, ContentRestrictionMode.ALLOW_ONLY);
|
||||
if (!allowedRatings.isEmpty()) {
|
||||
spec = spec.and(hasContentRatingIn(allowedRatings));
|
||||
}
|
||||
|
||||
Integer maxAgeRating = maxAgeRating(restrictions);
|
||||
if (maxAgeRating != null) {
|
||||
spec = spec.and(Specification.not(hasAgeRatingAtLeast(maxAgeRating)));
|
||||
}
|
||||
|
||||
return spec;
|
||||
}
|
||||
|
||||
private enum CollectionType {
|
||||
CATEGORY(ContentRestrictionType.CATEGORY, "categories"),
|
||||
TAG(ContentRestrictionType.TAG, "tags"),
|
||||
MOOD(ContentRestrictionType.MOOD, "moods");
|
||||
|
||||
final ContentRestrictionType restrictionType;
|
||||
final String attribute;
|
||||
|
||||
CollectionType(ContentRestrictionType restrictionType, String attribute) {
|
||||
this.restrictionType = restrictionType;
|
||||
this.attribute = attribute;
|
||||
}
|
||||
}
|
||||
|
||||
private static Specification<BookEntity> hasAnyCollectionValue(String collectionAttribute, Set<String> loweredValues) {
|
||||
return (root, query, cb) -> {
|
||||
Subquery<Long> sq = query.subquery(Long.class);
|
||||
Root<BookMetadataEntity> m = sq.from(BookMetadataEntity.class);
|
||||
Join<Object, Object> value = m.join(collectionAttribute);
|
||||
sq.select(m.get("bookId")).where(
|
||||
cb.equal(m.get("bookId"), root.get("id")),
|
||||
cb.lower(value.get("name")).in(loweredValues));
|
||||
return cb.exists(sq);
|
||||
};
|
||||
}
|
||||
|
||||
private static Specification<BookEntity> hasContentRatingIn(Set<String> loweredValues) {
|
||||
return (root, query, cb) -> {
|
||||
Subquery<Long> sq = query.subquery(Long.class);
|
||||
Root<BookMetadataEntity> m = sq.from(BookMetadataEntity.class);
|
||||
sq.select(m.get("bookId")).where(
|
||||
cb.equal(m.get("bookId"), root.get("id")),
|
||||
cb.lower(m.get("contentRating")).in(loweredValues));
|
||||
return cb.exists(sq);
|
||||
};
|
||||
}
|
||||
|
||||
private static Specification<BookEntity> hasAgeRatingAtLeast(int threshold) {
|
||||
return (root, query, cb) -> {
|
||||
Subquery<Long> sq = query.subquery(Long.class);
|
||||
Root<BookMetadataEntity> m = sq.from(BookMetadataEntity.class);
|
||||
sq.select(m.get("bookId")).where(
|
||||
cb.equal(m.get("bookId"), root.get("id")),
|
||||
cb.greaterThanOrEqualTo(m.get("ageRating"), threshold));
|
||||
return cb.exists(sq);
|
||||
};
|
||||
}
|
||||
|
||||
private static Set<String> values(List<UserContentRestrictionEntity> restrictions,
|
||||
ContentRestrictionType type, ContentRestrictionMode mode) {
|
||||
return restrictions.stream()
|
||||
.filter(r -> r.getRestrictionType() == type && r.getMode() == mode)
|
||||
.map(r -> r.getValue().toLowerCase())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private static Integer maxAgeRating(List<UserContentRestrictionEntity> restrictions) {
|
||||
return restrictions.stream()
|
||||
.filter(r -> r.getRestrictionType() == ContentRestrictionType.AGE_RATING)
|
||||
.filter(r -> r.getMode() == ContentRestrictionMode.EXCLUDE)
|
||||
.map(r -> {
|
||||
try {
|
||||
return Integer.parseInt(r.getValue());
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.min(Integer::compareTo)
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.booklore.service.book;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.booklore.app.specification.AppBookSpecification;
|
||||
import org.booklore.mapper.v2.BookMapperV2;
|
||||
import org.booklore.model.dto.Book;
|
||||
import org.booklore.model.dto.BookMetadata;
|
||||
@@ -8,11 +9,14 @@ import org.booklore.model.dto.BookRecommendationLite;
|
||||
import org.booklore.model.dto.ComicMetadata;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.UserContentRestrictionRepository;
|
||||
import org.booklore.security.policy.ContentRestrictionSpecification;
|
||||
import org.booklore.service.restriction.ContentRestrictionService;
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -28,6 +32,7 @@ public class BookQueryService {
|
||||
private final BookRepository bookRepository;
|
||||
private final BookMapperV2 bookMapperV2;
|
||||
private final ContentRestrictionService contentRestrictionService;
|
||||
private final UserContentRestrictionRepository restrictionRepository;
|
||||
|
||||
public List<Book> getAllBooks(boolean includeDescription, boolean stripForListView) {
|
||||
List<BookEntity> books = bookRepository.findAllWithMetadata();
|
||||
@@ -46,14 +51,29 @@ public class BookQueryService {
|
||||
}
|
||||
|
||||
public Page<Book> getAllBooksByLibraryIdsPaged(Collection<Long> libraryIds, Long userId, Pageable pageable) {
|
||||
Page<BookEntity> page = bookRepository.findAllWithMetadataByLibraryIdsPage(libraryIds, pageable);
|
||||
List<BookEntity> filtered = contentRestrictionService.applyRestrictions(page.getContent(), userId);
|
||||
List<Book> dtos = filtered.stream()
|
||||
Page<BookEntity> page = bookRepository.findAll(visibleBooks(libraryIds, userId), pageable);
|
||||
Map<Long, BookEntity> booksById = bookRepository
|
||||
.findAllWithMetadataByIds(page.getContent().stream().map(BookEntity::getId).collect(Collectors.toSet()))
|
||||
.stream()
|
||||
.collect(Collectors.toMap(BookEntity::getId, book -> book));
|
||||
List<Book> dtos = page.getContent().stream()
|
||||
.map(book -> booksById.get(book.getId()))
|
||||
.filter(Objects::nonNull)
|
||||
.map(book -> mapBookToDto(book, false, userId, true))
|
||||
.toList();
|
||||
return new PageImpl<>(dtos, pageable, page.getTotalElements());
|
||||
}
|
||||
|
||||
private Specification<BookEntity> visibleBooks(Collection<Long> libraryIds, Long userId) {
|
||||
Specification<BookEntity> inLibraries = (root, query, cb) ->
|
||||
libraryIds == null || libraryIds.isEmpty()
|
||||
? cb.disjunction()
|
||||
: root.get("library").get("id").in(libraryIds);
|
||||
return AppBookSpecification.notDeleted()
|
||||
.and(inLibraries)
|
||||
.and(ContentRestrictionSpecification.from(restrictionRepository.findByUserId(userId)));
|
||||
}
|
||||
|
||||
public List<BookEntity> getAllFullBookEntitiesBatch(Pageable pageable) {
|
||||
List<BookEntity> books = bookRepository.findAllFullBooksBatch(pageable);
|
||||
for (BookEntity book : books) {
|
||||
|
||||
Reference in New Issue
Block a user