Bookmarks in web app (#177)

* Bookmarks api

* Lint fix

* first pass

* Lint fix

* comment

* bookmarks, loading feedSources, and pagination

* comment

* null check

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
Brahim Hadricheandgithub-action linter authored and GitHub committed 2023-10-23 20:39:44 -04:00
1 parent 58ce2d6396
commit 57fa1c5c3f
23 files changed
+510 -192

No files matched your search

@@ -0,0 +1,65 @@
import "pkg:/source/utils/RegistryUtils.bs"
namespace Http
class BookmarksRouter extends HttpRouter
function new()
super()
m.Get("/api/bookmarks", function(context as object) as boolean
response = context.response
bookmarksString = RegistryUtils.Read(RegistryUtils.BOOKMARKS)
if bookmarksString = invalid
response.Json({
groups: []
})
return true
end if
response.SetBodyDataString(bookmarksString)
response.ContentType("application/json")
response.http_code = 200
return true
end function)
m.Get("/api/bookmarks/feeds", function(context as object) as boolean
response = context.response
router = context.router
bookmarks = context.server.task.bookmarks
feeds = router.BookmarksContentToFeeds(bookmarks.content)
response.Json(feeds)
return true
end function)
end function
' TODO:P1 refactor to share code with Bookmarks screen
function BookmarksContentToFeeds(bookmarksContent as object) as object
bookmarkGroupNodes = bookmarksContent.getChildren(-1, 0)
feeds = []
for each bookmarkGroupNode in bookmarkGroupNodes
bookmarkNodes = bookmarkGroupNode.getChildren(-1, 0)
feedSources = []
for each bookmarkNode in bookmarkNodes
feedSources.push(bookmarkNode.feedSource)
end for
feeds.push({
"title": bookmarkGroupNode.title,
"feedSources": feedSources
})
end for
return feeds
end function
end class
end namespace
@@ -47,14 +47,15 @@ namespace Http
' This is an endpoint allowing the web app to make an authenticated request
' without needing the Invidious token
' TODO:P2 this should be a POST request
m.Get("/invidious/authenticated-request", function(context as object) as boolean
request = context.request
response = context.response
invidiousNode = context.server.task.invidious
requestData = request.query["request-data"]
if requestData = invalid
response.Default(400, "Expected request-data")
feedSource = request.query["feed-source"]
if feedSource = invalid
response.Default(400, "Expected feed-source")
return true
end if
@@ -64,12 +65,10 @@ namespace Http
return true
end if
requestData = ParseJson(requestData)
feedSource = ParseJson(feedSource)
' TODO:P0 handle pagination
' Perhaps the page arg can be added to queryParam of requestData directly
invService = new Invidious.InvidiousService(invidiousNode)
invResponse = invService.MakeRequest(requestData)
invResponse = invService.MakeRequest(feedSource, feedSource.state?.queryParams)
if not invResponse.success
response.Default(500, `Failed to make request: ${invResponse.error}`)
return true
@@ -1,3 +1,4 @@
import "pkg:/components/Web/PlayletWebServer/Middleware/BookmarksRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/DashRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs"
@@ -38,6 +39,7 @@ function SetupRoutes(server as object)
server.UseRouter(new Http.PreferencesRouter())
server.UseRouter(new Http.InvidiousRouter())
server.UseRouter(new Http.PlayQueueRouter())
server.UseRouter(new Http.BookmarksRouter())
server.UseRouter(new Http.SearchHistoryRouter())
server.UseRouter(new Http.PlayletLibUrlsRouter())
@@ -4,6 +4,7 @@
<field id="invidious" type="node" />
<field id="applicationInfo" type="node" />
<field id="playQueue" type="node" />
<field id="bookmarks" type="node" />
<function name="GetServerAddress" />
</interface>
</component>