From 86152a457a55411e559cb9a7c136a3d43b102c1a Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 29 May 2023 19:03:45 +0200 Subject: [PATCH 1/2] Add route to change playlist description and add PlaylistsHelper --- .../me/kavin/piped/server/ServerLauncher.java | 13 ++++- .../handlers/auth/AuthPlaylistHandlers.java | 58 ++++++++++++++----- .../me/kavin/piped/utils/PlaylistHelpers.java | 19 ++++++ .../me/kavin/piped/utils/PlaylistResult.java | 21 +++++++ 4 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 src/main/java/me/kavin/piped/utils/PlaylistHelpers.java create mode 100644 src/main/java/me/kavin/piped/utils/PlaylistResult.java diff --git a/src/main/java/me/kavin/piped/server/ServerLauncher.java b/src/main/java/me/kavin/piped/server/ServerLauncher.java index 1c6b3f3..b1cc688 100644 --- a/src/main/java/me/kavin/piped/server/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/server/ServerLauncher.java @@ -47,7 +47,6 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { private static final HttpHeader FILE_NAME = HttpHeaders.of("x-file-name"); private static final HttpHeader LAST_ETAG = HttpHeaders.of("x-last-etag"); - @Provides Executor executor() { return Multithreading.getCachedExecutor(); @@ -403,6 +402,18 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } catch (Exception e) { return getErrorResponse(e, request.getPath()); } + })).map(POST, "/user/playlists/description/change", AsyncServlet.ofBlocking(executor, request -> { + try { + var json = mapper.readTree(request.loadBody().getResult().asArray()); + var playlistId = json.get("playlistId").textValue(); + var description = json.get("description").textValue(); + return getJsonResponse( + AuthPlaylistHandlers.editPlaylistDescriptionResponse(request.getHeader(AUTHORIZATION), + playlistId, description), + "private"); + } catch (Exception e) { + return getErrorResponse(e, request.getPath()); + } })).map(GET, "/user/playlists", AsyncServlet.ofBlocking(executor, request -> { try { return getJsonResponse(AuthPlaylistHandlers.playlistsResponse(request.getHeader(AUTHORIZATION)), "private"); diff --git a/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java b/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java index a9a8b05..90ad25d 100644 --- a/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java +++ b/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java @@ -148,15 +148,12 @@ public class AuthPlaylistHandlers { ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); try (Session s = DatabaseSessionFactory.createSession()) { - var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId); - - if (playlist == null) + var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId); + if (playlistResult.getError() != null) { return mapper.writeValueAsBytes(mapper.createObjectNode() - .put("error", "Playlist not found")); - - if (playlist.getOwner().getId() != user.getId()) - return mapper.writeValueAsBytes(mapper.createObjectNode() - .put("error", "You do not own this playlist")); + .put("error", playlistResult.getError())); + } + var playlist = playlistResult.getPlaylist(); playlist.setName(newName); @@ -169,6 +166,38 @@ public class AuthPlaylistHandlers { return mapper.writeValueAsBytes(new AcceptedResponse()); } + public static byte[] editPlaylistDescriptionResponse(String session, String playlistId, String newDescription) + throws IOException { + + if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId) || StringUtils.isBlank(newDescription)) + ExceptionHandler + .throwErrorResponse( + new InvalidRequestResponse("session, playlistId and description are required parameters")); + + User user = DatabaseHelper.getUserFromSession(session); + + if (user == null) + ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); + + try (Session s = DatabaseSessionFactory.createSession()) { + var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId); + if (playlistResult.getError() != null) { + return mapper.writeValueAsBytes(mapper.createObjectNode() + .put("error", playlistResult.getError())); + } + var playlist = playlistResult.getPlaylist(); + + playlist.setShortDescription(newDescription); + + var tr = s.beginTransaction(); + s.merge(playlist); + tr.commit(); + + } + + return mapper.writeValueAsBytes(new AcceptedResponse()); + } + public static byte[] deletePlaylistResponse(String session, String playlistId) throws IOException { if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId)) @@ -180,15 +209,12 @@ public class AuthPlaylistHandlers { ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); try (Session s = DatabaseSessionFactory.createSession()) { - var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId); - - if (playlist == null) + var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId); + if (playlistResult.getError() != null) { return mapper.writeValueAsBytes(mapper.createObjectNode() - .put("error", "Playlist not found")); - - if (playlist.getOwner().getId() != user.getId()) - return mapper.writeValueAsBytes(mapper.createObjectNode() - .put("error", "You do not own this playlist")); + .put("error", playlistResult.getError())); + } + var playlist = playlistResult.getPlaylist(); var tr = s.beginTransaction(); s.remove(playlist); diff --git a/src/main/java/me/kavin/piped/utils/PlaylistHelpers.java b/src/main/java/me/kavin/piped/utils/PlaylistHelpers.java new file mode 100644 index 0000000..884c438 --- /dev/null +++ b/src/main/java/me/kavin/piped/utils/PlaylistHelpers.java @@ -0,0 +1,19 @@ +package me.kavin.piped.utils; + +import org.hibernate.Session; + +import me.kavin.piped.utils.obj.db.User; + +public class PlaylistHelpers { + public static PlaylistResult getUserPlaylist(Session s, User user, String playlistId) { + var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId); + + if (playlist == null) + return new PlaylistResult(null, "Playlist not found"); + + if (playlist.getOwner().getId() != user.getId()) + return new PlaylistResult(null, "You do not own this playlist"); + + return new PlaylistResult(playlist, null); + } +} diff --git a/src/main/java/me/kavin/piped/utils/PlaylistResult.java b/src/main/java/me/kavin/piped/utils/PlaylistResult.java new file mode 100644 index 0000000..49cf4ad --- /dev/null +++ b/src/main/java/me/kavin/piped/utils/PlaylistResult.java @@ -0,0 +1,21 @@ +package me.kavin.piped.utils; + +import me.kavin.piped.utils.obj.db.Playlist; + +public final class PlaylistResult { + private final Playlist playlist; + private final String error; + + public PlaylistResult(Playlist playlist, String error) { + this.playlist = playlist; + this.error = error; + } + + public Playlist getPlaylist() { + return playlist; + } + + public String getError() { + return error; + } +} From 71b13d59c4181895ded04fd2e7b860c53c47271f Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 29 May 2023 21:35:56 +0200 Subject: [PATCH 2/2] Simplify PlaylistHelper#getUserPlaylist --- .../me/kavin/piped/server/ServerLauncher.java | 5 ++-- .../handlers/auth/AuthPlaylistHandlers.java | 30 +++++++------------ .../me/kavin/piped/utils/PlaylistHelpers.java | 9 +++--- .../me/kavin/piped/utils/PlaylistResult.java | 21 ------------- 4 files changed, 17 insertions(+), 48 deletions(-) delete mode 100644 src/main/java/me/kavin/piped/utils/PlaylistResult.java diff --git a/src/main/java/me/kavin/piped/server/ServerLauncher.java b/src/main/java/me/kavin/piped/server/ServerLauncher.java index b1cc688..f7f48e9 100644 --- a/src/main/java/me/kavin/piped/server/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/server/ServerLauncher.java @@ -37,8 +37,7 @@ import java.util.concurrent.TimeUnit; import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress; import static io.activej.http.HttpHeaders.*; -import static io.activej.http.HttpMethod.GET; -import static io.activej.http.HttpMethod.POST; +import static io.activej.http.HttpMethod.*; import static java.nio.charset.StandardCharsets.UTF_8; import static me.kavin.piped.consts.Constants.mapper; @@ -402,7 +401,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } catch (Exception e) { return getErrorResponse(e, request.getPath()); } - })).map(POST, "/user/playlists/description/change", AsyncServlet.ofBlocking(executor, request -> { + })).map(PATCH, "/user/playlists/description", AsyncServlet.ofBlocking(executor, request -> { try { var json = mapper.readTree(request.loadBody().getResult().asArray()); var playlistId = json.get("playlistId").textValue(); diff --git a/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java b/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java index 90ad25d..f6c6da3 100644 --- a/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java +++ b/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java @@ -23,6 +23,7 @@ import me.kavin.piped.utils.resp.AuthenticationFailureResponse; import me.kavin.piped.utils.resp.InvalidRequestResponse; import org.apache.commons.lang3.StringUtils; import org.hibernate.Session; +import org.hibernate.internal.util.ExceptionHelper; import org.schabi.newpipe.extractor.Page; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.playlist.PlaylistInfo; @@ -148,19 +149,15 @@ public class AuthPlaylistHandlers { ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); try (Session s = DatabaseSessionFactory.createSession()) { - var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId); - if (playlistResult.getError() != null) { - return mapper.writeValueAsBytes(mapper.createObjectNode() - .put("error", playlistResult.getError())); - } - var playlist = playlistResult.getPlaylist(); - + var playlist = PlaylistHelpers.getUserPlaylist(s, user, playlistId); playlist.setName(newName); var tr = s.beginTransaction(); s.merge(playlist); tr.commit(); + } catch (IllegalArgumentException e) { + ExceptionHandler.throwErrorResponse(new InvalidRequestResponse(e.getMessage())); } return mapper.writeValueAsBytes(new AcceptedResponse()); @@ -180,19 +177,15 @@ public class AuthPlaylistHandlers { ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); try (Session s = DatabaseSessionFactory.createSession()) { - var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId); - if (playlistResult.getError() != null) { - return mapper.writeValueAsBytes(mapper.createObjectNode() - .put("error", playlistResult.getError())); - } - var playlist = playlistResult.getPlaylist(); - + var playlist = PlaylistHelpers.getUserPlaylist(s, user, playlistId); playlist.setShortDescription(newDescription); var tr = s.beginTransaction(); s.merge(playlist); tr.commit(); + } catch (IllegalArgumentException e) { + ExceptionHandler.throwErrorResponse(new InvalidRequestResponse(e.getMessage())); } return mapper.writeValueAsBytes(new AcceptedResponse()); @@ -209,17 +202,14 @@ public class AuthPlaylistHandlers { ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse()); try (Session s = DatabaseSessionFactory.createSession()) { - var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId); - if (playlistResult.getError() != null) { - return mapper.writeValueAsBytes(mapper.createObjectNode() - .put("error", playlistResult.getError())); - } - var playlist = playlistResult.getPlaylist(); + var playlist = PlaylistHelpers.getUserPlaylist(s, user, playlistId); var tr = s.beginTransaction(); s.remove(playlist); tr.commit(); + } catch (IllegalArgumentException e) { + ExceptionHandler.throwErrorResponse(new InvalidRequestResponse(e.getMessage())); } return mapper.writeValueAsBytes(new AcceptedResponse()); diff --git a/src/main/java/me/kavin/piped/utils/PlaylistHelpers.java b/src/main/java/me/kavin/piped/utils/PlaylistHelpers.java index 884c438..f5ea3ea 100644 --- a/src/main/java/me/kavin/piped/utils/PlaylistHelpers.java +++ b/src/main/java/me/kavin/piped/utils/PlaylistHelpers.java @@ -1,19 +1,20 @@ package me.kavin.piped.utils; +import me.kavin.piped.utils.obj.db.Playlist; import org.hibernate.Session; import me.kavin.piped.utils.obj.db.User; public class PlaylistHelpers { - public static PlaylistResult getUserPlaylist(Session s, User user, String playlistId) { + public static Playlist getUserPlaylist(Session s, User user, String playlistId) throws IllegalArgumentException { var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId); if (playlist == null) - return new PlaylistResult(null, "Playlist not found"); + throw new IllegalArgumentException("Playlist not found"); if (playlist.getOwner().getId() != user.getId()) - return new PlaylistResult(null, "You do not own this playlist"); + throw new IllegalArgumentException("You do not own this playlist"); - return new PlaylistResult(playlist, null); + return playlist; } } diff --git a/src/main/java/me/kavin/piped/utils/PlaylistResult.java b/src/main/java/me/kavin/piped/utils/PlaylistResult.java deleted file mode 100644 index 49cf4ad..0000000 --- a/src/main/java/me/kavin/piped/utils/PlaylistResult.java +++ /dev/null @@ -1,21 +0,0 @@ -package me.kavin.piped.utils; - -import me.kavin.piped.utils.obj.db.Playlist; - -public final class PlaylistResult { - private final Playlist playlist; - private final String error; - - public PlaylistResult(Playlist playlist, String error) { - this.playlist = playlist; - this.error = error; - } - - public Playlist getPlaylist() { - return playlist; - } - - public String getError() { - return error; - } -}