diff --git a/src/main/java/me/kavin/piped/server/ServerLauncher.java b/src/main/java/me/kavin/piped/server/ServerLauncher.java index bccba11..1963b2b 100644 --- a/src/main/java/me/kavin/piped/server/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/server/ServerLauncher.java @@ -412,6 +412,14 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } catch (Exception e) { return getErrorResponse(e, request.getPath()); } + })).map(POST, "/user/playlists/clear", AsyncServlet.ofBlocking(executor, request -> { + try { + var json = Constants.mapper.readTree(request.loadBody().getResult().asArray()); + var playlistId = json.get("playlistId").textValue(); + return getJsonResponse(AuthPlaylistHandlers.clearPlaylistResponse(request.getHeader(AUTHORIZATION), playlistId), "private"); + } catch (Exception e) { + return getErrorResponse(e, request.getPath()); + } })).map(POST, "/user/playlists/rename", AsyncServlet.ofBlocking(executor, request -> { try { var json = Constants.mapper.readTree(request.loadBody().getResult().asArray()); 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 253598d..f1c6105 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 @@ -318,6 +318,36 @@ public class AuthPlaylistHandlers { } } + public static byte[] clearPlaylistResponse(String session, String playlistId) throws IOException { + + if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId)) + ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session and playlistId are required parameters")); + + try (Session s = DatabaseSessionFactory.createSession()) { + var cb = s.getCriteriaBuilder(); + var query = cb.createQuery(me.kavin.piped.utils.obj.db.Playlist.class); + var root = query.from(me.kavin.piped.utils.obj.db.Playlist.class); + query.where(cb.equal(root.get("playlist_id"), UUID.fromString(playlistId))); + var playlist = s.createQuery(query).uniqueResult(); + + if (playlist == null) + return mapper.writeValueAsBytes(mapper.createObjectNode() + .put("error", "Playlist not found")); + + if (playlist.getOwner().getId() != DatabaseHelper.getUserFromSession(session).getId()) + return mapper.writeValueAsBytes(mapper.createObjectNode() + .put("error", "You are not the owner this playlist")); + + playlist.setVideos(new ObjectArrayList<>()); + + var tr = s.beginTransaction(); + s.merge(playlist); + tr.commit(); + + return mapper.writeValueAsBytes(new AcceptedResponse()); + } + } + public static byte[] importPlaylistResponse(String session, String playlistId) throws IOException, ExtractionException { if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId)) diff --git a/testing/api-test.sh b/testing/api-test.sh index 3f2489c..9568d08 100755 --- a/testing/api-test.sh +++ b/testing/api-test.sh @@ -126,6 +126,9 @@ curl ${CURLOPTS[@]} $HOST/user/playlists/remove -X POST -H "Content-Type: applic # Rename Playlist Test curl ${CURLOPTS[@]} $HOST/user/playlists/delete -X POST -H "Content-Type: application/json" -H "Authorization: $AUTH_TOKEN" -d $(jq -n --compact-output --arg playlistId $PLAYLIST_ID --arg newName $RENAMED_PLAYLIST_NAME '{"playlistId": $playlistId, "newName": $newName}') || exit 1 +# Clear Playlist Test +curl ${CURLOPTS[@]} $HOST/user/playlists/clear -X POST -H "Content-Type: application/json" -H "Authorization: $AUTH_TOKEN" -d $(jq -n --compact-output --arg playlistId $PLAYLIST_ID '{"playlistId": $playlistId}') || exit 1 + # Delete Playlist Test curl ${CURLOPTS[@]} $HOST/user/playlists/delete -X POST -H "Content-Type: application/json" -H "Authorization: $AUTH_TOKEN" -d $(jq -n --compact-output --arg playlistId $PLAYLIST_ID '{"playlistId": $playlistId}') || exit 1