Don't delete PlaylistVideos from other users' playlists

This commit is contained in:
theanonymousexyz 2022-04-23 02:21:34 +02:00
parent cffd93f383
commit c64162ee2b
No known key found for this signature in database
GPG key ID: 35EE09F5481049BB

View file

@ -633,7 +633,9 @@ public class ResponseHelper {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
User user = DatabaseHelper.getUserFromSession(session); User user = DatabaseHelper.getUserFromSession(session);
if (user != null) { if (user == null)
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
String hash = user.getPassword(); String hash = user.getPassword();
boolean passMatch = boolean passMatch =
(hash.startsWith("$argon2") && argon2PasswordEncoder.matches(pass, hash)) || bcryptPasswordEncoder.matches(pass, hash); (hash.startsWith("$argon2") && argon2PasswordEncoder.matches(pass, hash)) || bcryptPasswordEncoder.matches(pass, hash);
@ -649,32 +651,22 @@ public class ResponseHelper {
plQuery.from(me.kavin.piped.utils.obj.db.Playlist.class); plQuery.from(me.kavin.piped.utils.obj.db.Playlist.class);
plQuery.select(plRoot).where(plCriteria.equal(plRoot.get("owner"), user.getId())); plQuery.select(plRoot).where(plCriteria.equal(plRoot.get("owner"), user.getId()));
List<me.kavin.piped.utils.obj.db.Playlist> playlists = s.createQuery(plQuery).getResultList(); List<me.kavin.piped.utils.obj.db.Playlist> playlists = s.createQuery(plQuery).getResultList();
Iterator<me.kavin.piped.utils.obj.db.Playlist> iter = playlists.iterator();
Iterator<me.kavin.piped.utils.obj.db.Playlist> plIter = playlists.iterator(); while (iter.hasNext())
s.delete(iter.next());
while (plIter.hasNext()) {
me.kavin.piped.utils.obj.db.Playlist pl = plIter.next();
Iterator<PlaylistVideo> pvIter = pl.getVideos().iterator();
while (pvIter.hasNext())
s.delete(pvIter.next());
s.delete(pl);
}
s.delete(user); s.delete(user);
s.getTransaction().begin(); s.getTransaction().begin();
s.getTransaction().commit(); s.getTransaction().commit();
return Constants.mapper.writeValueAsBytes(new DeleteUserResponse(user.getUsername()));
} catch (Exception e) { } catch (Exception e) {
return Constants.mapper.writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e), e.getMessage())); return Constants.mapper.writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e), e.getMessage()));
} }
}
}
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse()); Multithreading.runAsync(() -> pruneUnusedPlaylistVideos());
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 {
@ -1057,6 +1049,8 @@ public class ResponseHelper {
s.getTransaction().begin(); s.getTransaction().begin();
s.getTransaction().commit(); s.getTransaction().commit();
Multithreading.runAsync(() -> pruneUnusedPlaylistVideos());
} }
return Constants.mapper.writeValueAsBytes(new AcceptedResponse()); return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
@ -1193,6 +1187,8 @@ public class ResponseHelper {
s.getTransaction().begin(); s.getTransaction().begin();
s.getTransaction().commit(); s.getTransaction().commit();
Multithreading.runAsync(() -> pruneUnusedPlaylistVideos());
return Constants.mapper.writeValueAsBytes(new AcceptedResponse()); return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
} }
} }
@ -1213,6 +1209,41 @@ public class ResponseHelper {
} }
} }
private static void pruneUnusedPlaylistVideos() {
try (Session s = DatabaseSessionFactory.createSession()) {
CriteriaQuery<me.kavin.piped.utils.obj.db.Playlist> plQuery =
s.getCriteriaBuilder().createQuery(me.kavin.piped.utils.obj.db.Playlist.class);
plQuery.select(plQuery.from(me.kavin.piped.utils.obj.db.Playlist.class));
List<me.kavin.piped.utils.obj.db.Playlist> playlists = s.createQuery(plQuery).getResultList();
CriteriaQuery<PlaylistVideo> pvQuery = s.getCriteriaBuilder().createQuery(PlaylistVideo.class);
pvQuery.select(pvQuery.from(PlaylistVideo.class));
List<PlaylistVideo> playlistVideos = s.createQuery(pvQuery).getResultList();
Iterator<PlaylistVideo> pvIter = playlistVideos.iterator();
while (pvIter.hasNext()) {
PlaylistVideo pv = pvIter.next();
boolean exists = false;
for (me.kavin.piped.utils.obj.db.Playlist pl : playlists) {
exists = false;
for (PlaylistVideo plpv : pl.getVideos()) {
if (plpv.getId().equals(pv.getId())) {
exists = true;
break;
}
}
if (exists) break;
}
if (!exists) s.delete(pv);
}
}
}
private static void handleNewVideo(StreamInfo info, long time, me.kavin.piped.utils.obj.db.Channel channel, Session s) { private static void handleNewVideo(StreamInfo info, long time, me.kavin.piped.utils.obj.db.Channel channel, Session s) {
if (channel == null) if (channel == null)