mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Add route to change playlist description and add PlaylistsHelper
This commit is contained in:
parent
914afee632
commit
86152a457a
4 changed files with 94 additions and 17 deletions
|
@ -47,7 +47,6 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
|
||||||
private static final HttpHeader FILE_NAME = HttpHeaders.of("x-file-name");
|
private static final HttpHeader FILE_NAME = HttpHeaders.of("x-file-name");
|
||||||
private static final HttpHeader LAST_ETAG = HttpHeaders.of("x-last-etag");
|
private static final HttpHeader LAST_ETAG = HttpHeaders.of("x-last-etag");
|
||||||
|
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
Executor executor() {
|
Executor executor() {
|
||||||
return Multithreading.getCachedExecutor();
|
return Multithreading.getCachedExecutor();
|
||||||
|
@ -403,6 +402,18 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return getErrorResponse(e, request.getPath());
|
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 -> {
|
})).map(GET, "/user/playlists", AsyncServlet.ofBlocking(executor, request -> {
|
||||||
try {
|
try {
|
||||||
return getJsonResponse(AuthPlaylistHandlers.playlistsResponse(request.getHeader(AUTHORIZATION)), "private");
|
return getJsonResponse(AuthPlaylistHandlers.playlistsResponse(request.getHeader(AUTHORIZATION)), "private");
|
||||||
|
|
|
@ -148,15 +148,12 @@ public class AuthPlaylistHandlers {
|
||||||
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());
|
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());
|
||||||
|
|
||||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId);
|
var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId);
|
||||||
|
if (playlistResult.getError() != null) {
|
||||||
if (playlist == null)
|
|
||||||
return mapper.writeValueAsBytes(mapper.createObjectNode()
|
return mapper.writeValueAsBytes(mapper.createObjectNode()
|
||||||
.put("error", "Playlist not found"));
|
.put("error", playlistResult.getError()));
|
||||||
|
}
|
||||||
if (playlist.getOwner().getId() != user.getId())
|
var playlist = playlistResult.getPlaylist();
|
||||||
return mapper.writeValueAsBytes(mapper.createObjectNode()
|
|
||||||
.put("error", "You do not own this playlist"));
|
|
||||||
|
|
||||||
playlist.setName(newName);
|
playlist.setName(newName);
|
||||||
|
|
||||||
|
@ -169,6 +166,38 @@ public class AuthPlaylistHandlers {
|
||||||
return mapper.writeValueAsBytes(new AcceptedResponse());
|
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 {
|
public static byte[] deletePlaylistResponse(String session, String playlistId) throws IOException {
|
||||||
|
|
||||||
if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId))
|
if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId))
|
||||||
|
@ -180,15 +209,12 @@ public class AuthPlaylistHandlers {
|
||||||
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());
|
ExceptionHandler.throwErrorResponse(new AuthenticationFailureResponse());
|
||||||
|
|
||||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
var playlist = DatabaseHelper.getPlaylistFromId(s, playlistId);
|
var playlistResult = PlaylistHelpers.getUserPlaylist(s, user, playlistId);
|
||||||
|
if (playlistResult.getError() != null) {
|
||||||
if (playlist == null)
|
|
||||||
return mapper.writeValueAsBytes(mapper.createObjectNode()
|
return mapper.writeValueAsBytes(mapper.createObjectNode()
|
||||||
.put("error", "Playlist not found"));
|
.put("error", playlistResult.getError()));
|
||||||
|
}
|
||||||
if (playlist.getOwner().getId() != user.getId())
|
var playlist = playlistResult.getPlaylist();
|
||||||
return mapper.writeValueAsBytes(mapper.createObjectNode()
|
|
||||||
.put("error", "You do not own this playlist"));
|
|
||||||
|
|
||||||
var tr = s.beginTransaction();
|
var tr = s.beginTransaction();
|
||||||
s.remove(playlist);
|
s.remove(playlist);
|
||||||
|
|
19
src/main/java/me/kavin/piped/utils/PlaylistHelpers.java
Normal file
19
src/main/java/me/kavin/piped/utils/PlaylistHelpers.java
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
21
src/main/java/me/kavin/piped/utils/PlaylistResult.java
Normal file
21
src/main/java/me/kavin/piped/utils/PlaylistResult.java
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue