From 37987f4447a7503e80c075c80c989545bac9f3de Mon Sep 17 00:00:00 2001
From: Jonas Heinrich <onny@project-insanity.org>
Date: Sun, 28 Mar 2021 22:25:52 +0200
Subject: [PATCH] include internal keys, access playtime, lastplayed and
 dateadded verywhere

---
 CHANGELOG.md                         |  6 +++++
 lib/Controller/EpisodeController.php | 34 +++++++++++++++++++++++++---
 lib/Controller/ShowController.php    | 17 ++++++++++----
 lib/Service/FyydApiService.php       |  1 -
 src/components/MediaHeader.vue       | 12 +++++-----
 src/store/episode.js                 | 22 ++++--------------
 src/store/player.js                  |  8 +++----
 src/store/show.js                    |  6 -----
 src/views/Show.vue                   |  4 ++--
 9 files changed, 66 insertions(+), 44 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 67d4fb8..6d96352 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,12 @@ yet implemented.
 - Show newest podcasts and episodes in library view
   [#214](https://git.project-insanity.org/onny/nextcloud-app-podcast/-/issues/214) @onny
 
+### Fixed
+- Listening view: Only show episods with playtime
+  [#213](https://git.project-insanity.org/onny/nextcloud-app-podcast/-/issues/213) @onny
+- Library view: Show list of newest episodes (which are present in the database)
+  [#213](https://git.project-insanity.org/onny/nextcloud-app-podcast/-/issues/213) @onny
+
 ### Changed
 - Update npm modules
   [#181](https://git.project-insanity.org/onny/nextcloud-app-podcast/-/issues/181) @onny
diff --git a/lib/Controller/EpisodeController.php b/lib/Controller/EpisodeController.php
index a2227ee..b113657 100644
--- a/lib/Controller/EpisodeController.php
+++ b/lib/Controller/EpisodeController.php
@@ -29,10 +29,15 @@ use OCA\Podcast\Service\FyydApiService;
 use OCP\AppFramework\Controller;
 use OCP\AppFramework\Http\DataResponse;
 use OCP\IRequest;
+use OCP\ILogger;
 
 use OCP\Http\Client\IClientService;
 
 class EpisodeController extends Controller {
+
+	/** @var ILogger */
+	private $logger;
+
 	/** @var EpisodesService */
 	private $service;
 
@@ -47,10 +52,12 @@ class EpisodeController extends Controller {
 	public function __construct(IRequest $request,
 								EpisodeService $service,
 								FyydApiService $fyydapi,
+								ILogger $logger,
 								$userId) {
 		parent::__construct(Application::APP_ID, $request);
 		$this->service = $service;
 		$this->fyydapi = $fyydapi;
+		$this->logger = $logger;
 		$this->userId = $userId;
 	}
 
@@ -60,13 +67,34 @@ class EpisodeController extends Controller {
 	public function index(int $episode_id = null, int $podcast_id = null,
 		int $count = 20, int $page = 0, string $sortBy = null): DataResponse {
 
+		$data = $this->service->findAll($this->userId, $sortBy);
+
 		if ($episode_id) {
-			return new DataResponse($this->fyydapi->queryEpisode($episode_id));
+			$list = $this->fyydapi->queryEpisode($episode_id);
+
+			foreach($data as $localEpisode) {
+				if ($localEpisode->getId() == $list['data']['id']) {
+						$list['data']['lastplayed'] = $localEpisode->getLastplayed();
+						$list['data']['playtime'] = $localEpisode->getPlaytime();
+				}
+			}
+
+			return new DataResponse($list);
 		} else if ($podcast_id) {
-			return new DataResponse($this->fyydapi->queryEpisodes($podcast_id, $count, $page));
+			$list = $this->fyydapi->queryEpisodes($podcast_id, $count, $page);
+
+			foreach($list['data']['episodes'] as $key=>$fyydEpisode) {
+				foreach($data as $localEpisode) {
+					if ($localEpisode->getId() == $fyydEpisode['id']) {
+							$list['data']['episodes'][$key]['lastplayed'] = $localEpisode->getLastplayed();
+							$list['data']['episodes'][$key]['playtime'] = $localEpisode->getPlaytime();
+					}
+				}
+			}
+
+			return new DataResponse($list);
 		}
 
-		$data = $this->service->findAll($this->userId, $sortBy);
 		$data = array_slice($data, $page * $count, $count);
 
 		if (count($data) == $count) {
diff --git a/lib/Controller/ShowController.php b/lib/Controller/ShowController.php
index 40e67ba..2d47fca 100644
--- a/lib/Controller/ShowController.php
+++ b/lib/Controller/ShowController.php
@@ -58,14 +58,23 @@ class ShowController extends Controller {
 	public function index(int $podcast_id = null, string $category = null,
 		int $count = 20, int $page = 0): DataResponse {
 
+		$data = $this->service->findAll($this->userId);
+
 		if ($category) {
-			return new DataResponse($this->fyydapi->queryCategory($category, $count,
-				$page));
+			$list = $this->fyydapi->queryCategory($category, $count, $page);
+			return new DataResponse($list);
 		} else if ($podcast_id) {
-			return new DataResponse($this->fyydapi->queryPodcast($podcast_id));
+			$list = $this->fyydapi->queryPodcast($podcast_id);
+
+			foreach($data as $localEpisode) {
+				if ($localEpisode->getId() == $list['data']['id']) {
+						$list['data']['dateadded'] = $localEpisode->getDateadded();
+				}
+			}
+
+			return new DataResponse($list);
 		}
 
-		$data = $this->service->findAll($this->userId);
 		$data = array_slice($data, $page * $count, $count);
 
 		if (count($data) == $count) {
diff --git a/lib/Service/FyydApiService.php b/lib/Service/FyydApiService.php
index 251cbc5..f5c7a1b 100644
--- a/lib/Service/FyydApiService.php
+++ b/lib/Service/FyydApiService.php
@@ -69,7 +69,6 @@ class FyydApiService {
 
 		$parsed = json_decode($body, true);
 
-
 		return $parsed;
 	}
 
diff --git a/src/components/MediaHeader.vue b/src/components/MediaHeader.vue
index 5ac3fa4..a92e41d 100644
--- a/src/components/MediaHeader.vue
+++ b/src/components/MediaHeader.vue
@@ -34,7 +34,7 @@
 					v-show="isshow"
 					class="podcastControls">
 					<button class="icon-add-white podcastButton button new-button"
-						:class="showExists(podcastid) ? 'icon-delete' : 'icon-add-white primary'"
+						:class="dateadded ? 'icon-delete' : 'icon-add-white primary'"
 						@click="doSubscribe()">
 						{{ getSubscribeText }}
 					</button>
@@ -62,7 +62,6 @@
 <script>
 import vueShowMoreText from 'vue-show-more-text'
 import { ShowApi } from './../services/ShowApi'
-import { mapGetters } from 'vuex'
 
 const apiClient = new ShowApi()
 
@@ -106,13 +105,14 @@ export default {
 			type: Boolean,
 			default: false,
 		},
+		dateadded: {
+			type: Boolean,
+			default: false,
+		},
 	},
 	computed: {
-		...mapGetters([
-			'showExists',
-		]),
 		getSubscribeText() {
-			if (this.showExists(this.podcastid)) {
+			if (this.dateadded) {
 				return t('podcast', 'Unsubscribe')
 			} else {
 				return t('podcast', 'Subscribe')
diff --git a/src/store/episode.js b/src/store/episode.js
index e6a812c..8cba43b 100644
--- a/src/store/episode.js
+++ b/src/store/episode.js
@@ -55,8 +55,10 @@ export default {
 		},
 		updateEpisode(state, { episode, playtime } = {}) {
 			const existingIndex = state.episodes.findIndex(_episode => _episode.id === episode.id)
-			state.episodes[existingIndex].playtime = playtime
-			state.episodes[existingIndex].lastplayed = Date.now()
+			if (existingIndex >= 0) {
+				state.episodes[existingIndex].playtime = playtime
+				state.episodes[existingIndex].lastplayed = Date.now()
+			}
 		},
 	},
 	actions: {
@@ -74,22 +76,6 @@ export default {
 				.then((episode) => {
 					commit('addEpisode', episode)
 				})
-			const testEpisode = {
-				id: 6461881,
-				imgURL: 'https://thumborcdn.acast.com/lYYH8lBYyScJ5JcMCkVGdrY1EHg=/3000x3000/https://mediacdn.acast.com/assets/4f71cda1-166c-4604-88a7-bf1e6686c9e9/cover-image-kluot63n-thehistoryofbyzantium_new_.jpg',
-				title: 'Episode 225 - test episode',
-				pubdate: '2021-03-04T10:57:48+01:00',
-				duration: 3834,
-				playtime: 0,
-				lastplayed: 1616511062258,
-				enclosure: 'https://sphinx.acast.com/thehistoryofbyzantium/episode225-belisariusinmetal/media.mp3',
-				description: 'test test test',
-				podcast_id: 48700,
-			}
-			episodeApiClient.addEpisode(testEpisode)
-				.then((episode) => {
-					commit('addEpisode', episode)
-				})
 		},
 		removeEpisode({ commit }, episode) {
 			episodeApiClient.removeEpisode(episode)
diff --git a/src/store/player.js b/src/store/player.js
index 6a9a6a4..693c907 100644
--- a/src/store/player.js
+++ b/src/store/player.js
@@ -45,6 +45,7 @@ export default {
 			playtime: null,
 			duration: null,
 			enclosure: null,
+			lastplayed: null,
 		},
 	},
 	getters: {
@@ -194,12 +195,11 @@ export default {
 		},
 
 		storePlaybacktime(context, position) {
-			const episodeId = context.state.episode.id
-			const episode = context.getters.episodeById(episodeId)
-			const episodeLastPlayed = episode.lastplayed
+			const episode = context.state.episode
 			const dateNow = Date.now()
-			if (dateNow - episodeLastPlayed >= 5000) {
+			if (dateNow - episode.lastplayed >= 5000) {
 				context.dispatch('updateEpisode', { episode, playtime: position })
+				context.state.episode.lastplayed = Date.now()
 			}
 		},
 
diff --git a/src/store/show.js b/src/store/show.js
index 2c022f6..7b29ce4 100644
--- a/src/store/show.js
+++ b/src/store/show.js
@@ -32,12 +32,6 @@ export default {
 		showById: state => (id) => {
 			return state.shows.find((show) => show.id === id)
 		},
-		showExists: state => (id) => {
-			if (id !== undefined) {
-				id = Number(id)
-				return state.shows.some((show) => show.id === id)
-			}
-		},
 	},
 	mutations: {
 		addShow(state, show) {
diff --git a/src/views/Show.vue b/src/views/Show.vue
index 98f9227..ed39a4a 100644
--- a/src/views/Show.vue
+++ b/src/views/Show.vue
@@ -32,6 +32,7 @@
 					:categories="podcast.categories"
 					:description="podcast.description"
 					:podcastid="podcast.id"
+					:dateadded="podcast.dateadded"
 					:isshow="true"
 					@doSubscribe="doSubscribe" />
 				<LoadMore :page="page"
@@ -74,7 +75,6 @@ export default {
 	}),
 	computed: {
 		...mapGetters([
-			'showExists',
 			'episodePlaying',
 		]),
 	},
@@ -103,7 +103,7 @@ export default {
 		]),
 
 		doSubscribe() {
-			if (this.showExists(this.podcastId)) {
+			if (this.podcast.showadded) {
 				this.removeShow(this.podcast)
 			} else {
 				this.addShow(this.podcast)
-- 
GitLab