ignore + unvote karma

This commit is contained in:
2021-10-05 08:41:57 +02:00
parent f2de0e0ba3
commit 5c14fbff6b
7 changed files with 130 additions and 69 deletions
@@ -147,9 +147,14 @@ public class CommentManager {
ignore.addAll(comment.getMetadata().keySet());
if (!ignore.contains("author")) {
comment.getMetadata().put("author", comment.getAuthor());
}
if (!ignore.contains("comments")) {
comment.getMetadata().put("comments", count(comment.getTarget(), comment.getId()));
}
if (!ignore.contains("points")) {
comment.getMetadata().put("points",
voteManager.getPoints(comment.getId(), Types.comment));
@@ -47,16 +47,16 @@ public class EntryManager {
private QVote qVote = QVote.vote;
@Value("${bstly.board.ranking.gravity:1.8}")
private double GRAVITY;
@Value("${bstly.board.unvoteThresh:10}")
private long UNVOTE_THRESH;
/**
* Fetch by ranking.
*
* @param date the date
* @param date the date
* @param gravity the gravity
* @param page the page
* @param size the size
* @param page the page
* @param size the size
* @return the page
*/
public Page<RankedEntry> fetchByRanking(Instant date, double gravity, int page, int size) {
@@ -80,15 +80,16 @@ public class EntryManager {
* Fetch by user.
*
* @param username the username
* @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<Entry> fetchByUser(String username, Instant date, int page, int size, boolean asc) {
Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created"));
return entryRepository.findAll(qEntry.author.equalsIgnoreCase(username).and(qEntry.created.before(date)),
return entryRepository.findAll(
qEntry.author.equalsIgnoreCase(username).and(qEntry.created.before(date)),
PageRequest.of(page, size, sort));
}
@@ -96,8 +97,8 @@ public class EntryManager {
* Fetch by bookmarks.
*
* @param username the username
* @param page the page
* @param size the size
* @param page the page
* @param size the size
* @return the page
*/
public Page<Entry> fetchByBookmarks(String username, int page, int size) {
@@ -115,39 +116,53 @@ public class EntryManager {
* Apply metadata.
*
* @param username the username
* @param entry the entry
* @param entry the entry
*/
public void applyMetadata(String username, Entry entry) {
if (!entry.getMetadata().containsKey("comments")) {
public void applyMetadata(String username, long karma, Entry entry, List<String> ignore) {
ignore.addAll(entry.getMetadata().keySet());
if (!ignore.contains("comments")) {
entry.getMetadata().put("comments", commentManager.count(entry.getId()));
}
if (!entry.getMetadata().containsKey("points")) {
if (!ignore.contains("points")) {
entry.getMetadata().put("points", voteManager.getPoints(entry.getId(), Types.entry));
}
if (!entry.getMetadata().containsKey("bookmarked")) {
if (!ignore.contains("bookmarked")) {
entry.getMetadata().put("bookmarked",
bookmarksManager.hasEntry(username, entry.getId()));
}
if (!entry.getMetadata().containsKey("upvoted")) {
if (!ignore.contains("upvoted")) {
entry.getMetadata().put("upvoted",
voteRepository.exists(qVote.target.eq(entry.getId())
.and(qVote.targetType.eq(Types.entry)).and(qVote.type.eq(VoteType.up))
.and(qVote.author.equalsIgnoreCase(username))));
}
if (!entry.getMetadata().containsKey("downvoted")) {
if (!ignore.contains("downvoted")) {
entry.getMetadata().put("downvoted",
voteRepository.exists(qVote.target.eq(entry.getId())
.and(qVote.targetType.eq(Types.entry)).and(qVote.type.eq(VoteType.down))
.and(qVote.author.equalsIgnoreCase(username))));
}
if (!entry.getMetadata().containsKey("unvote")) {
entry.getMetadata().put("unvote", voteRepository.exists(qVote.target.eq(entry.getId())
.and(qVote.targetType.eq(Types.entry)).and(qVote.author.equalsIgnoreCase(username))));
if (voteRepository
.exists(qVote.target.eq(entry.getId()).and(qVote.targetType.eq(Types.entry))
.and(qVote.author.equalsIgnoreCase(username)))) {
if (!ignore.contains("unvote")) {
entry.getMetadata().put("unvote", true);
}
} else {
if (!ignore.contains("vote")) {
entry.getMetadata().put("vote", true);
}
if (!ignore.contains("unvote") && karma >= UNVOTE_THRESH) {
entry.getMetadata().put("unvote", true);
}
}
}
@@ -155,11 +170,12 @@ public class EntryManager {
* Apply metadata.
*
* @param username the username
* @param entries the entries
* @param entries the entries
*/
public void applyMetadata(String username, List<Entry> entries) {
public void applyMetadata(String username, long karma, List<Entry> entries,
List<String> ignore) {
for (Entry entry : entries) {
applyMetadata(username, entry);
applyMetadata(username, karma, entry, ignore);
}
}
@@ -221,7 +237,7 @@ public class EntryManager {
/**
* Gets the user points.
*
* @param entryId the entry id
* @param entryId the entry id
* @param username the username
* @return the user points
*/
@@ -209,7 +209,7 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
String locale = token.getPrincipal().getAttribute("locale");
if (!StringUtils.hasText(locale)) {
locale = "en";
locale = "de";
}
localUser.setLocale(locale);
@@ -240,15 +240,23 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
}
if (!user.getMetadata().containsKey("points")) {
long points = 0;
for (Entry entry : entryRepository
.findAll(qEntry.author.equalsIgnoreCase(user.getUsername()))) {
points += entryManager.getUserPoints(entry.getId(), user.getUsername());
}
user.getMetadata().put("points", points);
user.getMetadata().put("points", getKarma(user.getUsername()));
}
}
/**
*
* @param username
* @return
*/
public long getKarma(String username) {
long karma = 0;
for (Entry entry : entryRepository.findAll(qEntry.author.equalsIgnoreCase(username))) {
karma += entryManager.getUserPoints(entry.getId(), username);
}
return karma;
}
/**
* Save.
*