From cadc1ac72c0df60ecf26a4cf34beb0f185abb661 Mon Sep 17 00:00:00 2001
From: FireMasterK <20838718+FireMasterK@users.noreply.github.com>
Date: Sun, 22 Aug 2021 01:18:16 +0530
Subject: [PATCH] Add support for client-side watch history.
---
src/App.vue | 17 +++++++
src/components/FeedPage.vue | 23 ++++++++-
src/components/HistoryPage.vue | 88 ++++++++++++++++++++++++++++++++++
src/components/LoginPage.vue | 1 -
src/components/Navigation.vue | 14 +++++-
src/components/Preferences.vue | 19 ++++++--
src/components/VideoItem.vue | 6 +++
src/components/WatchVideo.vue | 23 ++++++---
src/router/router.js | 5 ++
9 files changed, 182 insertions(+), 14 deletions(-)
create mode 100644 src/components/HistoryPage.vue
diff --git a/src/App.vue b/src/App.vue
index 4e74b80b..66a70eb1 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -41,6 +41,23 @@ export default {
default:
break;
}
+
+ if (this.getPreferenceBoolean("watchHistory", false))
+ if ("indexedDB" in window) {
+ const request = indexedDB.open("piped-db", 1);
+ request.onupgradeneeded = function() {
+ const db = request.result;
+ console.log("Upgrading object store.");
+ if (!db.objectStoreNames.contains("watch_history")) {
+ const store = db.createObjectStore("watch_history", { keyPath: "videoId" });
+ store.createIndex("video_id_idx", "videoId", { unique: true });
+ store.createIndex("id_idx", "id", { unique: true, autoIncrement: true });
+ }
+ };
+ request.onsuccess = e => {
+ window.db = e.target.result;
+ };
+ } else console.log("This browser doesn't support IndexedDB");
},
};
diff --git a/src/components/FeedPage.vue b/src/components/FeedPage.vue
index 90d505be..c764f2f2 100644
--- a/src/components/FeedPage.vue
+++ b/src/components/FeedPage.vue
@@ -44,7 +44,28 @@ export default {
};
},
mounted() {
- this.fetchFeed().then(videos => (this.videos = videos));
+ this.fetchFeed().then(videos => {
+ this.videos = videos;
+ (async () => {
+ if (window.db) {
+ var tx = window.db.transaction("watch_history", "readonly");
+ var store = tx.objectStore("watch_history");
+ const cursorRequest = store.openCursor();
+ cursorRequest.onsuccess = e => {
+ const cursor = e.target.result;
+ if (cursor) {
+ const video = this.videos.filter(
+ video => video.url.substr(-11) === cursor.value.videoId,
+ )[0];
+ if (video != null) {
+ video.watched = true;
+ }
+ cursor.continue();
+ }
+ };
+ }
+ })();
+ });
},
activated() {
document.title = "Feed - Piped";
diff --git a/src/components/HistoryPage.vue b/src/components/HistoryPage.vue
new file mode 100644
index 00000000..0a87a796
--- /dev/null
+++ b/src/components/HistoryPage.vue
@@ -0,0 +1,88 @@
+
+ Watch History
+
+
+ Sort by:
+
+
+
+
+