diff options
Diffstat (limited to 'client/src/root-helpers')
-rw-r--r-- | client/src/root-helpers/index.ts | 1 | ||||
-rw-r--r-- | client/src/root-helpers/local-storage-utils.ts | 10 | ||||
-rw-r--r-- | client/src/root-helpers/plugins-manager.ts | 56 | ||||
-rw-r--r-- | client/src/root-helpers/users/index.ts | 1 | ||||
-rw-r--r-- | client/src/root-helpers/users/user-local-storage-keys.ts | 16 | ||||
-rw-r--r-- | client/src/root-helpers/users/user-local-storage-manager.ts | 55 | ||||
-rw-r--r-- | client/src/root-helpers/users/user-tokens.ts | 69 |
7 files changed, 96 insertions, 112 deletions
diff --git a/client/src/root-helpers/index.ts b/client/src/root-helpers/index.ts index 63d55d7ef..aa3b442dd 100644 --- a/client/src/root-helpers/index.ts +++ b/client/src/root-helpers/index.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | export * from './users' | 1 | export * from './users' |
2 | export * from './bytes' | 2 | export * from './bytes' |
3 | export * from './images' | 3 | export * from './images' |
4 | export * from './local-storage-utils' | ||
4 | export * from './peertube-web-storage' | 5 | export * from './peertube-web-storage' |
5 | export * from './utils' | 6 | export * from './utils' |
6 | export * from './plugins-manager' | 7 | export * from './plugins-manager' |
diff --git a/client/src/root-helpers/local-storage-utils.ts b/client/src/root-helpers/local-storage-utils.ts new file mode 100644 index 000000000..c2b3f9035 --- /dev/null +++ b/client/src/root-helpers/local-storage-utils.ts | |||
@@ -0,0 +1,10 @@ | |||
1 | function getBoolOrDefault (value: string, defaultValue: boolean) { | ||
2 | if (value === 'true') return true | ||
3 | if (value === 'false') return false | ||
4 | |||
5 | return defaultValue | ||
6 | } | ||
7 | |||
8 | export { | ||
9 | getBoolOrDefault | ||
10 | } | ||
diff --git a/client/src/root-helpers/plugins-manager.ts b/client/src/root-helpers/plugins-manager.ts index a1b763ff2..61731032a 100644 --- a/client/src/root-helpers/plugins-manager.ts +++ b/client/src/root-helpers/plugins-manager.ts | |||
@@ -7,36 +7,46 @@ import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' | |||
7 | import { | 7 | import { |
8 | ClientHookName, | 8 | ClientHookName, |
9 | clientHookObject, | 9 | clientHookObject, |
10 | ClientScript, | 10 | ClientScriptJSON, |
11 | HTMLServerConfig, | 11 | HTMLServerConfig, |
12 | PluginClientScope, | 12 | PluginClientScope, |
13 | PluginType, | 13 | PluginType, |
14 | RegisterClientFormFieldOptions, | 14 | RegisterClientFormFieldOptions, |
15 | RegisterClientHookOptions, | 15 | RegisterClientHookOptions, |
16 | RegisterClientSettingsScript, | 16 | RegisterClientRouteOptions, |
17 | RegisterClientSettingsScriptOptions, | ||
17 | RegisterClientVideoFieldOptions, | 18 | RegisterClientVideoFieldOptions, |
19 | RegisteredExternalAuthConfig, | ||
18 | ServerConfigPlugin | 20 | ServerConfigPlugin |
19 | } from '../../../shared/models' | 21 | } from '@shared/models' |
20 | import { environment } from '../environments/environment' | 22 | import { environment } from '../environments/environment' |
21 | import { ClientScript as ClientScriptModule } from '../types/client-script.model' | 23 | import { ClientScript } from '../types' |
22 | 24 | ||
23 | interface HookStructValue extends RegisterClientHookOptions { | 25 | interface HookStructValue extends RegisterClientHookOptions { |
24 | plugin: ServerConfigPlugin | 26 | plugin: ServerConfigPlugin |
25 | clientScript: ClientScript | 27 | clientScript: ClientScriptJSON |
26 | } | 28 | } |
27 | 29 | ||
28 | type Hooks = { [ name: string ]: HookStructValue[] } | 30 | type Hooks = { [ name: string ]: HookStructValue[] } |
29 | 31 | ||
30 | type PluginInfo = { | 32 | type PluginInfo = { |
31 | plugin: ServerConfigPlugin | 33 | plugin: ServerConfigPlugin |
32 | clientScript: ClientScript | 34 | clientScript: ClientScriptJSON |
33 | pluginType: PluginType | 35 | pluginType: PluginType |
34 | isTheme: boolean | 36 | isTheme: boolean |
35 | } | 37 | } |
36 | 38 | ||
37 | type PeertubeHelpersFactory = (pluginInfo: PluginInfo) => RegisterClientHelpers | 39 | type PeertubeHelpersFactory = (pluginInfo: PluginInfo) => RegisterClientHelpers |
38 | type OnFormFields = (options: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void | 40 | |
39 | type OnSettingsScripts = (pluginInfo: PluginInfo, options: RegisterClientSettingsScript) => void | 41 | type OnFormFields = ( |
42 | pluginInfo: PluginInfo, | ||
43 | options: RegisterClientFormFieldOptions, | ||
44 | videoFormOptions: RegisterClientVideoFieldOptions | ||
45 | ) => void | ||
46 | |||
47 | type OnSettingsScripts = (pluginInfo: PluginInfo, options: RegisterClientSettingsScriptOptions) => void | ||
48 | |||
49 | type OnClientRoute = (options: RegisterClientRouteOptions) => void | ||
40 | 50 | ||
41 | const logger = debug('peertube:plugins') | 51 | const logger = debug('peertube:plugins') |
42 | 52 | ||
@@ -63,21 +73,29 @@ class PluginsManager { | |||
63 | private readonly peertubeHelpersFactory: PeertubeHelpersFactory | 73 | private readonly peertubeHelpersFactory: PeertubeHelpersFactory |
64 | private readonly onFormFields: OnFormFields | 74 | private readonly onFormFields: OnFormFields |
65 | private readonly onSettingsScripts: OnSettingsScripts | 75 | private readonly onSettingsScripts: OnSettingsScripts |
76 | private readonly onClientRoute: OnClientRoute | ||
66 | 77 | ||
67 | constructor (options: { | 78 | constructor (options: { |
68 | peertubeHelpersFactory: PeertubeHelpersFactory | 79 | peertubeHelpersFactory: PeertubeHelpersFactory |
69 | onFormFields?: OnFormFields | 80 | onFormFields?: OnFormFields |
70 | onSettingsScripts?: OnSettingsScripts | 81 | onSettingsScripts?: OnSettingsScripts |
82 | onClientRoute?: OnClientRoute | ||
71 | }) { | 83 | }) { |
72 | this.peertubeHelpersFactory = options.peertubeHelpersFactory | 84 | this.peertubeHelpersFactory = options.peertubeHelpersFactory |
73 | this.onFormFields = options.onFormFields | 85 | this.onFormFields = options.onFormFields |
74 | this.onSettingsScripts = options.onSettingsScripts | 86 | this.onSettingsScripts = options.onSettingsScripts |
87 | this.onClientRoute = options.onClientRoute | ||
75 | } | 88 | } |
76 | 89 | ||
77 | static getPluginPathPrefix (isTheme: boolean) { | 90 | static getPluginPathPrefix (isTheme: boolean) { |
78 | return isTheme ? '/themes' : '/plugins' | 91 | return isTheme ? '/themes' : '/plugins' |
79 | } | 92 | } |
80 | 93 | ||
94 | static getExternalAuthHref (auth: RegisteredExternalAuthConfig) { | ||
95 | return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}` | ||
96 | |||
97 | } | ||
98 | |||
81 | loadPluginsList (config: HTMLServerConfig) { | 99 | loadPluginsList (config: HTMLServerConfig) { |
82 | for (const plugin of config.plugin.registered) { | 100 | for (const plugin of config.plugin.registered) { |
83 | this.addPlugin(plugin) | 101 | this.addPlugin(plugin) |
@@ -212,10 +230,10 @@ class PluginsManager { | |||
212 | throw new Error('Video field registration is not supported') | 230 | throw new Error('Video field registration is not supported') |
213 | } | 231 | } |
214 | 232 | ||
215 | return this.onFormFields(commonOptions, videoFormOptions) | 233 | return this.onFormFields(pluginInfo, commonOptions, videoFormOptions) |
216 | } | 234 | } |
217 | 235 | ||
218 | const registerSettingsScript = (options: RegisterClientSettingsScript) => { | 236 | const registerSettingsScript = (options: RegisterClientSettingsScriptOptions) => { |
219 | if (!this.onSettingsScripts) { | 237 | if (!this.onSettingsScripts) { |
220 | throw new Error('Registering settings script is not supported') | 238 | throw new Error('Registering settings script is not supported') |
221 | } | 239 | } |
@@ -223,13 +241,29 @@ class PluginsManager { | |||
223 | return this.onSettingsScripts(pluginInfo, options) | 241 | return this.onSettingsScripts(pluginInfo, options) |
224 | } | 242 | } |
225 | 243 | ||
244 | const registerClientRoute = (options: RegisterClientRouteOptions) => { | ||
245 | if (!this.onClientRoute) { | ||
246 | throw new Error('Registering client route is not supported') | ||
247 | } | ||
248 | |||
249 | return this.onClientRoute(options) | ||
250 | } | ||
251 | |||
226 | const peertubeHelpers = this.peertubeHelpersFactory(pluginInfo) | 252 | const peertubeHelpers = this.peertubeHelpersFactory(pluginInfo) |
227 | 253 | ||
228 | console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) | 254 | console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) |
229 | 255 | ||
230 | const absURL = (environment.apiUrl || window.location.origin) + clientScript.script | 256 | const absURL = (environment.apiUrl || window.location.origin) + clientScript.script |
231 | return dynamicImport(absURL) | 257 | return dynamicImport(absURL) |
232 | .then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers })) | 258 | .then((script: ClientScript) => { |
259 | return script.register({ | ||
260 | registerHook, | ||
261 | registerVideoField, | ||
262 | registerSettingsScript, | ||
263 | registerClientRoute, | ||
264 | peertubeHelpers | ||
265 | }) | ||
266 | }) | ||
233 | .then(() => this.sortHooksByPriority()) | 267 | .then(() => this.sortHooksByPriority()) |
234 | .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) | 268 | .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) |
235 | } | 269 | } |
diff --git a/client/src/root-helpers/users/index.ts b/client/src/root-helpers/users/index.ts index 8fbaca9e3..2b11d0b7e 100644 --- a/client/src/root-helpers/users/index.ts +++ b/client/src/root-helpers/users/index.ts | |||
@@ -1,3 +1,2 @@ | |||
1 | export * from './user-local-storage-keys' | 1 | export * from './user-local-storage-keys' |
2 | export * from './user-local-storage-manager' | ||
3 | export * from './user-tokens' | 2 | export * from './user-tokens' |
diff --git a/client/src/root-helpers/users/user-local-storage-keys.ts b/client/src/root-helpers/users/user-local-storage-keys.ts index 5f915899c..c3934ae3c 100644 --- a/client/src/root-helpers/users/user-local-storage-keys.ts +++ b/client/src/root-helpers/users/user-local-storage-keys.ts | |||
@@ -1,15 +1,25 @@ | |||
1 | export const UserLocalStorageKeys = { | 1 | export const UserLocalStorageKeys = { |
2 | ID: 'id', | 2 | ID: 'id', |
3 | USERNAME: 'username', | ||
3 | ROLE: 'role', | 4 | ROLE: 'role', |
4 | EMAIL: 'email', | 5 | EMAIL: 'email', |
6 | |||
5 | VIDEOS_HISTORY_ENABLED: 'videos-history-enabled', | 7 | VIDEOS_HISTORY_ENABLED: 'videos-history-enabled', |
6 | USERNAME: 'username', | ||
7 | NSFW_POLICY: 'nsfw_policy', | 8 | NSFW_POLICY: 'nsfw_policy', |
8 | WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled', | 9 | P2P_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled', |
10 | |||
9 | AUTO_PLAY_VIDEO: 'auto_play_video', | 11 | AUTO_PLAY_VIDEO: 'auto_play_video', |
10 | SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO: 'auto_play_next_video', | 12 | AUTO_PLAY_NEXT_VIDEO: 'auto_play_next_video', |
11 | AUTO_PLAY_VIDEO_PLAYLIST: 'auto_play_video_playlist', | 13 | AUTO_PLAY_VIDEO_PLAYLIST: 'auto_play_video_playlist', |
14 | |||
12 | THEME: 'theme', | 15 | THEME: 'theme', |
13 | LAST_ACTIVE_THEME: 'last_active_theme', | 16 | LAST_ACTIVE_THEME: 'last_active_theme', |
17 | |||
14 | VIDEO_LANGUAGES: 'video_languages' | 18 | VIDEO_LANGUAGES: 'video_languages' |
15 | } | 19 | } |
20 | |||
21 | export const UserTokenLocalStorageKeys = { | ||
22 | ACCESS_TOKEN: 'access_token', | ||
23 | REFRESH_TOKEN: 'refresh_token', | ||
24 | TOKEN_TYPE: 'token_type' | ||
25 | } | ||
diff --git a/client/src/root-helpers/users/user-local-storage-manager.ts b/client/src/root-helpers/users/user-local-storage-manager.ts deleted file mode 100644 index c75cea127..000000000 --- a/client/src/root-helpers/users/user-local-storage-manager.ts +++ /dev/null | |||
@@ -1,55 +0,0 @@ | |||
1 | import { NSFWPolicyType, UserRole } from '@shared/models' | ||
2 | import { peertubeLocalStorage } from '../peertube-web-storage' | ||
3 | import { UserLocalStorageKeys } from './user-local-storage-keys' | ||
4 | |||
5 | function getUserInfoFromLocalStorage () { | ||
6 | const usernameLocalStorage = peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME) | ||
7 | |||
8 | if (!usernameLocalStorage) return undefined | ||
9 | |||
10 | return { | ||
11 | id: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ID), 10), | ||
12 | username: peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME), | ||
13 | email: peertubeLocalStorage.getItem(UserLocalStorageKeys.EMAIL), | ||
14 | role: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole, | ||
15 | nsfwPolicy: peertubeLocalStorage.getItem(UserLocalStorageKeys.NSFW_POLICY) as NSFWPolicyType, | ||
16 | webTorrentEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) === 'true', | ||
17 | autoPlayVideo: peertubeLocalStorage.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true', | ||
18 | videosHistoryEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED) === 'true' | ||
19 | } | ||
20 | } | ||
21 | |||
22 | function flushUserInfoFromLocalStorage () { | ||
23 | peertubeLocalStorage.removeItem(UserLocalStorageKeys.ID) | ||
24 | peertubeLocalStorage.removeItem(UserLocalStorageKeys.USERNAME) | ||
25 | peertubeLocalStorage.removeItem(UserLocalStorageKeys.EMAIL) | ||
26 | peertubeLocalStorage.removeItem(UserLocalStorageKeys.ROLE) | ||
27 | peertubeLocalStorage.removeItem(UserLocalStorageKeys.NSFW_POLICY) | ||
28 | peertubeLocalStorage.removeItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) | ||
29 | peertubeLocalStorage.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) | ||
30 | peertubeLocalStorage.removeItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED) | ||
31 | } | ||
32 | |||
33 | function saveUserInfoIntoLocalStorage (info: { | ||
34 | id: number | ||
35 | username: string | ||
36 | email: string | ||
37 | role: UserRole | ||
38 | nsfwPolicy: NSFWPolicyType | ||
39 | webTorrentEnabled: boolean | ||
40 | autoPlayVideo: boolean | ||
41 | }) { | ||
42 | peertubeLocalStorage.setItem(UserLocalStorageKeys.ID, info.id.toString()) | ||
43 | peertubeLocalStorage.setItem(UserLocalStorageKeys.USERNAME, info.username) | ||
44 | peertubeLocalStorage.setItem(UserLocalStorageKeys.EMAIL, info.email) | ||
45 | peertubeLocalStorage.setItem(UserLocalStorageKeys.ROLE, info.role.toString()) | ||
46 | peertubeLocalStorage.setItem(UserLocalStorageKeys.NSFW_POLICY, info.nsfwPolicy.toString()) | ||
47 | peertubeLocalStorage.setItem(UserLocalStorageKeys.WEBTORRENT_ENABLED, JSON.stringify(info.webTorrentEnabled)) | ||
48 | peertubeLocalStorage.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO, JSON.stringify(info.autoPlayVideo)) | ||
49 | } | ||
50 | |||
51 | export { | ||
52 | getUserInfoFromLocalStorage, | ||
53 | saveUserInfoIntoLocalStorage, | ||
54 | flushUserInfoFromLocalStorage | ||
55 | } | ||
diff --git a/client/src/root-helpers/users/user-tokens.ts b/client/src/root-helpers/users/user-tokens.ts index d42e1c8f3..a6d614cb7 100644 --- a/client/src/root-helpers/users/user-tokens.ts +++ b/client/src/root-helpers/users/user-tokens.ts | |||
@@ -1,46 +1,11 @@ | |||
1 | import { peertubeLocalStorage } from '../peertube-web-storage' | 1 | import { UserTokenLocalStorageKeys } from './user-local-storage-keys' |
2 | |||
3 | export type TokenOptions = { | ||
4 | accessToken: string | ||
5 | refreshToken: string | ||
6 | tokenType: string | ||
7 | } | ||
8 | |||
9 | // Private class only used by User | ||
10 | export class Tokens { | ||
11 | private static KEYS = { | ||
12 | ACCESS_TOKEN: 'access_token', | ||
13 | REFRESH_TOKEN: 'refresh_token', | ||
14 | TOKEN_TYPE: 'token_type' | ||
15 | } | ||
16 | 2 | ||
3 | export class UserTokens { | ||
17 | accessToken: string | 4 | accessToken: string |
18 | refreshToken: string | 5 | refreshToken: string |
19 | tokenType: string | 6 | tokenType: string |
20 | 7 | ||
21 | static load () { | 8 | constructor (hash?: Partial<UserTokens>) { |
22 | const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN) | ||
23 | const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN) | ||
24 | const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE) | ||
25 | |||
26 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | ||
27 | return new Tokens({ | ||
28 | accessToken: accessTokenLocalStorage, | ||
29 | refreshToken: refreshTokenLocalStorage, | ||
30 | tokenType: tokenTypeLocalStorage | ||
31 | }) | ||
32 | } | ||
33 | |||
34 | return null | ||
35 | } | ||
36 | |||
37 | static flush () { | ||
38 | peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN) | ||
39 | peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN) | ||
40 | peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE) | ||
41 | } | ||
42 | |||
43 | constructor (hash?: TokenOptions) { | ||
44 | if (hash) { | 9 | if (hash) { |
45 | this.accessToken = hash.accessToken | 10 | this.accessToken = hash.accessToken |
46 | this.refreshToken = hash.refreshToken | 11 | this.refreshToken = hash.refreshToken |
@@ -53,9 +18,29 @@ export class Tokens { | |||
53 | } | 18 | } |
54 | } | 19 | } |
55 | 20 | ||
56 | save () { | 21 | static getUserTokens (localStorage: Pick<Storage, 'getItem'>) { |
57 | peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) | 22 | const accessTokenLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.ACCESS_TOKEN) |
58 | peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) | 23 | const refreshTokenLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.REFRESH_TOKEN) |
59 | peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) | 24 | const tokenTypeLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.TOKEN_TYPE) |
25 | |||
26 | if (!accessTokenLocalStorage || !refreshTokenLocalStorage || !tokenTypeLocalStorage) return null | ||
27 | |||
28 | return new UserTokens({ | ||
29 | accessToken: accessTokenLocalStorage, | ||
30 | refreshToken: refreshTokenLocalStorage, | ||
31 | tokenType: tokenTypeLocalStorage | ||
32 | }) | ||
33 | } | ||
34 | |||
35 | static saveToLocalStorage (localStorage: Pick<Storage, 'setItem'>, tokens: UserTokens) { | ||
36 | localStorage.setItem(UserTokenLocalStorageKeys.ACCESS_TOKEN, tokens.accessToken) | ||
37 | localStorage.setItem(UserTokenLocalStorageKeys.REFRESH_TOKEN, tokens.refreshToken) | ||
38 | localStorage.setItem(UserTokenLocalStorageKeys.TOKEN_TYPE, tokens.tokenType) | ||
39 | } | ||
40 | |||
41 | static flushLocalStorage (localStorage: Pick<Storage, 'removeItem'>) { | ||
42 | localStorage.removeItem(UserTokenLocalStorageKeys.ACCESS_TOKEN) | ||
43 | localStorage.removeItem(UserTokenLocalStorageKeys.REFRESH_TOKEN) | ||
44 | localStorage.removeItem(UserTokenLocalStorageKeys.TOKEN_TYPE) | ||
60 | } | 45 | } |
61 | } | 46 | } |