Fix:Discard progress for local and server media progress #646

This commit is contained in:
advplyr 2023-05-20 16:29:54 -05:00
parent fafaad8b5f
commit d7feb52dd8
2 changed files with 68 additions and 33 deletions

View file

@ -144,10 +144,23 @@ export default {
libraryItemId() { libraryItemId() {
return this.libraryItem.id return this.libraryItem.id
}, },
localLibraryItemId() { isConnectedToServer() {
if (this.localLibraryItem) return this.localLibraryItem.id 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 return null
}, },
localLibraryItemId() {
return this.localLibraryItem?.id || null
},
localEpisode() { localEpisode() {
if (this.isLocal) return this.episode if (this.isLocal) return this.episode
return this.episode.localEpisode return this.episode.localEpisode
@ -156,6 +169,13 @@ export default {
if (this.localEpisode) return this.localEpisode.id if (this.localEpisode) return this.localEpisode.id
return null return null
}, },
serverEpisodeId() {
if (!this.isLocal) return this.episode.id
if (this.isConnectedToServer) {
return this.episode.serverEpisodeId
}
return null
},
podcast() { podcast() {
return this.libraryItem.media return this.libraryItem.media
}, },
@ -202,16 +222,16 @@ export default {
return this.$store.state.playerIsPlaying && this.isPlaying return this.$store.state.playerIsPlaying && this.isPlaying
}, },
userItemProgress() { userItemProgress() {
if (this.isLocal) return this.$store.getters['globals/getLocalMediaProgressById'](this.libraryItemId, this.episode.id) if (this.isLocal) return this.localItemProgress
return this.$store.getters['user/getUserMediaProgress'](this.libraryItemId, this.episode.id) return this.serverItemProgress
}, },
localMediaProgress() { localItemProgress() {
if (this.isLocal) return this.$store.getters['globals/getLocalMediaProgressById'](this.libraryItemId, this.episode.id) if (!this.localLibraryItemId || !this.localEpisodeId) return null
else if (this.localLibraryItemId && this.localEpisodeId) {
return this.$store.getters['globals/getLocalMediaProgressById'](this.localLibraryItemId, this.localEpisodeId) return this.$store.getters['globals/getLocalMediaProgressById'](this.localLibraryItemId, this.localEpisodeId)
} else { },
return null serverItemProgress() {
} if (!this.serverLibraryItemId || !this.serverEpisodeId) return null
return this.$store.getters['user/getUserMediaProgress'](this.serverLibraryItemId, this.serverEpisodeId)
}, },
progressPercent() { progressPercent() {
return this.userItemProgress ? this.userItemProgress.progress : 0 return this.userItemProgress ? this.userItemProgress.progress : 0
@ -293,13 +313,15 @@ export default {
this.$eventBus.$emit('play-item', { this.$eventBus.$emit('play-item', {
libraryItemId: this.localLibraryItemId, libraryItemId: this.localLibraryItemId,
episodeId: this.localEpisodeId, episodeId: this.localEpisodeId,
serverLibraryItemId: this.libraryItemId, serverLibraryItemId: this.serverLibraryItemId,
serverEpisodeId: this.episode.id serverEpisodeId: this.serverEpisodeId
}) })
} else { } else {
this.$eventBus.$emit('play-item', { this.$eventBus.$emit('play-item', {
libraryItemId: this.libraryItemId, 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) { if (value) {
this.resettingProgress = true this.resettingProgress = true
if (this.isLocal) {
// TODO: If connected to server also sync with server const serverItemProgressId = this.serverItemProgress?.id
await this.$db.removeLocalMediaProgress(this.localMediaProgress.id) if (this.localItemProgress) {
this.$store.commit('globals/removeLocalMediaProgressForItem', this.localMediaProgress.id) await this.$db.removeLocalMediaProgress(this.localItemProgress.id)
} else { this.$store.commit('globals/removeLocalMediaProgress', this.localItemProgress.id)
}
if (serverItemProgressId) {
await this.$axios await this.$axios
.$delete(`/api/me/progress/${this.userItemProgress.id}`) .$delete(`/api/me/progress/${serverItemProgressId}`)
.then(() => { .then(() => {
console.log('Progress reset complete') console.log('Progress reset complete')
this.$toast.success(`Your progress was reset`) this.$toast.success(`Your progress was reset`)
this.$store.commit('user/removeMediaProgress', this.userItemProgress.id) this.$store.commit('user/removeMediaProgress', serverItemProgressId)
}) })
.catch((error) => { .catch((error) => {
console.error('Progress reset failed', error) console.error('Progress reset failed', error)
}) })
} }
this.resettingProgress = false this.resettingProgress = false
} }
}, },

View file

@ -204,12 +204,12 @@ export default {
return this.libraryItem.localLibraryItem || null return this.libraryItem.localLibraryItem || null
}, },
localLibraryItemId() { localLibraryItemId() {
return this.localLibraryItem ? this.localLibraryItem.id : null return this.localLibraryItem?.id || null
}, },
localLibraryItemEpisodes() { localLibraryItemEpisodes() {
if (!this.isPodcast || !this.localLibraryItem) return [] if (!this.isPodcast || !this.localLibraryItem) return []
var podcastMedia = this.localLibraryItem.media var podcastMedia = this.localLibraryItem.media
return podcastMedia ? podcastMedia.episodes || [] : [] return podcastMedia?.episodes || []
}, },
serverLibraryItemId() { serverLibraryItemId() {
if (!this.isLocal) return this.libraryItem.id if (!this.isLocal) return this.libraryItem.id
@ -293,8 +293,16 @@ export default {
}, },
userItemProgress() { userItemProgress() {
if (this.isPodcast) return null if (this.isPodcast) return null
if (this.isLocal) return this.$store.getters['globals/getLocalMediaProgressById'](this.libraryItemId) if (this.isLocal) return this.localItemProgress
return this.$store.getters['user/getUserMediaProgress'](this.libraryItemId) 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() { userIsFinished() {
return this.userItemProgress ? !!this.userItemProgress.isFinished : false return this.userItemProgress ? !!this.userItemProgress.isFinished : false
@ -554,18 +562,19 @@ export default {
}) })
if (value) { if (value) {
this.resettingProgress = true this.resettingProgress = true
if (this.isLocal) { const serverMediaProgressId = this.serverItemProgress?.id
// TODO: If connected to server also sync with server if (this.localLibraryItemId) {
await this.$db.removeLocalMediaProgress(this.libraryItemId) await this.$db.removeLocalMediaProgress(this.localLibraryItemId)
this.$store.commit('globals/removeLocalMediaProgressForItem', this.libraryItemId) this.$store.commit('globals/removeLocalMediaProgressForItem', this.localLibraryItemId)
} else { }
var progressId = this.userItemProgress.id
if (this.serverLibraryItemId && serverMediaProgressId) {
await this.$axios await this.$axios
.$delete(`/api/me/progress/${this.libraryItemId}`) .$delete(`/api/me/progress/${serverMediaProgressId}`)
.then(() => { .then(() => {
console.log('Progress reset complete') console.log('Progress reset complete')
this.$toast.success(`Your progress was reset`) this.$toast.success(`Your progress was reset`)
this.$store.commit('user/removeMediaProgress', progressId) this.$store.commit('user/removeMediaProgress', serverMediaProgressId)
}) })
.catch((error) => { .catch((error) => {
console.error('Progress reset failed', error) console.error('Progress reset failed', error)