ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

345 lines
11 KiB

  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 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 = [
  170. ...this.settings.filter(s => !s.name || s.name !== options.name),
  171. options
  172. ]
  173. }
  174. }
  175. private buildRegisterHook () {
  176. return (options: RegisterServerHookOptions) => {
  177. if (serverHookObject[options.target] !== true) {
  178. logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
  179. return
  180. }
  181. return this.onHookAdded(options)
  182. }
  183. }
  184. private buildRegisterIdAndPassAuth () {
  185. return (options: RegisterServerAuthPassOptions) => {
  186. if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
  187. logger.error('Cannot register auth plugin %s: authName, getWeight or login are not valid.', this.npmName, { options })
  188. return
  189. }
  190. this.idAndPassAuths.push(options)
  191. }
  192. }
  193. private buildRegisterExternalAuth () {
  194. const self = this
  195. return (options: RegisterServerAuthExternalOptions) => {
  196. if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
  197. logger.error('Cannot register auth plugin %s: authName, authDisplayName or onAuthRequest are not valid.', this.npmName, { options })
  198. return
  199. }
  200. this.externalAuths.push(options)
  201. return {
  202. userAuthenticated (result: RegisterServerExternalAuthenticatedResult): void {
  203. onExternalUserAuthenticated({
  204. npmName: self.npmName,
  205. authName: options.authName,
  206. authResult: result
  207. }).catch(err => {
  208. logger.error('Cannot execute onExternalUserAuthenticated.', { npmName: self.npmName, authName: options.authName, err })
  209. })
  210. }
  211. } as RegisterServerAuthExternalResult
  212. }
  213. }
  214. private buildUnregisterExternalAuth () {
  215. return (authName: string) => {
  216. this.externalAuths = this.externalAuths.filter(a => a.authName !== authName)
  217. }
  218. }
  219. private buildUnregisterIdAndPassAuth () {
  220. return (authName: string) => {
  221. this.idAndPassAuths = this.idAndPassAuths.filter(a => a.authName !== authName)
  222. }
  223. }
  224. private buildSettingsManager (): PluginSettingsManager {
  225. return {
  226. getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name, this.settings),
  227. getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names, this.settings),
  228. setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
  229. onSettingsChange: (cb: SettingsChangeCallback) => this.onSettingsChangeCallbacks.push(cb)
  230. }
  231. }
  232. private buildStorageManager (): PluginStorageManager {
  233. return {
  234. getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
  235. storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
  236. }
  237. }
  238. private buildTranscodingManager () {
  239. const self = this
  240. function addProfile (type: 'live' | 'vod', encoder: string, profile: string, builder: EncoderOptionsBuilder) {
  241. if (profile === 'default') {
  242. logger.error('A plugin cannot add a default live transcoding profile')
  243. return false
  244. }
  245. VideoTranscodingProfilesManager.Instance.addProfile({
  246. type,
  247. encoder,
  248. profile,
  249. builder
  250. })
  251. if (!self.transcodingProfiles[self.npmName]) self.transcodingProfiles[self.npmName] = []
  252. self.transcodingProfiles[self.npmName].push({ type, encoder, profile })
  253. return true
  254. }
  255. function addEncoderPriority (type: 'live' | 'vod', streamType: 'audio' | 'video', encoder: string, priority: number) {
  256. VideoTranscodingProfilesManager.Instance.addEncoderPriority(type, streamType, encoder, priority)
  257. if (!self.transcodingEncoders[self.npmName]) self.transcodingEncoders[self.npmName] = []
  258. self.transcodingEncoders[self.npmName].push({ type, streamType, encoder, priority })
  259. }
  260. return {
  261. addLiveProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
  262. return addProfile('live', encoder, profile, builder)
  263. },
  264. addVODProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
  265. return addProfile('vod', encoder, profile, builder)
  266. },
  267. addLiveEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
  268. return addEncoderPriority('live', streamType, encoder, priority)
  269. },
  270. addVODEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
  271. return addEncoderPriority('vod', streamType, encoder, priority)
  272. },
  273. removeAllProfilesAndEncoderPriorities () {
  274. return self.reinitTranscodingProfilesAndEncoders(self.npmName)
  275. }
  276. }
  277. }
  278. }