new entrie sortings, link titles

This commit is contained in:
_Bastler 2021-10-06 09:47:50 +02:00
parent d07805cd54
commit 4b6d9233ac
15 changed files with 245 additions and 113 deletions

View File

@ -76,6 +76,19 @@ public class EntryManager {
return entryRepository.findAllByComments(date, gravity, PageRequest.of(page, size));
}
/**
* Fetch by comments.
*
* @param date the date
* @param gravity the gravity
* @param page the page
* @param size the size
* @return the page
*/
public Page<Entry> fetchByLastComment(Instant date, int page, int size) {
return entryRepository.findAllByLastComment(date, PageRequest.of(page, size));
}
/**
* Fetch by date.
*

View File

@ -57,30 +57,10 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
@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
* de.bstly.board.businesslogic.LocalUserManager#loadUserByUsername(java.lang.
* String)
*/
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
LocalUser localUser = getByUsername(username);
@ -118,30 +98,10 @@ 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()
*/
/*
*
* @see org.springframework.beans.factory.SmartInitializingSingleton#
* afterSingletonsInstantiated()
*/
@Override
public void afterSingletonsInstantiated() {
if (!localUserRepository.exists(qLocalUser.roles.contains("ROLE_ADMIN"))) {

View File

@ -52,13 +52,13 @@ public class CommentController extends BaseController {
/**
* 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 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
* @param ignoreParameter the ignore parameter
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@ -79,12 +79,12 @@ public class CommentController extends BaseController {
/**
* Fetch by date.
*
* @param target the target
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param descParameter the desc parameter
* @param target the target
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param descParameter the desc parameter
* @param ignoreParameter the ignore parameter
* @return the page
*/
@ -109,12 +109,12 @@ public class CommentController extends BaseController {
/**
* Fetch by username.
*
* @param username the username
* @param parent the parent
* @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 parent the parent
* @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
*/
@ -152,7 +152,7 @@ public class CommentController extends BaseController {
/**
* Gets the comment.
*
* @param id the id
* @param id the id
* @param ignoreParameter the ignore parameter
* @return the comment
*/
@ -174,7 +174,7 @@ public class CommentController extends BaseController {
/**
* Creates the comment.
*
* @param comment the comment
* @param comment the comment
* @param ignoreParameter the ignore parameter
* @return the comment
*/

View File

@ -3,9 +3,14 @@
*/
package de.bstly.board.controller;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
@ -96,7 +101,35 @@ public class EntryController extends BaseController {
}
/**
* Fetch by ranking.
* Fetch by date.
*
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param ignoreParameter the ignore parameter
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/new")
public Page<Entry> fetchByDate(@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
if (sizeParameter.isPresent() && sizeParameter.get() > 100) {
sizeParameter = Optional.of(100);
}
Page<Entry> entries = entryManager.fetchByDate(dateParameter.orElse(Instant.now()),
pageParameter.orElse(0), sizeParameter.orElse(SIZE));
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
entries.getContent(), ignore);
return entries;
}
/**
* Fetch by comments.
*
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
@ -134,17 +167,18 @@ public class EntryController extends BaseController {
}
/**
* Fetch by date.
* Fetch by comments.
*
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param ignoreParameter the ignore 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
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/new")
public Page<Entry> fetchByDate(@RequestParam("page") Optional<Integer> pageParameter,
@GetMapping("/last")
public Page<Entry> fetchByLast(@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("ignore") Optional<List<String>> ignoreParameter) {
@ -153,8 +187,9 @@ public class EntryController extends BaseController {
sizeParameter = Optional.of(100);
}
Page<Entry> entries = entryManager.fetchByDate(dateParameter.orElse(Instant.now()),
Page<Entry> entries = entryManager.fetchByLastComment(dateParameter.orElse(Instant.now()),
pageParameter.orElse(0), sizeParameter.orElse(SIZE));
List<String> ignore = ignoreParameter.orElse(Lists.newArrayList());
entryManager.applyMetadata(getCurrentUsername(), userManager.getKarma(getCurrentUsername()),
entries.getContent(), ignore);
@ -256,4 +291,36 @@ public class EntryController extends BaseController {
return entry;
}
/**
* Gets the title.
*
* @param url the url
* @return the title
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/helper/title")
public String getTitle(@RequestParam("url") String url) {
InputStream response = null;
Scanner scanner = null;
try {
response = new URL(URLDecoder.decode(url, "utf-8")).openStream();
scanner = new Scanner(response);
String responseBody = scanner.useDelimiter("\\A").next();
return responseBody.substring(responseBody.indexOf("<title>") + 7,
responseBody.indexOf("</title>"));
} catch (IOException ex) {
} finally {
try {
if (response != null) {
response.close();
}
if (scanner != null) {
scanner.close();
}
} catch (IOException ex) {
}
}
return "";
}
}

View File

@ -4,6 +4,7 @@
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;
@ -15,13 +16,13 @@ import org.springframework.web.bind.annotation.RestController;
import de.bstly.board.businesslogic.CommentManager;
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.model.Types;
import de.bstly.board.model.Vote;
import de.bstly.board.model.VoteType;
/**
* The Class VoteController.
*/
@ -29,17 +30,17 @@ import de.bstly.board.model.VoteType;
@RequestMapping("/votes")
public class VoteController extends BaseController {
@Autowired
private VoteManager voteManager;
@Autowired
private EntryManager entryManager;
@Autowired
private CommentManager commentManager;
@Autowired
private UserManager userManager;
@Value("${bstly.board.unvoteThresh:10}")
private long UNVOTE_THRESH;
/**
* Gets the entry points.
@ -95,6 +96,10 @@ public class VoteController extends BaseController {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
}
if (userManager.getKarma(getCurrentUsername()) < UNVOTE_THRESH) {
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
}
Vote vote = voteManager.get(getCurrentUsername(), Types.entry, id);
if (vote == null) {
vote = new Vote();

View File

@ -78,6 +78,7 @@ public class EntityResponseStatusException extends NestedRuntimeException {
return this.body;
}
/*
* @see org.springframework.core.NestedRuntimeException#getMessage()
*/

View File

@ -0,0 +1,94 @@
/**
*
*/
package de.bstly.board.controller.support;
import java.io.IOException;
import java.lang.reflect.Type;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import com.google.gson.Gson;
import com.google.gson.JsonPrimitive;
/**
* The Class JsonStringBodyControllerAdvice.
*/
@ControllerAdvice
public class JsonStringBodyControllerAdvice implements RequestBodyAdvice, ResponseBodyAdvice<String> {
private Gson gson = new Gson();
/*
* @see org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice#supports(org.springframework.core.MethodParameter, java.lang.reflect.Type, java.lang.Class)
*/
@Override
public boolean supports(MethodParameter methodParameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
return targetType instanceof Class && String.class.equals((Class<?>) targetType);
}
/*
* @see org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice#beforeBodyRead(org.springframework.http.HttpInputMessage, org.springframework.core.MethodParameter, java.lang.reflect.Type, java.lang.Class)
*/
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
return inputMessage;
}
/*
* @see org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice#afterBodyRead(java.lang.Object, org.springframework.http.HttpInputMessage, org.springframework.core.MethodParameter, java.lang.reflect.Type, java.lang.Class)
*/
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
body = ((String) body).replaceAll("^\"|\"$", "");
return body;
}
/*
* @see org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice#handleEmptyBody(java.lang.Object, org.springframework.http.HttpInputMessage, org.springframework.core.MethodParameter, java.lang.reflect.Type, java.lang.Class)
*/
@Override
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}
/*
* @see org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice#supports(org.springframework.core.MethodParameter, java.lang.Class)
*/
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return converterType == StringHttpMessageConverter.class;
}
/*
* @see org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice#beforeBodyWrite(java.lang.Object, org.springframework.core.MethodParameter, org.springframework.http.MediaType, java.lang.Class, org.springframework.http.server.ServerHttpRequest, org.springframework.http.server.ServerHttpResponse)
*/
@Override
public String beforeBodyWrite(String body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
response.getHeaders().set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return gson.toJson(new JsonPrimitive(body));
}
}

View File

@ -35,14 +35,10 @@ public class RequestBodyErrors extends AbstractBindingResult {
return target;
}
/*
* @see org.springframework.validation.AbstractBindingResult#getActualFieldValue(java.lang.String)
*/
/*
* @see
* org.springframework.validation.AbstractBindingResult#getActualFieldValue(java
* .lang.String)
*/
@Override
protected Object getActualFieldValue(String field) {
// Not necessary

View File

@ -20,14 +20,12 @@ import de.bstly.board.model.Comment;
@Component
public class CommentValidator implements Validator {
@Autowired
private CommentManager commentManager;
@Autowired
private EntryManager entryManager;
/*
* @see org.springframework.validation.Validator#supports(java.lang.Class)
*/
@ -36,9 +34,9 @@ public class CommentValidator implements Validator {
return clazz.isAssignableFrom(Comment.class);
}
/*
* @see org.springframework.validation.Validator#validate(java.lang.Object,
* org.springframework.validation.Errors)
* @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
*/
@Override
public void validate(Object target, Errors errors) {

View File

@ -20,6 +20,7 @@ public class EntryValidator implements Validator {
private UrlValidator urlValidator = new UrlValidator();
/*
* @see org.springframework.validation.Validator#supports(java.lang.Class)
*/
@ -28,9 +29,9 @@ public class EntryValidator implements Validator {
return clazz.isAssignableFrom(Entry.class);
}
/*
* @see org.springframework.validation.Validator#validate(java.lang.Object,
* org.springframework.validation.Errors)
* @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
*/
@Override
public void validate(Object target, Errors errors) {

View File

@ -158,13 +158,10 @@ public class I18nManager implements SmartInitializingSingleton {
}
}
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
*/
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#
* afterSingletonsInstantiated()
*/
@Override
public void afterSingletonsInstantiated() {
try {

View File

@ -27,7 +27,7 @@ public interface EntryRepository
static final String DOWNVOTES_QUERY = "SELECT downvote.target,COUNT(downvote.id) AS count FROM votes as downvote WHERE downvote.type = 1 AND downvote.target_type = 1 GROUP BY downvote.target";
static final String COMMENTS_QUERY = "SELECT comment.target,COUNT(comment.id) AS count FROM comments as comment GROUP BY comment.target";
static final String COMMENTS_QUERY = "SELECT comment.target,MAX(comment.created) as last,COUNT(comment.id) AS count FROM comments as comment GROUP BY comment.target";
static final String RANK_CALCULATION_QUERY = "SELECT entry.*, (IFNULL(upvote.count,0) - IFNULL(downvote.count,0)) as points, (IFNULL(upvote.count,0) - IFNULL(downvote.count,0)) / POW(TIMESTAMPDIFF(HOUR, entry.created, :before)+2,:gravity) AS ranking FROM entries AS entry LEFT JOIN ("
+ UPVOTES_QUERY
@ -35,10 +35,14 @@ public interface EntryRepository
+ DOWNVOTES_QUERY
+ ") AS downvote ON downvote.target = entry.id WHERE entry.created < :before AND entry.entry_status = 'NORMAL' ORDER BY ranking DESC, entry.created DESC";
static final String COMMENT_CALCULATION_QUERY = "SELECT entry.*, IFNULL(comment.count,0) as comments, IFNULL(comment.count,0) / POW(TIMESTAMPDIFF(HOUR, entry.created, :before)+2,:gravity) AS ranking FROM entries AS entry LEFT JOIN ("
static final String COMMENT_CALCULATION_QUERY = "SELECT entry.*, IFNULL(comment.count,0) as comments, IFNULL(comment.count,0) / POW(TIMESTAMPDIFF(HOUR, comment.last, :before)+2,:gravity) AS ranking FROM entries AS entry LEFT JOIN ("
+ COMMENTS_QUERY
+ ") AS comment ON comment.target = entry.id WHERE entry.created < :before AND entry.entry_status = 'NORMAL' ORDER BY ranking DESC, entry.created DESC";
static final String LAST_COMMENT_QUERY = "SELECT entry.* FROM entries AS entry LEFT JOIN ("
+ COMMENTS_QUERY
+ ") AS comment ON comment.target = entry.id WHERE entry.created < :before AND entry.entry_status = 'NORMAL' ORDER BY comment.last DESC, entry.created DESC";
static final String ARCHIVE_CALCULATION_QUERY = "SELECT entry.*, (IFNULL(upvote.count,0) - IFNULL(downvote.count,0)) as points, (IFNULL(upvote.count,0) - IFNULL(downvote.count,0)) / POW(TIMESTAMPDIFF(HOUR, entry.created, :before)+2,:gravity) AS ranking FROM entries AS entry LEFT JOIN ("
+ UPVOTES_QUERY
+ ") AS upvote ON upvote.target = entry.id LEFT JOIN ("
@ -75,6 +79,17 @@ public interface EntryRepository
Page<RankedEntry> findAllByComments(@Param("before") Instant before,
@Param("gravity") double gravity, Pageable pageable);
/**
* Find all by comments.
*
* @param before the before
* @param gravity the gravity
* @param pageable the pageable
* @return the page
*/
@Query(value = LAST_COMMENT_QUERY, countQuery = COUNT_QUERY, nativeQuery = true)
Page<Entry> findAllByLastComment(@Param("before") Instant before, Pageable pageable);
/**
* Find all by ranking archive.
*

View File

@ -27,15 +27,10 @@ public class LocalRememberMeServices extends PersistentTokenBasedRememberMeServi
super(key, userDetailsService, tokenRepository);
}
/*
* @see org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices#rememberMeRequested(javax.servlet.http.HttpServletRequest, java.lang.String)
*/
/*
*
* @see org.springframework.security.web.authentication.rememberme.
* AbstractRememberMeServices#rememberMeRequested(javax.servlet.http.
* HttpServletRequest, java.lang.String)
*/
@Override
protected boolean rememberMeRequested(HttpServletRequest request, String parameter) {
Object value = request.getAttribute(parameter);

View File

@ -37,15 +37,10 @@ public class OAuth2AuthenticationSuccessHandler
private RememberMeServices rememberMeServices;
/*
* @see org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler#onAuthenticationSuccess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.core.Authentication)
*/
/*
* @see org.springframework.security.web.authentication.
* SavedRequestAwareAuthenticationSuccessHandler#onAuthenticationSuccess(javax.
* servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
* org.springframework.security.core.Authentication)
*/
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {

View File

@ -53,15 +53,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${loginTargetUrl:/}")
private String loginTargetUrl;
/*
* @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity)
*/
/*
*
* @see org.springframework.security.config.annotation.web.configuration.
* WebSecurityConfigurerAdapter#configure(org.springframework.security.config.
* annotation.web.builders.HttpSecurity)
*/
@Override
protected void configure(HttpSecurity http) throws Exception {