Merge pull request #600 from Bnyro/user-playlist-description

Add route to change playlist description and add PlaylistsHelper
This commit is contained in:
Kavin 2023-05-29 21:26:03 +01:00 committed by GitHub
commit 332f500fa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 22 deletions

View File

@ -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;
@ -47,7 +46,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 +401,18 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(PATCH, "/user/playlists/description", 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");

View File

@ -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,22 +149,43 @@ public class AuthPlaylistHandlers {
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());
try (Session s = DatabaseSessionFactory.createSession()) {
var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId);
if (playlist == 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"));
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());
}
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 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());
@ -180,20 +202,14 @@ public class AuthPlaylistHandlers {
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());
try (Session s = DatabaseSessionFactory.createSession()) {
var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId);
if (playlist == 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"));
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());

View File

@ -0,0 +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 Playlist getUserPlaylist(Session s, User user, String playlistId) throws IllegalArgumentException {
var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId);
if (playlist == null)
throw new IllegalArgumentException("Playlist not found");
if (playlist.getOwner().getId() != user.getId())
throw new IllegalArgumentException("You do not own this playlist");
return playlist;
}
}