Files
playlet-piped/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs
T
Brahim Hadricheandgithub-action linter 394dd23999 Multiple profiles (#312)
* Navbar user profile

* profiles stuff

* fix profile

* Lint fix

* Some ui experiments

* More ui work

* progress

* Lint fix

* migration, web api, wip

* profile view and web app

* web app and apis

* Invidious and profiles

* Lint fix

* bug fix

* Remove ActionContentNode

* Profile icon button, profile color

* Lint fix

* reset server url

* href that doesn't work

* remove activate button on active profile

* TODO

* Fin.

* Final touches

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
2024-03-04 09:04:34 -05:00

195 lines
6.7 KiB
Plaintext

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)
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
' Invidious returns 500 and "{"error":"Closed stream"}" when the Popular feed is disabled
if feedSource.endpoint = "popular" and response.error.instr("Closed stream") > -1
LogWarn("Popular 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)
if hasContinuation
feedSource.state.continuation = response.result.continuation
end if
if response.result.items.Count() > 0
pageType = feedSource.state.paginationType
if pageType = PaginationType.Continuation and 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.ShowDialog(error, "Failed to load feed", true)
end function