import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Services/Invidious/InvidiousService.bs" import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs" ' TODO:P1 handle case where all feedSources are empty @asynctask function VideoRowListRowContentTask(input as object) as object try rowList = input.rowList feedContentNode = input.feedContentNode invidiousNode = input.invidious service = new Invidious.InvidiousService(invidiousNode) instance = service.GetInstance() totalfetchedItems = 0 someContentReadySet = false while true if m.top.cancel feedContentNode.loadState = FeedLoadState.None return invalid end if feedSources = feedContentNode.feedSources feedSourcesIndex = feedContentNode.feedSourcesIndex if feedSourcesIndex >= feedSources.Count() feedContentNode.loadState = FeedLoadState.Loaded if not someContentReadySet rowList.someContentReady = false end if return { success: true } end if feedSource = feedSources[feedSourcesIndex] if feedSource.state.loadState = FeedLoadState.Loaded or feedSource.state.loadState = FeedLoadState.Error feedSourcesIndex += 1 if feedSourcesIndex >= feedSources.Count() feedContentNode.loadState = FeedLoadState.Loaded if not someContentReadySet rowList.someContentReady = false end if return { success: true } end if feedContentNode.feedSourcesIndex = feedSourcesIndex continue while end if feedSource = service.MarkFeedPagination(feedContentNode) hadContinuation = not StringUtils.IsNullOrEmpty(feedSource.state.continuation) response = service.MakeRequest(feedSource, feedSource.state.queryParams, m.top.cancellation) if not response.success if response.error = Invidious.ERROR_NOT_AUTHENTICATED if feedSources.Count() = 1 parent = feedContentNode.getParent() parent.removeChild(feedContentNode) return { success: true } end if continue while end if if response.error.instr("Administrator has disabled this endpoint.") > -1 LogWarn(feedSource.endpoint, "feedSource not available:", response.error) if feedSources.Count() = 1 parent = feedContentNode.getParent() parent.removeChild(feedContentNode) return { success: true } end if continue while end if feedSource.state.loadState = FeedLoadState.Error feedSources[feedSourcesIndex] = feedSource feedContentNode.feedSources = feedSources if feedContentNode.title.instr(" (Failed to load)") = -1 feedContentNode.title += " (Failed to load)" end if HandleFeedLoadErrorDialog(response.error, rowList) continue while end if itemNodes = [] for each item in response.result.items itemNode = InvidiousContent.ToRowCellContentNode(item, instance) if itemNode <> invalid itemNode.feedSourcesIndex = feedSourcesIndex itemNodes.Push(itemNode) end if end for RemovePlaceHolderItems(feedContentNode) feedContentNode.appendChildren(itemNodes) hasContinuation = not StringUtils.IsNullOrEmpty(response.result.continuation) feedSource.state.continuation = ValidString(response.result.continuation) if response.result.items.Count() > 0 pageType = feedSource.state.paginationType if response.result.isLastPage = true or (hadContinuation and not hasContinuation) feedSource.state.loadState = FeedLoadState.Loaded else if hasContinuation feedSource.state.loadState = FeedLoadState.LoadedPage else if pageType = PaginationType.Pages feedSource.state.loadState = FeedLoadState.LoadedPage else feedSource.state.loadState = FeedLoadState.Loaded end if else feedSource.state.loadState = FeedLoadState.Loaded end if feedSources[feedSourcesIndex] = feedSource feedContentNode.feedSources = feedSources if feedSource.state.loadState = FeedLoadState.Loaded and feedSourcesIndex = feedSources.Count() - 1 feedContentNode.loadState = FeedLoadState.Loaded else feedContentNode.loadState = FeedLoadState.LoadedPage end if if itemNodes.Count() > 0 and not someContentReadySet someContentReadySet = true rowList.someContentReady = true end if totalfetchedItems += response.result.items.Count() if totalfetchedItems > 3 exit while end if end while catch error HandleFeedLoadErrorDialog(error, rowList) return { success: false error: error } end try return { success: true } end function function RemovePlaceHolderItems(contentNode as object) childCount = contentNode.getChildCount() removeIndex = childCount removeCount = 0 index = childCount - 1 isLoading = true while isLoading and index >= 0 child = contentNode.getChild(index) isLoading = ValidString(child.type) = "loading" if not isLoading exit while end if removeCount += 1 removeIndex -= 1 index -= 1 end while if removeCount > 0 contentNode.removeChildrenIndex(removeCount, removeIndex) end if end function function HandleFeedLoadErrorDialog(error as object, rowList as object) as void error = ErrorUtils.Format(error) LogError(error) rowList.onError = error if rowList.hasField("dispose") and rowList.dispose return end if DialogUtils.ShowDialogEx({ message: error title: "Failed to load feed" large: true }) end function