Adding playlist database logic (#802)

* Added playlist database logic.

* Added grab.

* Added removeOnWatched.
This commit is contained in:
Violet Rose 2021-01-01 10:15:24 -08:00 committed by GitHub
parent cb42ef9b4c
commit 35345855c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 159 additions and 23 deletions

View File

@ -16,48 +16,184 @@ if (window && window.process && window.process.type === 'renderer') {
dbLocation = 'playlists.db' dbLocation = 'playlists.db'
} }
const subDb = new Datastore({ const playlistDb = new Datastore({
filename: dbLocation, filename: dbLocation,
autoload: true autoload: true
}) })
const state = { const state = {
activePlaylistId: '', playlists: [
activePlaylistVideoList: [], {
watchedVideosWithinPlaylist: [] _id: 'favorites',
protected: true,
videos: []
},
{
_id: 'watchLater',
protected: true,
removeOnWatched: true,
videos: []
}
]
} }
const mutations = { const getters = {
addSubscription (state, payload) { getAllPlaylists: () => state.playlists,
state.subscriptions.push(payload) getFavorites: () => state.playlists[0],
}, getPlaylist: (playlistId) => state.playlists.find(playlist => playlist._id === playlistId),
setSubscriptions (state, payload) { getWatchLater: () => state.playlists[1]
state.subscriptions = payload
}
} }
const actions = { const actions = {
addSubscriptions ({ commit }, payload) { addPlaylist ({ commit }, payload) {
subDb.insert(payload, (err, payload) => { playlistDb.insert(payload, (err, payload) => {
if (!err) { if (err) {
commit('addSubscription', payload) console.error(err)
} else {
commit('addPlaylist', payload)
} }
}) })
}, },
getSubscriptions ({ commit }, payload) { addPlaylists ({ commit }, payload) {
subDb.find({}, (err, payload) => { playlistDb.insert(payload, (err, payload) => {
if (!err) { if (err) {
commit('setSubscriptions', payload) console.error(err)
} else {
commit('addPlaylists', payload)
} }
}) })
}, },
removeSubscription ({ commit }, channelId) { addVideo ({ commit }, payload) {
subDb.remove({ channelId: channelId }, {}, () => { playlistDb.update({ _id: payload.playlistId }, { $push: { videos: payload.videoId } }, { upsert: true }, err => {
commit('setSubscriptions', this.state.subscriptions.filter(sub => sub.channelId !== channelId)) if (err) {
console.error(err)
} else {
commit('addVideo', payload)
}
})
},
addVideos ({ commit }, payload) {
playlistDb.update({ _id: payload.playlistId }, { $push: { videos: { $each: payload.videosIds } } }, { upsert: true }, err => {
if (err) {
console.error(err)
} else {
commit('addVideos', payload)
}
})
},
grabAllPlaylists({ commit }) {
playlistDb.getAllData((err, payload) => {
if (err) {
console.error(err)
} else {
commit('setAllPlaylists', payload)
}
})
},
removeAllPlaylists ({ commit }) {
playlistDb.remove({ protected: { $ne: true } }, err => {
if (err) {
console.error(err)
} else {
commit('removeAllPlaylists')
}
})
},
removeAllVideos ({ commit }, playlistId) {
playlistDb.update({ _id: playlistId }, { $set: { videos: [] } }, { upsert: true }, err => {
if (err) {
console.error(err)
} else {
commit('removeAllVideos', playlistId)
}
})
},
removePlaylist ({ commit }, playlistId) {
playlistDb.remove({ _id: playlistId, protected: { $ne: true } }, (err, playlistId) => {
if (err) {
console.error(err)
} else {
commit('removePlaylist', playlistId)
}
})
},
removePlaylists ({ commit }, playlistIds) {
playlistDb.remove({ _id: { $in: playlistIds }, protected: { $ne: true } }, (err, playlistIds) => {
if (err) {
console.error(err)
} else {
commit('removePlaylists', playlistIds)
}
})
},
removeVideo ({ commit }, payload) {
playlistDb.update({ _id: payload.playlistId }, { $pull: { videos: payload.videoId } }, { upsert: true }, err => {
if (err) {
console.error(err)
} else {
commit('removeVideo', payload)
}
})
},
removeVideos ({ commit }, payload) {
playlistDb.update({ _id: payload.playlistId }, { $pull: { videos: { $in: payload.videoIds } } }, { upsert: true }, err => {
if (err) {
console.error(err)
} else {
commit('removeVideos', payload)
}
}) })
} }
} }
const getters = {}
const mutations = {
addPlaylist (state, payload) {
state.playlists.push(payload)
},
addPlaylists (state, payload) {
state.playlists = state.playlists.concat(payload)
},
addVideo (state, payload) {
const playlist = state.playlists.find(playlist => playlist._id === payload.playlistId)
if (playlist) {
playlist.videos.push(payload.videoId)
}
},
addVideos (state, payload) {
const playlist = state.playlists.find(playlist => playlist._id === payload.playlistId)
if (playlist) {
playlist.videos = playlist.videos.concat(payload.playlistIds)
}
},
removeAllPlaylists (state) {
state.playlists = state.playlists.filter(playlist => playlist.protected !== true)
},
removeAllVideos (state, playlistId) {
const playlist = state.playlists.find(playlist => playlist._id === playlistId)
if (playlist) {
playlist.videos = []
}
},
removeVideo (state, payload) {
const playlist = state.playlists.find(playlist => playlist._id === payload.playlistId)
if (playlist) {
playlist.videos = playlist.videos.filter(video => video !== payload.videoId)
}
},
removeVideos (state, payload) {
const playlist = state.playlists.find(playlist => playlist._id === payload.playlistId)
if (playlist) {
playlist.videos = playlist.videos.filter(video => payload.videoIds.indexOf(video) === -1)
}
},
removePlaylist (state, playlistId) {
state.playlists = state.playlists.filter(playlist => playlist._id !== playlistId || playlist.protected)
},
setAllPlaylists (state, payload) {
state.playlists = payload
}
}
export default { export default {
state, state,
getters, getters,