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

+92 -3
View File
@@ -12,6 +12,13 @@ export class InvidiousApi {
// Note: handlers for authenticated requests are not needed, since they are handled server side
this.responseHandlers = {
"DefaultHandler": (requestData, response) => this.DefaultHandler(requestData, response),
"PlaylistHandler": (requestData, response) => this.PlaylistHandler(requestData, response),
"VideoInfoHandler": (requestData, response) => this.VideoInfoHandler(requestData, response),
"PlaylistInfoHandler": (requestData, response) => this.PlaylistInfoHandler(requestData, response),
"ChannelInfoHandler": (requestData, response) => this.ChannelInfoHandler(requestData, response),
"ChannelVideosHandler": (requestData, response) => this.ChannelVideosHandler(requestData, response),
"ChannelPlaylistsHandler": (requestData, response) => this.ChannelPlaylistsHandler(requestData, response),
"ChannelRelatedChannelsHandler": (requestData, response) => this.ChannelRelatedChannelsHandler(requestData, response),
}
}
@@ -44,10 +51,39 @@ export class InvidiousApi {
return await response.json();
}
public async makeRequest(feed: any) {
// TODO:P0 handle multiple feed sources
const feedSource = feed.feedSources[0]
public async markFeedSourcePagination(feedSource: any) {
const endpoint = this.endpoints[feedSource.endpoint]
if (!endpoint || !endpoint.paginationType) {
return;
}
const feedSourceState = feedSource.state
feedSourceState.queryParams = feedSourceState.queryParams || {}
feedSourceState.paginationType = endpoint.paginationType
if (feedSourceState.paginationType === "Pages") {
if (!Number.isInteger(feedSourceState.page)) {
feedSourceState.page = 0;
}
feedSourceState.page += 1;
feedSourceState.queryParams.page = feedSourceState.page;
} else if (feedSourceState.paginationType === "Continuation") {
const continuation = feedSourceState.continuation;
if (continuation) {
feedSourceState.queryParams.continuation = continuation;
} else {
delete feedSourceState.queryParams.continuation;
}
}
}
public canMakeRequest() {
return !!(this.instance && this.endpoints && Object.keys(this.endpoints).length);
}
public async makeRequest(feedSource: any) {
// TODO:P0 implement localStorage caching
if (!feedSource || !this.instance || !this.endpoints) {
return null;
}
@@ -90,6 +126,10 @@ export class InvidiousApi {
queryParams = { ...queryParams, ...feedSource.queryParams };
}
if (feedSource.state.queryParams !== undefined) {
queryParams = { ...queryParams, ...feedSource.state.queryParams };
}
if (feedSource.pathParams !== undefined) {
for (let param in feedSource.pathParams) {
url = url.replace(`{${param}}`, feedSource.pathParams[param]);
@@ -111,6 +151,55 @@ export class InvidiousApi {
return { items };
}
private async PlaylistHandler(feedSource, response) {
const json = await response.json();
return {
items: json.videos,
};
}
private async VideoInfoHandler(feedSource, response) {
const info = await response.json();
info.type = "video";
return { items: [info] };
}
private async ChannelInfoHandler(feedSource, response) {
const info = await response.json();
info.type = "channel";
return { items: [info] };
}
private async PlaylistInfoHandler(feedSource, response) {
const info = await response.json();
info.type = "playlist";
return { items: [info] };
}
private async ChannelVideosHandler(feedSource, response) {
const json = await response.json();
return {
items: json.videos,
continuation: json.continuation
};
}
private async ChannelPlaylistsHandler(feedSource, response) {
const json = await response.json();
return {
items: json.playlists,
continuation: json.continuation
};
}
private async ChannelRelatedChannelsHandler(feedSource, response) {
const json = await response.json();
return {
items: json.relatedChannels,
continuation: json.continuation
};
}
private makeUrl(url: string, params: any) {
const encodedUrl = new URL(url);
const existingParams = new URLSearchParams(encodedUrl.search);
+7 -2
View File
@@ -23,8 +23,8 @@ export class PlayletApi {
return await response.json();
}
static async invidiousAuthenticatedRequest(requestData) {
const url = PlayletApi.host() + "/invidious/authenticated-request?request-data=" + encodeURIComponent(JSON.stringify(requestData));
static async invidiousAuthenticatedRequest(feedSource) {
const url = PlayletApi.host() + "/invidious/authenticated-request?feed-source=" + encodeURIComponent(JSON.stringify(feedSource));
const response = await fetch(url);
return await response.json();
}
@@ -128,6 +128,11 @@ export class PlayletApi {
return await fetch(`${PlayletApi.host()}/api/search-history`, { method: "DELETE" });
}
static async getBookmarkFeeds() {
const response = await fetch(`${PlayletApi.host()}/api/bookmarks/feeds`);
return await response.json();
}
static async updateInstance(instance) {
return await PlayletApi.putJson(`${PlayletApi.host()}/api/preferences`, { "invidious.instance": instance });
}