Fix remaining issues with local playlists

This commit is contained in:
Bnyro 2023-06-15 22:41:26 +02:00
parent 37d6423e08
commit a52c511d29
3 changed files with 16 additions and 24 deletions

View File

@ -28,7 +28,7 @@ export default {
required: true, required: true,
}, },
videoId: { videoId: {
type: Object, type: String,
required: true, required: true,
}, },
}, },

View File

@ -177,17 +177,7 @@ export default {
methods: { methods: {
removeVideo() { removeVideo() {
this.$refs.removeButton.disabled = true; this.$refs.removeButton.disabled = true;
this.fetchJson(this.authApiUrl() + "/user/playlists/remove", null, { this.removeVideoFromPlaylist(this.playlistId, null, this.index).then(json => {
method: "POST",
body: JSON.stringify({
playlistId: this.playlistId,
index: this.index,
}),
headers: {
Authorization: this.getAuthToken(),
"Content-Type": "application/json",
},
}).then(json => {
if (json.error) alert(json.error); if (json.error) alert(json.error);
else this.$emit("remove"); else this.$emit("remove");
}); });

View File

@ -316,7 +316,7 @@ const mixin = {
}, },
// needs to handle both, streamInfo items and streams items // needs to handle both, streamInfo items and streams items
createLocalPlaylistVideo(videoId, videoInfo) { createLocalPlaylistVideo(videoId, videoInfo) {
if (videoInfo === null || videoId === null) return; if (videoInfo === undefined || videoId === null) return;
var tx = window.db.transaction("playlistVideos", "readwrite"); var tx = window.db.transaction("playlistVideos", "readwrite");
var store = tx.objectStore("playlistVideos"); var store = tx.objectStore("playlistVideos");
@ -326,7 +326,7 @@ const mixin = {
type: "stream", type: "stream",
shortDescription: videoInfo.shortDescription ?? videoInfo.description, shortDescription: videoInfo.shortDescription ?? videoInfo.description,
url: `/watch?v=${videoId}`, url: `/watch?v=${videoId}`,
thumbnailUrl: videoInfo.thumbnailUrl, thumbnail: videoInfo.thumbnail ?? videoInfo.thumbnailUrl,
uploaderVerified: videoInfo.uploaderVerified, uploaderVerified: videoInfo.uploaderVerified,
duration: videoInfo.duration, duration: videoInfo.duration,
uploaderAvatar: videoInfo.uploaderAvatar, uploaderAvatar: videoInfo.uploaderAvatar,
@ -341,7 +341,7 @@ const mixin = {
const req = store.openCursor(videoId); const req = store.openCursor(videoId);
let video = null; let video = null;
req.onsuccess = e => { req.onsuccess = e => {
video = e.target.result; video = e.target.result.value;
}; };
while (video == null) { while (video == null) {
await this.delay(10); await this.delay(10);
@ -384,7 +384,8 @@ const mixin = {
const playlist = await this.getLocalPlaylist(playlistId); const playlist = await this.getLocalPlaylist(playlistId);
const videoIds = JSON.parse(playlist.videoIds); const videoIds = JSON.parse(playlist.videoIds);
const videosFuture = videoIds.map(videoId => this.getLocalPlaylistVideo(videoId)); const videosFuture = videoIds.map(videoId => this.getLocalPlaylistVideo(videoId));
playlist.relatedStreams = Promise.all(videosFuture); playlist.relatedStreams = await Promise.all(videosFuture);
console.log(playlist);
return playlist; return playlist;
} }
@ -392,7 +393,8 @@ const mixin = {
}, },
async createPlaylist(name) { async createPlaylist(name) {
if (!this.authenticated) { if (!this.authenticated) {
const playlistId = "local-1"; const uuid = crypto.randomUUID();
const playlistId = `local-${uuid}`;
this.createOrUpdateLocalPlaylist({ this.createOrUpdateLocalPlaylist({
playlistId: playlistId, playlistId: playlistId,
// remapping needed for the playlists page // remapping needed for the playlists page
@ -479,9 +481,9 @@ const mixin = {
if (!this.authenticated) { if (!this.authenticated) {
const playlist = await this.getLocalPlaylist(playlistId); const playlist = await this.getLocalPlaylist(playlistId);
const currentVideoIds = JSON.parse(playlist.videoIds); const currentVideoIds = JSON.parse(playlist.videoIds);
if (currentVideoIds.length == 0) playlist.thumbnailUrl = videoInfos[0].thumbnailUrl; if (currentVideoIds.length == 0) playlist.thumbnail = videoInfos[0].thumbnail;
videoIds.push(...videoIds); currentVideoIds.push(...videoIds);
playlist.videoIds = JSON.stringify(videoIds); playlist.videoIds = JSON.stringify(currentVideoIds);
this.createOrUpdateLocalPlaylist(playlist); this.createOrUpdateLocalPlaylist(playlist);
for (let i in videoIds) { for (let i in videoIds) {
this.createLocalPlaylistVideo(videoIds[i], videoInfos[i]); this.createLocalPlaylistVideo(videoIds[i], videoInfos[i]);
@ -501,22 +503,22 @@ const mixin = {
}, },
}); });
}, },
async removeVideoFromPlaylist(playlistId, videoId) { async removeVideoFromPlaylist(playlistId, videoId, index) {
if (!this.authenticated) { if (!this.authenticated) {
const playlist = await this.getLocalPlaylist(playlistId); const playlist = await this.getLocalPlaylist(playlistId);
const videoIds = JSON.parse(playlist.videoIds); const videoIds = JSON.parse(playlist.videoIds);
videoIds.splice(videoIds.indexOf(videoId), 1); videoIds.splice(videoIds.indexOf(videoId), 1);
playlist.videoIds = JSON.stringify(videoIds); playlist.videoIds = JSON.stringify(videoIds);
if (videoIds.length == 0) playlist.thumbnailUrl = ""; if (videoIds.length == 0) playlist.thumbnail = "https://pipedproxy.kavin.rocks/?host=i.ytimg.com";
this.createOrUpdateLocalPlaylist(playlist); this.createOrUpdateLocalPlaylist(playlist);
return { message: "ok" }; return { message: "ok" };
} }
return await this.fetchJson(this.authApiUrl() + "/user/playlists/add", null, { return await this.fetchJson(this.authApiUrl() + "/user/playlists/remove", null, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
playlistId: playlistId, playlistId: playlistId,
videoId: videoId, index: index,
}), }),
headers: { headers: {
Authorization: this.getAuthToken(), Authorization: this.getAuthToken(),