diff --git a/pom.xml b/pom.xml index 2acdf7e..91a30c9 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ UTF-8 11 - 0.3.3-SNAPSHOT + 0.3.4-SNAPSHOT diff --git a/src/main/java/de/bstly/board/businesslogic/CommentManager.java b/src/main/java/de/bstly/board/businesslogic/CommentManager.java index c846768..d89cb47 100644 --- a/src/main/java/de/bstly/board/businesslogic/CommentManager.java +++ b/src/main/java/de/bstly/board/businesslogic/CommentManager.java @@ -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)); diff --git a/src/main/java/de/bstly/board/businesslogic/EntryManager.java b/src/main/java/de/bstly/board/businesslogic/EntryManager.java index 676afee..f7039fa 100644 --- a/src/main/java/de/bstly/board/businesslogic/EntryManager.java +++ b/src/main/java/de/bstly/board/businesslogic/EntryManager.java @@ -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 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 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 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 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 entries) { + public void applyMetadata(String username, long karma, List entries, + List 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 */ diff --git a/src/main/java/de/bstly/board/businesslogic/UserManager.java b/src/main/java/de/bstly/board/businesslogic/UserManager.java index 9620991..b1f8ade 100644 --- a/src/main/java/de/bstly/board/businesslogic/UserManager.java +++ b/src/main/java/de/bstly/board/businesslogic/UserManager.java @@ -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. * diff --git a/src/main/java/de/bstly/board/controller/BookmarksController.java b/src/main/java/de/bstly/board/controller/BookmarksController.java index fc8ecf7..f4dfe23 100644 --- a/src/main/java/de/bstly/board/controller/BookmarksController.java +++ b/src/main/java/de/bstly/board/controller/BookmarksController.java @@ -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 getEntries(@RequestParam("page") Optional pageParameter, - @RequestParam("size") Optional sizeParameter) { + @RequestParam("size") Optional sizeParameter, + @RequestParam("ignore") Optional> ignoreParameter) { if (sizeParameter.isPresent() && sizeParameter.get() > 100) { sizeParameter = Optional.of(100); @@ -56,7 +63,9 @@ public class BookmarksController extends BaseController { Page entries = entryManager.fetchByBookmarks(getCurrentUsername(), pageParameter.orElse(0), sizeParameter.orElse(SIZE)); - entryManager.applyMetadata(getCurrentUsername(), entries.getContent()); + List ignore = ignoreParameter.orElse(Lists.newArrayList()); + entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()), + entries.getContent(), ignore); return entries; } diff --git a/src/main/java/de/bstly/board/controller/CommentController.java b/src/main/java/de/bstly/board/controller/CommentController.java index 9ee8d4f..e051a4c 100644 --- a/src/main/java/de/bstly/board/controller/CommentController.java +++ b/src/main/java/de/bstly/board/controller/CommentController.java @@ -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 pageParameter, @RequestParam("size") Optional sizeParameter, @RequestParam("date") Optional dateParameter, - @RequestParam("gravity") Optional gravityParameter) { + @RequestParam("gravity") Optional gravityParameter, + @RequestParam("ignore") Optional> ignoreParameter) { Page comments = fetchByDate(target, parent, pageParameter, sizeParameter, - dateParameter, Optional.of(false)); - List 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 pageParameter, @RequestParam("size") Optional sizeParameter, @RequestParam("date") Optional dateParameter, - @RequestParam("desc") Optional descParameter) { + @RequestParam("desc") Optional descParameter, + @RequestParam("ignore") Optional> ignoreParameter) { Page comments = commentManager.fetchByDate(target, parent.orElse(null), dateParameter.orElse(Instant.now()), pageParameter.orElse(0), sizeParameter.orElse(SIZE), descParameter.orElse(false)); - List ignore = Lists.newArrayList("entry"); + + List 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 pageParameter, @RequestParam("size") Optional sizeParameter, @RequestParam("date") Optional dateParameter, - @RequestParam("asc") Optional ascParameter) { + @RequestParam("asc") Optional ascParameter, + @RequestParam("ignore") Optional> ignoreParameter) { Page 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 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> ignoreParameter) { Comment comment = commentManager.get(id); if (comment == null) { throw new EntityResponseStatusException(HttpStatus.NOT_FOUND); } - commentManager.applyMetadata(getCurrentUsername(), comment, Lists.newArrayList()); - + List 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> 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 ignore = Lists.newArrayList("entry"); + List ignore = ignoreParameter.orElse(Lists.newArrayList("entry")); commentManager.applyMetadata(getCurrentUsername(), comment, ignore); return comment; diff --git a/src/main/java/de/bstly/board/controller/EntryController.java b/src/main/java/de/bstly/board/controller/EntryController.java index 76fd37e..8d1ab0d 100644 --- a/src/main/java/de/bstly/board/controller/EntryController.java +++ b/src/main/java/de/bstly/board/controller/EntryController.java @@ -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,25 +46,25 @@ 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; /** * Fetch by ranking. * - * @param pageParameter the page parameter - * @param sizeParameter the size parameter - * @param dateParameter the date parameter + * @param pageParameter the page parameter + * @param sizeParameter the size parameter + * @param dateParameter the date parameter * @param gravityParameter the gravity parameter * @return the page */ @@ -69,7 +73,8 @@ public class EntryController extends BaseController { public Page fetchByRanking(@RequestParam("page") Optional pageParameter, @RequestParam("size") Optional sizeParameter, @RequestParam("date") Optional dateParameter, - @RequestParam("gravity") Optional gravityParameter) { + @RequestParam("gravity") Optional gravityParameter, + @RequestParam("ignore") Optional> 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 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 fetchByDate(@RequestParam("page") Optional pageParameter, @RequestParam("size") Optional sizeParameter, - @RequestParam("date") Optional dateParameter) { + @RequestParam("date") Optional dateParameter, + @RequestParam("ignore") Optional> ignoreParameter) { if (sizeParameter.isPresent() && sizeParameter.get() > 100) { sizeParameter = Optional.of(100); @@ -108,18 +116,20 @@ public class EntryController extends BaseController { Page entries = entryManager.fetchByDate(dateParameter.orElse(Instant.now()), pageParameter.orElse(0), sizeParameter.orElse(SIZE)); - entryManager.applyMetadata(getCurrentUsername(), entries.getContent()); + List ignore = ignoreParameter.orElse(Lists.newArrayList()); + entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()), + entries.getContent(), ignore); return entries; } /** * Fetch by user. * - * @param username the username + * @param username the username * @param pageParameter the page parameter * @param sizeParameter the size parameter * @param dateParameter the date parameter - * @param ascParameter the asc parameter + * @param ascParameter the asc parameter * @return the page */ @PreAuthorize("isAuthenticated()") @@ -128,7 +138,8 @@ public class EntryController extends BaseController { @RequestParam("page") Optional pageParameter, @RequestParam("size") Optional sizeParameter, @RequestParam("date") Optional dateParameter, - @RequestParam("asc") Optional ascParameter) { + @RequestParam("asc") Optional ascParameter, + @RequestParam("ignore") Optional> ignoreParameter) { if (sizeParameter.isPresent() && sizeParameter.get() > 100) { sizeParameter = Optional.of(100); @@ -137,7 +148,9 @@ public class EntryController extends BaseController { Page entries = entryManager.fetchByUser(username, dateParameter.orElse(Instant.now()), pageParameter.orElse(0), sizeParameter.orElse(SIZE), ascParameter.orElse(false)); - entryManager.applyMetadata(getCurrentUsername(), entries.getContent()); + List 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> ignoreParameter) { Entry entry = entryManager.get(id); if (entry == null) { throw new EntityResponseStatusException(HttpStatus.NOT_FOUND); } - entryManager.applyMetadata(getCurrentUsername(), entry); + List 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> 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 ignore = ignoreParameter.orElse(Lists.newArrayList()); + entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()), + entry, ignore); return entry; }