Bookmarks (#153)

* Empty bookmarks screen

* Bookmarks object

* Long press support

* Lint fix

* Context menu v1

* A working context menu

* Basic bookmarks in context menu

* Use a root content node for bookmarks

* context menu animation

* small tweak

* move logger around, adjust animation

* Lint fix

* Restore logger fix

* Cache channel info

* Bookmarks v1

* Added comment

* Adjust "popular" feed error handling

* Progress with bookmarks

* Add todo

* add todo

* handle roDateTime logging

* logger adjustment

* Use yaml for api and layout

* rename requestData to feedSource

* Restructure things

* Lint fix

* remove comment

* introduce feedSource id

* small cleanup

* playlist context menu

* Context menu for the channel view

* changelog

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
Brahim Hadricheandgithub-action linter authored and GitHub committed 2023-10-19 19:16:27 -04:00
1 parent 620a8a6e2b
commit c740d5e0a0
63 files changed
+1889 -510

No files matched your search

@@ -0,0 +1,326 @@
import "pkg:/components/ContextMenu/ContextMenuUtils.bs"
import "pkg:/source/utils/CryptoUtils.bs"
import "pkg:/source/utils/Logging.bs"
import "pkg:/source/utils/RegistryUtils.bs"
import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/Types.bs"
const VIDEOS_GROUP = "Videos"
const CHANNELS_GROUP = "Channels"
const PLAYLISTS_GROUP = "Playlists"
function Init()
m.top.content = m.top.findNode("content")
m.bookmarksString = ""
Load()
m.top.ObserveField("contentChange", FuncName(OnContentChange))
end function
function Load() as void
bookmarksString = RegistryUtils.Read(RegistryUtils.BOOKMARKS)
if bookmarksString = invalid
return
end if
m.bookmarksString = bookmarksString
bookmarks = ParseJson(bookmarksString)
if bookmarks = invalid
return
end if
for each bookmarkGroup in bookmarks.groups
bookmarkGroupNode = GetOrCreateBookmarkGroup(bookmarkGroup.title)
for each bookmark in bookmarkGroup.bookmarks
AddFeedSourceBookmark(bookmark.feedSource, bookmark.id, bookmarkGroupNode)
end for
end for
end function
function Save() as void
bookmarkGroupNodes = m.top.content.getChildren(-1, 0)
if bookmarkGroupNodes.Count() = 0
RegistryUtils.Delete(RegistryUtils.BOOKMARKS)
return
end if
groups = []
for each bookmarkGroupNode in bookmarkGroupNodes
bookmarks = []
bookmarkNodes = bookmarkGroupNode.getChildren(-1, 0)
for each bookmarkNode in bookmarkNodes
bookmarks.push({
id: bookmarkNode.id,
feedSource: bookmarkNode.feedSource
})
end for
groups.push({
title: bookmarkGroupNode.title,
bookmarks: bookmarks
})
end for
bookmarksString = FormatJson({
__version: m.top.__version,
groups: groups
})
if m.bookmarksString = bookmarksString
return
end if
RegistryUtils.Write(RegistryUtils.BOOKMARKS, bookmarksString)
m.bookmarksString = bookmarksString
end function
function GetOrCreateBookmarkGroup(groupName as string) as object
id = CryptoUtils.GetMd5(groupName)
node = m.top.content.findNode(id)
if node <> invalid
return node
end if
node = CreateObject("roSGNode", "ContentNode")
node.id = id
node.title = groupName
m.top.content.appendChild(node)
LogInfo("Added bookmark group:", groupName)
return node
end function
function AddFeedSourceBookmark(feedSource as object, id as string, bookmarkGroupNode as dynamic)
if IsString(bookmarkGroupNode)
bookmarkGroupNode = GetOrCreateBookmarkGroup(bookmarkGroupNode)
end if
feedSource.Delete("state")
bookmarkNode = CreateObject("roSGNode", "BookmarkContentNode")
bookmarkNode.id = id
bookmarkNode.feedSource = feedSource
bookmarkGroupNode.insertChild(bookmarkNode, 0)
LogInfo("Added feedSource bookmark:", feedSource.title, "id:", id)
m.top.contentChange = true
end function
function AddVideoBookmark(id as string, groupName as string)
bookmarkGroupNode = GetOrCreateBookmarkGroup(groupName)
feedSource = {
apiType: "Invidious",
endpoint: "video_info",
pathParams: {
id: id
}
}
AddFeedSourceBookmark(feedSource, id, bookmarkGroupNode)
end function
function AddPlaylistBookmark(id as string, groupName as string)
bookmarkGroupNode = GetOrCreateBookmarkGroup(groupName)
feedSource = {
apiType: "Invidious",
endpoint: "playlist_info",
pathParams: {
plid: id
}
}
AddFeedSourceBookmark(feedSource, id, bookmarkGroupNode)
end function
function AddPlaylistBookmarkWithSpread(id as string, groupName as string)
bookmarkGroupNode = GetOrCreateBookmarkGroup(groupName)
feedSource = {
id: `inv_playlist_${id}`,
title: `${groupName} videos`,
apiType: "Invidious",
endpoint: "playlist",
pathParams: {
plid: id
}
}
AddFeedSourceBookmark(feedSource, `inv_playlist_${id}`, bookmarkGroupNode)
feedSource = {
apiType: "Invidious",
endpoint: "playlist_info",
pathParams: {
plid: id
}
}
AddFeedSourceBookmark(feedSource, id, bookmarkGroupNode)
end function
function AddChannelBookmark(id as string, groupName as string)
bookmarkGroupNode = GetOrCreateBookmarkGroup(groupName)
feedSource = {
apiType: "Invidious",
endpoint: "channel_info",
pathParams: {
ucid: id
}
}
AddFeedSourceBookmark(feedSource, id, bookmarkGroupNode)
end function
function AddChannelBookmarkWithSpread(id as string, groupName as string)
bookmarkGroupNode = GetOrCreateBookmarkGroup(groupName)
feedSource = {
id: `inv_channel_videos_${id}`,
title: `${groupName} videos`,
apiType: "Invidious",
endpoint: "channel_videos",
pathParams: {
ucid: id
}
}
AddFeedSourceBookmark(feedSource, `inv_channel_videos_${id}`, bookmarkGroupNode)
feedSource = {
apiType: "Invidious",
endpoint: "channel_info",
pathParams: {
ucid: id
}
}
AddFeedSourceBookmark(feedSource, id, bookmarkGroupNode)
end function
function RemoveBookmark(id as string) as void
node = m.top.content.findNode(id)
if node = invalid
return
end if
group = node.getParent()
group.removeChild(node)
LogInfo("Removed bookmark:", id)
if group.getChildCount() = 0
m.top.content.removeChild(group)
LogInfo("Removed bookmark group:", group.title)
end if
m.top.contentChange = true
end function
function OnContentChange(event as object) as void
Save()
end function
function GetContextMenuOptionsForItem(item as object) as object
options = []
if item.type = "video"
options.append(GetMenuForVideo(item))
else if item.type = "playlist"
options.append(GetMenuForPlaylist(item))
else if item.type = "channel"
options.append(GetMenuForChannel(item))
end if
options.append(GetMenuForParentFeedSource(item))
return options
end function
function GetMenuForVideo(videoNode as object) as object
videoId = videoNode.videoId
if StringUtils.IsNullOrEmpty(videoId)
return []
end if
menu = []
bookmark = m.top.content.findNode(videoId)
isInBookmarks = bookmark <> invalid
if isInBookmarks
bookmarkGroup = bookmark.getParent()
item = ContextMenuUtils.CreateOption(`Remove from "${bookmarkGroup.title}" bookmarks`, m.top, "RemoveBookmark", [videoId])
menu.push(item)
else
item = ContextMenuUtils.CreateOption(`Add to "${VIDEOS_GROUP}" bookmark`, m.top, "AddVideoBookmark", [videoId, VIDEOS_GROUP])
menu.push(item)
end if
return menu
end function
function GetMenuForPlaylist(playlistNode as object) as object
playlistId = playlistNode.playlistId
if StringUtils.IsNullOrEmpty(playlistId) or playlistId.StartsWith("IV")
return []
end if
menu = []
bookmark = m.top.content.findNode(playlistId)
isInBookmarks = bookmark <> invalid
if isInBookmarks
bookmarkGroup = bookmark.getParent()
item = ContextMenuUtils.CreateOption(`Remove from "${bookmarkGroup.title}" bookmarks`, m.top, "RemoveBookmark", [playlistId])
menu.push(item)
else
item = ContextMenuUtils.CreateOption(`Add to "${PLAYLISTS_GROUP}" bookmark`, m.top, "AddPlaylistBookmark", [playlistId, PLAYLISTS_GROUP])
menu.push(item)
item = ContextMenuUtils.CreateOption(`Add to "${playlistNode.title}" bookmark`, m.top, "AddPlaylistBookmarkWithSpread", [playlistId, playlistNode.title])
menu.push(item)
end if
return menu
end function
function GetMenuForChannel(channelNode as object) as object
authorId = channelNode.authorId
if StringUtils.IsNullOrEmpty(authorId)
return []
end if
menu = []
bookmark = m.top.content.findNode(authorId)
isInBookmarks = bookmark <> invalid
if isInBookmarks
bookmarkGroup = bookmark.getParent()
item = ContextMenuUtils.CreateOption(`Remove from "${bookmarkGroup.title}" bookmarks`, m.top, "RemoveBookmark", [authorId])
menu.push(item)
else
item = ContextMenuUtils.CreateOption(`Add to "${CHANNELS_GROUP}" bookmark`, m.top, "AddChannelBookmark", [authorId, CHANNELS_GROUP])
menu.push(item)
item = ContextMenuUtils.CreateOption(`Add to "${channelNode._author}" bookmark`, m.top, "AddChannelBookmarkWithSpread", [authorId, channelNode._author])
menu.push(item)
end if
return menu
end function
function GetMenuForParentFeedSource(itemNode as object) as object
if itemNode = invalid
return []
end if
if itemNode.type <> "video" and itemNode.type <> "playlist" and itemNode.type <> "channel"
return []
end if
if not IsInt(itemNode.feedSourcesIndex) or itemNode.feedSourcesIndex = -1
return []
end if
feedContentNode = itemNode.getParent()
if feedContentNode = invalid or feedContentNode.subtype() <> "FeedContentNode"
return []
end if
feedSource = feedContentNode.feedSources[itemNode.feedSourcesIndex]
if StringUtils.IsNullOrEmpty(feedSource.title) or StringUtils.IsNullOrEmpty(feedSource.id)
return []
end if
title = feedSource.title
feedSourceId = feedSource.id
menu = []
isInBookmarks = m.top.content.findNode(feedSourceId) <> invalid
if isInBookmarks
item = ContextMenuUtils.CreateOption(`Remove "${feedSource.title}" from bookmarks`, m.top, "RemoveBookmark", [feedSourceId])
menu.push(item)
else
item = ContextMenuUtils.CreateOption(`Add to "${title}" bookmark`, m.top, "AddFeedSourceBookmark", [feedSource, feedSourceId, title])
menu.push(item)
end if
return menu
end function
@@ -0,0 +1,20 @@
<component name="Bookmarks" extends="Node" includes="ContextMenuProvider">
<interface>
<field id="__version" type="integer" value="1" />
<field id="content" type="node" />
<field id="contentChange" type="boolean" alwaysNotify="true" />
<function name="AddFeedSourceBookmark" />
<function name="AddVideoBookmark" />
<function name="AddPlaylistBookmark" />
<function name="AddPlaylistBookmarkWithSpread" />
<function name="AddChannelBookmark" />
<function name="AddChannelBookmarkWithSpread" />
<function name="RemoveBookmark" />
<function name="GetMenuForVideo" />
<function name="GetMenuForPlaylist" />
<function name="GetMenuForChannel" />
</interface>
<children>
<ContentNode id="content" />
</children>
</component>
@@ -1,7 +1,7 @@
import "InvidiousService.bs"
function Init()
m.top.apiDefinitions = ParseJson(ReadAsciiFile("libpkg:/config/invidious_video_api.json5"))
m.top.apiDefinitions = ParseJson(ReadAsciiFile("libpkg:/config/invidious_video_api.yaml"))
m.service = new Invidious.InvidiousService(m.top)
m.top.authToken = m.service.GetAuthToken()
end function
@@ -36,19 +36,16 @@ namespace Invidious
function new(node as object)
m.node = node
' TODO:P2 api definitions are not needed everytime, move them to a separate function and lazy load
apiDefinitions = node.apiDefinitions
m.endpoints = {}
for each apiDefinition in apiDefinitions.endpoints
m.endpoints[apiDefinition.name] = apiDefinition
end for
m.endpoints = node.apiDefinitions
m.responseHanlders = {
DefaultHandler: m.DefaultHandler,
AuthFeedHandler: m.AuthFeedHandler,
PlaylistHandler: m.PlaylistHandler,
VideoInfoHandler: m.VideoInfoHandler,
PlaylistInfoHandler: m.PlaylistInfoHandler,
ChannelInfoHandler: m.ChannelInfoHandler,
ChannelVideosHandler: m.ChannelVideosHandler,
ChannelPlaylistsHandler: m.ChannelPlaylistsHandler,
ChannelRelatedChannelsHandler: m.ChannelRelatedChannelsHandler
@@ -149,6 +146,7 @@ namespace Invidious
request = HttpClient.Get(url)
request.QueryParams(m.DefaultQueryParams())
request.Cancellation(cancellation)
request.CacheSeconds(60 * 60 * 24)' 24 hours
return request.Await()
end function
@@ -158,39 +156,50 @@ namespace Invidious
return `${instance}/latest_version?id=${videoId}`
end function
function MarkFeedPagination(contentNode as object) as void
requestData = contentNode.feed
function MarkFeedPagination(feedContentNode as object) as object
feedSources = feedContentNode.feedSources
feedSourcesIndex = feedContentNode.feedSourcesIndex
endpoint = m.endpoints[requestData.endpoint]
feedSource = feedSources[feedSourcesIndex]
endpoint = m.endpoints[feedSource.endpoint]
if endpoint = invalid or StringUtils.IsNullOrEmpty(endpoint.paginationType)
return
return feedSource
end if
if not requestData.DoesExist("queryParams")
requestData.queryParams = {}
feedSourceState = feedSource.state
if not feedSourceState.DoesExist("queryParams")
feedSourceState.queryParams = {}
end if
contentNode.paginationType = endpoint.paginationType
if endpoint.paginationType = PaginationType.Pages
contentNode.page += 1
requestData.queryParams.page = contentNode.page
else if endpoint.paginationType = PaginationType.Continuation
continuation = contentNode.continuation
feedSourceState.paginationType = endpoint.paginationType
if feedSourceState.paginationType = PaginationType.Pages
if not IsInt(feedSourceState.page)
feedSourceState.page = 0
end if
feedSourceState.page += 1
feedSourceState.queryParams.page = feedSourceState.page
else if feedSourceState.paginationType = PaginationType.Continuation
continuation = feedSourceState.continuation
if not StringUtils.IsNullOrEmpty(continuation)
requestData.queryParams.continuation = continuation
feedSourceState.queryParams.continuation = continuation
else
feedSourceState.queryParams.Delete("continuation")
end if
end if
contentNode.feed = requestData
feedSource.state = feedSourceState
feedSources[feedSourcesIndex] = feedSource
feedContentNode.feedSources = feedSources
return feedSource
end function
function MakeRequest(requestData as object, cancellation = invalid as object) as object
endpoint = m.endpoints[requestData.endpoint]
function MakeRequest(feedSource as object, additionalQueryParams = invalid as object, cancellation = invalid as object) as object
endpoint = m.endpoints[feedSource.endpoint]
if endpoint = invalid
return {
success: false,
error: `Endpoint ${requestData.endpoint} not found`
error: `Endpoint ${feedSource.endpoint} not found`
}
end if
@@ -233,18 +242,22 @@ namespace Invidious
end for
end if
if requestData.cacheSeconds <> invalid
request.CacheSeconds(requestData.cacheSeconds)
if feedSource.cacheSeconds <> invalid
request.CacheSeconds(feedSource.cacheSeconds)
else if endpoint.cacheSeconds <> invalid
request.CacheSeconds(endpoint.cacheSeconds)
end if
if requestData.queryParams <> invalid
request.QueryParams(requestData.queryParams)
if feedSource.queryParams <> invalid
request.QueryParams(feedSource.queryParams)
end if
if requestData.pathParams <> invalid
request.PathParams(requestData.pathParams)
if additionalQueryParams <> invalid
request.QueryParams(additionalQueryParams)
end if
if feedSource.pathParams <> invalid
request.PathParams(feedSource.pathParams)
end if
request.Cancellation(cancellation)
@@ -253,7 +266,7 @@ namespace Invidious
responseHandler = endpoint.responseHandler <> invalid ? m.responseHanlders[endpoint.responseHandler] : m.responseHanlders["DefaultHandler"]
result = responseHandler(m, requestData, response)
result = responseHandler(m, feedSource, response)
if response.IsSuccess() and result <> invalid
return {
@@ -268,7 +281,7 @@ namespace Invidious
end if
end function
function DefaultHandler(m as object, requestData as object, response as object) as object
function DefaultHandler(m as object, feedSource as object, response as object) as object
if response.StatusCode() = 200
json = response.Json()
return {
@@ -278,7 +291,7 @@ namespace Invidious
return invalid
end function
function PlaylistHandler(m as object, requestData as object, response as object) as object
function PlaylistHandler(m as object, feedSource as object, response as object) as object
if response.StatusCode() = 200
json = response.Json()
return {
@@ -288,7 +301,7 @@ namespace Invidious
return invalid
end function
function ChannelVideosHandler(m as object, requestData as object, response as object) as object
function ChannelVideosHandler(m as object, feedSource as object, response as object) as object
if response.StatusCode() = 200
json = response.Json()
return {
@@ -299,7 +312,7 @@ namespace Invidious
return invalid
end function
function ChannelPlaylistsHandler(m as object, requestData as object, response as object) as object
function ChannelPlaylistsHandler(m as object, feedSource as object, response as object) as object
if response.StatusCode() = 200
json = response.Json()
return {
@@ -310,7 +323,7 @@ namespace Invidious
return invalid
end function
function ChannelRelatedChannelsHandler(m as object, requestData as object, response as object) as object
function ChannelRelatedChannelsHandler(m as object, feedSource as object, response as object) as object
if response.StatusCode() = 200
json = response.Json()
return {
@@ -321,7 +334,40 @@ namespace Invidious
return invalid
end function
function AuthFeedHandler(m as object, requestData as object, response as object) as object
function VideoInfoHandler(m as object, feedSource as object, response as object) as object
if response.StatusCode() = 200
json = response.Json()
json.type = "video"
return {
items: [json]
}
end if
return invalid
end function
function PlaylistInfoHandler(m as object, feedSource as object, response as object) as object
if response.StatusCode() = 200
json = response.Json()
json.type = "playlist"
return {
items: [json]
}
end if
return invalid
end function
function ChannelInfoHandler(m as object, feedSource as object, response as object) as object
if response.StatusCode() = 200
json = response.Json()
json.type = "channel"
return {
items: [json]
}
end if
return invalid
end function
function AuthFeedHandler(m as object, feedSource as object, response as object) as object
m.DeleteExpiredToken(response)
if response.StatusCode() = 200
@@ -34,6 +34,7 @@ namespace InvidiousContent
node.type = "video"
SetIfExists(node, "author", item, "author")
SetIfExists(node, "authorId", item, "authorId")
SetIfExists(node, "hlsUrl", item, "hlsUrl")
SetIfExists(node, "isUpcoming", item, "isUpcoming")
SetIfExists(node, "lengthSeconds", item, "lengthSeconds")
@@ -62,6 +63,7 @@ namespace InvidiousContent
' NOTE: "_author" not "author". See PlaylistContentNode.xml for explanation.
SetIfExists(node, "_author", item, "author")
SetIfExists(node, "authorId", item, "authorId")
SetIfExists(node, "description", item, "description")
SetIfExists(node, "playlistId", item, "playlistId")
SetIfExists(node, "title", item, "title")