gravity settings

This commit is contained in:
_Bastler 2021-10-06 11:13:33 +02:00
parent b12c7c77a5
commit 46e642be51
7 changed files with 43 additions and 62 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.0-SNAPSHOT</revision> <revision>0.4.1-SNAPSHOT</revision>
</properties> </properties>
<parent> <parent>

View File

@ -57,10 +57,13 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
@Value("${bstly.board.ranking.gravity:1.2}") @Value("${bstly.board.ranking.gravity:1.2}")
private double GRAVITY; 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)
*/
@Override @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
LocalUser localUser = getByUsername(username); LocalUser localUser = getByUsername(username);
@ -83,25 +86,16 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
} }
LocalUserDetails userDetails = new LocalUserDetails(username, passwordHash, authorities); 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; return userDetails;
} }
/* /*
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated() * @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
*/ */
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#
* afterSingletonsInstantiated()
*/
@Override @Override
public void afterSingletonsInstantiated() { public void afterSingletonsInstantiated() {
if (!localUserRepository.exists(qLocalUser.roles.contains("ROLE_ADMIN"))) { if (!localUserRepository.exists(qLocalUser.roles.contains("ROLE_ADMIN"))) {
@ -213,6 +207,7 @@ 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,6 +4,7 @@
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;
@ -19,6 +20,9 @@ public class BaseController {
@Autowired @Autowired
private UserManager localUserManager; private UserManager localUserManager;
@Value("${bstly.board.ranking.gravity:1.2}")
private double GRAVITY;
/** /**
* Authenticated. * Authenticated.
* *
@ -37,8 +41,8 @@ public class BaseController {
protected String getCurrentUsername() { protected String getCurrentUsername() {
LocalUserDetails localUserDetails = getLocalUserDetails(); LocalUserDetails localUserDetails = getLocalUserDetails();
return localUserDetails != null ? localUserDetails.getUsername() : null; return localUserDetails != null ? localUserDetails.getUsername() : null;
} }
/** /**
* Gets the local user details. * Gets the local user details.
* *
@ -60,4 +64,24 @@ public class BaseController {
return localUserManager.getByAuth(SecurityContextHolder.getContext().getAuthentication()); 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;
}
} }

View File

@ -84,8 +84,8 @@ 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(getLocalUserDetails().getGravity()), gravityParameter.orElse(getGravity()), pageParameter.orElse(0),
pageParameter.orElse(0), sizeParameter.orElse(SIZE)); sizeParameter.orElse(SIZE));
Page<Entry> transformed = new PageImpl<Entry>( Page<Entry> transformed = new PageImpl<Entry>(
entries.getContent().stream().map(rankedEntry -> { entries.getContent().stream().map(rankedEntry -> {
@ -151,8 +151,7 @@ public class EntryController extends BaseController {
} }
Page<RankedEntry> entries = entryManager.fetchByComments( Page<RankedEntry> entries = entryManager.fetchByComments(
dateParameter.orElse(Instant.now()), dateParameter.orElse(Instant.now()), gravityParameter.orElse(getGravity()),
gravityParameter.orElse(getLocalUserDetails().getGravity()),
pageParameter.orElse(0), sizeParameter.orElse(SIZE)); pageParameter.orElse(0), sizeParameter.orElse(SIZE));
Page<Entry> transformed = new PageImpl<Entry>( Page<Entry> transformed = new PageImpl<Entry>(

View File

@ -86,6 +86,8 @@ public class UserController extends BaseController {
user.setPasswordHash(null); user.setPasswordHash(null);
userManager.applyMetadata(getCurrentUsername(), user);
return user; return user;
} }

View File

@ -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 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 + UPVOTES_QUERY
+ ") AS upvote ON upvote.target = entry.id LEFT JOIN (" + ") AS upvote ON upvote.target = entry.id LEFT JOIN ("
+ DOWNVOTES_QUERY + 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"; + ") 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 + 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"; + ") AS comment ON comment.target = entry.id WHERE entry.created < :before AND entry.entry_status = 'NORMAL' ORDER BY ranking DESC, entry.created DESC";

View File

@ -18,9 +18,6 @@ public class LocalUserDetails extends User {
*/ */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private double gravity;
private long karma;
/** /**
* Instantiates a new local user details. * Instantiates a new local user details.
* *
@ -33,40 +30,4 @@ public class LocalUserDetails extends User {
super(username, password, authorities); 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;
}
} }