diff --git a/pom.xml b/pom.xml index d3f70fe..2a9a491 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ UTF-8 11 - 0.4.0-SNAPSHOT + 0.4.1-SNAPSHOT diff --git a/src/main/java/de/bstly/board/businesslogic/UserManager.java b/src/main/java/de/bstly/board/businesslogic/UserManager.java index 10a3b5a..99b3c55 100644 --- a/src/main/java/de/bstly/board/businesslogic/UserManager.java +++ b/src/main/java/de/bstly/board/businesslogic/UserManager.java @@ -57,10 +57,13 @@ 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) + */ @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { LocalUser localUser = getByUsername(username); @@ -83,25 +86,16 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet } LocalUserDetails userDetails = new LocalUserDetails(username, passwordHash, authorities); - - userDetails.setGravity(GRAVITY); - - if (localUser.getSettings().containsKey("gravity")) { - try { - userDetails.setGravity(Double.parseDouble(localUser.getSettings().get("gravity"))); - } catch (Exception e) { - } - } - - userDetails.setKarma(getKarma(username)); - return userDetails; } - /* * @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"))) { @@ -213,6 +207,7 @@ 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 bb5b5ce..ac08445 100644 --- a/src/main/java/de/bstly/board/controller/BaseController.java +++ b/src/main/java/de/bstly/board/controller/BaseController.java @@ -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.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -19,6 +20,9 @@ public class BaseController { @Autowired private UserManager localUserManager; + @Value("${bstly.board.ranking.gravity:1.2}") + private double GRAVITY; + /** * Authenticated. * @@ -37,8 +41,8 @@ public class BaseController { protected String getCurrentUsername() { LocalUserDetails localUserDetails = getLocalUserDetails(); return localUserDetails != null ? localUserDetails.getUsername() : null; - } - + } + /** * Gets the local user details. * @@ -60,4 +64,24 @@ public class BaseController { return localUserManager.getByAuth(SecurityContextHolder.getContext().getAuthentication()); } + + /** + * Gets the gravity. + * + * @return the gravity + */ + protected double getGravity() { + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + if (auth != null && auth.isAuthenticated()) { + LocalUser localUser = localUserManager.getByAuth(auth); + if (localUser.getSettings().containsKey("gravity")) { + try { + return Double.parseDouble(localUser.getSettings().get("gravity")); + } catch (Exception e) { + } + } + } + return GRAVITY; + } + } diff --git a/src/main/java/de/bstly/board/controller/EntryController.java b/src/main/java/de/bstly/board/controller/EntryController.java index 0719410..99f603b 100644 --- a/src/main/java/de/bstly/board/controller/EntryController.java +++ b/src/main/java/de/bstly/board/controller/EntryController.java @@ -84,8 +84,8 @@ public class EntryController extends BaseController { } Page entries = entryManager.fetchByRanking(dateParameter.orElse(Instant.now()), - gravityParameter.orElse(getLocalUserDetails().getGravity()), - pageParameter.orElse(0), sizeParameter.orElse(SIZE)); + gravityParameter.orElse(getGravity()), pageParameter.orElse(0), + sizeParameter.orElse(SIZE)); Page transformed = new PageImpl( entries.getContent().stream().map(rankedEntry -> { @@ -151,8 +151,7 @@ public class EntryController extends BaseController { } Page entries = entryManager.fetchByComments( - dateParameter.orElse(Instant.now()), - gravityParameter.orElse(getLocalUserDetails().getGravity()), + dateParameter.orElse(Instant.now()), gravityParameter.orElse(getGravity()), pageParameter.orElse(0), sizeParameter.orElse(SIZE)); Page transformed = new PageImpl( diff --git a/src/main/java/de/bstly/board/controller/UserController.java b/src/main/java/de/bstly/board/controller/UserController.java index 55a03a1..168cf04 100644 --- a/src/main/java/de/bstly/board/controller/UserController.java +++ b/src/main/java/de/bstly/board/controller/UserController.java @@ -86,6 +86,8 @@ public class UserController extends BaseController { user.setPasswordHash(null); + userManager.applyMetadata(getCurrentUsername(), user); + return user; } diff --git a/src/main/java/de/bstly/board/repository/EntryRepository.java b/src/main/java/de/bstly/board/repository/EntryRepository.java index 4d5768c..f1948c2 100644 --- a/src/main/java/de/bstly/board/repository/EntryRepository.java +++ b/src/main/java/de/bstly/board/repository/EntryRepository.java @@ -29,13 +29,13 @@ public interface EntryRepository 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 (" + 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)) / IF(:gravity > 0, POW(TIMESTAMPDIFF(HOUR, entry.created, :before)+2,:gravity), 1) AS ranking FROM entries AS entry LEFT JOIN (" + UPVOTES_QUERY + ") AS upvote ON upvote.target = entry.id LEFT JOIN (" + 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, comment.last, :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) / IF(:gravity > 0, POW(TIMESTAMPDIFF(HOUR, comment.last, :before)+2,:gravity), 1) 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"; diff --git a/src/main/java/de/bstly/board/security/LocalUserDetails.java b/src/main/java/de/bstly/board/security/LocalUserDetails.java index 738550f..df6ffe8 100644 --- a/src/main/java/de/bstly/board/security/LocalUserDetails.java +++ b/src/main/java/de/bstly/board/security/LocalUserDetails.java @@ -18,9 +18,6 @@ public class LocalUserDetails extends User { */ private static final long serialVersionUID = 1L; - private double gravity; - private long karma; - /** * Instantiates a new local user details. * @@ -33,40 +30,4 @@ public class LocalUserDetails extends User { super(username, password, authorities); } - /** - * Gets the gravity. - * - * @return the gravity - */ - public double getGravity() { - return gravity; - } - - /** - * Sets the gravity. - * - * @param gravity the new gravity - */ - public void setGravity(double gravity) { - this.gravity = gravity; - } - - /** - * Gets the karma. - * - * @return the karma - */ - public long getKarma() { - return karma; - } - - /** - * Sets the karma. - * - * @param karma the new karma - */ - public void setKarma(long karma) { - this.karma = karma; - } - }