From fb030cee9718546aa968b18f9d0b9cbe33a9fb29 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 13 May 2025 13:56:32 -0400 Subject: [PATCH] OAuth: Fix auth playlist duplication issue --- .../VideoFeed/VideoRowListRowContentTask.bs | 60 ++++++++++++++- .../src/lib/VideoFeed/VideoListRow.svelte | 76 +++++++++++++------ 2 files changed, 107 insertions(+), 29 deletions(-) diff --git a/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs b/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs index a6e11781..5823f502 100644 --- a/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs +++ b/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs @@ -111,12 +111,23 @@ function VideoRowListRowContentTask(input as object) as object end for RemovePlaceHolderItems(feedContentNode) - feedContentNode.appendChildren(itemNodes) + + addedItemCount = 0 + ' For some reason, Innertube's "FEplaylist_aggregation" (which maps to "inv_auth_playlists" + ' aka Authenticated user playlists) returns continuation for the next page, but returns + ' items from the first page. So we deduplicate to avoid loading this feed infnitely, + ' with the same items repeating. + if feedSource.id = "inv_auth_playlists" + addedItemCount = DeduplicateAppendAuthPlaylists(feedContentNode, itemNodes) + else + feedContentNode.appendChildren(itemNodes) + addedItemCount = itemNodes.Count() + end if hasContinuation = not StringUtils.IsNullOrEmpty(response.result.continuation) feedSource.state.continuation = ValidString(response.result.continuation) - if response.result.items.Count() > 0 + if addedItemCount > 0 pageType = feedSource.state.paginationType if response.result.isLastPage = true or (hadContinuation and not hasContinuation) feedSource.state.loadState = FeedLoadState.Loaded @@ -140,12 +151,12 @@ function VideoRowListRowContentTask(input as object) as object feedContentNode.loadState = FeedLoadState.LoadedPage end if - if itemNodes.Count() > 0 and not someContentReadySet + if addedItemCount > 0 and not someContentReadySet someContentReadySet = true rowList.someContentReady = true end if - totalfetchedItems += response.result.items.Count() + totalfetchedItems += addedItemCount if totalfetchedItems > 3 exit while end if @@ -187,6 +198,47 @@ function RemovePlaceHolderItems(contentNode as object) end if end function +function DeduplicateAppendAuthPlaylists(feedContentNode as object, itemNodes as object) as integer + if itemNodes.Count() = 0 + return 0 + end if + + currentChildCount = feedContentNode.getChildCount() + if currentChildCount = 0 + feedContentNode.appendChildren(itemNodes) + return itemNodes.Count() + end if + + currentChildren = feedContentNode.getChildren(-1, 0) + currentChildrenMap = {} + for i = 0 to currentChildCount - 1 + playlistId = currentChildren[i].playlistId + if StringUtils.IsNullOrEmpty(playlistId) + continue for + end if + currentChildrenMap[playlistId] = true + end for + + nodesToAdd = [] + for i = 0 to itemNodes.Count() - 1 + itemNode = itemNodes[i] + playlistId = itemNode.playlistId + if StringUtils.IsNullOrEmpty(playlistId) + continue for + end if + if currentChildrenMap[playlistId] = true + continue for + end if + currentChildrenMap[playlistId] = true + nodesToAdd.Push(itemNode) + end for + if nodesToAdd.Count() = 0 + return 0 + end if + feedContentNode.appendChildren(nodesToAdd) + return nodesToAdd.Count() +end function + function HandleFeedLoadErrorDialog(error as object, isCancelled as boolean, rowList as object) as void error = ErrorUtils.Format(error) LogError(error) diff --git a/playlet-web/src/lib/VideoFeed/VideoListRow.svelte b/playlet-web/src/lib/VideoFeed/VideoListRow.svelte index e284e3f3..a0fed398 100644 --- a/playlet-web/src/lib/VideoFeed/VideoListRow.svelte +++ b/playlet-web/src/lib/VideoFeed/VideoListRow.svelte @@ -171,7 +171,13 @@ let hasContinuation = !!result.continuation; feedSource.state.continuation = result.continuation || ""; - if (result.items.length > 0) { + // See brightscript version for the "inv_auth_playlists" case + const items = + feedSource.id === "inv_auth_playlists" + ? deduplicateAuthPlaylists(result.items) + : result.items || []; + + if (items.length > 0) { const paginationType = feedSource.state.paginationType; if (hadContinuation && !hasContinuation) { feedSource.state.loadState = FeedLoadState.Loaded; @@ -195,32 +201,30 @@ feedLoadState = FeedLoadState.LoadedPage; } - if (result && result.items) { - const newVideos = result.items; + const newVideos = items; - newVideos.forEach((item) => { - if (item.videoId) { - item.type = "video"; - } else if (item.playlistId) { - item.type = "playlist"; - } - }); - - const newItemWidths = newVideos.map((video) => { - if (video.type === "channel") { - return channelItemWidth; - } else { - return videoItemWidth; - } - }); - - itemWidths = [...itemWidths, ...newItemWidths]; - videos = [...(videos || []), ...newVideos]; - - totalFetchedItems += result.items.length; - if (totalFetchedItems >= 6) { - break; + newVideos.forEach((item) => { + if (item.videoId) { + item.type = "video"; + } else if (item.playlistId) { + item.type = "playlist"; } + }); + + const newItemWidths = newVideos.map((video) => { + if (video.type === "channel") { + return channelItemWidth; + } else { + return videoItemWidth; + } + }); + + itemWidths = [...itemWidths, ...newItemWidths]; + videos = [...(videos || []), ...newVideos]; + + totalFetchedItems += items.length; + if (totalFetchedItems >= 6) { + break; } } catch (error) { // TODO:P0 handle case of unauthenticated calls @@ -232,6 +236,28 @@ } } + function deduplicateAuthPlaylists(items) { + if (!items || items.length === 0) { + return []; + } + + if (videos.length === 0) { + return items; + } + + const existingPlaylistIds = new Set( + videos.map((video) => video.playlistId).filter((id) => id) + ); + + const newItems = items.filter((item) => { + if (item.playlistId && existingPlaylistIds.has(item.playlistId)) { + return false; + } + return true; + }); + return newItems; + } + function recalculateVisibileCells() { let scrollLeft = carouselElement.scrollLeft; const clientWidth = carouselElement.clientWidth || window.innerWidth;