From 394dd23999c2f29a5d0de8784f64c011ba4779b5 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Mon, 4 Mar 2024 09:04:34 -0500 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + docs/playlet-web-api.yml | 87 ++++- .../ContentNode/ActionContentNode.xml | 6 - .../ContentNode/ProfileContentNode.xml | 12 + .../src/components/Dialog/LoginDialog.bs | 1 + .../src/components/MainScene.transpiled.xml | 4 +- playlet-lib/src/components/MainScene.xml | 16 +- .../MainScene_bindings.transpiled.brs | 7 +- .../src/components/NavBar/IconButton.bs | 4 +- playlet-lib/src/components/NavBar/NavBar.bs | 4 +- .../src/components/NavBar/NavBarItem.xml | 2 + .../components/NavBar/ProfileIconButton.bs | 111 ++++++ .../components/NavBar/ProfileIconButton.xml | 40 +++ .../src/components/ProfileView/ProfileView.bs | 160 +++++++++ .../components/ProfileView/ProfileView.xml | 97 +++++ .../ProfileView/ProfileViewUtils.bs | 11 + .../ProfileRowList/ProfileRowList.xml | 2 + .../ProfileRowList/ProfileRowListCell.bs | 68 ++++ .../ProfileRowList/ProfileRowListCell.xml | 106 ++++++ .../Screens/ProfileScreen/ProfileScreen.bs | 63 ++++ .../Screens/ProfileScreen/ProfileScreen.xml | 21 ++ .../RemoteScreen/GenerateQrCodeTask.bs | 2 + .../Services/Invidious/Invidious.bs | 5 - .../Services/Invidious/Invidious.xml | 2 +- .../Services/Invidious/InvidiousService.bs | 172 ++++----- .../Invidious/InvidiousUnregisterTokenTask.bs | 12 - .../components/Services/Profiles/Profiles.bs | 331 ++++++++++++++++++ .../components/Services/Profiles/Profiles.xml | 31 ++ .../ProfilesInvidiousUnregisterTokenTask.bs | 22 ++ .../Profiles/ProfilesVerifyTokensTask.bs | 38 ++ .../VideoFeed/VideoRowCell/ActionRowCell.bs | 13 - .../VideoFeed/VideoRowCell/ActionRowCell.xml | 17 - .../VideoFeed/VideoRowCell/RowCell.bs | 2 - .../VideoFeed/VideoRowCell/RowCell.xml | 3 - .../src/components/VideoFeed/VideoRowList.bs | 8 +- .../VideoFeed/VideoRowListRowContentTask.bs | 19 +- .../Middleware/InvidiousRouter.bs | 21 +- .../Middleware/ProfilesRouter.bs | 73 ++++ .../Middleware/StateApiRouter.bs | 24 +- .../Web/PlayletWebServer/PlayletWebServer.bs | 2 + .../Web/PlayletWebServer/PlayletWebServer.xml | 1 + playlet-lib/src/images/icons/crown.png | Bin 0 -> 2480 bytes playlet-lib/src/images/icons/crown.svg | 39 +++ .../src/images/icons/crown.svg.meta.json5 | 11 + playlet-lib/src/images/icons/user-200.png | Bin 0 -> 7948 bytes playlet-lib/src/images/icons/user.png | Bin 2074 -> 2351 bytes playlet-lib/src/images/icons/user.svg | 13 +- .../src/images/icons/user.svg.meta.json5 | 14 +- playlet-lib/src/source/utils/ArrayUtils.bs | 12 + playlet-lib/src/source/utils/ColorUtils.bs | 25 +- .../src/source/utils/ColorUtils.spec.bs | 8 +- playlet-lib/src/source/utils/RegistryUtils.bs | 5 +- playlet-web/src/lib/Api/PlayletApi.ts | 13 +- playlet-web/src/lib/NavBar.svelte | 96 ++--- playlet-web/src/lib/ProfileAvatar.svelte | 32 ++ playlet-web/src/lib/ProfilesDialog.svelte | 136 +++++++ playlet-web/src/lib/Screens/InfoScreen.svelte | 7 + playlet-web/src/lib/Types.ts | 1 + .../src/lib/VideoFeed/VideoListRow.svelte | 2 +- tools/bs-plugins/type-gen-plugin.ts | 1 + 60 files changed, 1725 insertions(+), 311 deletions(-) delete mode 100644 playlet-lib/src/components/ContentNode/ActionContentNode.xml create mode 100644 playlet-lib/src/components/ContentNode/ProfileContentNode.xml create mode 100644 playlet-lib/src/components/NavBar/ProfileIconButton.bs create mode 100644 playlet-lib/src/components/NavBar/ProfileIconButton.xml create mode 100644 playlet-lib/src/components/ProfileView/ProfileView.bs create mode 100644 playlet-lib/src/components/ProfileView/ProfileView.xml create mode 100644 playlet-lib/src/components/ProfileView/ProfileViewUtils.bs create mode 100644 playlet-lib/src/components/Screens/ProfileScreen/ProfileRowList/ProfileRowList.xml create mode 100644 playlet-lib/src/components/Screens/ProfileScreen/ProfileRowList/ProfileRowListCell.bs create mode 100644 playlet-lib/src/components/Screens/ProfileScreen/ProfileRowList/ProfileRowListCell.xml create mode 100644 playlet-lib/src/components/Screens/ProfileScreen/ProfileScreen.bs create mode 100644 playlet-lib/src/components/Screens/ProfileScreen/ProfileScreen.xml delete mode 100644 playlet-lib/src/components/Services/Invidious/InvidiousUnregisterTokenTask.bs create mode 100644 playlet-lib/src/components/Services/Profiles/Profiles.bs create mode 100644 playlet-lib/src/components/Services/Profiles/Profiles.xml create mode 100644 playlet-lib/src/components/Services/Profiles/ProfilesInvidiousUnregisterTokenTask.bs create mode 100644 playlet-lib/src/components/Services/Profiles/ProfilesVerifyTokensTask.bs delete mode 100644 playlet-lib/src/components/VideoFeed/VideoRowCell/ActionRowCell.bs delete mode 100644 playlet-lib/src/components/VideoFeed/VideoRowCell/ActionRowCell.xml create mode 100644 playlet-lib/src/components/Web/PlayletWebServer/Middleware/ProfilesRouter.bs create mode 100644 playlet-lib/src/images/icons/crown.png create mode 100644 playlet-lib/src/images/icons/crown.svg create mode 100644 playlet-lib/src/images/icons/crown.svg.meta.json5 create mode 100644 playlet-lib/src/images/icons/user-200.png create mode 100644 playlet-web/src/lib/ProfileAvatar.svelte create mode 100644 playlet-web/src/lib/ProfilesDialog.svelte diff --git a/CHANGELOG.md b/CHANGELOG.md index d147cc05..bb39b3b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Support for multiple profiles. You can now use multiple Invidious accounts. - When a video error dialog shows, a timer with 10 seconds starts to play the next video ### Fixed diff --git a/docs/playlet-web-api.yml b/docs/playlet-web-api.yml index 1f3a95ef..d65c1bb9 100644 --- a/docs/playlet-web-api.yml +++ b/docs/playlet-web-api.yml @@ -322,14 +322,63 @@ paths: schema: type: string format: uri - /invidious/logout: + /api/profiles: get: - summary: Logout from Invidious - description: Logout from Invidious, and invalidate the token if any. - operationId: invidiousLogout + summary: Get profiles + description: Get the logged profiles, and the currently active profile. + operationId: getProfiles responses: - "204": - description: No Content + "200": + description: OK + content: + application/json: + schema: + type: object + properties: + currentProfile: + type: string + profiles: + type: array + items: + $ref: "#/components/schemas/ProfileObject" + delete: + summary: Logout from a profile + description: Logout from a profile. This will remove the profile from the list of logged profiles. + operationId: logoutProfile + parameters: + - name: id + in: query + description: The ID of the profile to logout + required: true + schema: + type: string + responses: + "200": + description: OK + "400": + description: Bad Request + "404": + description: Not Found + /api/profiles/activate: + post: + summary: Activate a profile + description: Activate a profile. This will set the profile as the active profile. + operationId: activateProfile + requestBody: + content: + application/json: + schema: + type: object + properties: + id: + type: string + responses: + "200": + description: OK + "400": + description: Bad Request + "404": + description: Not Found # Roku External Control Protocol (ECP) /input/{appId}: @@ -530,14 +579,17 @@ components: type: string auth_url: type: string - logged_in: - type: boolean - logged_in_instance: - type: string - logged_in_username: - type: string preferences: $ref: "#/components/schemas/PreferencesObject" + profiles: + type: object + properties: + currentProfile: + type: string + profiles: + type: array + items: + $ref: "#/components/schemas/ProfileObject" PreferencesObject: type: object properties: @@ -560,6 +612,17 @@ components: type: boolean sponsorblock.show_notifications: type: boolean + ProfileObject: + type: object + properties: + id: + type: string + type: + type: string + username: + type: string + serverUrl: + type: string # TODO:P1 add the rest of properties VideoQueueObject: type: object diff --git a/playlet-lib/src/components/ContentNode/ActionContentNode.xml b/playlet-lib/src/components/ContentNode/ActionContentNode.xml deleted file mode 100644 index 9643fc02..00000000 --- a/playlet-lib/src/components/ContentNode/ActionContentNode.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/playlet-lib/src/components/ContentNode/ProfileContentNode.xml b/playlet-lib/src/components/ContentNode/ProfileContentNode.xml new file mode 100644 index 00000000..acd96368 --- /dev/null +++ b/playlet-lib/src/components/ContentNode/ProfileContentNode.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/playlet-lib/src/components/Dialog/LoginDialog.bs b/playlet-lib/src/components/Dialog/LoginDialog.bs index b5fc59ce..70278838 100644 --- a/playlet-lib/src/components/Dialog/LoginDialog.bs +++ b/playlet-lib/src/components/Dialog/LoginDialog.bs @@ -42,5 +42,6 @@ function OnUrlSet() end function function OnAuthTokenChange() + m.invidious.unobserveFieldScoped("authToken") Close() end function diff --git a/playlet-lib/src/components/MainScene.transpiled.xml b/playlet-lib/src/components/MainScene.transpiled.xml index 435af8f6..6a22bbd9 100644 --- a/playlet-lib/src/components/MainScene.transpiled.xml +++ b/playlet-lib/src/components/MainScene.transpiled.xml @@ -22,8 +22,9 @@ - + + @@ -41,6 +42,7 @@ + diff --git a/playlet-lib/src/components/MainScene.xml b/playlet-lib/src/components/MainScene.xml index ec5bdeb6..d9aab65a 100644 --- a/playlet-lib/src/components/MainScene.xml +++ b/playlet-lib/src/components/MainScene.xml @@ -8,8 +8,15 @@ videoQueue="bind:./VideoQueue"> - + + + + + + preferences="bind:../Preferences" + profiles="bind:../Profiles" /> + + \ No newline at end of file diff --git a/playlet-lib/src/components/NavBar/ProfileIconButton.bs b/playlet-lib/src/components/NavBar/ProfileIconButton.bs new file mode 100644 index 00000000..4b971a07 --- /dev/null +++ b/playlet-lib/src/components/NavBar/ProfileIconButton.bs @@ -0,0 +1,111 @@ +import "pkg:/source/utils/Logging.bs" +import "pkg:/source/utils/MathUtils.bs" +import "pkg:/source/utils/StringUtils.bs" +import "pkg:/source/utils/Types.bs" + +' TODO:P1 this file is a copy paste of IconButton. It should be refactored. +function Init() + m.buttonBackground = m.top.findNode("buttonBackground") + m.buttonIcon = m.top.findNode("buttonIcon") + m.buttonText = m.top.findNode("buttonText") + + m.circlePoster = m.top.findNode("circlePoster") + m.circlePosterInner = m.top.findNode("circlePosterInner") + m.letterLabel = m.top.findNode("LetterLabel") + + m.top.observeField("background", FuncName(onBackgroundChanged)) + m.top.observeField("icon", FuncName(onIconChanged)) + m.top.observeField("text", FuncName(onTextChanged)) + m.top.observeField("height", FuncName(onHeightChanged)) + m.top.observeField("width", FuncName(onWidthChanged)) + m.top.observeField("padding", FuncName(onPaddingChanged)) + + profiles = m.top.getScene().findNode("Profiles") + if profiles <> invalid + m.profiles = profiles + profiles.observeField("currentProfile", FuncName(OnCurrentProfile)) + OnCurrentProfile() + end if +end function + +function OnFocusChange() + if m.top.focus + m.buttonBackground.blendColor = m.top.focusBackground + else + m.buttonBackground.blendColor = m.top.background + end if + m.buttonBackground.visible = m.top.focus + m.buttonText.visible = m.top.focus +end function + +function onBackgroundChanged() + m.buttonBackground.blendColor = m.top.background +end function + +function onIconChanged() + m.buttonIcon.uri = m.top.icon +end function + +function onTextChanged() + m.buttonText.text = m.top.text +end function + +function setIconSize() + height = m.top.height + width = m.top.width + if height > 0 and width > 0 + iconSize = MathUtils.Min(height, width) - m.top.padding + m.buttonIcon.height = iconSize + m.buttonIcon.width = iconSize + + iconTranslation = [((width - iconSize) / 2), ((height - iconSize) / 2)] + m.buttonIcon.translation = iconTranslation + m.buttonText.translation = [-5, height + 2] + m.buttonText.width = width + 10 + + m.circlePoster.width = iconSize + m.circlePoster.height = iconSize + m.circlePoster.translation = iconTranslation + + m.circlePosterInner.width = iconSize - 4 + m.circlePosterInner.height = iconSize - 4 + + m.letterLabel.width = iconSize - 4 + m.letterLabel.height = iconSize - 4 + end if +end function + +function onHeightChanged() + m.buttonBackground.height = m.top.height + setIconSize() +end function + +function onWidthChanged() + m.buttonBackground.width = m.top.width - 10 + m.buttonBackground.translation = [5, 0] + setIconSize() +end function + +function onPaddingChanged() + setIconSize() +end function + +function OnCurrentProfile() as void + currentProfile = m.profiles.currentProfile + if currentProfile = invalid + m.circlePoster.visible = false + m.buttonIcon.visible = true + return + end if + + username = currentProfile.username + if not StringUtils.IsNullOrEmpty(username) + m.LetterLabel.text = UCase(username.Left(1)) + else + m.LetterLabel.text = "" + end if + m.circlePosterInner.blendColor = currentProfile.color + + m.circlePoster.visible = true + m.buttonIcon.visible = false +end function diff --git a/playlet-lib/src/components/NavBar/ProfileIconButton.xml b/playlet-lib/src/components/NavBar/ProfileIconButton.xml new file mode 100644 index 00000000..34da6903 --- /dev/null +++ b/playlet-lib/src/components/NavBar/ProfileIconButton.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/playlet-lib/src/components/ProfileView/ProfileView.bs b/playlet-lib/src/components/ProfileView/ProfileView.bs new file mode 100644 index 00000000..9b79515b --- /dev/null +++ b/playlet-lib/src/components/ProfileView/ProfileView.bs @@ -0,0 +1,160 @@ +import "pkg:/components/Navigation/Navigation.bs" +import "pkg:/source/utils/DisposableObserve.bs" +import "pkg:/source/utils/FocusManagement.bs" +import "pkg:/source/utils/StringUtils.bs" +import "pkg:/source/utils/Types.bs" + +function Init() + m.activateButton = m.top.findNode("activateButton") + m.logoutButton = m.top.findNode("logoutButton") + m.closeButton = m.top.findNode("closeButton") + m.buttonGroup = m.top.findNode("buttonGroup") + + DisposableObserveField(m.activateButton, "buttonSelected", FuncName(Activate)) + DisposableObserveField(m.logoutButton, "buttonSelected", FuncName(Logout)) + DisposableObserveField(m.closeButton, "buttonSelected", FuncName(Close)) +end function + +function OnNodeReady() + SetNavigation(m.activateButton, "down", m.logoutButton) + SetNavigation(m.logoutButton, "down", m.closeButton) + SetNavigation(m.closeButton, "up", m.logoutButton) + SetNavigation(m.logoutButton, "up", m.activateButton) + + DisposableObserveFieldScoped(m.profiles, "currentProfile", FuncName(OnCunrrentProfileChanged)) + DisposableObserveFieldScoped(m.profiles, "onProfileLogout", FuncName(OnProfileLogout)) +end function + +function OnContentSet() as void + content = m.top.content + if content = invalid + return + end if + + m.top.circlePosterInnerUri = "pkg:/images/white-circle.png" + m.top.circlePosterInnerBlendColor = content.color + username = content.username + m.top.username = username + if not StringUtils.IsNullOrEmpty(username) + m.top.letter = UCase(username.Left(1)) + else + m.top.letter = "" + end if + m.top.serverUrl = content.serverUrl + m.top.crownVisible = content.isSelected + UpdateActivateButton() +end function + +function OnFocusChange() as void + if not m.top.focus + return + end if + + if m.activateButton.focusable + NodeSetFocus(m.activateButton, true) + else + NodeSetFocus(m.logoutButton, true) + end if +end function + +function OnkeyEvent(key as string, press as boolean) as boolean + if NavigationKeyHandler(key, press).handled + return true + end if + + if key = "options" + ' A pass-through to the app controller, so it can toggle picture-in-picture + return false + end if + + if key = "back" and press + Close() + return true + end if + + return true +end function + +function Activate() as void + content = m.top.content + if content = invalid + return + end if + + profile = m.profiles.content.getChild(0).findNode(content.id) + if profile = invalid + return + end if + + m.top.content = invalid + m.profiles@.SetCurrentProfile(profile) + Close() +end function + +function Logout() as void + content = m.top.content + if content = invalid + return + end if + + m.top.content = invalid + m.profiles@.LogoutWithProfile(content) + Close() +end function + +function Close() + m.top.appController@.PopScreen() + m.top.dispose = true +end function + +function OnDispose() + DisposeObservers() + m.top.content = invalid +end function + +function OnCunrrentProfileChanged() as void + content = m.top.content + if content = invalid + return + end if + m.top.crownVisible = content.isSelected + UpdateActivateButton() +end function + +function OnProfileLogout() as void + content = m.top.content + if content = invalid + return + end if + + profile = m.profiles.content.getChild(0).findNode(content.id) + if profile = invalid + Close() + end if +end function + +function UpdateActivateButton() as void + content = m.top.content + if content = invalid + return + end if + + isSelected = content.isSelected + if isSelected + if m.activateButton.hasFocus() + NodeSetFocus(m.logoutButton, true) + end if + + parent = m.activateButton.getParent() + if parent <> invalid + m.buttonGroup.removeChild(m.activateButton) + end if + m.activateButton.focusable = false + else + parent = m.activateButton.getParent() + if parent = invalid + m.buttonGroup.insertChild(m.activateButton, 0) + end if + m.activateButton.focusable = true + end if +end function diff --git a/playlet-lib/src/components/ProfileView/ProfileView.xml b/playlet-lib/src/components/ProfileView/ProfileView.xml new file mode 100644 index 00000000..4620088f --- /dev/null +++ b/playlet-lib/src/components/ProfileView/ProfileView.xml @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - {:else} -
  • -
    - -
    -
  • - {/if} - - + + + diff --git a/playlet-web/src/lib/ProfileAvatar.svelte b/playlet-web/src/lib/ProfileAvatar.svelte new file mode 100644 index 00000000..84ffb88d --- /dev/null +++ b/playlet-web/src/lib/ProfileAvatar.svelte @@ -0,0 +1,32 @@ + + +
    + {#if currentProfile && currentProfile.username} +
    +
    + {currentProfile.username.substring(0, 1).toUpperCase()} +
    +
    + {:else} +
    + +
    + {/if} +
    diff --git a/playlet-web/src/lib/ProfilesDialog.svelte b/playlet-web/src/lib/ProfilesDialog.svelte new file mode 100644 index 00000000..091c2939 --- /dev/null +++ b/playlet-web/src/lib/ProfilesDialog.svelte @@ -0,0 +1,136 @@ + + + + + + diff --git a/playlet-web/src/lib/Screens/InfoScreen.svelte b/playlet-web/src/lib/Screens/InfoScreen.svelte index ee28226c..3333162f 100644 --- a/playlet-web/src/lib/Screens/InfoScreen.svelte +++ b/playlet-web/src/lib/Screens/InfoScreen.svelte @@ -83,6 +83,7 @@ let deviceInfo = {}; let invidiousInfo = {}; let preferencesInfo = {}; + let profilesInfo = {}; const feedbackTitle = encodeURIComponent("[Feedback] Playlet"); let githubUrlIssue = "https://github.com/iBicha/playlet/issues/new"; let mailToUrl = "mailto:brahim.hadriche@gmail.com"; @@ -92,6 +93,7 @@ deviceInfo = value.device || {}; invidiousInfo = value.invidious || {}; preferencesInfo = value.preferences || {}; + profilesInfo = value.profiles || {}; githubUrlIssue = createGithubIssueUrl(); mailToUrl = createMailToUrl(); }); @@ -118,6 +120,11 @@ ${JSON.stringify(invidiousInfo, null, 2)} #### User preferences \`\`\` ${JSON.stringify(preferencesInfo, null, 2)} +\`\`\` + +#### Profiles +\`\`\` +${JSON.stringify(profilesInfo, null, 2)} \`\`\``; } diff --git a/playlet-web/src/lib/Types.ts b/playlet-web/src/lib/Types.ts index 8da878c2..b790ea7b 100644 --- a/playlet-web/src/lib/Types.ts +++ b/playlet-web/src/lib/Types.ts @@ -9,4 +9,5 @@ export type PlayletState = { device?: any, invidious?: any, preferences?: any, + profiles?: any, } diff --git a/playlet-web/src/lib/VideoFeed/VideoListRow.svelte b/playlet-web/src/lib/VideoFeed/VideoListRow.svelte index d6a02824..968c2749 100644 --- a/playlet-web/src/lib/VideoFeed/VideoListRow.svelte +++ b/playlet-web/src/lib/VideoFeed/VideoListRow.svelte @@ -84,7 +84,7 @@ playletStateStore.subscribe((value) => { invidiousApi.instance = value?.invidious?.current_instance; invidiousApi.userCountryCode = value?.device?.user_country_code ?? "US"; - invidiousApi.isLoggedIn = value.invidious?.logged_in ?? false; + invidiousApi.isLoggedIn = !!(value.profiles?.currentProfile ?? false); loadRow(); }); diff --git a/tools/bs-plugins/type-gen-plugin.ts b/tools/bs-plugins/type-gen-plugin.ts index 610cd579..538c7ca2 100644 --- a/tools/bs-plugins/type-gen-plugin.ts +++ b/tools/bs-plugins/type-gen-plugin.ts @@ -10,6 +10,7 @@ const types = [ { name: 'AssociativeArray', interface: 'ifAssociativeArray', type: 'object', defaultValue: '{}' }, { name: 'ByteArray', interface: 'ifByteArray', type: null, defaultValue: null }, { name: 'Function', interface: 'ifFunction', type: null, defaultValue: null }, + { name: 'Node', interface: 'ifSGNodeDict', type: null, defaultValue: null }, ]; const generatedCodeHeader = `' The rest of this file is generated by the TypeGenPlugin`