ignore + unvote karma

This commit is contained in:
_Bastler 2021-10-05 08:41:57 +02:00
parent f2de0e0ba3
commit 5c14fbff6b
7 changed files with 130 additions and 69 deletions

View File

@ -10,7 +10,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version>
<revision>0.3.3-SNAPSHOT</revision>
<revision>0.3.4-SNAPSHOT</revision>
</properties>
<parent>

View File

@ -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));

View File

@ -47,8 +47,8 @@ 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.
@ -88,7 +88,8 @@ public class EntryManager {
*/
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));
}
@ -117,37 +118,51 @@ public class EntryManager {
* @param username the username
* @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);
}
}
}
@ -157,9 +172,10 @@ public class EntryManager {
* @param username the username
* @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);
}
}

View File

@ -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,13 +240,21 @@ 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", getKarma(user.getUsername()));
}
user.getMetadata().put("points", points);
}
/**
*
* @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;
}
/**

View File

@ -3,6 +3,7 @@
*/
package de.bstly.board.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
@ -18,8 +19,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Lists;
import de.bstly.board.businesslogic.BookmarksManager;
import de.bstly.board.businesslogic.EntryManager;
import de.bstly.board.businesslogic.UserManager;
import de.bstly.board.controller.support.EntityResponseStatusException;
import de.bstly.board.model.Entry;
@ -34,6 +38,8 @@ public class BookmarksController extends BaseController {
private BookmarksManager bookmarksManager;
@Autowired
private EntryManager entryManager;
@Autowired
private UserManager userManager;
@Value("${bstly.board.size:30}")
private int SIZE;
@ -47,7 +53,8 @@ public class BookmarksController extends BaseController {
@PreAuthorize("isAuthenticated()")
@GetMapping()
public Page<Entry> getEntries(@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter) {
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
if (sizeParameter.isPresent() && sizeParameter.get() > 100) {
sizeParameter = Optional.of(100);
@ -56,7 +63,9 @@ public class BookmarksController extends BaseController {
Page<Entry> entries = entryManager.fetchByBookmarks(getCurrentUsername(),
pageParameter.orElse(0), sizeParameter.orElse(SIZE));
entryManager.applyMetadata(getCurrentUsername(), entries.getContent());
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
entries.getContent(), ignore);
return entries;
}

View File

@ -51,7 +51,7 @@ public class CommentController extends BaseController {
@Value("${bstly.board.size:30}")
private int SIZE;
@Value("${bstly.board.ranking.gravity:1.8}")
@Value("${bstly.board.ranking.gravity:1.2}")
private double GRAVITY;
/**
@ -72,12 +72,11 @@ public class CommentController extends BaseController {
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("gravity") Optional<Double> gravityParameter) {
@RequestParam("gravity") Optional<Double> gravityParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Page<Comment> comments = fetchByDate(target, parent, pageParameter, sizeParameter,
dateParameter, Optional.of(false));
List<String> ignore = Lists.newArrayList("entry");
commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore);
dateParameter, Optional.of(false), ignoreParameter);
return comments;
}
@ -99,11 +98,13 @@ public class CommentController extends BaseController {
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("desc") Optional<Boolean> descParameter) {
@RequestParam("desc") Optional<Boolean> descParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Page<Comment> comments = commentManager.fetchByDate(target, parent.orElse(null),
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), descParameter.orElse(false));
List<String> ignore = Lists.newArrayList("entry");
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList("entry"));
commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore);
return comments;
}
@ -126,12 +127,13 @@ public class CommentController extends BaseController {
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("asc") Optional<Boolean> ascParameter) {
@RequestParam("asc") Optional<Boolean> ascParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Page<Comment> comments = commentManager.fetchByUsername(username, parent.orElse(null),
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), ascParameter.orElse(false));
commentManager.applyMetadata(getCurrentUsername(), comments.getContent(),
Lists.newArrayList());
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore);
return comments;
}
@ -157,15 +159,16 @@ public class CommentController extends BaseController {
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/comment/{id}")
public Comment getComment(@PathVariable("id") Long id) {
public Comment getComment(@PathVariable("id") Long id,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Comment comment = commentManager.get(id);
if (comment == null) {
throw new EntityResponseStatusException(HttpStatus.NOT_FOUND);
}
commentManager.applyMetadata(getCurrentUsername(), comment, Lists.newArrayList());
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
commentManager.applyMetadata(getCurrentUsername(), comment, ignore);
return comment;
}
@ -177,7 +180,8 @@ public class CommentController extends BaseController {
*/
@PreAuthorize("isAuthenticated()")
@PostMapping
public Comment createComment(@RequestBody Comment comment) {
public Comment createComment(@RequestBody Comment comment,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
RequestBodyErrors bindingResult = new RequestBodyErrors(comment);
commentValidator.validate(comment, bindingResult);
@ -197,7 +201,7 @@ public class CommentController extends BaseController {
vote.setAuthor(getCurrentUsername());
voteManager.save(vote);
List<String> ignore = Lists.newArrayList("entry");
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList("entry"));
commentManager.applyMetadata(getCurrentUsername(), comment, ignore);
return comment;

View File

@ -4,6 +4,7 @@
package de.bstly.board.controller;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@ -21,7 +22,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Lists;
import de.bstly.board.businesslogic.EntryManager;
import de.bstly.board.businesslogic.UserManager;
import de.bstly.board.businesslogic.VoteManager;
import de.bstly.board.controller.support.EntityResponseStatusException;
import de.bstly.board.controller.support.RequestBodyErrors;
@ -42,17 +46,17 @@ public class EntryController extends BaseController {
@Autowired
private EntryManager entryManager;
@Autowired
private UserManager userManager;
@Autowired
private EntryValidator entryValidator;
@Autowired
private VoteManager voteManager;
@Value("${bstly.board.size:30}")
private int SIZE;
@Value("${bstly.board.ranking.gravity:1.8}")
@Value("${bstly.board.ranking.gravity:1.2}")
private double GRAVITY;
/**
@ -69,7 +73,8 @@ public class EntryController extends BaseController {
public Page<Entry> fetchByRanking(@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("gravity") Optional<Double> gravityParameter) {
@RequestParam("gravity") Optional<Double> gravityParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
if (sizeParameter.isPresent() && sizeParameter.get() > 100) {
sizeParameter = Optional.of(100);
@ -84,7 +89,9 @@ public class EntryController extends BaseController {
.collect(Collectors.toList()),
entries.getPageable(), entries.getTotalElements());
entryManager.applyMetadata(getCurrentUsername(), transformed.getContent());
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
transformed.getContent(), ignore);
return transformed;
}
@ -100,7 +107,8 @@ public class EntryController extends BaseController {
@GetMapping("/new")
public Page<Entry> fetchByDate(@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter) {
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
if (sizeParameter.isPresent() && sizeParameter.get() > 100) {
sizeParameter = Optional.of(100);
@ -108,7 +116,9 @@ public class EntryController extends BaseController {
Page<Entry> entries = entryManager.fetchByDate(dateParameter.orElse(Instant.now()),
pageParameter.orElse(0), sizeParameter.orElse(SIZE));
entryManager.applyMetadata(getCurrentUsername(), entries.getContent());
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
entries.getContent(), ignore);
return entries;
}
@ -128,7 +138,8 @@ public class EntryController extends BaseController {
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("asc") Optional<Boolean> ascParameter) {
@RequestParam("asc") Optional<Boolean> ascParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
if (sizeParameter.isPresent() && sizeParameter.get() > 100) {
sizeParameter = Optional.of(100);
@ -137,7 +148,9 @@ public class EntryController extends BaseController {
Page<Entry> entries = entryManager.fetchByUser(username,
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), ascParameter.orElse(false));
entryManager.applyMetadata(getCurrentUsername(), entries.getContent());
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
entries.getContent(), ignore);
return entries;
}
@ -149,14 +162,17 @@ public class EntryController extends BaseController {
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/entry/{id}")
public Entry getEntry(@PathVariable("id") Long id) {
public Entry getEntry(@PathVariable("id") Long id,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Entry entry = entryManager.get(id);
if (entry == null) {
throw new EntityResponseStatusException(HttpStatus.NOT_FOUND);
}
entryManager.applyMetadata(getCurrentUsername(), entry);
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
entry, ignore);
return entry;
}
@ -169,7 +185,8 @@ public class EntryController extends BaseController {
*/
@PreAuthorize("isAuthenticated()")
@PostMapping()
public Entry createEntry(@RequestBody Entry entry) {
public Entry createEntry(@RequestBody Entry entry,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
RequestBodyErrors bindingResult = new RequestBodyErrors(entry);
entryValidator.validate(entry, bindingResult);
@ -190,7 +207,9 @@ public class EntryController extends BaseController {
vote.setAuthor(getCurrentUsername());
voteManager.save(vote);
entryManager.applyMetadata(getCurrentUsername(), entry);
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
entry, ignore);
return entry;
}