update playlist APIs

This commit is contained in:
IrosTheBeggar 2021-08-29 16:50:01 -04:00
parent e534a245d9
commit 1e2043c196
2 changed files with 80 additions and 75 deletions

View File

@ -30,7 +30,7 @@ exports.setup = (mstream) => {
}
try {
if (!db.getPlaylistCollection()) { throw 'DB Error'; }
if (!db.getPlaylistCollection()) { throw new Error('DB Error'); }
db.getPlaylistCollection().findAndRemove({
'$and': [
@ -60,7 +60,7 @@ exports.setup = (mstream) => {
}
try {
if (!db.getPlaylistCollection()) { throw 'No DB'; }
if (!db.getPlaylistCollection()) { throw new Error('No DB'); }
db.getPlaylistCollection().insert({
name: req.body.playlist,
filepath: req.body.song,
@ -84,8 +84,13 @@ exports.setup = (mstream) => {
}
try {
if (!db.getPlaylistCollection()) { throw 'No DB'; }
db.getPlaylistCollection().findAndRemove({ '$loki': req.body.lokiid });
if (!db.getPlaylistCollection()) { throw new Error('No DB'); }
const result = db.getPlaylistCollection().get(req.body.lokiid);
if (result.user !== req.user.username) {
throw new Error(`User ${req.user.username} tried accessing a resource they don't have access to. Playlist Loki ID: ${req.body.lokiid}`);
}
db.getPlaylistCollection().remove(result);
res.json({});
db.saveUserDB();
}catch (err) {

View File

@ -131,7 +131,7 @@ function renderFileWithMetadataHtml(filepath, lokiId, metadata) {
<span class="explorer-label-1">${(!metadata || !metadata.title) ? filepath : `${metadata.artist} - ${metadata.title}`}</span>
</div>
<div class="song-button-box">
<span data-lokiid="${lokiId}" class="removePlaylistSong">remove</span>
<span data-lokiid="${lokiId}" class="removePlaylistSong" onclick="removePlaylistSong(this);">remove</span>
</div>
</div>`;
}
@ -171,6 +171,15 @@ function createFileplaylistHtml(dataDirectory) {
</div>';
}
function renderPlaylist(playlistName) {
return `<div data-playlistname="${encodeURIComponent(playlistName)}" class="playlist_row_container">
<span data-playlistname="${encodeURIComponent(playlistName)}" class="playlistz force-width" onclick="onPlaylistClick(this);">${escapeHtml(playlistName)}</span>
<div class="song-button-box">
<span data-playlistname="${encodeURIComponent(playlistName)}" class="deletePlaylist" onclick="deletePlaylist(this);">Delete</span>
</div>
</div>`;
}
function createMusicfileHtml(fileLocation, title, titleClass) {
return `<div data-file_location="${fileLocation}" class="filez">
<svg class="music-image" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40"><path d="M9 37.5c-3.584 0-6.5-2.916-6.5-6.5s2.916-6.5 6.5-6.5a6.43 6.43 0 012.785.634l.715.34V5.429l25-3.846V29c0 3.584-2.916 6.5-6.5 6.5s-6.5-2.916-6.5-6.5 2.916-6.5 6.5-6.5a6.43 6.43 0 012.785.634l.715.34V11.023l-19 2.931V31c0 3.584-2.916 6.5-6.5 6.5z" fill="#8bb7f0"/><path d="M37 2.166V29c0 3.308-2.692 6-6 6s-6-2.692-6-6 2.692-6 6-6a5.93 5.93 0 012.57.586l1.43.68V10.441l-1.152.178-18 2.776-.848.13V31c0 3.308-2.692 6-6 6s-6-2.692-6-6 2.692-6 6-6a5.93 5.93 0 012.57.586l1.43.68V5.858l24-3.692M38 1L12 5v19.683A6.962 6.962 0 009 24a7 7 0 107 7V14.383l18-2.776v11.076A6.962 6.962 0 0031 22a7 7 0 107 7V1z" fill="#4e7ab5"/></svg>
@ -395,14 +404,7 @@ function getAllPlaylists(previousState, el) {
// loop through the json array and make an array of corresponding divs
let playlists = '';
response.forEach(p => {
console.log(p)
playlists +=
`<div data-playlistname="${encodeURIComponent(p.name)}" class="playlist_row_container">
<span data-playlistname="${encodeURIComponent(p.name)}" class="playlistz force-width">${escapeHtml(p.name)}</span>
<div class="song-button-box">
<span data-playlistname="${encodeURIComponent(p.name)}" class="deletePlaylist" onclick="deletePlaylist(this);">Delete</span>
</div>
</div>`;
playlists += renderPlaylist(p.name);
const lol = { name: p.name, type: 'playlist' };
currentBrowsingList.push(lol);
VUEPLAYER.playlists.push(lol);
@ -451,6 +453,65 @@ function deletePlaylist(el) {
});
}
function onPlaylistClick(el) {
var playlistname = decodeURIComponent(el.getAttribute('data-playlistname'));
document.getElementById('directoryName').innerHTML = 'Playlist: ' + playlistname;
document.getElementById('filelist').innerHTML = getLoadingSvg();
currentBrowsingList = [];
programState.push({
state: 'playlist',
name: playlistname,
previousScroll: document.getElementById('filelist').scrollTop,
previousSearch: $('#search_folders').val()
});
document.getElementById('search_folders').value = '';
MSTREAMAPI.loadPlaylist(playlistname, (response, error) => {
if (error !== false) {
document.getElementById('filelist').innerHTML = '<div>Server call failed</div>';
return boilerplateFailure(response, error);
}
// Add the playlist name to the modal
document.getElementById('playlist_name').value = playlistname;
let files = '';
response.forEach(value => {
currentBrowsingList.push({
type: 'file',
name: (!value.metadata || !value.metadata.title) ? value.filepath : `${value.metadata.artist} - ${value.metadata.title}`,
metadata: value.metadata,
filepath: value.filepath,
lokiId: value.lokiId
});
files += renderFileWithMetadataHtml(value.filepath, value.lokiId, value.metadata);
});
document.getElementById('filelist').innerHTML = files;
// update lazy load plugin
ll.update();
});
}
function removePlaylistSong(el) {
const lokiId = el.getAttribute('data-lokiid');
MSTREAMAPI.removePlaylistSong(lokiId, (response, error) => {
if (error !== false) {
return boilerplateFailure(response, error);
}
// remove from currentBrowsingList
currentBrowsingList = currentBrowsingList.filter(item =>{
return item.lokiId !== lokiId
});
document.querySelector(`div[data-lokiid="${lokiId}"]`).remove();
});
}
/////////////// Artists
function getAllArtists(previousState, el) {
setBrowserRootPanel(el, 'Artists', 'scrollBoxHeight1');
@ -1173,7 +1234,7 @@ $(document).ready(function () {
if (this.type === 'directory') {
filelist.push(renderDirHtml(this.name));
} else if (this.type === 'playlist') {
filelist.push('<div data-playlistname="' + encodeURIComponent(this.name) + '" class="playlist_row_container"><span data-playlistname="' + encodeURIComponent(this.name) + '" class="playlistz force-width">' + escapeHtml(this.name) + '</span><div class="song-button-box"><span data-playlistname="' + encodeURIComponent(this.name) + '" onclick="deletePlaylist(this);" class="deletePlaylist">Delete</span></div></div>');
filelist.push(renderPlaylist(this.name));
} else if (this.type === 'album') {
var artistString = this.artist ? 'data-artist="' + this.artist + '"' : '';
var albumString = this.name ? this.name : 'SINGLES';
@ -1320,67 +1381,6 @@ $(document).ready(function () {
});
});
$("#filelist").on('click', '.removePlaylistSong', function () {
const lokiId = $(this).data('lokiid');
MSTREAMAPI.removePlaylistSong(lokiId, function (response, error) {
if (error !== false) {
return boilerplateFailure(response, error);
}
// remove from currentBrowsingList
currentBrowsingList = currentBrowsingList.filter(item =>{
return item.lokiId !== lokiId
});
$('div[data-lokiid="' + lokiId + '"]').remove();
});
});
// load up a playlist
$("#filelist").on('click', '.playlistz', function () {
var playlistname = decodeURIComponent($(this).data('playlistname'));
var name = $(this).html();
$('.directoryName').html('Playlist: ' + name);
$('#filelist').html('<div class="loading-screen"><svg class="spinner" width="65px" height="65px" viewBox="0 0 66 66" xmlns="http://www.w3.org/2000/svg"><circle class="spinner-path" fill="none" stroke-width="6" stroke-linecap="round" cx="33" cy="33" r="30"></circle></svg></div>');
currentBrowsingList = [];
programState.push({
state: 'playlist',
name: playlistname,
previousScroll: document.getElementById('filelist').scrollTop,
previousSearch: $('#search_folders').val()
});
$('#search_folders').val('');
MSTREAMAPI.loadPlaylist(playlistname, (response, error) => {
if (error !== false) {
$('#filelist').html('<div>Server call failed</div>');
return boilerplateFailure(response, error);
}
// Add the playlist name to the modal
$('#playlist_name').val(name);
const files = [];
response.forEach(value => {
currentBrowsingList.push({
type: 'file',
name: (!value.metadata || !value.metadata.title) ? value.filepath : `${value.metadata.artist} - ${value.metadata.title}`,
metadata: value.metadata,
filepath: value.filepath,
lokiId: value.lokiId
});
files.push(renderFileWithMetadataHtml(value.filepath, value.lokiId, value.metadata));
});
$('#filelist').html(files);
// update lazy load plugin
ll.update();
});
});
/////////////// Download Playlist
$('#downloadPlaylist').click(function () {
// Loop through array and add each file to the playlist