Cleanup and add OneToMany relationship.

This commit is contained in:
Kavin 2022-05-05 19:37:17 +01:00
parent a5420d48d9
commit 05ac4dc3ce
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
3 changed files with 77 additions and 85 deletions

View file

@ -16,15 +16,19 @@ public class DatabaseHelper {
public static User getUserFromSession(String session) { public static User getUserFromSession(String session) {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
s.setHibernateFlushMode(FlushMode.MANUAL); s.setHibernateFlushMode(FlushMode.MANUAL);
CriteriaBuilder cb = s.getCriteriaBuilder(); return getUserFromSession(session, s);
CriteriaQuery<User> cr = cb.createQuery(User.class);
Root<User> root = cr.from(User.class);
cr.select(root).where(cb.equal(root.get("sessionId"), session));
return s.createQuery(cr).uniqueResult();
} }
} }
public static User getUserFromSession(String session, Session s) {
CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<User> cr = cb.createQuery(User.class);
Root<User> root = cr.from(User.class);
cr.select(root).where(cb.equal(root.get("sessionId"), session));
return s.createQuery(cr).uniqueResult();
}
public static User getUserFromSessionWithSubscribed(String session) { public static User getUserFromSessionWithSubscribed(String session) {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
s.setHibernateFlushMode(FlushMode.MANUAL); s.setHibernateFlushMode(FlushMode.MANUAL);

View file

@ -47,12 +47,9 @@ import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.JoinType; import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -626,54 +623,6 @@ public class ResponseHelper {
} }
private static final Argon2PasswordEncoder argon2PasswordEncoder = new Argon2PasswordEncoder();
public static byte[] deleteUserResponse(String session, String pass) throws IOException {
if (StringUtils.isBlank(pass))
return Constants.mapper.writeValueAsBytes(new InvalidRequestResponse());
try (Session s = DatabaseSessionFactory.createSession()) {
User user = DatabaseHelper.getUserFromSession(session);
if (user == null)
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
String hash = user.getPassword();
boolean passMatch =
(hash.startsWith("$argon2") && argon2PasswordEncoder.matches(pass, hash)) || bcryptPasswordEncoder.matches(pass, hash);
if (!passMatch)
return Constants.mapper.writeValueAsBytes(new IncorrectCredentialsResponse());
try {
CriteriaBuilder plCriteria = s.getCriteriaBuilder();
CriteriaQuery<me.kavin.piped.utils.obj.db.Playlist> plQuery =
plCriteria.createQuery(me.kavin.piped.utils.obj.db.Playlist.class);
Root<me.kavin.piped.utils.obj.db.Playlist> plRoot =
plQuery.from(me.kavin.piped.utils.obj.db.Playlist.class);
plQuery.select(plRoot).where(plCriteria.equal(plRoot.get("owner"), user.getId()));
List<me.kavin.piped.utils.obj.db.Playlist> playlists = s.createQuery(plQuery).getResultList();
Iterator<me.kavin.piped.utils.obj.db.Playlist> iter = playlists.iterator();
while (iter.hasNext())
s.delete(iter.next());
s.delete(user);
s.getTransaction().begin();
s.getTransaction().commit();
Multithreading.runAsync(() -> pruneUnusedPlaylistVideos());
} catch (Exception e) {
return Constants.mapper.writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e), e.getMessage()));
}
return Constants.mapper.writeValueAsBytes(new DeleteUserResponse(user.getUsername()));
}
}
public static byte[] registerResponse(String user, String pass) throws IOException { public static byte[] registerResponse(String user, String pass) throws IOException {
if (Constants.DISABLE_REGISTRATION) if (Constants.DISABLE_REGISTRATION)
@ -717,8 +666,16 @@ public class ResponseHelper {
} }
} }
private static final Argon2PasswordEncoder argon2PasswordEncoder = new Argon2PasswordEncoder();
private static final BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder(); private static final BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
private static boolean hashMatch(String hash, String pass) {
return hash.startsWith("$argon2") ?
argon2PasswordEncoder.matches(pass, hash) :
bcryptPasswordEncoder.matches(pass, hash);
}
public static byte[] loginResponse(String user, String pass) public static byte[] loginResponse(String user, String pass)
throws IOException { throws IOException {
@ -737,11 +694,7 @@ public class ResponseHelper {
if (dbuser != null) { if (dbuser != null) {
String hash = dbuser.getPassword(); String hash = dbuser.getPassword();
if (hash.startsWith("$argon2")) { if (hashMatch(hash, pass)) {
if (argon2PasswordEncoder.matches(pass, hash)) {
return Constants.mapper.writeValueAsBytes(new LoginResponse(dbuser.getSessionId()));
}
} else if (bcryptPasswordEncoder.matches(pass, hash)) {
return Constants.mapper.writeValueAsBytes(new LoginResponse(dbuser.getSessionId())); return Constants.mapper.writeValueAsBytes(new LoginResponse(dbuser.getSessionId()));
} }
} }
@ -750,6 +703,37 @@ public class ResponseHelper {
} }
} }
public static byte[] deleteUserResponse(String session, String pass) throws IOException {
if (StringUtils.isBlank(pass))
return Constants.mapper.writeValueAsBytes(new InvalidRequestResponse());
try (Session s = DatabaseSessionFactory.createSession()) {
User user = DatabaseHelper.getUserFromSession(session);
if (user == null)
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
String hash = user.getPassword();
if (!hashMatch(hash, pass))
return Constants.mapper.writeValueAsBytes(new IncorrectCredentialsResponse());
try {
s.delete(user);
s.getTransaction().begin();
s.getTransaction().commit();
Multithreading.runAsync(() -> pruneUnusedPlaylistVideos());
} catch (Exception e) {
return Constants.mapper.writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e), e.getMessage()));
}
return Constants.mapper.writeValueAsBytes(new DeleteUserResponse(user.getUsername()));
}
}
public static byte[] subscribeResponse(String session, String channelId) public static byte[] subscribeResponse(String session, String channelId)
throws IOException { throws IOException {
@ -792,9 +776,7 @@ public class ResponseHelper {
if (user != null) { if (user != null) {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
if (user.getSubscribed().contains(channelId)) { if (user.getSubscribed().contains(channelId)) {
Set<String> subscribed = user.getSubscribed(); user.getSubscribed().remove(channelId);
subscribed.removeIf(sub -> sub.equals(channelId));
user.setSubscribed(subscribed);
s.update(user); s.update(user);
s.getTransaction().begin(); s.getTransaction().begin();
@ -957,7 +939,7 @@ public class ResponseHelper {
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
var channels = DatabaseHelper.getChannelsFromIds(s, Arrays.asList(channelIds)); var channels = DatabaseHelper.getChannelsFromIds(s, Arrays.asList(channelIds));
outer: outer:
for (String channelId : channelIds) { for (String channelId : channelIds) {
for (var channel : channels) for (var channel : channels)
@ -1057,7 +1039,7 @@ public class ResponseHelper {
s.getTransaction().begin(); s.getTransaction().begin();
s.getTransaction().commit(); s.getTransaction().commit();
Multithreading.runAsync(() -> pruneUnusedPlaylistVideos()); Multithreading.runAsync(() -> pruneUnusedPlaylistVideos());
} }
@ -1066,21 +1048,16 @@ public class ResponseHelper {
public static byte[] playlistsResponse(String session) throws IOException { public static byte[] playlistsResponse(String session) throws IOException {
User user = DatabaseHelper.getUserFromSession(session);
if (user == null)
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
var cb = s.getCriteriaBuilder();
var query = cb.createQuery(me.kavin.piped.utils.obj.db.Playlist.class); User user = DatabaseHelper.getUserFromSession(session, s);
var root = query.from(me.kavin.piped.utils.obj.db.Playlist.class);
query.select(root); if (user == null)
query.where(cb.equal(root.get("owner"), user)); return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
var playlists = new ObjectArrayList<>(); var playlists = new ObjectArrayList<>();
for (var playlist : s.createQuery(query).list()) { for (var playlist : user.getPlaylists()) {
ObjectNode node = Constants.mapper.createObjectNode(); ObjectNode node = Constants.mapper.createObjectNode();
node.put("id", String.valueOf(playlist.getPlaylistId())); node.put("id", String.valueOf(playlist.getPlaylistId()));
node.put("name", playlist.getName()); node.put("name", playlist.getName());
@ -1221,15 +1198,15 @@ public class ResponseHelper {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
CriteriaBuilder cb = s.getCriteriaBuilder(); CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaDelete<PlaylistVideo> pvQuery = cb.createCriteriaDelete(PlaylistVideo.class);
Root<PlaylistVideo> pvRoot = pvQuery.from(PlaylistVideo.class);
Subquery<String> subQuery = pvQuery.subquery(String.class); var pvQuery = cb.createCriteriaDelete(PlaylistVideo.class);
Root<me.kavin.piped.utils.obj.db.Playlist> subRoot = var pvRoot = pvQuery.from(PlaylistVideo.class);
subQuery.from(me.kavin.piped.utils.obj.db.Playlist.class);
var subQuery = pvQuery.subquery(me.kavin.piped.utils.obj.db.Playlist.class);
var subRoot = subQuery.from(me.kavin.piped.utils.obj.db.Playlist.class);
subQuery.select(subRoot.join("videos").get("id")); subQuery.select(subRoot.join("videos").get("id"));
pvQuery.where(cb.not(pvRoot.get("id").in(subQuery))); pvQuery.where(cb.not(pvRoot.get("id").in(subQuery)));
s.getTransaction().begin(); s.getTransaction().begin();

View file

@ -34,6 +34,9 @@ public class User implements Serializable {
@Column(name = "channel", length = 30) @Column(name = "channel", length = 30)
private Set<String> subscribed_ids; private Set<String> subscribed_ids;
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL)
private Set<Playlist> playlists;
public User() { public User() {
} }
@ -83,4 +86,12 @@ public class User implements Serializable {
public void setSubscribed(Set<String> subscribed_ids) { public void setSubscribed(Set<String> subscribed_ids) {
this.subscribed_ids = subscribed_ids; this.subscribed_ids = subscribed_ids;
} }
public Set<Playlist> getPlaylists() {
return playlists;
}
public void setPlaylists(Set<Playlist> playlists) {
this.playlists = playlists;
}
} }