OAuth: Fix auth playlist duplication issue

This commit is contained in:
Brahim Hadriche committed 2025-05-13 13:56:32 -04:00
1 parent 6e13e3009a
commit fb030cee97
2 files changed
+107 -29

No files matched your search

@@ -111,12 +111,23 @@ function VideoRowListRowContentTask(input as object) as object
end for end for
RemovePlaceHolderItems(feedContentNode) 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) hasContinuation = not StringUtils.IsNullOrEmpty(response.result.continuation)
feedSource.state.continuation = ValidString(response.result.continuation) feedSource.state.continuation = ValidString(response.result.continuation)
if response.result.items.Count() > 0 if addedItemCount > 0
pageType = feedSource.state.paginationType pageType = feedSource.state.paginationType
if response.result.isLastPage = true or (hadContinuation and not hasContinuation) if response.result.isLastPage = true or (hadContinuation and not hasContinuation)
feedSource.state.loadState = FeedLoadState.Loaded feedSource.state.loadState = FeedLoadState.Loaded
@@ -140,12 +151,12 @@ function VideoRowListRowContentTask(input as object) as object
feedContentNode.loadState = FeedLoadState.LoadedPage feedContentNode.loadState = FeedLoadState.LoadedPage
end if end if
if itemNodes.Count() > 0 and not someContentReadySet if addedItemCount > 0 and not someContentReadySet
someContentReadySet = true someContentReadySet = true
rowList.someContentReady = true rowList.someContentReady = true
end if end if
totalfetchedItems += response.result.items.Count() totalfetchedItems += addedItemCount
if totalfetchedItems > 3 if totalfetchedItems > 3
exit while exit while
end if end if
@@ -187,6 +198,47 @@ function RemovePlaceHolderItems(contentNode as object)
end if end if
end function 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 function HandleFeedLoadErrorDialog(error as object, isCancelled as boolean, rowList as object) as void
error = ErrorUtils.Format(error) error = ErrorUtils.Format(error)
LogError(error) LogError(error)
@@ -171,7 +171,13 @@
let hasContinuation = !!result.continuation; let hasContinuation = !!result.continuation;
feedSource.state.continuation = 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; const paginationType = feedSource.state.paginationType;
if (hadContinuation && !hasContinuation) { if (hadContinuation && !hasContinuation) {
feedSource.state.loadState = FeedLoadState.Loaded; feedSource.state.loadState = FeedLoadState.Loaded;
@@ -195,32 +201,30 @@
feedLoadState = FeedLoadState.LoadedPage; feedLoadState = FeedLoadState.LoadedPage;
} }
if (result && result.items) { const newVideos = items;
const newVideos = result.items;
newVideos.forEach((item) => { newVideos.forEach((item) => {
if (item.videoId) { if (item.videoId) {
item.type = "video"; item.type = "video";
} else if (item.playlistId) { } else if (item.playlistId) {
item.type = "playlist"; 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;
} }
});
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) { } catch (error) {
// TODO:P0 handle case of unauthenticated calls // 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() { function recalculateVisibileCells() {
let scrollLeft = carouselElement.scrollLeft; let scrollLeft = carouselElement.scrollLeft;
const clientWidth = carouselElement.clientWidth || window.innerWidth; const clientWidth = carouselElement.clientWidth || window.innerWidth;