flagging
This commit is contained in:
@@ -16,6 +16,7 @@ import org.springframework.stereotype.Component;
|
||||
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.QVote;
|
||||
@@ -38,6 +39,9 @@ public class CommentManager {
|
||||
private VoteRepository voteRepository;
|
||||
@Autowired
|
||||
private VoteManager voteManager;
|
||||
@Autowired
|
||||
private FlagManager flagManager;
|
||||
|
||||
private QComment qComment = QComment.comment;
|
||||
private QVote qVote = QVote.vote;
|
||||
private QEntry qEntry = QEntry.entry;
|
||||
@@ -46,45 +50,62 @@ public class CommentManager {
|
||||
* Fetch by date.
|
||||
*
|
||||
* @param username the username
|
||||
* @param target the target
|
||||
* @param parent the parent
|
||||
* @param date the date
|
||||
* @param page the page
|
||||
* @param size the size
|
||||
* @param desc the desc
|
||||
* @param target the target
|
||||
* @param parent the parent
|
||||
* @param date the date
|
||||
* @param page the page
|
||||
* @param size the size
|
||||
* @param desc the desc
|
||||
* @return the page
|
||||
*/
|
||||
public Page<Comment> fetchByDate(String username, Long target, Long parent, Instant date,
|
||||
int page, int size, boolean desc) {
|
||||
Sort sort = Sort.by(desc ? Order.desc("created") : Order.asc("created"));
|
||||
if (parent == null) {
|
||||
return commentRepository.findAll(
|
||||
qComment.target.eq(target).and(qComment.parent.isNull())
|
||||
.and(qComment.created.before(date).or(qComment.author.eq(username))),
|
||||
PageRequest.of(page, size, sort));
|
||||
return commentRepository
|
||||
.findAll(
|
||||
qComment.target.eq(target).and(qComment.parent.isNull())
|
||||
.and(qComment.flaggedStatus.eq(FlaggedStatus.NORMAL))
|
||||
.and(qComment.created.before(date)
|
||||
.or(qComment.author.eq(username))),
|
||||
PageRequest.of(page, size, sort));
|
||||
}
|
||||
|
||||
return commentRepository.findAll(qComment.target.eq(target).and(qComment.parent.eq(parent))
|
||||
.and(qComment.flaggedStatus.eq(FlaggedStatus.NORMAL))
|
||||
.and(qComment.created.before(date)), PageRequest.of(page, size, sort));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch flagged.
|
||||
*
|
||||
* @param page the page
|
||||
* @param size the size
|
||||
* @param asc the asc
|
||||
* @return the page
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch by username.
|
||||
*
|
||||
* @param username the username
|
||||
* @param orElse the or else
|
||||
* @param date the date
|
||||
* @param page the page
|
||||
* @param size the size
|
||||
* @param asc the asc
|
||||
* @param date the date
|
||||
* @param page the page
|
||||
* @param size the size
|
||||
* @param asc the asc
|
||||
* @return the page
|
||||
*/
|
||||
public Page<Comment> fetchByUsername(String username, Long orElse, Instant date, int page,
|
||||
int size, boolean asc) {
|
||||
public Page<Comment> fetchByUsername(String username, Instant date, int page, int size,
|
||||
boolean asc) {
|
||||
Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created"));
|
||||
return commentRepository.findAll(
|
||||
qComment.author.equalsIgnoreCase(username).and(qComment.created.before(date)),
|
||||
PageRequest.of(page, size, sort));
|
||||
return commentRepository.findAll(qComment.author.equalsIgnoreCase(username)
|
||||
.and(qComment.flaggedStatus.eq(FlaggedStatus.NORMAL))
|
||||
.and(qComment.created.before(date)), PageRequest.of(page, size, sort));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,6 +121,7 @@ public class CommentManager {
|
||||
}
|
||||
|
||||
return commentRepository.count(qComment.target.eq(target).and(qComment.parent.eq(parent))
|
||||
.and(qComment.flaggedStatus.eq(FlaggedStatus.NORMAL))
|
||||
.and(qComment.created.before(Instant.now())));
|
||||
}
|
||||
|
||||
@@ -110,16 +132,17 @@ public class CommentManager {
|
||||
* @return the long
|
||||
*/
|
||||
public Long count(Long target) {
|
||||
return commentRepository
|
||||
.count(qComment.target.eq(target).and(qComment.created.before(Instant.now())));
|
||||
return commentRepository.count(
|
||||
qComment.target.eq(target).and(qComment.flaggedStatus.eq(FlaggedStatus.NORMAL))
|
||||
.and(qComment.created.before(Instant.now())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply metadata.
|
||||
*
|
||||
* @param username the username
|
||||
* @param comment the comment
|
||||
* @param ignore the ignore
|
||||
* @param comment the comment
|
||||
* @param ignore the ignore
|
||||
*/
|
||||
public void applyMetadata(String username, Comment comment, List<String> ignore) {
|
||||
|
||||
@@ -160,6 +183,16 @@ public class CommentManager {
|
||||
.and(qVote.author.equalsIgnoreCase(username))));
|
||||
}
|
||||
|
||||
if (!username.equals(comment.getAuthor()) && !ignore.contains("flag")) {
|
||||
comment.getMetadata().put("flag",
|
||||
flagManager.get(username, comment.getId(), Types.comment) == null);
|
||||
}
|
||||
|
||||
if (!username.equals(comment.getAuthor()) && !ignore.contains("unflag")) {
|
||||
comment.getMetadata().put("unflag",
|
||||
flagManager.get(username, comment.getId(), Types.comment) != null);
|
||||
}
|
||||
|
||||
if (!ignore.contains("entry")) {
|
||||
comment.getMetadata().put("entry", jpaQueryFactory.selectFrom(qEntry)
|
||||
.where(qEntry.id.eq(comment.getTarget())).select(qEntry.title).fetchOne());
|
||||
@@ -170,8 +203,8 @@ public class CommentManager {
|
||||
* Apply metadata.
|
||||
*
|
||||
* @param username the username
|
||||
* @param entries the entries
|
||||
* @param ignore the ignore
|
||||
* @param entries the entries
|
||||
* @param ignore the ignore
|
||||
*/
|
||||
public void applyMetadata(String username, List<Comment> entries, List<String> ignore) {
|
||||
for (Comment comment : entries) {
|
||||
|
||||
@@ -17,6 +17,8 @@ import com.google.common.collect.Lists;
|
||||
|
||||
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.QVote;
|
||||
import de.bstly.board.model.RankedEntry;
|
||||
@@ -43,6 +45,8 @@ public class EntryManager {
|
||||
private BookmarksManager bookmarksManager;
|
||||
@Autowired
|
||||
private SettingsManager settingsManager;
|
||||
@Autowired
|
||||
private FlagManager flagManager;
|
||||
|
||||
private QEntry qEntry = QEntry.entry;
|
||||
|
||||
@@ -95,10 +99,25 @@ public class EntryManager {
|
||||
* @return the page
|
||||
*/
|
||||
public Page<Entry> fetchByDate(Instant date, int page, int size) {
|
||||
return entryRepository.findAll(qEntry.created.before(date),
|
||||
return entryRepository.findAll(
|
||||
qEntry.created.before(date).and(qEntry.entryStatus.eq(EntryStatus.NORMAL)),
|
||||
PageRequest.of(page, size, Sort.by(Order.desc("created"))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch flagged.
|
||||
*
|
||||
* @param page the page
|
||||
* @param size the size
|
||||
* @param asc the asc
|
||||
* @return the page
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch by user.
|
||||
*
|
||||
@@ -155,8 +174,13 @@ public class EntryManager {
|
||||
entry.getMetadata().put("points", voteManager.getPoints(entry.getId(), Types.entry));
|
||||
}
|
||||
|
||||
if (!ignore.contains("bookmarked")) {
|
||||
entry.getMetadata().put("bookmarked",
|
||||
if (!ignore.contains("bookmark")) {
|
||||
entry.getMetadata().put("bookmark",
|
||||
!bookmarksManager.hasEntry(username, entry.getId()));
|
||||
}
|
||||
|
||||
if (!ignore.contains("removeBookmark")) {
|
||||
entry.getMetadata().put("removeBookmark",
|
||||
bookmarksManager.hasEntry(username, entry.getId()));
|
||||
}
|
||||
|
||||
@@ -174,6 +198,16 @@ public class EntryManager {
|
||||
.and(qVote.author.equalsIgnoreCase(username))));
|
||||
}
|
||||
|
||||
if (!username.equals(entry.getAuthor()) && !ignore.contains("flag")) {
|
||||
entry.getMetadata().put("flag",
|
||||
flagManager.get(username, entry.getId(), Types.entry) == null);
|
||||
}
|
||||
|
||||
if (!username.equals(entry.getAuthor()) && !ignore.contains("unflag")) {
|
||||
entry.getMetadata().put("unflag",
|
||||
flagManager.get(username, entry.getId(), Types.entry) != null);
|
||||
}
|
||||
|
||||
if (voteRepository
|
||||
.exists(qVote.target.eq(entry.getId()).and(qVote.targetType.eq(Types.entry))
|
||||
.and(qVote.author.equalsIgnoreCase(username)))) {
|
||||
|
||||
@@ -0,0 +1,219 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.board.businesslogic;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import de.bstly.board.model.Comment;
|
||||
import de.bstly.board.model.Entry;
|
||||
import de.bstly.board.model.Flag;
|
||||
import de.bstly.board.model.FlaggedStatus;
|
||||
import de.bstly.board.model.QComment;
|
||||
import de.bstly.board.model.QFlag;
|
||||
import de.bstly.board.model.Types;
|
||||
import de.bstly.board.repository.CommentRepository;
|
||||
import de.bstly.board.repository.FlagRepository;
|
||||
|
||||
/**
|
||||
* The Class FlagManager.
|
||||
*/
|
||||
@Component
|
||||
public class FlagManager {
|
||||
|
||||
@Autowired
|
||||
private FlagRepository flagRepository;
|
||||
@Autowired
|
||||
private SettingsManager settingsManager;
|
||||
@Autowired
|
||||
private EntryManager entryManager;
|
||||
@Autowired
|
||||
private CommentManager commentManager;
|
||||
@Autowired
|
||||
private CommentRepository commentRepository;
|
||||
|
||||
private QFlag qFlag = QFlag.flag;
|
||||
private QComment qComment = QComment.comment;
|
||||
|
||||
/**
|
||||
* Gets the.
|
||||
*
|
||||
* @param author the author
|
||||
* @param target the target
|
||||
* @param targetType the target type
|
||||
* @return the flag
|
||||
*/
|
||||
public Flag get(String author, Long target, Types targetType) {
|
||||
return flagRepository.findOne(qFlag.author.eq(author).and(qFlag.targetType.eq(targetType))
|
||||
.and(qFlag.target.eq(target))).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save.
|
||||
*
|
||||
* @param flag the flag
|
||||
* @return the flag
|
||||
*/
|
||||
public Flag save(Flag flag) {
|
||||
return flagRepository.save(flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete.
|
||||
*
|
||||
* @param flag the flag
|
||||
*/
|
||||
public void delete(Flag flag) {
|
||||
flagRepository.delete(flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete by target.
|
||||
*
|
||||
* @param target the target
|
||||
* @param targetType the target type
|
||||
*/
|
||||
public void deleteByTarget(Long target, Types targetType) {
|
||||
for (Flag flag : flagRepository
|
||||
.findAll(qFlag.target.eq(target).and(qFlag.targetType.eq(targetType)))) {
|
||||
delete(flag);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the flags.
|
||||
*
|
||||
* @param target the target
|
||||
* @param targetType the target type
|
||||
* @return the flags
|
||||
*/
|
||||
public long getFlags(Long target, Types targetType) {
|
||||
return flagRepository.count(qFlag.target.eq(target).and(qFlag.targetType.eq(targetType)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check flag status.
|
||||
*
|
||||
* @param target the target
|
||||
* @param targetType the target type
|
||||
*/
|
||||
public void checkFlagStatus(Long target, Types targetType) {
|
||||
switch (targetType) {
|
||||
case comment:
|
||||
checkCommentFlagStatus(target);
|
||||
break;
|
||||
case entry:
|
||||
checkEntryFlagStatus(target);
|
||||
break;
|
||||
case user:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check comment flag status.
|
||||
*
|
||||
* @param target the target
|
||||
*/
|
||||
public void checkCommentFlagStatus(Long target) {
|
||||
Assert.isTrue(commentManager.exists(target), "Comment not exists: '"
|
||||
+ target
|
||||
+ "'!");
|
||||
Comment comment = commentManager.get(target);
|
||||
|
||||
if (getFlags(target, Types.comment) >= settingsManager.getFlahThresh()) {
|
||||
if (!FlaggedStatus.FLAGGED.equals(comment.getFlaggedStatus())) {
|
||||
flagComment(comment);
|
||||
}
|
||||
} else if (FlaggedStatus.FLAGGED.equals(comment.getFlaggedStatus())) {
|
||||
unflagComment(comment);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag comment.
|
||||
*
|
||||
* @param comment the comment
|
||||
*/
|
||||
protected void flagComment(Comment comment) {
|
||||
comment.setFlaggedStatus(FlaggedStatus.FLAGGED);
|
||||
comment = commentManager.save(comment);
|
||||
hideSubcomments(comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide subcomments.
|
||||
*
|
||||
* @param comment the comment
|
||||
*/
|
||||
protected void hideSubcomments(Comment comment) {
|
||||
for (Comment subcomment : commentRepository.findAll(qComment.parent.eq(comment.getId())
|
||||
.and(qComment.flaggedStatus.eq(FlaggedStatus.NORMAL)))) {
|
||||
subcomment.setFlaggedStatus(FlaggedStatus.HIDDEN);
|
||||
subcomment = commentManager.save(subcomment);
|
||||
hideSubcomments(subcomment);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unflag comment.
|
||||
*
|
||||
* @param comment the comment
|
||||
*/
|
||||
protected void unflagComment(Comment comment) {
|
||||
comment.setFlaggedStatus(FlaggedStatus.NORMAL);
|
||||
comment = commentManager.save(comment);
|
||||
unhideSubcomments(comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unhide subcomments.
|
||||
*
|
||||
* @param comment the comment
|
||||
*/
|
||||
protected void unhideSubcomments(Comment comment) {
|
||||
for (Comment subcomment : commentRepository.findAll(qComment.parent.eq(comment.getId())
|
||||
.and(qComment.flaggedStatus.eq(FlaggedStatus.HIDDEN)))) {
|
||||
subcomment.setFlaggedStatus(FlaggedStatus.NORMAL);
|
||||
subcomment = commentManager.save(subcomment);
|
||||
hideSubcomments(subcomment);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check entry flag status.
|
||||
*
|
||||
* @param target the target
|
||||
*/
|
||||
public void checkEntryFlagStatus(Long target) {
|
||||
Assert.isTrue(entryManager.exists(target), "Entry not exists: '"
|
||||
+ target
|
||||
+ "'!");
|
||||
Entry entry = entryManager.get(target);
|
||||
if (getFlags(target, Types.entry) >= settingsManager.getFlahThresh()) {
|
||||
if (!FlaggedStatus.FLAGGED.equals(entry.getFlaggedStatus())) {
|
||||
entry.setFlaggedStatus(FlaggedStatus.FLAGGED);
|
||||
entryManager.save(entry);
|
||||
}
|
||||
} else if (FlaggedStatus.FLAGGED.equals(entry.getFlaggedStatus())) {
|
||||
entry.setFlaggedStatus(FlaggedStatus.NORMAL);
|
||||
entryManager.save(entry);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unflag.
|
||||
*
|
||||
* @param target the target
|
||||
* @param targetType the target type
|
||||
*/
|
||||
public void unflag(Long target, Types targetType) {
|
||||
deleteByTarget(target, targetType);
|
||||
checkFlagStatus(target, targetType);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,6 +20,8 @@ public class SettingsManager {
|
||||
private long COMMENT_CHANGE_PERIDO;
|
||||
@Value("${bstly.board.unvoteThresh:10}")
|
||||
private long UNVOTE_THRESH;
|
||||
@Value("${bstly.board.flagThresh:3}")
|
||||
private long FLAG_THRESH;
|
||||
|
||||
/**
|
||||
* Gets the gravity.
|
||||
@@ -56,4 +58,13 @@ public class SettingsManager {
|
||||
public long getUnvoteThresh() {
|
||||
return UNVOTE_THRESH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the flah thresh.
|
||||
*
|
||||
* @return the flah thresh
|
||||
*/
|
||||
public long getFlahThresh() {
|
||||
return FLAG_THRESH;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,39 @@ 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)
|
||||
*/
|
||||
/*
|
||||
* @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)
|
||||
*/
|
||||
/*
|
||||
* @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)
|
||||
*/
|
||||
/*
|
||||
* @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)
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.security.core.userdetails.UserDetailsService#
|
||||
* loadUserByUsername(java.lang.String)
|
||||
@@ -116,6 +149,39 @@ 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()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#
|
||||
* afterSingletonsInstantiated()
|
||||
|
||||
@@ -27,11 +27,11 @@ public class VoteManager {
|
||||
* Gets the.
|
||||
*
|
||||
* @param author the author
|
||||
* @param targetType the target type
|
||||
* @param target the target
|
||||
* @param targetType the target type
|
||||
* @return the vote
|
||||
*/
|
||||
public Vote get(String author, Types targetType, Long target) {
|
||||
public Vote get(String author, Long target, Types targetType) {
|
||||
return voteRepository.findOne(qVote.author.eq(author).and(qVote.targetType.eq(targetType))
|
||||
.and(qVote.target.eq(target))).orElse(null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user