comment period

This commit is contained in:
_Bastler 2021-10-06 18:58:26 +02:00
parent 321eb2865f
commit cdcdb64d60
13 changed files with 295 additions and 150 deletions

View File

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

View File

@ -42,49 +42,26 @@ public class CommentManager {
private QVote qVote = QVote.vote; private QVote qVote = QVote.vote;
private QEntry qEntry = QEntry.entry; 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<Comment> 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. * Fetch by date.
* *
* @param username the username
* @param target the target * @param target the target
* @param parent the parent * @param parent the parent
* @param date the date * @param date the date
* @param page the page * @param page the page
* @param size the size * @param size the size
* @param desc the desc * @param desc the desc
* @return the page * @return the page
*/ */
public Page<Comment> fetchByDate(Long target, Long parent, Instant date, int page, int size, public Page<Comment> fetchByDate(String username, Long target, Long parent, Instant date,
boolean desc) { int page, int size, boolean desc) {
Sort sort = Sort.by(desc ? Order.desc("created") : Order.asc("created")); Sort sort = Sort.by(desc ? Order.desc("created") : Order.asc("created"));
if (parent == null) { if (parent == null) {
return commentRepository return commentRepository.findAll(
.findAll( qComment.target.eq(target).and(qComment.parent.isNull())
qComment.target.eq(target).and(qComment.parent.isNull()) .and(qComment.created.before(date).or(qComment.author.eq(username))),
.and(qComment.created.before(date)), PageRequest.of(page, size, sort));
PageRequest.of(page, size, sort));
} }
return commentRepository.findAll(qComment.target.eq(target).and(qComment.parent.eq(parent)) return commentRepository.findAll(qComment.target.eq(target).and(qComment.parent.eq(parent))
@ -95,11 +72,11 @@ public class CommentManager {
* Fetch by username. * Fetch by username.
* *
* @param username the username * @param username the username
* @param orElse the or else * @param orElse the or else
* @param date the date * @param date the date
* @param page the page * @param page the page
* @param size the size * @param size the size
* @param asc the asc * @param asc the asc
* @return the page * @return the page
*/ */
public Page<Comment> fetchByUsername(String username, Long orElse, Instant date, int page, public Page<Comment> fetchByUsername(String username, Long orElse, Instant date, int page,
@ -139,8 +116,8 @@ public class CommentManager {
* Apply metadata. * Apply metadata.
* *
* @param username the username * @param username the username
* @param comment the comment * @param comment the comment
* @param ignore the ignore * @param ignore the ignore
*/ */
public void applyMetadata(String username, Comment comment, List<String> ignore) { public void applyMetadata(String username, Comment comment, List<String> ignore) {
@ -191,8 +168,8 @@ public class CommentManager {
* Apply metadata. * Apply metadata.
* *
* @param username the username * @param username the username
* @param entries the entries * @param entries the entries
* @param ignore the ignore * @param ignore the ignore
*/ */
public void applyMetadata(String username, List<Comment> entries, List<String> ignore) { public void applyMetadata(String username, List<Comment> entries, List<String> ignore) {
for (Comment comment : entries) { for (Comment comment : entries) {

View File

@ -7,7 +7,6 @@ import java.time.Instant;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; 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.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
@ -42,14 +41,13 @@ public class EntryManager {
private VoteRepository voteRepository; private VoteRepository voteRepository;
@Autowired @Autowired
private BookmarksManager bookmarksManager; private BookmarksManager bookmarksManager;
@Autowired
private SettingsManager settingsManager;
private QEntry qEntry = QEntry.entry; private QEntry qEntry = QEntry.entry;
private QVote qVote = QVote.vote; private QVote qVote = QVote.vote;
@Value("${bstly.board.unvoteThresh:10}")
private long UNVOTE_THRESH;
/** /**
* Fetch by ranking. * Fetch by ranking.
* *
@ -187,7 +185,7 @@ public class EntryManager {
entry.getMetadata().put("vote", true); entry.getMetadata().put("vote", true);
} }
if (!ignore.contains("downvote") && karma >= UNVOTE_THRESH) { if (!ignore.contains("downvote") && karma >= settingsManager.getUnvoteThresh()) {
entry.getMetadata().put("downvote", true); entry.getMetadata().put("downvote", true);
} }
} }

View File

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

View File

@ -54,9 +54,20 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
@Value("${admin.password:}") @Value("${admin.password:}")
private String adminPassword; 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) * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
*/ */
@ -89,6 +100,19 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
return userDetails; 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() * @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
*/ */
@ -207,7 +231,6 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
if (user.getUsername().equalsIgnoreCase(username) if (user.getUsername().equalsIgnoreCase(username)
&& !user.getMetadata().containsKey("self")) { && !user.getMetadata().containsKey("self")) {
user.getMetadata().put("self", true); user.getMetadata().put("self", true);
user.getMetadata().put("defaultGravity", GRAVITY);
} }
if (!user.getMetadata().containsKey("points")) { if (!user.getMetadata().containsKey("points")) {

View File

@ -4,10 +4,10 @@
package de.bstly.board.controller; package de.bstly.board.controller;
import org.springframework.beans.factory.annotation.Autowired; 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.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import de.bstly.board.businesslogic.SettingsManager;
import de.bstly.board.businesslogic.UserManager; import de.bstly.board.businesslogic.UserManager;
import de.bstly.board.model.LocalUser; import de.bstly.board.model.LocalUser;
import de.bstly.board.security.LocalUserDetails; import de.bstly.board.security.LocalUserDetails;
@ -19,9 +19,8 @@ public class BaseController {
@Autowired @Autowired
private UserManager localUserManager; private UserManager localUserManager;
@Autowired
@Value("${bstly.board.ranking.gravity:1.2}") private SettingsManager settingsManager;
private double GRAVITY;
/** /**
* Authenticated. * Authenticated.
@ -55,6 +54,27 @@ public class BaseController {
: null; : 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. * 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();
} }
} }

View File

@ -7,7 +7,6 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; 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.Page;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize; 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.BookmarksManager;
import de.bstly.board.businesslogic.EntryManager; import de.bstly.board.businesslogic.EntryManager;
import de.bstly.board.businesslogic.SettingsManager;
import de.bstly.board.businesslogic.UserManager; import de.bstly.board.businesslogic.UserManager;
import de.bstly.board.controller.support.EntityResponseStatusException; import de.bstly.board.controller.support.EntityResponseStatusException;
import de.bstly.board.model.Entry; import de.bstly.board.model.Entry;
@ -40,8 +40,8 @@ public class BookmarksController extends BaseController {
private EntryManager entryManager; private EntryManager entryManager;
@Autowired @Autowired
private UserManager userManager; private UserManager userManager;
@Value("${bstly.board.size:30}") @Autowired
private int SIZE; private SettingsManager settingsManager;
/** /**
* Gets the entries. * Gets the entries.
@ -62,7 +62,7 @@ public class BookmarksController extends BaseController {
} }
Page<Entry> entries = entryManager.fetchByBookmarks(getCurrentUsername(), Page<Entry> entries = entryManager.fetchByBookmarks(getCurrentUsername(),
pageParameter.orElse(0), sizeParameter.orElse(SIZE)); pageParameter.orElse(0), sizeParameter.orElse(settingsManager.getPageSize()));
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList()); List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()), entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),

View File

@ -4,15 +4,17 @@
package de.bstly.board.controller; package de.bstly.board.controller;
import java.time.Instant; import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; 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.Page;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize; 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.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; 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 com.google.common.collect.Lists;
import de.bstly.board.businesslogic.CommentManager; import de.bstly.board.businesslogic.CommentManager;
import de.bstly.board.businesslogic.SettingsManager;
import de.bstly.board.businesslogic.VoteManager; import de.bstly.board.businesslogic.VoteManager;
import de.bstly.board.controller.support.EntityResponseStatusException; import de.bstly.board.controller.support.EntityResponseStatusException;
import de.bstly.board.controller.support.RequestBodyErrors; import de.bstly.board.controller.support.RequestBodyErrors;
@ -45,36 +48,8 @@ public class CommentController extends BaseController {
private CommentValidator commentValidator; private CommentValidator commentValidator;
@Autowired @Autowired
private VoteManager voteManager; private VoteManager voteManager;
@Autowired
@Value("${bstly.board.size:30}") private SettingsManager settingsManager;
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<Comment> fetchByRank(@PathVariable("target") Long target,
@PathVariable("parent") Optional<Long> parent,
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("gravity") Optional<Double> gravityParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Page<Comment> comments = fetchByDate(target, parent, pageParameter, sizeParameter,
dateParameter, Optional.of(false), ignoreParameter);
return comments;
}
/** /**
* Fetch by date. * Fetch by date.
@ -97,9 +72,9 @@ public class CommentController extends BaseController {
@RequestParam("date") Optional<Instant> dateParameter, @RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("desc") Optional<Boolean> descParameter, @RequestParam("desc") Optional<Boolean> descParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) { @RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Page<Comment> comments = commentManager.fetchByDate(target, parent.orElse(null), Page<Comment> comments = commentManager.fetchByDate(getCurrentUsername(), target,
dateParameter.orElse(Instant.now()), pageParameter.orElse(0), parent.orElse(null), dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), descParameter.orElse(false)); sizeParameter.orElse(settingsManager.getPageSize()), descParameter.orElse(false));
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList("entry")); List<String> ignore = ignoreParameter.orElse(Lists.newArrayList("entry"));
commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore); commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore);
@ -129,7 +104,7 @@ public class CommentController extends BaseController {
@RequestParam("ignore") Optional<List<String>> ignoreParameter) { @RequestParam("ignore") Optional<List<String>> ignoreParameter) {
Page<Comment> comments = commentManager.fetchByUsername(username, parent.orElse(null), Page<Comment> comments = commentManager.fetchByUsername(username, parent.orElse(null),
dateParameter.orElse(Instant.now()), pageParameter.orElse(0), dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), ascParameter.orElse(false)); sizeParameter.orElse(settingsManager.getPageSize()), ascParameter.orElse(false));
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList()); List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore); commentManager.applyMetadata(getCurrentUsername(), comments.getContent(), ignore);
return comments; return comments;
@ -190,7 +165,7 @@ public class CommentController extends BaseController {
HttpStatus.UNPROCESSABLE_ENTITY); HttpStatus.UNPROCESSABLE_ENTITY);
} }
comment.setCreated(Instant.now()); comment.setCreated(Instant.now().plus(getCommentDelay(), ChronoUnit.MINUTES));
comment.setAuthor(getCurrentUsername()); comment.setAuthor(getCurrentUsername());
comment.setText(comment.getText().trim()); comment.setText(comment.getText().trim());
comment = commentManager.save(comment); comment = commentManager.save(comment);
@ -208,4 +183,53 @@ public class CommentController extends BaseController {
return comment; 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<List<String>> 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<String> 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);
}
} }

View File

@ -14,7 +14,6 @@ import java.util.Scanner;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; 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.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -30,6 +29,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import de.bstly.board.businesslogic.EntryManager; import de.bstly.board.businesslogic.EntryManager;
import de.bstly.board.businesslogic.SettingsManager;
import de.bstly.board.businesslogic.UserManager; import de.bstly.board.businesslogic.UserManager;
import de.bstly.board.businesslogic.VoteManager; import de.bstly.board.businesslogic.VoteManager;
import de.bstly.board.controller.support.EntityResponseStatusException; import de.bstly.board.controller.support.EntityResponseStatusException;
@ -57,18 +57,17 @@ public class EntryController extends BaseController {
private EntryValidator entryValidator; private EntryValidator entryValidator;
@Autowired @Autowired
private VoteManager voteManager; private VoteManager voteManager;
@Autowired
@Value("${bstly.board.size:30}") private SettingsManager settingsManager;
private int SIZE;
/** /**
* Fetch by ranking. * Fetch by ranking.
* *
* @param pageParameter the page parameter * @param pageParameter the page parameter
* @param sizeParameter the size parameter * @param sizeParameter the size parameter
* @param dateParameter the date parameter * @param dateParameter the date parameter
* @param gravityParameter the gravity parameter * @param gravityParameter the gravity parameter
* @param ignoreParameter the ignore parameter * @param ignoreParameter the ignore parameter
* @return the page * @return the page
*/ */
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@ -85,7 +84,7 @@ public class EntryController extends BaseController {
Page<RankedEntry> entries = entryManager.fetchByRanking(dateParameter.orElse(Instant.now()), Page<RankedEntry> entries = entryManager.fetchByRanking(dateParameter.orElse(Instant.now()),
gravityParameter.orElse(getGravity()), pageParameter.orElse(0), gravityParameter.orElse(getGravity()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE)); sizeParameter.orElse(settingsManager.getPageSize()));
Page<Entry> transformed = new PageImpl<Entry>( Page<Entry> transformed = new PageImpl<Entry>(
entries.getContent().stream().map(rankedEntry -> { entries.getContent().stream().map(rankedEntry -> {
@ -103,9 +102,9 @@ public class EntryController extends BaseController {
/** /**
* Fetch by date. * Fetch by date.
* *
* @param pageParameter the page parameter * @param pageParameter the page parameter
* @param sizeParameter the size parameter * @param sizeParameter the size parameter
* @param dateParameter the date parameter * @param dateParameter the date parameter
* @param ignoreParameter the ignore parameter * @param ignoreParameter the ignore parameter
* @return the page * @return the page
*/ */
@ -121,7 +120,7 @@ public class EntryController extends BaseController {
} }
Page<Entry> entries = entryManager.fetchByDate(dateParameter.orElse(Instant.now()), Page<Entry> entries = entryManager.fetchByDate(dateParameter.orElse(Instant.now()),
pageParameter.orElse(0), sizeParameter.orElse(SIZE)); pageParameter.orElse(0), sizeParameter.orElse(settingsManager.getPageSize()));
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList()); List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()), entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
entries.getContent(), ignore); entries.getContent(), ignore);
@ -131,11 +130,11 @@ public class EntryController extends BaseController {
/** /**
* Fetch by comments. * Fetch by comments.
* *
* @param pageParameter the page parameter * @param pageParameter the page parameter
* @param sizeParameter the size parameter * @param sizeParameter the size parameter
* @param dateParameter the date parameter * @param dateParameter the date parameter
* @param gravityParameter the gravity parameter * @param gravityParameter the gravity parameter
* @param ignoreParameter the ignore parameter * @param ignoreParameter the ignore parameter
* @return the page * @return the page
*/ */
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@ -152,7 +151,7 @@ public class EntryController extends BaseController {
Page<RankedEntry> entries = entryManager.fetchByComments( Page<RankedEntry> entries = entryManager.fetchByComments(
dateParameter.orElse(Instant.now()), gravityParameter.orElse(getGravity()), dateParameter.orElse(Instant.now()), gravityParameter.orElse(getGravity()),
pageParameter.orElse(0), sizeParameter.orElse(SIZE)); pageParameter.orElse(0), sizeParameter.orElse(settingsManager.getPageSize()));
Page<Entry> transformed = new PageImpl<Entry>( Page<Entry> transformed = new PageImpl<Entry>(
entries.getContent().stream().map(rankedEntry -> Entry.fromRankedEntry(rankedEntry)) entries.getContent().stream().map(rankedEntry -> Entry.fromRankedEntry(rankedEntry))
@ -168,9 +167,9 @@ public class EntryController extends BaseController {
/** /**
* Fetch by last. * Fetch by last.
* *
* @param pageParameter the page parameter * @param pageParameter the page parameter
* @param sizeParameter the size parameter * @param sizeParameter the size parameter
* @param dateParameter the date parameter * @param dateParameter the date parameter
* @param ignoreParameter the ignore parameter * @param ignoreParameter the ignore parameter
* @return the page * @return the page
*/ */
@ -186,7 +185,7 @@ public class EntryController extends BaseController {
} }
Page<Entry> entries = entryManager.fetchByLastComment(dateParameter.orElse(Instant.now()), Page<Entry> entries = entryManager.fetchByLastComment(dateParameter.orElse(Instant.now()),
pageParameter.orElse(0), sizeParameter.orElse(SIZE)); pageParameter.orElse(0), sizeParameter.orElse(settingsManager.getPageSize()));
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList()); List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()), entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
@ -197,11 +196,11 @@ public class EntryController extends BaseController {
/** /**
* Fetch by user. * Fetch by user.
* *
* @param username the username * @param username the username
* @param pageParameter the page parameter * @param pageParameter the page parameter
* @param sizeParameter the size parameter * @param sizeParameter the size parameter
* @param dateParameter the date parameter * @param dateParameter the date parameter
* @param ascParameter the asc parameter * @param ascParameter the asc parameter
* @param ignoreParameter the ignore parameter * @param ignoreParameter the ignore parameter
* @return the page * @return the page
*/ */
@ -220,7 +219,7 @@ public class EntryController extends BaseController {
Page<Entry> entries = entryManager.fetchByUser(username, Page<Entry> entries = entryManager.fetchByUser(username,
dateParameter.orElse(Instant.now()), pageParameter.orElse(0), dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), ascParameter.orElse(false)); sizeParameter.orElse(settingsManager.getPageSize()), ascParameter.orElse(false));
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList()); List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()), entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
entries.getContent(), ignore); entries.getContent(), ignore);
@ -230,7 +229,7 @@ public class EntryController extends BaseController {
/** /**
* Gets the entry. * Gets the entry.
* *
* @param id the id * @param id the id
* @param ignoreParameter the ignore parameter * @param ignoreParameter the ignore parameter
* @return the entry * @return the entry
*/ */
@ -254,7 +253,7 @@ public class EntryController extends BaseController {
/** /**
* Creates the entry. * Creates the entry.
* *
* @param entry the entry * @param entry the entry
* @param ignoreParameter the ignore parameter * @param ignoreParameter the ignore parameter
* @return the entry * @return the entry
*/ */

View File

@ -31,12 +31,8 @@ public class ModerationController {
@Autowired @Autowired
private CommentManager commentManager; private CommentManager commentManager;
@Autowired @Autowired
private EntryManager entryManager; private EntryManager entryManager;
@Autowired @Autowired
private UserManager userManager; private UserManager userManager;

View File

@ -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<String, Object> getDefaultSettings() {
Map<String, Object> 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;
}
}

View File

@ -4,7 +4,6 @@
package de.bstly.board.controller; package de.bstly.board.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping; 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.CommentManager;
import de.bstly.board.businesslogic.EntryManager; import de.bstly.board.businesslogic.EntryManager;
import de.bstly.board.businesslogic.SettingsManager;
import de.bstly.board.businesslogic.UserManager; import de.bstly.board.businesslogic.UserManager;
import de.bstly.board.businesslogic.VoteManager; import de.bstly.board.businesslogic.VoteManager;
import de.bstly.board.controller.support.EntityResponseStatusException; import de.bstly.board.controller.support.EntityResponseStatusException;
@ -38,9 +38,8 @@ public class VoteController extends BaseController {
private CommentManager commentManager; private CommentManager commentManager;
@Autowired @Autowired
private UserManager userManager; private UserManager userManager;
@Autowired
@Value("${bstly.board.unvoteThresh:10}") private SettingsManager settingsManager;
private long UNVOTE_THRESH;
/** /**
* Gets the entry points. * Gets the entry points.
@ -96,7 +95,7 @@ public class VoteController extends BaseController {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY); throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
} }
if (userManager.getKarma(getCurrentUsername()) < UNVOTE_THRESH) { if (userManager.getKarma(getCurrentUsername()) < settingsManager.getUnvoteThresh()) {
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN); throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
} }

View File

@ -20,7 +20,6 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
/** /**
* The Class Comment. * The Class Comment.
*/ */
@ -29,34 +28,21 @@ import com.google.common.collect.Maps;
@EntityListeners({ AuditingEntityListener.class }) @EntityListeners({ AuditingEntityListener.class })
public class Comment { public class Comment {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false) @Column(name = "id", nullable = false)
private Long id; private Long id;
@Column(name = "author", nullable = false) @Column(name = "author", nullable = false)
private String author; private String author;
@Column(name = "created", nullable = false) @Column(name = "created", nullable = false)
private Instant created; private Instant created;
@Column(name = "target", nullable = false) @Column(name = "target", nullable = false)
private Long target; private Long target;
@Column(name = "parent", nullable = true) @Column(name = "parent", nullable = true)
private Long parent; private Long parent;
@Lob @Lob
@Column(name = "text", nullable = false) @Column(name = "text", nullable = false)
private String text; private String text;
@Transient @Transient
private Map<String, Object> metadata; private Map<String, Object> metadata;