fixes+improvements
This commit is contained in:
@@ -182,4 +182,28 @@ public class CommentManager {
|
|||||||
return upvotes - downvotes;
|
return upvotes - downvotes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param comment
|
||||||
|
*/
|
||||||
|
public void delete(Comment comment) {
|
||||||
|
for (Comment subcomment : commentRepository.findAll(qComment.parent.eq(comment.getId()))) {
|
||||||
|
delete(subcomment);
|
||||||
|
}
|
||||||
|
|
||||||
|
voteManager.deleteByTarget(comment.getId(), Types.comment);
|
||||||
|
|
||||||
|
commentRepository.delete(comment);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
*/
|
||||||
|
public void deleteByTarget(Long target) {
|
||||||
|
for (Comment comment : commentRepository.findAll(qComment.target.eq(target))) {
|
||||||
|
delete(comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -142,6 +142,16 @@ public class EntryManager {
|
|||||||
return entryRepository.save(entry);
|
return entryRepository.save(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param entry
|
||||||
|
*/
|
||||||
|
public void delete(Entry entry) {
|
||||||
|
commentManager.deleteByTarget(entry.getId());
|
||||||
|
voteManager.deleteByTarget(entry.getId(), Types.entry);
|
||||||
|
entryRepository.delete(entry);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param entryId
|
* @param entryId
|
||||||
|
|||||||
@@ -52,6 +52,17 @@ public class VoteManager {
|
|||||||
voteRepository.delete(vote);
|
voteRepository.delete(vote);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param vote
|
||||||
|
*/
|
||||||
|
public void deleteByTarget(Long target, Types targetType) {
|
||||||
|
for (Vote vote : voteRepository
|
||||||
|
.findAll(qVote.target.eq(target).and(qVote.targetType.eq(targetType)))) {
|
||||||
|
delete(vote);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param target
|
* @param target
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.apache.logging.log4j.LogManager;
|
|||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -87,6 +88,7 @@ public class DebugController extends BaseController {
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||||
@GetMapping("/random")
|
@GetMapping("/random")
|
||||||
public void random() {
|
public void random() {
|
||||||
logger.warn("start random generation");
|
logger.warn("start random generation");
|
||||||
@@ -96,17 +98,23 @@ public class DebugController extends BaseController {
|
|||||||
|
|
||||||
for (long i = userCount; i < userCount + users; i++) {
|
for (long i = userCount; i < userCount + users; i++) {
|
||||||
LocalUser localUser = new LocalUser();
|
LocalUser localUser = new LocalUser();
|
||||||
String username = "user" + i;
|
String username = "user"
|
||||||
|
+ i;
|
||||||
localUser.setUsername(username);
|
localUser.setUsername(username);
|
||||||
localUser.setPasswordHash(passwordEncoder.encode(username));
|
localUser.setPasswordHash(passwordEncoder.encode(username));
|
||||||
localUserRepository.save(localUser);
|
localUserRepository.save(localUser);
|
||||||
logger.trace("Created user: '" + username + "'");
|
logger.trace("Created user: '"
|
||||||
|
+ username
|
||||||
|
+ "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Created " + users + " users");
|
logger.info("Created "
|
||||||
|
+ users
|
||||||
|
+ " users");
|
||||||
|
|
||||||
for (long id = 0; id <= userCount; id++) {
|
for (long id = 0; id <= userCount; id++) {
|
||||||
entries("user" + id, userCount);
|
entries("user"
|
||||||
|
+ id, userCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.warn("finished random generation");
|
logger.warn("finished random generation");
|
||||||
@@ -124,27 +132,40 @@ public class DebugController extends BaseController {
|
|||||||
entry.setText(RandomStringUtils.randomAscii(RandomUtils.nextInt(0, 2500)));
|
entry.setText(RandomStringUtils.randomAscii(RandomUtils.nextInt(0, 2500)));
|
||||||
entry.setEntryStatus(EntryStatus.NORMAL);
|
entry.setEntryStatus(EntryStatus.NORMAL);
|
||||||
entry = entryManager.save(entry);
|
entry = entryManager.save(entry);
|
||||||
logger.trace("Created entry: '" + entry.getId() + "'");
|
logger.trace("Created entry: '"
|
||||||
|
+ entry.getId()
|
||||||
|
+ "'");
|
||||||
comments(entry.getId(), entry.getCreated(), userCount);
|
comments(entry.getId(), entry.getCreated(), userCount);
|
||||||
votes(entry.getId(), Types.entry, userCount);
|
votes(entry.getId(), Types.entry, userCount);
|
||||||
}
|
}
|
||||||
logger.info("Created " + numEntries + " entries of '" + username + "'");
|
logger.info("Created "
|
||||||
|
+ numEntries
|
||||||
|
+ " entries of '"
|
||||||
|
+ username
|
||||||
|
+ "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void comments(Long target, Instant date, long userCount) {
|
protected void comments(Long target, Instant date, long userCount) {
|
||||||
long numComments = RandomUtils.nextLong(minComments, maxComments);
|
long numComments = RandomUtils.nextLong(minComments, maxComments);
|
||||||
logger.debug("Create " + numComments + " comments for '" + target + "'");
|
logger.debug("Create "
|
||||||
|
+ numComments
|
||||||
|
+ " comments for '"
|
||||||
|
+ target
|
||||||
|
+ "'");
|
||||||
for (int i = 0; i < numComments; i++) {
|
for (int i = 0; i < numComments; i++) {
|
||||||
Comment comment = new Comment();
|
Comment comment = new Comment();
|
||||||
comment.setTarget(target);
|
comment.setTarget(target);
|
||||||
comment.setAuthor("user" + RandomUtils.nextLong(0, userCount));
|
comment.setAuthor("user"
|
||||||
|
+ RandomUtils.nextLong(0, userCount));
|
||||||
comment.setText(RandomStringUtils.randomAscii(RandomUtils.nextInt(0, 2500)));
|
comment.setText(RandomStringUtils.randomAscii(RandomUtils.nextInt(0, 2500)));
|
||||||
comment.setCreated(Instant.now()
|
comment.setCreated(Instant.now()
|
||||||
.minus(RandomUtils.nextLong(0,
|
.minus(RandomUtils.nextLong(0,
|
||||||
(Instant.now().toEpochMilli() - date.toEpochMilli()) / 1000),
|
(Instant.now().toEpochMilli() - date.toEpochMilli()) / 1000),
|
||||||
ChronoUnit.SECONDS));
|
ChronoUnit.SECONDS));
|
||||||
comment = commentRepository.save(comment);
|
comment = commentRepository.save(comment);
|
||||||
logger.trace("Created comment: '" + comment.getId() + "'");
|
logger.trace("Created comment: '"
|
||||||
|
+ comment.getId()
|
||||||
|
+ "'");
|
||||||
subComments(target, comment.getId(), comment.getCreated(), subCommentsFactor,
|
subComments(target, comment.getId(), comment.getCreated(), subCommentsFactor,
|
||||||
subCommentsThresh, 0, userCount);
|
subCommentsThresh, 0, userCount);
|
||||||
}
|
}
|
||||||
@@ -154,19 +175,26 @@ public class DebugController extends BaseController {
|
|||||||
int depth, long userCount) {
|
int depth, long userCount) {
|
||||||
if (depth < subCommentsDepth && RandomUtils.nextDouble(0, 1) < thresh) {
|
if (depth < subCommentsDepth && RandomUtils.nextDouble(0, 1) < thresh) {
|
||||||
long numSubComments = RandomUtils.nextLong(0, Math.round(maxComments * factor));
|
long numSubComments = RandomUtils.nextLong(0, Math.round(maxComments * factor));
|
||||||
logger.debug("Create " + numSubComments + " subComments for '" + parent + "'");
|
logger.debug("Create "
|
||||||
|
+ numSubComments
|
||||||
|
+ " subComments for '"
|
||||||
|
+ parent
|
||||||
|
+ "'");
|
||||||
for (int i = 0; i < numSubComments; i++) {
|
for (int i = 0; i < numSubComments; i++) {
|
||||||
Comment comment = new Comment();
|
Comment comment = new Comment();
|
||||||
comment.setTarget(target);
|
comment.setTarget(target);
|
||||||
comment.setParent(parent);
|
comment.setParent(parent);
|
||||||
comment.setAuthor("user" + RandomUtils.nextLong(0, userCount));
|
comment.setAuthor("user"
|
||||||
|
+ RandomUtils.nextLong(0, userCount));
|
||||||
comment.setText(RandomStringUtils.randomAscii(RandomUtils.nextInt(0, 2500)));
|
comment.setText(RandomStringUtils.randomAscii(RandomUtils.nextInt(0, 2500)));
|
||||||
comment.setCreated(Instant.now()
|
comment.setCreated(Instant.now()
|
||||||
.minus(RandomUtils.nextLong(0,
|
.minus(RandomUtils.nextLong(0,
|
||||||
(Instant.now().toEpochMilli() - date.toEpochMilli()) / 1000),
|
(Instant.now().toEpochMilli() - date.toEpochMilli()) / 1000),
|
||||||
ChronoUnit.SECONDS));
|
ChronoUnit.SECONDS));
|
||||||
comment = commentRepository.save(comment);
|
comment = commentRepository.save(comment);
|
||||||
logger.trace("Created subComment: '" + comment.getId() + "'");
|
logger.trace("Created subComment: '"
|
||||||
|
+ comment.getId()
|
||||||
|
+ "'");
|
||||||
subComments(target, comment.getId(), comment.getCreated(), factor * 0.5,
|
subComments(target, comment.getId(), comment.getCreated(), factor * 0.5,
|
||||||
thresh * 0.5, depth++, userCount);
|
thresh * 0.5, depth++, userCount);
|
||||||
}
|
}
|
||||||
@@ -175,27 +203,41 @@ public class DebugController extends BaseController {
|
|||||||
|
|
||||||
protected void votes(Long target, Types targetType, long userCount) {
|
protected void votes(Long target, Types targetType, long userCount) {
|
||||||
long numUpvotes = RandomUtils.nextLong(minUpvotes, maxUpvotes);
|
long numUpvotes = RandomUtils.nextLong(minUpvotes, maxUpvotes);
|
||||||
logger.debug("Create " + numUpvotes + " upvotes for '" + target + "'");
|
logger.debug("Create "
|
||||||
|
+ numUpvotes
|
||||||
|
+ " upvotes for '"
|
||||||
|
+ target
|
||||||
|
+ "'");
|
||||||
for (int i = 0; i < numUpvotes; i++) {
|
for (int i = 0; i < numUpvotes; i++) {
|
||||||
Vote upvote = new Vote();
|
Vote upvote = new Vote();
|
||||||
upvote.setTarget(target);
|
upvote.setTarget(target);
|
||||||
upvote.setType(VoteType.up);
|
upvote.setType(VoteType.up);
|
||||||
upvote.setTargetType(targetType);
|
upvote.setTargetType(targetType);
|
||||||
upvote.setAuthor("user" + RandomUtils.nextLong(0, userCount));
|
upvote.setAuthor("user"
|
||||||
|
+ RandomUtils.nextLong(0, userCount));
|
||||||
upvote = voteRepository.save(upvote);
|
upvote = voteRepository.save(upvote);
|
||||||
logger.trace("Created upvote: '" + upvote.getId() + "'");
|
logger.trace("Created upvote: '"
|
||||||
|
+ upvote.getId()
|
||||||
|
+ "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
long numDownvotes = RandomUtils.nextLong(minDownvotes, maxDownvotes);
|
long numDownvotes = RandomUtils.nextLong(minDownvotes, maxDownvotes);
|
||||||
logger.debug("Create " + numDownvotes + " downvotes for '" + target + "'");
|
logger.debug("Create "
|
||||||
|
+ numDownvotes
|
||||||
|
+ " downvotes for '"
|
||||||
|
+ target
|
||||||
|
+ "'");
|
||||||
for (int i = 0; i < numDownvotes; i++) {
|
for (int i = 0; i < numDownvotes; i++) {
|
||||||
Vote downvote = new Vote();
|
Vote downvote = new Vote();
|
||||||
downvote.setTarget(target);
|
downvote.setTarget(target);
|
||||||
downvote.setType(VoteType.down);
|
downvote.setType(VoteType.down);
|
||||||
downvote.setTargetType(targetType);
|
downvote.setTargetType(targetType);
|
||||||
downvote.setAuthor("user" + RandomUtils.nextLong(0, userCount));
|
downvote.setAuthor("user"
|
||||||
|
+ RandomUtils.nextLong(0, userCount));
|
||||||
downvote = voteRepository.save(downvote);
|
downvote = voteRepository.save(downvote);
|
||||||
logger.trace("Created downvote: '" + downvote.getId() + "'");
|
logger.trace("Created downvote: '"
|
||||||
|
+ downvote.getId()
|
||||||
|
+ "'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package de.bstly.board.controller;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
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.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import de.bstly.board.businesslogic.CommentManager;
|
||||||
|
import de.bstly.board.businesslogic.EntryManager;
|
||||||
|
import de.bstly.board.controller.support.EntityResponseStatusException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lurkars
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/m")
|
||||||
|
public class ModerationController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CommentManager commentManager;
|
||||||
|
@Autowired
|
||||||
|
private EntryManager entryManager;
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_MOD')")
|
||||||
|
@DeleteMapping("/c/{id}")
|
||||||
|
public void deleteComment(@PathVariable("id") Long id) {
|
||||||
|
if (!commentManager.exists(id)) {
|
||||||
|
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||||
|
}
|
||||||
|
|
||||||
|
commentManager.delete(commentManager.get(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_MOD')")
|
||||||
|
@DeleteMapping("/e/{id}")
|
||||||
|
public void deleteEntry(@PathVariable("id") Long id) {
|
||||||
|
if (!entryManager.exists(id)) {
|
||||||
|
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||||
|
}
|
||||||
|
|
||||||
|
entryManager.delete(entryManager.get(id));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user