From 9f718dc23816929d8d3c1edca495c26d6f5bd933 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 16 Apr 2023 11:26:57 +0530 Subject: [PATCH] Add watched videos to a playlist --- src/main.js | 1 + src/routes/WatchVideo.vue | 8 ++-- src/store/index.js | 4 +- src/store/watched-videos-db.js | 5 ++- src/store/watched-videos-playlist.js | 60 ++++++++++++++++++++++++++++ src/translations/en.json | 3 +- 6 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 src/store/watched-videos-playlist.js diff --git a/src/main.js b/src/main.js index c492b71..695b5aa 100644 --- a/src/main.js +++ b/src/main.js @@ -16,6 +16,7 @@ Vue.config.productionTip = false Promise.all([ store.dispatch('prefs/loadState'), store.dispatch('auth/initializeAuth'), + store.dispatch('plHistory/resolvePlaylistID'), i18nInitialized ]).then(() => { new Vue({ diff --git a/src/routes/WatchVideo.vue b/src/routes/WatchVideo.vue index 02ea047..f0a3f10 100644 --- a/src/routes/WatchVideo.vue +++ b/src/routes/WatchVideo.vue @@ -383,14 +383,14 @@ export default { .replaceAll('\n', '
') ) try { - if (!this.$store.getters['prefs/getPreference']('disableDuplicateHistoryEntries', false)) { - this.dbID = await addWatchedVideo(video) + if (!this.$store.getters['prefs/getPreference']('disableDuplicateHistoryEntries', false) && false) { + this.dbID = await addWatchedVideo(this.$store, video) this.lastWatch = await findLastWatch(this.videoId) } else { let dbObj = await findLastWatch(this.videoId) if (dbObj == null) { - this.dbID = await addWatchedVideo(video) + this.dbID = await addWatchedVideo(this.$store, video) dbObj = await findLastWatch(this.videoId) } else { this.dbID = dbObj.id @@ -415,7 +415,7 @@ export default { if (this.dbID == null || !this.$refs.player) { return } - return updateWatchedVideoProgress(this.dbID, this.$refs.player.getCurrentTime(), this.video.duration) + return updateWatchedVideoProgress(this.$store, this.dbID, this.$refs.player.getCurrentTime(), this.video.duration) }, 500) }, computed: { diff --git a/src/store/index.js b/src/store/index.js index ab79f8b..ddcabff 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -3,6 +3,7 @@ import Vuex from 'vuex' import { PrefsStore, initializePrefEvents } from '@/store/prefs-store' import { AuthenticationStore, initializeAuthEvents } from '@/store/authentication-store' +import { WatchedVideosPlaylist } from '@/store/watched-videos-playlist' import { i18nStore } from '@/store/i18n-store' Vue.use(Vuex) @@ -11,7 +12,8 @@ const store = new Vuex.Store({ modules: { prefs: PrefsStore, auth: AuthenticationStore, - i18n: i18nStore + i18n: i18nStore, + plHistory: WatchedVideosPlaylist } }) diff --git a/src/store/watched-videos-db.js b/src/store/watched-videos-db.js index 7362970..54c7daa 100644 --- a/src/store/watched-videos-db.js +++ b/src/store/watched-videos-db.js @@ -6,7 +6,8 @@ PMDB.version(3).stores({ watchedVideos: '++id,videoId,progressPcnt,timestamp' }) -export async function addWatchedVideo (videoObj) { +export async function addWatchedVideo (store, videoObj) { + await store.dispatch('plHistory/addVideo', videoObj.videoId) return PMDB.watchedVideos.add({ videoId: videoObj.videoId, video: videoObj, @@ -16,7 +17,7 @@ export async function addWatchedVideo (videoObj) { }) } -export function updateWatchedVideoProgress (videoID, prog, dur) { +export function updateWatchedVideoProgress (_, videoID, prog, dur) { return PMDB.watchedVideos.update(videoID, { progress: prog, progressPcnt: Math.min((prog / dur) * 100, 100) diff --git a/src/store/watched-videos-playlist.js b/src/store/watched-videos-playlist.js new file mode 100644 index 0000000..d6c9df5 --- /dev/null +++ b/src/store/watched-videos-playlist.js @@ -0,0 +1,60 @@ +const WATCHED_HISTORY_PLAYLIST_UNIQID = '5d0615c7-b6a7-4bd5-8450-fa740cd92df7' + +export const WatchedVideosPlaylist = { + namespaced: true, + state: () => ({ + playlistID: '' + }), + mutations: { + replaceID (state, nextID) { + state.playlistID = nextID + } + }, + actions: { + async resolvePlaylistID ({ dispatch, commit }) { + const playlists = await dispatch('auth/makeRequest', { + method: 'GET', + path: '/user/playlists' + }, { + root: true + }) + let found = false; let pl + for (const _pl of playlists) { + const sdp = _pl.shortDescription ?? '' + if (sdp.includes(WATCHED_HISTORY_PLAYLIST_UNIQID) || _pl.name === 'Watch History') { + found = true + pl = _pl + break + } + } + if (!found) { + const { playlistId } = await dispatch('auth/makeRequest', { + method: 'POST', + path: '/user/playlists/create', + data: { + name: 'Watch History', + shortDescription: WATCHED_HISTORY_PLAYLIST_UNIQID + } + }, { + root: true + }) + pl = { + id: playlistId + } + } + commit('replaceID', pl.id) + }, + async addVideo ({ state, dispatch }, videoID) { + await dispatch('auth/makeRequest', { + method: 'POST', + path: '/user/playlists/add', + data: { + playlistId: state.playlistID, + videoId: videoID + } + }, { + root: true + }) + } + } +} diff --git a/src/translations/en.json b/src/translations/en.json index ce8828f..9dfa6e2 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -24,7 +24,8 @@ "remove_video": "Remove this video from the playlist", "delete_self": "Delete", "rename_self": "Rename", - "new_name_msg": "What do you want the new name to be?" + "new_name_msg": "What do you want the new name to be?", + "watched_history_playlist_name": "Watched History" }, "misc": { "searchBarLabel": "What are you looking for?",