]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Add auth user client hook actions
authorChocobozzz <me@florianbigard.com>
Wed, 24 Mar 2021 10:34:31 +0000 (11:34 +0100)
committerChocobozzz <me@florianbigard.com>
Wed, 24 Mar 2021 17:18:41 +0000 (18:18 +0100)
client/src/app/core/plugins/hooks.service.ts
client/src/app/core/plugins/plugin.service.ts
client/src/types/register-client-option.model.ts
shared/models/plugins/client-hook.model.ts

index ec47aa48c562fa621857c3b608e07edea5481dd1..ddde198d2ffb0fe2c7ec7ab0fd7023259a6ba36c 100644 (file)
@@ -3,13 +3,29 @@ import { mergeMap, switchMap } from 'rxjs/operators'
 import { Injectable } from '@angular/core'
 import { PluginService } from '@app/core/plugins/plugin.service'
 import { ClientActionHookName, ClientFilterHookName, PluginClientScope } from '@shared/models'
+import { AuthService, AuthStatus } from '../auth'
 
 type RawFunction<U, T> = (params: U) => T
 type ObservableFunction<U, T> = RawFunction<U, Observable<T>>
 
 @Injectable()
 export class HooksService {
-  constructor (private pluginService: PluginService) { }
+  constructor (
+    private authService: AuthService,
+    private pluginService: PluginService
+  ) {
+    // Run auth hooks
+    this.authService.userInformationLoaded
+      .subscribe(() => this.runAction('action:auth-user.information-loaded', 'common', { user: this.authService.getUser() }))
+
+    this.authService.loginChangedSource.subscribe(obj => {
+      if (obj === AuthStatus.LoggedIn) {
+        this.runAction('action:auth-user.logged-in', 'common')
+      } else if (obj === AuthStatus.LoggedOut) {
+        this.runAction('action:auth-user.logged-out', 'common')
+      }
+    })
+  }
 
   wrapObsFun
     <P, R, H1 extends ClientFilterHookName, H2 extends ClientFilterHookName>
index b755fda2c6e3344c963c860987804985cbcaab6a..54dba5e178b5ee8095bb57ec58cd74a65335f0fc 100644 (file)
@@ -235,6 +235,12 @@ export class PluginService implements ClientHook {
                    .toPromise()
       },
 
+      getServerConfig: () => {
+        return this.server.getConfig()
+          .pipe(catchError(res => this.restExtractor.handleError(res)))
+          .toPromise()
+      },
+
       isLoggedIn: () => {
         return this.authService.isLoggedIn()
       },
index e3c6d803d476d165f3d8464a00f6f9efa307c471..7e5356a2b4fe9f35d9c2908b4bd0eb40b1b71f0f 100644 (file)
@@ -1,5 +1,6 @@
 import { RegisterClientFormFieldOptions, RegisterClientVideoFieldOptions } from '@shared/models/plugins/register-client-form-field.model'
 import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model'
+import { ServerConfig } from '@shared/models/server'
 
 export type RegisterClientOptions = {
   registerHook: (options: RegisterClientHookOptions) => void
@@ -16,6 +17,8 @@ export type RegisterClientHelpers = {
 
   getSettings: () => Promise<{ [ name: string ]: string }>
 
+  getServerConfig: () => Promise<ServerConfig>
+
   notifier: {
     info: (text: string, title?: string, timeout?: number) => void,
     error: (text: string, title?: string, timeout?: number) => void,
index 6c92ef3c67812ee61b14a3e41842bc3519b0701b..f8ca32771507519fcf1ab035997b77ba579c40d5 100644 (file)
@@ -94,6 +94,12 @@ export const clientActionHookObject = {
   // Fired when the "Go Live" page is being initalized
   'action:go-live.init': true,
 
+  // Fired when the user explicitely logged in/logged out
+  'action:auth-user.logged-in': true,
+  'action:auth-user.logged-out': true,
+  // Fired when the application loaded user information (using tokens from the local storage or after a successful login)
+  'action:auth-user.information-loaded': true,
+
   // Fired when the modal to download a video/caption is shown
   'action:modal.video-download.shown': true,