1 import { logger } from '@server/helpers/logger'
2 import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PLAYLIST_PRIVACIES, VIDEO_PRIVACIES } from '@server/initializers/constants'
3 import { onExternalUserAuthenticated } from '@server/lib/auth'
4 import { PluginModel } from '@server/models/server/plugin'
5 import { RegisterServerOptions } from '@server/typings/plugins'
6 import { PluginPlaylistPrivacyManager } from '@shared/models/plugins/plugin-playlist-privacy-manager.model'
7 import { PluginSettingsManager } from '@shared/models/plugins/plugin-settings-manager.model'
8 import { PluginStorageManager } from '@shared/models/plugins/plugin-storage-manager.model'
9 import { PluginVideoCategoryManager } from '@shared/models/plugins/plugin-video-category-manager.model'
10 import { PluginVideoLanguageManager } from '@shared/models/plugins/plugin-video-language-manager.model'
11 import { PluginVideoLicenceManager } from '@shared/models/plugins/plugin-video-licence-manager.model'
12 import { PluginVideoPrivacyManager } from '@shared/models/plugins/plugin-video-privacy-manager.model'
13 import { RegisterServerAuthExternalOptions, RegisterServerAuthExternalResult, RegisterServerAuthPassOptions, RegisterServerExternalAuthenticatedResult } from '@shared/models/plugins/register-server-auth.model'
14 import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model'
15 import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
16 import { serverHookObject } from '@shared/models/plugins/server-hook.model'
17 import * as express from 'express'
18 import { buildPluginHelpers } from './plugin-helpers'
20 type AlterableVideoConstant = 'language' | 'licence' | 'category' | 'privacy' | 'playlistPrivacy'
21 type VideoConstant = { [key in number | string]: string }
23 type UpdatedVideoConstant = {
24 [name in AlterableVideoConstant]: {
25 added: { key: number | string, label: string }[]
26 deleted: { key: number | string, label: string }[]
30 export class RegisterHelpersStore {
31 private readonly updatedVideoConstants: UpdatedVideoConstant = {
32 playlistPrivacy: { added: [], deleted: [] },
33 privacy: { added: [], deleted: [] },
34 language: { added: [], deleted: [] },
35 licence: { added: [], deleted: [] },
36 category: { added: [], deleted: [] }
39 private readonly settings: RegisterServerSettingOptions[] = []
41 private readonly idAndPassAuths: RegisterServerAuthPassOptions[] = []
42 private readonly externalAuths: RegisterServerAuthExternalOptions[] = []
44 private readonly router: express.Router
47 private readonly npmName: string,
48 private readonly plugin: PluginModel,
49 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
51 this.router = express.Router()
54 buildRegisterHelpers (): RegisterServerOptions {
55 const registerHook = this.buildRegisterHook()
56 const registerSetting = this.buildRegisterSetting()
58 const getRouter = this.buildGetRouter()
60 const settingsManager = this.buildSettingsManager()
61 const storageManager = this.buildStorageManager()
63 const videoLanguageManager = this.buildVideoLanguageManager()
65 const videoLicenceManager = this.buildVideoLicenceManager()
66 const videoCategoryManager = this.buildVideoCategoryManager()
68 const videoPrivacyManager = this.buildVideoPrivacyManager()
69 const playlistPrivacyManager = this.buildPlaylistPrivacyManager()
71 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
72 const registerExternalAuth = this.buildRegisterExternalAuth()
74 const peertubeHelpers = buildPluginHelpers(this.npmName)
90 playlistPrivacyManager,
92 registerIdAndPassAuth,
99 reinitVideoConstants (npmName: string) {
101 language: VIDEO_LANGUAGES,
102 licence: VIDEO_LICENCES,
103 category: VIDEO_CATEGORIES,
104 privacy: VIDEO_PRIVACIES,
105 playlistPrivacy: VIDEO_PLAYLIST_PRIVACIES
107 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category', 'privacy', 'playlistPrivacy' ]
109 for (const type of types) {
110 const updatedConstants = this.updatedVideoConstants[type][npmName]
111 if (!updatedConstants) continue
113 for (const added of updatedConstants.added) {
114 delete hash[type][added.key]
117 for (const deleted of updatedConstants.deleted) {
118 hash[type][deleted.key] = deleted.label
121 delete this.updatedVideoConstants[type][npmName]
133 getIdAndPassAuths () {
134 return this.idAndPassAuths
137 getExternalAuths () {
138 return this.externalAuths
141 private buildGetRouter () {
142 return () => this.router
145 private buildRegisterSetting () {
146 return (options: RegisterServerSettingOptions) => {
147 this.settings.push(options)
151 private buildRegisterHook () {
152 return (options: RegisterServerHookOptions) => {
153 if (serverHookObject[options.target] !== true) {
154 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
158 return this.onHookAdded(options)
162 private buildRegisterIdAndPassAuth () {
163 return (options: RegisterServerAuthPassOptions) => {
164 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
165 logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName)
169 this.idAndPassAuths.push(options)
173 private buildRegisterExternalAuth () {
176 return (options: RegisterServerAuthExternalOptions) => {
177 this.externalAuths.push(options)
180 userAuthenticated (result: RegisterServerExternalAuthenticatedResult): void {
181 onExternalUserAuthenticated({
182 npmName: self.npmName,
183 authName: options.authName,
186 logger.error('Cannot execute onExternalUserAuthenticated.', { npmName: self.npmName, authName: options.authName, err })
189 } as RegisterServerAuthExternalResult
193 private buildSettingsManager (): PluginSettingsManager {
195 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name),
197 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names),
199 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value)
203 private buildStorageManager (): PluginStorageManager {
205 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
207 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
211 private buildVideoLanguageManager (): PluginVideoLanguageManager {
213 addLanguage: (key: string, label: string) => {
214 return this.addConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label })
217 deleteLanguage: (key: string) => {
218 return this.deleteConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
223 private buildVideoCategoryManager (): PluginVideoCategoryManager {
225 addCategory: (key: number, label: string) => {
226 return this.addConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label })
229 deleteCategory: (key: number) => {
230 return this.deleteConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
235 private buildVideoPrivacyManager (): PluginVideoPrivacyManager {
237 deletePrivacy: (key: number) => {
238 return this.deleteConstant({ npmName: this.npmName, type: 'privacy', obj: VIDEO_PRIVACIES, key })
243 private buildPlaylistPrivacyManager (): PluginPlaylistPrivacyManager {
245 deletePlaylistPrivacy: (key: number) => {
246 return this.deleteConstant({ npmName: this.npmName, type: 'playlistPrivacy', obj: VIDEO_PLAYLIST_PRIVACIES, key })
251 private buildVideoLicenceManager (): PluginVideoLicenceManager {
253 addLicence: (key: number, label: string) => {
254 return this.addConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key, label })
257 deleteLicence: (key: number) => {
258 return this.deleteConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key })
263 private addConstant<T extends string | number> (parameters: {
265 type: AlterableVideoConstant
270 const { npmName, type, obj, key, label } = parameters
273 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
277 if (!this.updatedVideoConstants[type][npmName]) {
278 this.updatedVideoConstants[type][npmName] = {
284 this.updatedVideoConstants[type][npmName].added.push({ key, label })
290 private deleteConstant<T extends string | number> (parameters: {
292 type: AlterableVideoConstant
296 const { npmName, type, obj, key } = parameters
299 logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key)
303 if (!this.updatedVideoConstants[type][npmName]) {
304 this.updatedVideoConstants[type][npmName] = {
310 this.updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] })