From d7feb52dd890d919e0ea16dbbe5a5ea23cf61804 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 20 May 2023 16:29:54 -0500 Subject: [PATCH] Fix:Discard progress for local and server media progress #646 --- pages/item/_id/_episode/index.vue | 68 +++++++++++++++++++++---------- pages/item/_id/index.vue | 33 +++++++++------ 2 files changed, 68 insertions(+), 33 deletions(-) diff --git a/pages/item/_id/_episode/index.vue b/pages/item/_id/_episode/index.vue index cbed747b..7ad8ff7c 100644 --- a/pages/item/_id/_episode/index.vue +++ b/pages/item/_id/_episode/index.vue @@ -144,10 +144,23 @@ export default { libraryItemId() { return this.libraryItem.id }, - localLibraryItemId() { - if (this.localLibraryItem) return this.localLibraryItem.id + isConnectedToServer() { + if (!this.isLocal) return true + if (!this.libraryItem.serverAddress) return false + return this.$store.getters['user/getServerAddress'] === this.libraryItem.serverAddress + }, + serverLibraryItemId() { + if (!this.isLocal) return this.libraryItemId + // Check if local library item is connected to the current server + if (!this.libraryItem.libraryItemId) return null + if (this.isConnectedToServer) { + return this.libraryItem.libraryItemId + } return null }, + localLibraryItemId() { + return this.localLibraryItem?.id || null + }, localEpisode() { if (this.isLocal) return this.episode return this.episode.localEpisode @@ -156,6 +169,13 @@ export default { if (this.localEpisode) return this.localEpisode.id return null }, + serverEpisodeId() { + if (!this.isLocal) return this.episode.id + if (this.isConnectedToServer) { + return this.episode.serverEpisodeId + } + return null + }, podcast() { return this.libraryItem.media }, @@ -202,16 +222,16 @@ export default { return this.$store.state.playerIsPlaying && this.isPlaying }, userItemProgress() { - if (this.isLocal) return this.$store.getters['globals/getLocalMediaProgressById'](this.libraryItemId, this.episode.id) - return this.$store.getters['user/getUserMediaProgress'](this.libraryItemId, this.episode.id) + if (this.isLocal) return this.localItemProgress + return this.serverItemProgress }, - localMediaProgress() { - if (this.isLocal) return this.$store.getters['globals/getLocalMediaProgressById'](this.libraryItemId, this.episode.id) - else if (this.localLibraryItemId && this.localEpisodeId) { - return this.$store.getters['globals/getLocalMediaProgressById'](this.localLibraryItemId, this.localEpisodeId) - } else { - return null - } + localItemProgress() { + if (!this.localLibraryItemId || !this.localEpisodeId) return null + return this.$store.getters['globals/getLocalMediaProgressById'](this.localLibraryItemId, this.localEpisodeId) + }, + serverItemProgress() { + if (!this.serverLibraryItemId || !this.serverEpisodeId) return null + return this.$store.getters['user/getUserMediaProgress'](this.serverLibraryItemId, this.serverEpisodeId) }, progressPercent() { return this.userItemProgress ? this.userItemProgress.progress : 0 @@ -293,13 +313,15 @@ export default { this.$eventBus.$emit('play-item', { libraryItemId: this.localLibraryItemId, episodeId: this.localEpisodeId, - serverLibraryItemId: this.libraryItemId, - serverEpisodeId: this.episode.id + serverLibraryItemId: this.serverLibraryItemId, + serverEpisodeId: this.serverEpisodeId }) } else { this.$eventBus.$emit('play-item', { libraryItemId: this.libraryItemId, - episodeId: this.episode.id + episodeId: this.episode.id, + serverLibraryItemId: this.serverLibraryItemId, + serverEpisodeId: this.serverEpisodeId }) } } @@ -395,22 +417,26 @@ export default { }) if (value) { this.resettingProgress = true - if (this.isLocal) { - // TODO: If connected to server also sync with server - await this.$db.removeLocalMediaProgress(this.localMediaProgress.id) - this.$store.commit('globals/removeLocalMediaProgressForItem', this.localMediaProgress.id) - } else { + + const serverItemProgressId = this.serverItemProgress?.id + if (this.localItemProgress) { + await this.$db.removeLocalMediaProgress(this.localItemProgress.id) + this.$store.commit('globals/removeLocalMediaProgress', this.localItemProgress.id) + } + + if (serverItemProgressId) { await this.$axios - .$delete(`/api/me/progress/${this.userItemProgress.id}`) + .$delete(`/api/me/progress/${serverItemProgressId}`) .then(() => { console.log('Progress reset complete') this.$toast.success(`Your progress was reset`) - this.$store.commit('user/removeMediaProgress', this.userItemProgress.id) + this.$store.commit('user/removeMediaProgress', serverItemProgressId) }) .catch((error) => { console.error('Progress reset failed', error) }) } + this.resettingProgress = false } }, diff --git a/pages/item/_id/index.vue b/pages/item/_id/index.vue index 74e8755d..b008acf7 100644 --- a/pages/item/_id/index.vue +++ b/pages/item/_id/index.vue @@ -204,12 +204,12 @@ export default { return this.libraryItem.localLibraryItem || null }, localLibraryItemId() { - return this.localLibraryItem ? this.localLibraryItem.id : null + return this.localLibraryItem?.id || null }, localLibraryItemEpisodes() { if (!this.isPodcast || !this.localLibraryItem) return [] var podcastMedia = this.localLibraryItem.media - return podcastMedia ? podcastMedia.episodes || [] : [] + return podcastMedia?.episodes || [] }, serverLibraryItemId() { if (!this.isLocal) return this.libraryItem.id @@ -293,8 +293,16 @@ export default { }, userItemProgress() { if (this.isPodcast) return null - if (this.isLocal) return this.$store.getters['globals/getLocalMediaProgressById'](this.libraryItemId) - return this.$store.getters['user/getUserMediaProgress'](this.libraryItemId) + if (this.isLocal) return this.localItemProgress + return this.serverItemProgress + }, + localItemProgress() { + if (this.isPodcast) return null + return this.$store.getters['globals/getLocalMediaProgressById'](this.localLibraryItemId) + }, + serverItemProgress() { + if (this.isPodcast) return null + return this.$store.getters['user/getUserMediaProgress'](this.serverLibraryItemId) }, userIsFinished() { return this.userItemProgress ? !!this.userItemProgress.isFinished : false @@ -554,18 +562,19 @@ export default { }) if (value) { this.resettingProgress = true - if (this.isLocal) { - // TODO: If connected to server also sync with server - await this.$db.removeLocalMediaProgress(this.libraryItemId) - this.$store.commit('globals/removeLocalMediaProgressForItem', this.libraryItemId) - } else { - var progressId = this.userItemProgress.id + const serverMediaProgressId = this.serverItemProgress?.id + if (this.localLibraryItemId) { + await this.$db.removeLocalMediaProgress(this.localLibraryItemId) + this.$store.commit('globals/removeLocalMediaProgressForItem', this.localLibraryItemId) + } + + if (this.serverLibraryItemId && serverMediaProgressId) { await this.$axios - .$delete(`/api/me/progress/${this.libraryItemId}`) + .$delete(`/api/me/progress/${serverMediaProgressId}`) .then(() => { console.log('Progress reset complete') this.$toast.success(`Your progress was reset`) - this.$store.commit('user/removeMediaProgress', progressId) + this.$store.commit('user/removeMediaProgress', serverMediaProgressId) }) .catch((error) => { console.error('Progress reset failed', error)