From 86243255239959fb45078cbc2043248e62ee05cf Mon Sep 17 00:00:00 2001 From: kontrollanten <6680299+kontrollanten@users.noreply.github.com> Date: Fri, 26 Apr 2024 10:08:46 +0200 Subject: [PATCH] feat(client/plugins): add getUser to helpers Let plugins access the user object. closes #6355 --- client/src/app/core/plugins/plugin.service.ts | 4 ++ client/src/app/core/users/user.model.ts | 50 ++----------------- .../videos/shared/peertube-plugin.ts | 2 + .../src/types/register-client-option.model.ts | 3 ++ 4 files changed, 13 insertions(+), 46 deletions(-) diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts index 64443dc85..43f10e02e 100644 --- a/client/src/app/core/plugins/plugin.service.ts +++ b/client/src/app/core/plugins/plugin.service.ts @@ -250,6 +250,10 @@ export class PluginService implements ClientHook { return firstValueFrom(obs) }, + getUser: () => { + return this.authService.getUser() + }, + getServerConfig: () => { const obs = this.server.getConfig() .pipe(catchError(res => this.restExtractor.handleError(res))) diff --git a/client/src/app/core/users/user.model.ts b/client/src/app/core/users/user.model.ts index ffc7c2b44..f075ece2b 100644 --- a/client/src/app/core/users/user.model.ts +++ b/client/src/app/core/users/user.model.ts @@ -74,54 +74,12 @@ export class User implements UserServerModel { createdAt: Date constructor (hash: Partial) { - this.id = hash.id - this.username = hash.username - this.email = hash.email + const { account, ...mergeProps }: Partial = hash - this.role = hash.role + Object.assign(this, mergeProps) - this.videoChannels = hash.videoChannels - - this.videoQuota = hash.videoQuota - this.videoQuotaDaily = hash.videoQuotaDaily - this.videoQuotaUsed = hash.videoQuotaUsed - this.videoQuotaUsedDaily = hash.videoQuotaUsedDaily - this.videosCount = hash.videosCount - this.abusesCount = hash.abusesCount - this.abusesAcceptedCount = hash.abusesAcceptedCount - this.abusesCreatedCount = hash.abusesCreatedCount - this.videoCommentsCount = hash.videoCommentsCount - - this.nsfwPolicy = hash.nsfwPolicy - this.p2pEnabled = hash.p2pEnabled - this.autoPlayVideo = hash.autoPlayVideo - this.autoPlayNextVideo = hash.autoPlayNextVideo - this.autoPlayNextVideoPlaylist = hash.autoPlayNextVideoPlaylist - this.videosHistoryEnabled = hash.videosHistoryEnabled - this.videoLanguages = hash.videoLanguages - - this.theme = hash.theme - - this.adminFlags = hash.adminFlags - - this.blocked = hash.blocked - this.blockedReason = hash.blockedReason - - this.noInstanceConfigWarningModal = hash.noInstanceConfigWarningModal - this.noWelcomeModal = hash.noWelcomeModal - this.noAccountSetupWarningModal = hash.noAccountSetupWarningModal - - this.notificationSettings = hash.notificationSettings - - this.twoFactorEnabled = hash.twoFactorEnabled - - this.createdAt = hash.createdAt - - this.pluginAuth = hash.pluginAuth - this.lastLoginDate = hash.lastLoginDate - - if (hash.account !== undefined) { - this.account = new Account(hash.account) + if (account !== undefined) { + this.account = new Account(account) } } diff --git a/client/src/standalone/videos/shared/peertube-plugin.ts b/client/src/standalone/videos/shared/peertube-plugin.ts index 95433299e..f9e668f1b 100644 --- a/client/src/standalone/videos/shared/peertube-plugin.ts +++ b/client/src/standalone/videos/shared/peertube-plugin.ts @@ -54,6 +54,8 @@ export class PeerTubePlugin { .then((obj: PublicServerSetting) => obj.publicSettings) }, + getUser: unimplemented, + isLoggedIn: () => this.http.isLoggedIn(), getAuthHeader: () => { if (!this.http.isLoggedIn()) return undefined diff --git a/client/src/types/register-client-option.model.ts b/client/src/types/register-client-option.model.ts index 2336119bb..a1dff377e 100644 --- a/client/src/types/register-client-option.model.ts +++ b/client/src/types/register-client-option.model.ts @@ -1,3 +1,4 @@ +import { AuthUser } from '@app/core' import { RegisterClientFormFieldOptions, RegisterClientHookOptions, @@ -35,6 +36,8 @@ export type RegisterClientHelpers = { getSettings: () => Promise + getUser: () => AuthUser + getServerConfig: () => Promise notifier: {