mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Add route to add video to playlist.
This commit is contained in:
parent
797a621b51
commit
00e32dc992
5 changed files with 103 additions and 22 deletions
|
@ -291,6 +291,15 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
|
|||
} catch (Exception e) {
|
||||
return getErrorResponse(e, request.getPath());
|
||||
}
|
||||
})).map(POST, "/user/playlists/add", AsyncServlet.ofBlocking(executor, request -> {
|
||||
try {
|
||||
var json = Constants.mapper.readTree(request.loadBody().getResult().asArray());
|
||||
var playlistId = json.get("playlistId").asText();
|
||||
var videoId = json.get("videoId").asText();
|
||||
return getJsonResponse(ResponseHelper.addToPlaylistResponse(request.getHeader(AUTHORIZATION), playlistId, videoId), "private");
|
||||
} catch (Exception e) {
|
||||
return getErrorResponse(e, request.getPath());
|
||||
}
|
||||
})).map(GET, "/registered/badge", AsyncServlet.ofBlocking(executor, request -> {
|
||||
try {
|
||||
return HttpResponse.ofCode(302).withHeader(LOCATION, ResponseHelper.registeredBadgeRedirect())
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package me.kavin.piped.utils;
|
||||
|
||||
import me.kavin.piped.utils.obj.db.Channel;
|
||||
import me.kavin.piped.utils.obj.db.PubSub;
|
||||
import me.kavin.piped.utils.obj.db.User;
|
||||
import me.kavin.piped.utils.obj.db.Video;
|
||||
import me.kavin.piped.utils.obj.db.*;
|
||||
import org.hibernate.Session;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
|
@ -13,7 +10,7 @@ import javax.persistence.criteria.Root;
|
|||
|
||||
public class DatabaseHelper {
|
||||
|
||||
public static final User getUserFromSession(String session) {
|
||||
public static User getUserFromSession(String session) {
|
||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||
CriteriaQuery<User> cr = cb.createQuery(User.class);
|
||||
|
@ -24,7 +21,7 @@ public class DatabaseHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public static final User getUserFromSessionWithSubscribed(String session) {
|
||||
public static User getUserFromSessionWithSubscribed(String session) {
|
||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||
CriteriaQuery<User> cr = cb.createQuery(User.class);
|
||||
|
@ -36,7 +33,7 @@ public class DatabaseHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public static final Channel getChannelFromId(Session s, String id) {
|
||||
public static Channel getChannelFromId(Session s, String id) {
|
||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Channel> cr = cb.createQuery(Channel.class);
|
||||
Root<Channel> root = cr.from(Channel.class);
|
||||
|
@ -45,7 +42,7 @@ public class DatabaseHelper {
|
|||
return s.createQuery(cr).uniqueResult();
|
||||
}
|
||||
|
||||
public static final Video getVideoFromId(Session s, String id) {
|
||||
public static Video getVideoFromId(Session s, String id) {
|
||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Video> cr = cb.createQuery(Video.class);
|
||||
Root<Video> root = cr.from(Video.class);
|
||||
|
@ -54,7 +51,16 @@ public class DatabaseHelper {
|
|||
return s.createQuery(cr).uniqueResult();
|
||||
}
|
||||
|
||||
public static final PubSub getPubSubFromId(Session s, String id) {
|
||||
public static PlaylistVideo getPlaylistVideoFromId(Session s, String id) {
|
||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||
CriteriaQuery<PlaylistVideo> cr = cb.createQuery(PlaylistVideo.class);
|
||||
Root<PlaylistVideo> root = cr.from(PlaylistVideo.class);
|
||||
cr.select(root).where(cb.equal(root.get("id"), id));
|
||||
|
||||
return s.createQuery(cr).uniqueResult();
|
||||
}
|
||||
|
||||
public static PubSub getPubSubFromId(Session s, String id) {
|
||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||
CriteriaQuery<PubSub> cr = cb.createQuery(PubSub.class);
|
||||
Root<PubSub> root = cr.from(PubSub.class);
|
||||
|
|
|
@ -11,6 +11,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
|||
import me.kavin.piped.consts.Constants;
|
||||
import me.kavin.piped.ipfs.IPFS;
|
||||
import me.kavin.piped.utils.obj.*;
|
||||
import me.kavin.piped.utils.obj.db.PlaylistVideo;
|
||||
import me.kavin.piped.utils.obj.db.PubSub;
|
||||
import me.kavin.piped.utils.obj.db.User;
|
||||
import me.kavin.piped.utils.obj.db.Video;
|
||||
|
@ -300,7 +301,11 @@ public class ResponseHelper {
|
|||
cq.select(root);
|
||||
cq.where(cb.equal(root.get("playlist_id"), UUID.fromString(playlistId)));
|
||||
var query = s.createQuery(cq);
|
||||
var pl = query.getSingleResult();
|
||||
var pl = query.uniqueResult();
|
||||
|
||||
if (pl == null)
|
||||
return Constants.mapper.writeValueAsBytes(Constants.mapper.createObjectNode()
|
||||
.put("error", "Playlist not found"));
|
||||
|
||||
final List<StreamItem> relatedStreams = new ObjectArrayList<>();
|
||||
|
||||
|
@ -1002,6 +1007,71 @@ public class ResponseHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public static byte[] addToPlaylistResponse(String session, String playlistId, String videoId) throws IOException, ExtractionException {
|
||||
|
||||
if (StringUtils.isBlank(playlistId) || StringUtils.isBlank(videoId))
|
||||
return Constants.mapper.writeValueAsBytes(new InvalidRequestResponse());
|
||||
|
||||
var user = DatabaseHelper.getUserFromSession(session);
|
||||
|
||||
if (user == null)
|
||||
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||
|
||||
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);
|
||||
root.fetch("videos", JoinType.LEFT);
|
||||
root.fetch("owner", JoinType.LEFT);
|
||||
query.where(cb.equal(root.get("playlist_id"), UUID.fromString(playlistId)));
|
||||
var playlist = s.createQuery(query).uniqueResult();
|
||||
|
||||
if (playlist == null)
|
||||
return Constants.mapper.writeValueAsBytes(Constants.mapper.createObjectNode()
|
||||
.put("error", "Playlist not found"));
|
||||
|
||||
if (playlist.getOwner().getId() != user.getId())
|
||||
return Constants.mapper.writeValueAsBytes(Constants.mapper.createObjectNode()
|
||||
.put("error", "You are not the owner this playlist"));
|
||||
|
||||
var video = DatabaseHelper.getPlaylistVideoFromId(s, videoId);
|
||||
|
||||
if (video == null) {
|
||||
StreamInfo info = StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId);
|
||||
|
||||
String channelId = StringUtils.substringAfter(info.getUploaderUrl(), "/channel/");
|
||||
|
||||
var channel = DatabaseHelper.getChannelFromId(s, channelId);
|
||||
|
||||
if (channel == null) {
|
||||
ChannelInfo channelInfo = ChannelInfo.getInfo(info.getUploaderUrl());
|
||||
|
||||
channel = new me.kavin.piped.utils.obj.db.Channel(channelId, channelInfo.getName(),
|
||||
channelInfo.getAvatarUrl(), channelInfo.isVerified());
|
||||
s.save(channel);
|
||||
|
||||
if (!s.getTransaction().isActive())
|
||||
s.getTransaction().begin();
|
||||
}
|
||||
|
||||
video = new PlaylistVideo(videoId, info.getName(), info.getThumbnailUrl(), info.getDuration(), channel);
|
||||
|
||||
s.save(video);
|
||||
|
||||
if (!s.getTransaction().isActive())
|
||||
s.getTransaction().begin();
|
||||
}
|
||||
|
||||
playlist.getVideos().add(video);
|
||||
|
||||
if (!s.getTransaction().isActive())
|
||||
s.getTransaction().begin();
|
||||
s.getTransaction().commit();
|
||||
|
||||
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
|
||||
}
|
||||
}
|
||||
|
||||
public static String registeredBadgeRedirect() {
|
||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||
long registered = (Long) s.createQuery("select count(*) from User").uniqueResult();
|
||||
|
|
|
@ -1,13 +1,6 @@
|
|||
package me.kavin.piped.utils.obj.db;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Index;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "playlist_videos", indexes = {@Index(columnList = "id", name = "playlist_videos_id_idx"),
|
||||
|
@ -17,11 +10,11 @@ public class PlaylistVideo {
|
|||
public PlaylistVideo() {
|
||||
}
|
||||
|
||||
public PlaylistVideo(String id, String title, long duration, String thumbnail, Channel channel) {
|
||||
public PlaylistVideo(String id, String title, String thumbnail, long duration, Channel channel) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.duration = duration;
|
||||
this.thumbnail = thumbnail;
|
||||
this.duration = duration;
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
|
|
|
@ -98,3 +98,6 @@ PLAYLIST_ID=$(curl -s -o - -f $HOST/user/playlists -H "Authorization: $AUTH_TOKE
|
|||
|
||||
# Playlist Test
|
||||
curl ${CURLOPTS[@]} $HOST/playlists/$PLAYLIST_ID || exit 1
|
||||
|
||||
# Add to Playlist Test
|
||||
curl ${CURLOPTS[@]} $HOST/user/playlists/add -X POST -H "Content-Type: application/json" -H "Authorization: $AUTH_TOKEN" -d $(jq -n --compact-output --arg videoId "BtN-goy9VOY" --arg playlistId $PLAYLIST_ID '{"videoId": $videoId, "playlistId": $playlistId}') || exit 1
|
||||
|
|
Loading…
Reference in a new issue