improve flagging

This commit is contained in:
_Bastler 2021-10-07 14:35:15 +02:00
parent db572e6f2b
commit a8c5515c11
3 changed files with 45 additions and 6 deletions

View File

@ -8,17 +8,21 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Component;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import de.bstly.board.model.Comment;
import de.bstly.board.model.FlaggedStatus;
import de.bstly.board.model.QComment;
import de.bstly.board.model.QEntry;
import de.bstly.board.model.QFlag;
import de.bstly.board.model.QVote;
import de.bstly.board.model.Types;
import de.bstly.board.model.VoteType;
@ -44,6 +48,7 @@ public class CommentManager {
private QComment qComment = QComment.comment;
private QVote qVote = QVote.vote;
private QFlag qFlag = QFlag.flag;
private QEntry qEntry = QEntry.entry;
/**
@ -86,8 +91,14 @@ public class CommentManager {
*/
public Page<Comment> fetchFlagged(int page, int size, boolean asc) {
Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created"));
return commentRepository.findAll(qComment.flaggedStatus.eq(FlaggedStatus.FLAGGED),
PageRequest.of(page, size, sort));
JPAQuery<Comment> query = jpaQueryFactory.selectFrom(qComment).leftJoin(qFlag)
.on(qComment.id.eq(qFlag.target)).where(qFlag.targetType.eq(Types.comment))
.groupBy(qFlag.target);
return new PageImpl<Comment>(query
.orderBy(new OrderSpecifier<>(asc ? com.querydsl.core.types.Order.ASC
: com.querydsl.core.types.Order.DESC, qComment.created))
.limit(size).offset(page * size).fetch(), PageRequest.of(page, size, sort),
query.fetchCount());
}
/**
@ -193,6 +204,11 @@ public class CommentManager {
flagManager.get(username, comment.getId(), Types.comment) != null);
}
if (!ignore.contains("flagged")) {
comment.getMetadata().put("flagged",
flagManager.getFlags(comment.getId(), Types.comment) > 0);
}
if (!ignore.contains("entry")) {
comment.getMetadata().put("entry", jpaQueryFactory.selectFrom(qEntry)
.where(qEntry.id.eq(comment.getTarget())).select(qEntry.title).fetchOne());

View File

@ -8,18 +8,22 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Component;
import com.google.common.collect.Lists;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import de.bstly.board.model.Bookmarks;
import de.bstly.board.model.Entry;
import de.bstly.board.model.EntryStatus;
import de.bstly.board.model.FlaggedStatus;
import de.bstly.board.model.QEntry;
import de.bstly.board.model.QFlag;
import de.bstly.board.model.QVote;
import de.bstly.board.model.RankedEntry;
import de.bstly.board.model.Types;
@ -47,10 +51,12 @@ public class EntryManager {
private SettingsManager settingsManager;
@Autowired
private FlagManager flagManager;
@Autowired
private JPAQueryFactory jpaQueryFactory;
private QEntry qEntry = QEntry.entry;
private QVote qVote = QVote.vote;
private QFlag qFlag = QFlag.flag;
/**
* Fetch by ranking.
@ -114,8 +120,14 @@ public class EntryManager {
*/
public Page<Entry> fetchFlagged(int page, int size, boolean asc) {
Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created"));
return entryRepository.findAll(qEntry.flaggedStatus.ne(FlaggedStatus.NORMAL),
PageRequest.of(page, size, sort));
JPAQuery<Entry> query = jpaQueryFactory.selectFrom(qEntry).leftJoin(qFlag)
.on(qEntry.id.eq(qFlag.target)).where(qFlag.targetType.eq(Types.entry))
.groupBy(qFlag.target);
return new PageImpl<Entry>(query
.orderBy(new OrderSpecifier<>(asc ? com.querydsl.core.types.Order.ASC
: com.querydsl.core.types.Order.DESC, qEntry.created))
.limit(size).offset(page * size).fetch(), PageRequest.of(page, size, sort),
query.fetchCount());
}
/**
@ -208,6 +220,11 @@ public class EntryManager {
flagManager.get(username, entry.getId(), Types.entry) != null);
}
if (!ignore.contains("flagged")) {
entry.getMetadata().put("flagged",
flagManager.getFlags(entry.getId(), Types.entry) > 0);
}
if (voteRepository
.exists(qVote.target.eq(entry.getId()).and(qVote.targetType.eq(Types.entry))
.and(qVote.author.equalsIgnoreCase(username)))) {

View File

@ -104,6 +104,9 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
/*
* @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
*/
/*
* @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
*/
/*
* @see org.springframework.security.core.userdetails.UserDetailsService#
* loadUserByUsername(java.lang.String)
@ -182,6 +185,9 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
*/
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
*/
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#
* afterSingletonsInstantiated()