Merge pull request #463 from Bnyro/playlists

Allow adding multiple videos to a playlist with one request
This commit is contained in:
Kavin 2022-11-26 19:48:41 +00:00 committed by GitHub
commit a654d22635
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 22 deletions

View file

@ -1,6 +1,7 @@
package me.kavin.piped.server; package me.kavin.piped.server;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.SyndFeedInput;
import io.activej.config.Config; import io.activej.config.Config;
@ -12,6 +13,7 @@ import io.activej.inject.annotation.Provides;
import io.activej.inject.module.AbstractModule; import io.activej.inject.module.AbstractModule;
import io.activej.inject.module.Module; import io.activej.inject.module.Module;
import io.activej.launchers.http.MultithreadedHttpServerLauncher; import io.activej.launchers.http.MultithreadedHttpServerLauncher;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.kavin.piped.consts.Constants; import me.kavin.piped.consts.Constants;
import me.kavin.piped.server.handlers.*; import me.kavin.piped.server.handlers.*;
import me.kavin.piped.server.handlers.auth.AuthPlaylistHandlers; import me.kavin.piped.server.handlers.auth.AuthPlaylistHandlers;
@ -377,8 +379,20 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
try { try {
var json = Constants.mapper.readTree(request.loadBody().getResult().asArray()); var json = Constants.mapper.readTree(request.loadBody().getResult().asArray());
var playlistId = json.get("playlistId").textValue(); var playlistId = json.get("playlistId").textValue();
var videoId = json.get("videoId").textValue(); var videoIds = new ObjectArrayList<String>();
return getJsonResponse(AuthPlaylistHandlers.addToPlaylistResponse(request.getHeader(AUTHORIZATION), playlistId, videoId), "private"); // backwards compatibility
var videoIdField = json.get("videoId");
if (videoIdField != null) {
videoIds.add(videoIdField.textValue());
}
var videoIdsField = json.get("videoIds");
if (videoIdsField != null) {
for (JsonNode node : videoIdsField) {
videoIds.add(node.textValue());
}
}
return getJsonResponse(AuthPlaylistHandlers.addToPlaylistResponse(request.getHeader(AUTHORIZATION), playlistId, videoIds), "private");
} catch (Exception e) { } catch (Exception e) {
return getErrorResponse(e, request.getPath()); return getErrorResponse(e, request.getPath());
} }

View file

@ -8,6 +8,7 @@ import com.rometools.rome.feed.synd.SyndFeedImpl;
import com.rometools.rome.io.SyndFeedOutput; import com.rometools.rome.io.SyndFeedOutput;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import jakarta.persistence.criteria.JoinType; import jakarta.persistence.criteria.JoinType;
import me.kavin.piped.consts.Constants; import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.*; import me.kavin.piped.utils.*;
@ -198,10 +199,14 @@ public class AuthPlaylistHandlers {
return mapper.writeValueAsBytes(new AcceptedResponse()); return mapper.writeValueAsBytes(new AcceptedResponse());
} }
public static byte[] addToPlaylistResponse(String session, String playlistId, String videoId) throws IOException, ExtractionException { public static byte[] addToPlaylistResponse(String session, String playlistId, List<String> videoIds) throws IOException, ExtractionException {
if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId) || StringUtils.isBlank(videoId)) videoIds = videoIds.stream()
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session, playlistId and videoId are required parameters")); .filter(StringUtils::isNotBlank)
.collect(Collectors.toList());
if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId) || videoIds.isEmpty())
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session, playlistId and videoId(s) are required parameters"));
var user = DatabaseHelper.getUserFromSession(session); var user = DatabaseHelper.getUserFromSession(session);
@ -223,7 +228,16 @@ public class AuthPlaylistHandlers {
return mapper.writeValueAsBytes(mapper.createObjectNode() return mapper.writeValueAsBytes(mapper.createObjectNode()
.put("error", "You are not the owner this playlist")); .put("error", "You are not the owner this playlist"));
var video = DatabaseHelper.getPlaylistVideoFromId(s, videoId); var playlistVideos = DatabaseHelper.getPlaylistVideosFromIds(s, new ObjectOpenHashSet<>(videoIds));
var videos = playlist.getVideos();
for (String videoId : videoIds) {
if (StringUtils.isEmpty(videoId)) continue;
var video = playlistVideos.stream().filter(v -> v.getId().equals(videoId))
.findFirst()
.orElse(null);
if (video == null) { if (video == null) {
StreamInfo info = StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId); StreamInfo info = StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId);
@ -238,16 +252,13 @@ public class AuthPlaylistHandlers {
video = new PlaylistVideo(videoId, info.getName(), info.getThumbnailUrl(), info.getDuration(), channel); video = new PlaylistVideo(videoId, info.getName(), info.getThumbnailUrl(), info.getDuration(), channel);
var tr = s.beginTransaction();
s.persist(video); s.persist(video);
tr.commit();
} }
if (playlist.getVideos().isEmpty()) if (playlist.getVideos().isEmpty()) playlist.setThumbnail(video.getThumbnail());
playlist.setThumbnail(video.getThumbnail());
playlist.getVideos().add(video); videos.add(video);
}
var tr = s.beginTransaction(); var tr = s.beginTransaction();
s.merge(playlist); s.merge(playlist);