diff --git a/pom.xml b/pom.xml index f64f930..99f8f4a 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ UTF-8 11 - 0.4.2-SNAPSHOT + 0.4.3-SNAPSHOT diff --git a/src/main/java/de/bstly/board/businesslogic/CommentManager.java b/src/main/java/de/bstly/board/businesslogic/CommentManager.java index 2563504..53ff11c 100644 --- a/src/main/java/de/bstly/board/businesslogic/CommentManager.java +++ b/src/main/java/de/bstly/board/businesslogic/CommentManager.java @@ -42,49 +42,26 @@ public class CommentManager { private QVote qVote = QVote.vote; private QEntry qEntry = QEntry.entry; - /** - * Fetch by ranking. - * - * @param target the target - * @param parent the parent - * @param date the date - * @param gravity the gravity - * @param page the page - * @param size the size - * @return the page - */ - - public Page fetchByRanking(Long target, Long parent, Instant date, double gravity, - int page, int size) { - if (parent == null) { - return commentRepository.findAllByRankingAndParent(target, date, gravity, - PageRequest.of(page, size)); - } - - return commentRepository.findAllByRankingAndParent(target, parent, date, gravity, - PageRequest.of(page, size)); - } - /** * 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 date the date + * @param page the page + * @param size the size + * @param desc the desc * @return the page */ - public Page fetchByDate(Long target, Long parent, Instant date, int page, int size, - boolean desc) { + public Page 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)), - PageRequest.of(page, size, sort)); + 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.eq(parent)) @@ -95,11 +72,11 @@ public class CommentManager { * 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 orElse the or else + * @param date the date + * @param page the page + * @param size the size + * @param asc the asc * @return the page */ public Page fetchByUsername(String username, Long orElse, Instant date, int page, @@ -139,8 +116,8 @@ public class CommentManager { * 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 ignore) { @@ -191,8 +168,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 entries, List ignore) { for (Comment comment : entries) { diff --git a/src/main/java/de/bstly/board/businesslogic/EntryManager.java b/src/main/java/de/bstly/board/businesslogic/EntryManager.java index 860953d..82ae37d 100644 --- a/src/main/java/de/bstly/board/businesslogic/EntryManager.java +++ b/src/main/java/de/bstly/board/businesslogic/EntryManager.java @@ -7,7 +7,6 @@ import java.time.Instant; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; @@ -42,14 +41,13 @@ public class EntryManager { private VoteRepository voteRepository; @Autowired private BookmarksManager bookmarksManager; + @Autowired + private SettingsManager settingsManager; private QEntry qEntry = QEntry.entry; private QVote qVote = QVote.vote; - @Value("${bstly.board.unvoteThresh:10}") - private long UNVOTE_THRESH; - /** * Fetch by ranking. * @@ -187,7 +185,7 @@ public class EntryManager { entry.getMetadata().put("vote", true); } - if (!ignore.contains("downvote") && karma >= UNVOTE_THRESH) { + if (!ignore.contains("downvote") && karma >= settingsManager.getUnvoteThresh()) { entry.getMetadata().put("downvote", true); } } diff --git a/src/main/java/de/bstly/board/businesslogic/SettingsManager.java b/src/main/java/de/bstly/board/businesslogic/SettingsManager.java new file mode 100644 index 0000000..8d22785 --- /dev/null +++ b/src/main/java/de/bstly/board/businesslogic/SettingsManager.java @@ -0,0 +1,59 @@ +/** + * + */ +package de.bstly.board.businesslogic; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * The Class SettingsManager. + */ +@Component +public class SettingsManager { + + @Value("${bstly.board.ranking.gravity:1.2}") + private double GRAVITY; + @Value("${bstly.board.size:30}") + private int SIZE; + @Value("${bstly.board.comment.changePeriod:1}") + private long COMMENT_CHANGE_PERIDO; + @Value("${bstly.board.unvoteThresh:10}") + private long UNVOTE_THRESH; + + /** + * Gets the gravity. + * + * @return the gravity + */ + public double getGravity() { + return GRAVITY; + } + + /** + * Gets the page size. + * + * @return the page size + */ + public int getPageSize() { + return SIZE; + } + + /** + * Gets the comment delay. + * + * @return the comment delay + */ + public long getCommentDelay() { + return COMMENT_CHANGE_PERIDO; + } + + /** + * Gets the unvote thresh. + * + * @return the unvote thresh + */ + public long getUnvoteThresh() { + return UNVOTE_THRESH; + } +} diff --git a/src/main/java/de/bstly/board/businesslogic/UserManager.java b/src/main/java/de/bstly/board/businesslogic/UserManager.java index 99b3c55..943aff4 100644 --- a/src/main/java/de/bstly/board/businesslogic/UserManager.java +++ b/src/main/java/de/bstly/board/businesslogic/UserManager.java @@ -54,9 +54,20 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet @Value("${admin.password:}") private String adminPassword; - @Value("${bstly.board.ranking.gravity:1.2}") - private double GRAVITY; + /* + * @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) */ @@ -89,6 +100,19 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet return userDetails; } + /* + * @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() */ @@ -207,7 +231,6 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet if (user.getUsername().equalsIgnoreCase(username) && !user.getMetadata().containsKey("self")) { user.getMetadata().put("self", true); - user.getMetadata().put("defaultGravity", GRAVITY); } if (!user.getMetadata().containsKey("points")) { diff --git a/src/main/java/de/bstly/board/controller/BaseController.java b/src/main/java/de/bstly/board/controller/BaseController.java index ed69071..f75384b 100644 --- a/src/main/java/de/bstly/board/controller/BaseController.java +++ b/src/main/java/de/bstly/board/controller/BaseController.java @@ -4,10 +4,10 @@ package de.bstly.board.controller; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; +import de.bstly.board.businesslogic.SettingsManager; import de.bstly.board.businesslogic.UserManager; import de.bstly.board.model.LocalUser; import de.bstly.board.security.LocalUserDetails; @@ -19,9 +19,8 @@ public class BaseController { @Autowired private UserManager localUserManager; - - @Value("${bstly.board.ranking.gravity:1.2}") - private double GRAVITY; + @Autowired + private SettingsManager settingsManager; /** * Authenticated. @@ -55,6 +54,27 @@ public class BaseController { : null; } + /** + * Gets the page size. + * + * @return the page size + */ + protected int getPageSize() { + String username = getCurrentUsername(); + if (username != null) { + LocalUser localUser = localUserManager.getByUsername(username); + if (localUser.getSettings() != null + && localUser.getSettings().containsKey("pageSize")) { + try { + return Integer.parseInt(localUser.getSettings().get("pageSize")); + } catch (Exception e) { + } + } + } + + return settingsManager.getPageSize(); + } + /** * Gets the gravity. * @@ -71,7 +91,27 @@ public class BaseController { } } } - return GRAVITY; + return settingsManager.getGravity(); + } + + /** + * Gets the comment delay. + * + * @return the comment delay + */ + protected long getCommentDelay() { + String username = getCurrentUsername(); + if (username != null) { + LocalUser localUser = localUserManager.getByUsername(username); + if (localUser.getSettings() != null + && localUser.getSettings().containsKey("commentDelay")) { + try { + return Long.parseLong(localUser.getSettings().get("commentDelay")); + } catch (Exception e) { + } + } + } + return settingsManager.getCommentDelay(); } } diff --git a/src/main/java/de/bstly/board/controller/BookmarksController.java b/src/main/java/de/bstly/board/controller/BookmarksController.java index 4509521..ce40801 100644 --- a/src/main/java/de/bstly/board/controller/BookmarksController.java +++ b/src/main/java/de/bstly/board/controller/BookmarksController.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.security.access.prepost.PreAuthorize; @@ -23,6 +22,7 @@ import com.google.common.collect.Lists; import de.bstly.board.businesslogic.BookmarksManager; import de.bstly.board.businesslogic.EntryManager; +import de.bstly.board.businesslogic.SettingsManager; import de.bstly.board.businesslogic.UserManager; import de.bstly.board.controller.support.EntityResponseStatusException; import de.bstly.board.model.Entry; @@ -40,8 +40,8 @@ public class BookmarksController extends BaseController { private EntryManager entryManager; @Autowired private UserManager userManager; - @Value("${bstly.board.size:30}") - private int SIZE; + @Autowired + private SettingsManager settingsManager; /** * Gets the entries. @@ -62,7 +62,7 @@ public class BookmarksController extends BaseController { } Page entries = entryManager.fetchByBookmarks(getCurrentUsername(), - pageParameter.orElse(0), sizeParameter.orElse(SIZE)); + pageParameter.orElse(0), sizeParameter.orElse(settingsManager.getPageSize())); List ignore = ignoreParameter.orElse(Lists.newArrayList()); entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()), diff --git a/src/main/java/de/bstly/board/controller/CommentController.java b/src/main/java/de/bstly/board/controller/CommentController.java index 972391b..e3cb934 100644 --- a/src/main/java/de/bstly/board/controller/CommentController.java +++ b/src/main/java/de/bstly/board/controller/CommentController.java @@ -4,15 +4,17 @@ package de.bstly.board.controller; import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -23,6 +25,7 @@ import org.springframework.web.bind.annotation.RestController; import com.google.common.collect.Lists; import de.bstly.board.businesslogic.CommentManager; +import de.bstly.board.businesslogic.SettingsManager; import de.bstly.board.businesslogic.VoteManager; import de.bstly.board.controller.support.EntityResponseStatusException; import de.bstly.board.controller.support.RequestBodyErrors; @@ -45,36 +48,8 @@ public class CommentController extends BaseController { private CommentValidator commentValidator; @Autowired private VoteManager voteManager; - - @Value("${bstly.board.size:30}") - private int SIZE; - - /** - * Fetch by rank. - * - * @param target the target - * @param parent the parent - * @param pageParameter the page parameter - * @param sizeParameter the size parameter - * @param dateParameter the date parameter - * @param gravityParameter the gravity parameter - * @param ignoreParameter the ignore parameter - * @return the page - */ - @PreAuthorize("isAuthenticated()") - @GetMapping({ "/{target}", "/{target}/{parent}" }) - public Page fetchByRank(@PathVariable("target") Long target, - @PathVariable("parent") Optional parent, - @RequestParam("page") Optional pageParameter, - @RequestParam("size") Optional sizeParameter, - @RequestParam("date") Optional dateParameter, - @RequestParam("gravity") Optional gravityParameter, - @RequestParam("ignore") Optional> ignoreParameter) { - - Page comments = fetchByDate(target, parent, pageParameter, sizeParameter, - dateParameter, Optional.of(false), ignoreParameter); - return comments; - } + @Autowired + private SettingsManager settingsManager; /** * Fetch by date. @@ -97,9 +72,9 @@ public class CommentController extends BaseController { @RequestParam("date") Optional dateParameter, @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)); + Page comments = commentManager.fetchByDate(getCurrentUsername(), target, + parent.orElse(null), dateParameter.orElse(Instant.now()), pageParameter.orElse(0), + sizeParameter.orElse(settingsManager.getPageSize()), descParameter.orElse(false)); List ignore = ignoreParameter.orElse(Lists.newArrayList("entry")); commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore); @@ -129,7 +104,7 @@ public class CommentController extends BaseController { @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)); + sizeParameter.orElse(settingsManager.getPageSize()), ascParameter.orElse(false)); List ignore = ignoreParameter.orElse(Lists.newArrayList()); commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore); return comments; @@ -190,7 +165,7 @@ public class CommentController extends BaseController { HttpStatus.UNPROCESSABLE_ENTITY); } - comment.setCreated(Instant.now()); + comment.setCreated(Instant.now().plus(getCommentDelay(), ChronoUnit.MINUTES)); comment.setAuthor(getCurrentUsername()); comment.setText(comment.getText().trim()); comment = commentManager.save(comment); @@ -208,4 +183,53 @@ public class CommentController extends BaseController { return comment; } + /** + * Update comment. + * + * @param comment the comment + * @param ignoreParameter the ignore parameter + * @return the comment + */ + @PreAuthorize("isAuthenticated()") + @PatchMapping + public Comment updateComment(@RequestBody Comment comment, + @RequestParam("ignore") Optional> ignoreParameter) { + Comment orgComment = commentManager.get(comment.getId()); + if (orgComment == null || !orgComment.getAuthor().equals(getCurrentUsername()) + || orgComment.getCreated().isBefore(Instant.now())) { + throw new EntityResponseStatusException(HttpStatus.FORBIDDEN); + } + + RequestBodyErrors bindingResult = new RequestBodyErrors(comment); + commentValidator.validate(comment, bindingResult); + + if (bindingResult.hasErrors()) { + throw new EntityResponseStatusException(bindingResult.getAllErrors(), + HttpStatus.UNPROCESSABLE_ENTITY); + } + + orgComment.setText(comment.getText()); + orgComment = commentManager.save(orgComment); + List ignore = ignoreParameter.orElse(Lists.newArrayList("entry")); + commentManager.applyMetadata(getCurrentUsername(), orgComment, ignore); + return comment; + } + + /** + * Detele comment. + * + * @param id the id + */ + @PreAuthorize("isAuthenticated()") + @DeleteMapping("{id}") + public void deteleComment(@PathVariable("id") Long id) { + Comment orgComment = commentManager.get(id); + if (orgComment == null || !orgComment.getAuthor().equals(getCurrentUsername()) + || orgComment.getCreated().isBefore(Instant.now())) { + throw new EntityResponseStatusException(HttpStatus.FORBIDDEN); + } + + commentManager.delete(orgComment); + } + } diff --git a/src/main/java/de/bstly/board/controller/EntryController.java b/src/main/java/de/bstly/board/controller/EntryController.java index e568499..0c369d0 100644 --- a/src/main/java/de/bstly/board/controller/EntryController.java +++ b/src/main/java/de/bstly/board/controller/EntryController.java @@ -14,7 +14,6 @@ import java.util.Scanner; import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.http.HttpStatus; @@ -30,6 +29,7 @@ import org.springframework.web.bind.annotation.RestController; import com.google.common.collect.Lists; import de.bstly.board.businesslogic.EntryManager; +import de.bstly.board.businesslogic.SettingsManager; import de.bstly.board.businesslogic.UserManager; import de.bstly.board.businesslogic.VoteManager; import de.bstly.board.controller.support.EntityResponseStatusException; @@ -57,18 +57,17 @@ public class EntryController extends BaseController { private EntryValidator entryValidator; @Autowired private VoteManager voteManager; - - @Value("${bstly.board.size:30}") - private int SIZE; + @Autowired + private SettingsManager settingsManager; /** * 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 - * @param ignoreParameter the ignore parameter + * @param ignoreParameter the ignore parameter * @return the page */ @PreAuthorize("isAuthenticated()") @@ -85,7 +84,7 @@ public class EntryController extends BaseController { Page entries = entryManager.fetchByRanking(dateParameter.orElse(Instant.now()), gravityParameter.orElse(getGravity()), pageParameter.orElse(0), - sizeParameter.orElse(SIZE)); + sizeParameter.orElse(settingsManager.getPageSize())); Page transformed = new PageImpl( entries.getContent().stream().map(rankedEntry -> { @@ -103,9 +102,9 @@ public class EntryController extends BaseController { /** * Fetch by date. * - * @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 ignoreParameter the ignore parameter * @return the page */ @@ -121,7 +120,7 @@ public class EntryController extends BaseController { } Page entries = entryManager.fetchByDate(dateParameter.orElse(Instant.now()), - pageParameter.orElse(0), sizeParameter.orElse(SIZE)); + pageParameter.orElse(0), sizeParameter.orElse(settingsManager.getPageSize())); List ignore = ignoreParameter.orElse(Lists.newArrayList()); entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()), entries.getContent(), ignore); @@ -131,11 +130,11 @@ public class EntryController extends BaseController { /** * Fetch by comments. * - * @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 - * @param ignoreParameter the ignore parameter + * @param ignoreParameter the ignore parameter * @return the page */ @PreAuthorize("isAuthenticated()") @@ -152,7 +151,7 @@ public class EntryController extends BaseController { Page entries = entryManager.fetchByComments( dateParameter.orElse(Instant.now()), gravityParameter.orElse(getGravity()), - pageParameter.orElse(0), sizeParameter.orElse(SIZE)); + pageParameter.orElse(0), sizeParameter.orElse(settingsManager.getPageSize())); Page transformed = new PageImpl( entries.getContent().stream().map(rankedEntry -> Entry.fromRankedEntry(rankedEntry)) @@ -168,9 +167,9 @@ public class EntryController extends BaseController { /** * Fetch by last. * - * @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 ignoreParameter the ignore parameter * @return the page */ @@ -186,7 +185,7 @@ public class EntryController extends BaseController { } Page entries = entryManager.fetchByLastComment(dateParameter.orElse(Instant.now()), - pageParameter.orElse(0), sizeParameter.orElse(SIZE)); + pageParameter.orElse(0), sizeParameter.orElse(settingsManager.getPageSize())); List ignore = ignoreParameter.orElse(Lists.newArrayList()); entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()), @@ -197,11 +196,11 @@ public class EntryController extends BaseController { /** * Fetch by user. * - * @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 username the username + * @param pageParameter the page parameter + * @param sizeParameter the size parameter + * @param dateParameter the date parameter + * @param ascParameter the asc parameter * @param ignoreParameter the ignore parameter * @return the page */ @@ -220,7 +219,7 @@ public class EntryController extends BaseController { Page entries = entryManager.fetchByUser(username, dateParameter.orElse(Instant.now()), pageParameter.orElse(0), - sizeParameter.orElse(SIZE), ascParameter.orElse(false)); + sizeParameter.orElse(settingsManager.getPageSize()), ascParameter.orElse(false)); List ignore = ignoreParameter.orElse(Lists.newArrayList()); entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()), entries.getContent(), ignore); @@ -230,7 +229,7 @@ public class EntryController extends BaseController { /** * Gets the entry. * - * @param id the id + * @param id the id * @param ignoreParameter the ignore parameter * @return the entry */ @@ -254,7 +253,7 @@ public class EntryController extends BaseController { /** * Creates the entry. * - * @param entry the entry + * @param entry the entry * @param ignoreParameter the ignore parameter * @return the entry */ diff --git a/src/main/java/de/bstly/board/controller/ModerationController.java b/src/main/java/de/bstly/board/controller/ModerationController.java index 2023af0..4e564fb 100644 --- a/src/main/java/de/bstly/board/controller/ModerationController.java +++ b/src/main/java/de/bstly/board/controller/ModerationController.java @@ -31,12 +31,8 @@ public class ModerationController { @Autowired private CommentManager commentManager; - - @Autowired private EntryManager entryManager; - - @Autowired private UserManager userManager; diff --git a/src/main/java/de/bstly/board/controller/SettingsController.java b/src/main/java/de/bstly/board/controller/SettingsController.java new file mode 100644 index 0000000..4a580e5 --- /dev/null +++ b/src/main/java/de/bstly/board/controller/SettingsController.java @@ -0,0 +1,44 @@ +/** + * + */ +package de.bstly.board.controller; + +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.google.common.collect.Maps; + +import de.bstly.board.businesslogic.SettingsManager; + +/** + * The Class SettingsController. + */ +@RestController +@RequestMapping("/settings") +public class SettingsController extends BaseController { + + @Autowired + private SettingsManager settingsManager; + + /** + * Gets the default settings. + * + * @return the default settings + */ + @GetMapping + public Map getDefaultSettings() { + Map settings = Maps.newHashMap(); + settings.put("gravity", getGravity()); + settings.put("pageSize", getPageSize()); + settings.put("commentDelay", getCommentDelay()); + settings.put("defaultGravity", settingsManager.getGravity()); + settings.put("defaultPageSize", settingsManager.getPageSize()); + settings.put("defaultCommentDelay", settingsManager.getCommentDelay()); + return settings; + } + +} diff --git a/src/main/java/de/bstly/board/controller/VoteController.java b/src/main/java/de/bstly/board/controller/VoteController.java index ce06841..8c25439 100644 --- a/src/main/java/de/bstly/board/controller/VoteController.java +++ b/src/main/java/de/bstly/board/controller/VoteController.java @@ -4,7 +4,6 @@ package de.bstly.board.controller; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.DeleteMapping; @@ -16,6 +15,7 @@ import org.springframework.web.bind.annotation.RestController; import de.bstly.board.businesslogic.CommentManager; import de.bstly.board.businesslogic.EntryManager; +import de.bstly.board.businesslogic.SettingsManager; import de.bstly.board.businesslogic.UserManager; import de.bstly.board.businesslogic.VoteManager; import de.bstly.board.controller.support.EntityResponseStatusException; @@ -38,9 +38,8 @@ public class VoteController extends BaseController { private CommentManager commentManager; @Autowired private UserManager userManager; - - @Value("${bstly.board.unvoteThresh:10}") - private long UNVOTE_THRESH; + @Autowired + private SettingsManager settingsManager; /** * Gets the entry points. @@ -96,7 +95,7 @@ public class VoteController extends BaseController { throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); } - if (userManager.getKarma(getCurrentUsername()) < UNVOTE_THRESH) { + if (userManager.getKarma(getCurrentUsername()) < settingsManager.getUnvoteThresh()) { throw new EntityResponseStatusException(HttpStatus.FORBIDDEN); } diff --git a/src/main/java/de/bstly/board/model/Comment.java b/src/main/java/de/bstly/board/model/Comment.java index b58e46c..9585cc1 100644 --- a/src/main/java/de/bstly/board/model/Comment.java +++ b/src/main/java/de/bstly/board/model/Comment.java @@ -20,7 +20,6 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener; import com.google.common.collect.Maps; - /** * The Class Comment. */ @@ -29,34 +28,21 @@ import com.google.common.collect.Maps; @EntityListeners({ AuditingEntityListener.class }) public class Comment { - @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", nullable = false) private Long id; - - @Column(name = "author", nullable = false) private String author; - - @Column(name = "created", nullable = false) private Instant created; - - @Column(name = "target", nullable = false) private Long target; - - @Column(name = "parent", nullable = true) private Long parent; - - @Lob @Column(name = "text", nullable = false) private String text; - - @Transient private Map metadata;