mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Don't delete PlaylistVideos from other users' playlists
This commit is contained in:
parent
cffd93f383
commit
c64162ee2b
1 changed files with 67 additions and 36 deletions
|
@ -633,48 +633,40 @@ 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)
|
||||||
String hash = user.getPassword();
|
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||||
boolean passMatch =
|
|
||||||
(hash.startsWith("$argon2") && argon2PasswordEncoder.matches(pass, hash)) || bcryptPasswordEncoder.matches(pass, hash);
|
|
||||||
|
|
||||||
if (!passMatch)
|
String hash = user.getPassword();
|
||||||
return Constants.mapper.writeValueAsBytes(new IncorrectCredentialsResponse());
|
boolean passMatch =
|
||||||
|
(hash.startsWith("$argon2") && argon2PasswordEncoder.matches(pass, hash)) || bcryptPasswordEncoder.matches(pass, hash);
|
||||||
|
|
||||||
try {
|
if (!passMatch)
|
||||||
CriteriaBuilder plCriteria = s.getCriteriaBuilder();
|
return Constants.mapper.writeValueAsBytes(new IncorrectCredentialsResponse());
|
||||||
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> plIter = playlists.iterator();
|
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 (plIter.hasNext()) {
|
while (iter.hasNext())
|
||||||
me.kavin.piped.utils.obj.db.Playlist pl = plIter.next();
|
s.delete(iter.next());
|
||||||
Iterator<PlaylistVideo> pvIter = pl.getVideos().iterator();
|
|
||||||
|
|
||||||
while (pvIter.hasNext())
|
s.delete(user);
|
||||||
s.delete(pvIter.next());
|
|
||||||
|
|
||||||
s.delete(pl);
|
s.getTransaction().begin();
|
||||||
}
|
s.getTransaction().commit();
|
||||||
|
} catch (Exception e) {
|
||||||
s.delete(user);
|
return Constants.mapper.writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e), e.getMessage()));
|
||||||
|
|
||||||
s.getTransaction().begin();
|
|
||||||
s.getTransaction().commit();
|
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new DeleteUserResponse(user.getUsername()));
|
|
||||||
} catch (Exception e) {
|
|
||||||
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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue