register-helpers.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import express from 'express'
  2. import { Server } from 'http'
  3. import {
  4. EncoderOptionsBuilder,
  5. PluginSettingsManager,
  6. PluginStorageManager,
  7. RegisterServerHookOptions,
  8. RegisterServerSettingOptions,
  9. serverHookObject,
  10. SettingsChangeCallback,
  11. VideoPlaylistPrivacyType,
  12. VideoPrivacyType
  13. } from '@peertube/peertube-models'
  14. import { logger } from '@server/helpers/logger.js'
  15. import { onExternalUserAuthenticated } from '@server/lib/auth/external-auth.js'
  16. import { VideoConstantManagerFactory } from '@server/lib/plugins/video-constant-manager-factory.js'
  17. import { PluginModel } from '@server/models/server/plugin.js'
  18. import {
  19. RegisterServerAuthExternalOptions,
  20. RegisterServerAuthExternalResult,
  21. RegisterServerAuthPassOptions,
  22. RegisterServerExternalAuthenticatedResult,
  23. RegisterServerOptions,
  24. RegisterServerWebSocketRouteOptions
  25. } from '@server/types/plugins/index.js'
  26. import { VideoTranscodingProfilesManager } from '../transcoding/default-transcoding-profiles.js'
  27. import { buildPluginHelpers } from './plugin-helpers-builder.js'
  28. export class RegisterHelpers {
  29. private readonly transcodingProfiles: {
  30. [ npmName: string ]: {
  31. type: 'vod' | 'live'
  32. encoder: string
  33. profile: string
  34. }[]
  35. } = {}
  36. private readonly transcodingEncoders: {
  37. [ npmName: string ]: {
  38. type: 'vod' | 'live'
  39. streamType: 'audio' | 'video'
  40. encoder: string
  41. priority: number
  42. }[]
  43. } = {}
  44. private readonly settings: RegisterServerSettingOptions[] = []
  45. private idAndPassAuths: RegisterServerAuthPassOptions[] = []
  46. private externalAuths: RegisterServerAuthExternalOptions[] = []
  47. private readonly onSettingsChangeCallbacks: SettingsChangeCallback[] = []
  48. private readonly webSocketRoutes: RegisterServerWebSocketRouteOptions[] = []
  49. private readonly router: express.Router
  50. private readonly videoConstantManagerFactory: VideoConstantManagerFactory
  51. constructor (
  52. private readonly npmName: string,
  53. private readonly plugin: PluginModel,
  54. private readonly server: Server,
  55. private readonly onHookAdded: (options: RegisterServerHookOptions) => void
  56. ) {
  57. this.router = express.Router()
  58. this.videoConstantManagerFactory = new VideoConstantManagerFactory(this.npmName)
  59. }
  60. buildRegisterHelpers (): RegisterServerOptions {
  61. const registerHook = this.buildRegisterHook()
  62. const registerSetting = this.buildRegisterSetting()
  63. const getRouter = this.buildGetRouter()
  64. const registerWebSocketRoute = this.buildRegisterWebSocketRoute()
  65. const settingsManager = this.buildSettingsManager()
  66. const storageManager = this.buildStorageManager()
  67. const videoLanguageManager = this.videoConstantManagerFactory.createVideoConstantManager<string>('language')
  68. const videoLicenceManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('licence')
  69. const videoCategoryManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('category')
  70. const videoPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPrivacyType>('privacy')
  71. const playlistPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPlaylistPrivacyType>('playlistPrivacy')
  72. const transcodingManager = this.buildTranscodingManager()
  73. const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
  74. const registerExternalAuth = this.buildRegisterExternalAuth()
  75. const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
  76. const unregisterExternalAuth = this.buildUnregisterExternalAuth()
  77. const peertubeHelpers = buildPluginHelpers(this.server, this.plugin, this.npmName)
  78. return {
  79. registerHook,
  80. registerSetting,
  81. getRouter,
  82. registerWebSocketRoute,
  83. settingsManager,
  84. storageManager,
  85. videoLanguageManager: {
  86. ...videoLanguageManager,
  87. /** @deprecated use `addConstant` instead **/
  88. addLanguage: videoLanguageManager.addConstant,
  89. /** @deprecated use `deleteConstant` instead **/
  90. deleteLanguage: videoLanguageManager.deleteConstant
  91. },
  92. videoCategoryManager: {
  93. ...videoCategoryManager,
  94. /** @deprecated use `addConstant` instead **/
  95. addCategory: videoCategoryManager.addConstant,
  96. /** @deprecated use `deleteConstant` instead **/
  97. deleteCategory: videoCategoryManager.deleteConstant
  98. },
  99. videoLicenceManager: {
  100. ...videoLicenceManager,
  101. /** @deprecated use `addConstant` instead **/
  102. addLicence: videoLicenceManager.addConstant,
  103. /** @deprecated use `deleteConstant` instead **/
  104. deleteLicence: videoLicenceManager.deleteConstant
  105. },
  106. videoPrivacyManager: {
  107. ...videoPrivacyManager,
  108. /** @deprecated use `deleteConstant` instead **/
  109. deletePrivacy: videoPrivacyManager.deleteConstant
  110. },
  111. playlistPrivacyManager: {
  112. ...playlistPrivacyManager,
  113. /** @deprecated use `deleteConstant` instead **/
  114. deletePlaylistPrivacy: playlistPrivacyManager.deleteConstant
  115. },
  116. transcodingManager,
  117. registerIdAndPassAuth,
  118. registerExternalAuth,
  119. unregisterIdAndPassAuth,
  120. unregisterExternalAuth,
  121. peertubeHelpers
  122. }
  123. }
  124. reinitVideoConstants (npmName: string) {
  125. this.videoConstantManagerFactory.resetVideoConstants(npmName)
  126. }
  127. reinitTranscodingProfilesAndEncoders (npmName: string) {
  128. const profiles = this.transcodingProfiles[npmName]
  129. if (Array.isArray(profiles)) {
  130. for (const profile of profiles) {
  131. VideoTranscodingProfilesManager.Instance.removeProfile(profile)
  132. }
  133. }
  134. const encoders = this.transcodingEncoders[npmName]
  135. if (Array.isArray(encoders)) {
  136. for (const o of encoders) {
  137. VideoTranscodingProfilesManager.Instance.removeEncoderPriority(o.type, o.streamType, o.encoder, o.priority)
  138. }
  139. }
  140. }
  141. getSettings () {
  142. return this.settings
  143. }
  144. getRouter () {
  145. return this.router
  146. }
  147. getIdAndPassAuths () {
  148. return this.idAndPassAuths
  149. }
  150. getExternalAuths () {
  151. return this.externalAuths
  152. }
  153. getOnSettingsChangedCallbacks () {
  154. return this.onSettingsChangeCallbacks
  155. }
  156. getWebSocketRoutes () {
  157. return this.webSocketRoutes
  158. }
  159. private buildGetRouter () {
  160. return () => this.router
  161. }
  162. private buildRegisterWebSocketRoute () {
  163. return (options: RegisterServerWebSocketRouteOptions) => {
  164. this.webSocketRoutes.push(options)
  165. }
  166. }
  167. private buildRegisterSetting () {
  168. return (options: RegisterServerSettingOptions) => {
  169. this.settings.push(options)
  170. }
  171. }
  172. private buildRegisterHook () {
  173. return (options: RegisterServerHookOptions) => {
  174. if (serverHookObject[options.target] !== true) {
  175. logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
  176. return
  177. }
  178. return this.onHookAdded(options)
  179. }
  180. }
  181. private buildRegisterIdAndPassAuth () {
  182. return (options: RegisterServerAuthPassOptions) => {
  183. if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
  184. logger.error('Cannot register auth plugin %s: authName, getWeight or login are not valid.', this.npmName, { options })
  185. return
  186. }
  187. this.idAndPassAuths.push(options)
  188. }
  189. }
  190. private buildRegisterExternalAuth () {
  191. const self = this
  192. return (options: RegisterServerAuthExternalOptions) => {
  193. if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
  194. logger.error('Cannot register auth plugin %s: authName, authDisplayName or onAuthRequest are not valid.', this.npmName, { options })
  195. return
  196. }
  197. this.externalAuths.push(options)
  198. return {
  199. userAuthenticated (result: RegisterServerExternalAuthenticatedResult): void {
  200. onExternalUserAuthenticated({
  201. npmName: self.npmName,
  202. authName: options.authName,
  203. authResult: result
  204. }).catch(err => {
  205. logger.error('Cannot execute onExternalUserAuthenticated.', { npmName: self.npmName, authName: options.authName, err })
  206. })
  207. }
  208. } as RegisterServerAuthExternalResult
  209. }
  210. }
  211. private buildUnregisterExternalAuth () {
  212. return (authName: string) => {
  213. this.externalAuths = this.externalAuths.filter(a => a.authName !== authName)
  214. }
  215. }
  216. private buildUnregisterIdAndPassAuth () {
  217. return (authName: string) => {
  218. this.idAndPassAuths = this.idAndPassAuths.filter(a => a.authName !== authName)
  219. }
  220. }
  221. private buildSettingsManager (): PluginSettingsManager {
  222. return {
  223. getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name, this.settings),
  224. getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names, this.settings),
  225. setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
  226. onSettingsChange: (cb: SettingsChangeCallback) => this.onSettingsChangeCallbacks.push(cb)
  227. }
  228. }
  229. private buildStorageManager (): PluginStorageManager {
  230. return {
  231. getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
  232. storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
  233. }
  234. }
  235. private buildTranscodingManager () {
  236. const self = this
  237. function addProfile (type: 'live' | 'vod', encoder: string, profile: string, builder: EncoderOptionsBuilder) {
  238. if (profile === 'default') {
  239. logger.error('A plugin cannot add a default live transcoding profile')
  240. return false
  241. }
  242. VideoTranscodingProfilesManager.Instance.addProfile({
  243. type,
  244. encoder,
  245. profile,
  246. builder
  247. })
  248. if (!self.transcodingProfiles[self.npmName]) self.transcodingProfiles[self.npmName] = []
  249. self.transcodingProfiles[self.npmName].push({ type, encoder, profile })
  250. return true
  251. }
  252. function addEncoderPriority (type: 'live' | 'vod', streamType: 'audio' | 'video', encoder: string, priority: number) {
  253. VideoTranscodingProfilesManager.Instance.addEncoderPriority(type, streamType, encoder, priority)
  254. if (!self.transcodingEncoders[self.npmName]) self.transcodingEncoders[self.npmName] = []
  255. self.transcodingEncoders[self.npmName].push({ type, streamType, encoder, priority })
  256. }
  257. return {
  258. addLiveProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
  259. return addProfile('live', encoder, profile, builder)
  260. },
  261. addVODProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
  262. return addProfile('vod', encoder, profile, builder)
  263. },
  264. addLiveEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
  265. return addEncoderPriority('live', streamType, encoder, priority)
  266. },
  267. addVODEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
  268. return addEncoderPriority('vod', streamType, encoder, priority)
  269. },
  270. removeAllProfilesAndEncoderPriorities () {
  271. return self.reinitTranscodingProfilesAndEncoders(self.npmName)
  272. }
  273. }
  274. }
  275. }