config-command.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import { merge } from 'lodash'
  2. import { About, CustomConfig, HttpStatusCode, ServerConfig } from '@shared/models'
  3. import { DeepPartial } from '@shared/typescript-utils'
  4. import { AbstractCommand, OverrideCommandOptions } from '../shared/abstract-command'
  5. export class ConfigCommand extends AbstractCommand {
  6. static getCustomConfigResolutions (enabled: boolean) {
  7. return {
  8. '144p': enabled,
  9. '240p': enabled,
  10. '360p': enabled,
  11. '480p': enabled,
  12. '720p': enabled,
  13. '1080p': enabled,
  14. '1440p': enabled,
  15. '2160p': enabled
  16. }
  17. }
  18. disableImports () {
  19. return this.setImportsEnabled(false)
  20. }
  21. enableImports () {
  22. return this.setImportsEnabled(true)
  23. }
  24. private setImportsEnabled (enabled: boolean) {
  25. return this.updateExistingSubConfig({
  26. newConfig: {
  27. import: {
  28. videos: {
  29. http: {
  30. enabled
  31. },
  32. torrent: {
  33. enabled
  34. }
  35. }
  36. }
  37. }
  38. })
  39. }
  40. private setChannelSyncEnabled (enabled: boolean) {
  41. return this.updateExistingSubConfig({
  42. newConfig: {
  43. import: {
  44. videoChannelSynchronization: {
  45. enabled
  46. }
  47. }
  48. }
  49. })
  50. }
  51. enableChannelSync () {
  52. return this.setChannelSyncEnabled(true)
  53. }
  54. disableChannelSync () {
  55. return this.setChannelSyncEnabled(false)
  56. }
  57. enableLive (options: {
  58. allowReplay?: boolean
  59. transcoding?: boolean
  60. resolutions?: 'min' | 'max' // Default max
  61. } = {}) {
  62. const { allowReplay, transcoding, resolutions = 'max' } = options
  63. return this.updateExistingSubConfig({
  64. newConfig: {
  65. live: {
  66. enabled: true,
  67. allowReplay: allowReplay ?? true,
  68. transcoding: {
  69. enabled: transcoding ?? true,
  70. resolutions: ConfigCommand.getCustomConfigResolutions(resolutions === 'max')
  71. }
  72. }
  73. }
  74. })
  75. }
  76. disableTranscoding () {
  77. return this.updateExistingSubConfig({
  78. newConfig: {
  79. transcoding: {
  80. enabled: false
  81. },
  82. videoStudio: {
  83. enabled: false
  84. }
  85. }
  86. })
  87. }
  88. enableTranscoding (webtorrent = true, hls = true) {
  89. return this.updateExistingSubConfig({
  90. newConfig: {
  91. transcoding: {
  92. enabled: true,
  93. allowAudioFiles: true,
  94. allowAdditionalExtensions: true,
  95. resolutions: ConfigCommand.getCustomConfigResolutions(true),
  96. webtorrent: {
  97. enabled: webtorrent
  98. },
  99. hls: {
  100. enabled: hls
  101. }
  102. }
  103. }
  104. })
  105. }
  106. enableMinimumTranscoding (webtorrent = true, hls = true) {
  107. return this.updateExistingSubConfig({
  108. newConfig: {
  109. transcoding: {
  110. enabled: true,
  111. resolutions: {
  112. ...ConfigCommand.getCustomConfigResolutions(false),
  113. '240p': true
  114. },
  115. webtorrent: {
  116. enabled: webtorrent
  117. },
  118. hls: {
  119. enabled: hls
  120. }
  121. }
  122. }
  123. })
  124. }
  125. enableStudio () {
  126. return this.updateExistingSubConfig({
  127. newConfig: {
  128. videoStudio: {
  129. enabled: true
  130. }
  131. }
  132. })
  133. }
  134. getConfig (options: OverrideCommandOptions = {}) {
  135. const path = '/api/v1/config'
  136. return this.getRequestBody<ServerConfig>({
  137. ...options,
  138. path,
  139. implicitToken: false,
  140. defaultExpectedStatus: HttpStatusCode.OK_200
  141. })
  142. }
  143. async getIndexHTMLConfig (options: OverrideCommandOptions = {}) {
  144. const text = await this.getRequestText({
  145. ...options,
  146. path: '/',
  147. implicitToken: false,
  148. defaultExpectedStatus: HttpStatusCode.OK_200
  149. })
  150. const match = text.match('<script type="application/javascript">window.PeerTubeServerConfig = (".+?")</script>')
  151. // We parse the string twice, first to extract the string and then to extract the JSON
  152. return JSON.parse(JSON.parse(match[1])) as ServerConfig
  153. }
  154. getAbout (options: OverrideCommandOptions = {}) {
  155. const path = '/api/v1/config/about'
  156. return this.getRequestBody<About>({
  157. ...options,
  158. path,
  159. implicitToken: false,
  160. defaultExpectedStatus: HttpStatusCode.OK_200
  161. })
  162. }
  163. getCustomConfig (options: OverrideCommandOptions = {}) {
  164. const path = '/api/v1/config/custom'
  165. return this.getRequestBody<CustomConfig>({
  166. ...options,
  167. path,
  168. implicitToken: true,
  169. defaultExpectedStatus: HttpStatusCode.OK_200
  170. })
  171. }
  172. updateCustomConfig (options: OverrideCommandOptions & {
  173. newCustomConfig: CustomConfig
  174. }) {
  175. const path = '/api/v1/config/custom'
  176. return this.putBodyRequest({
  177. ...options,
  178. path,
  179. fields: options.newCustomConfig,
  180. implicitToken: true,
  181. defaultExpectedStatus: HttpStatusCode.OK_200
  182. })
  183. }
  184. deleteCustomConfig (options: OverrideCommandOptions = {}) {
  185. const path = '/api/v1/config/custom'
  186. return this.deleteRequest({
  187. ...options,
  188. path,
  189. implicitToken: true,
  190. defaultExpectedStatus: HttpStatusCode.OK_200
  191. })
  192. }
  193. async updateExistingSubConfig (options: OverrideCommandOptions & {
  194. newConfig: DeepPartial<CustomConfig>
  195. }) {
  196. const existing = await this.getCustomConfig({ ...options, expectedStatus: HttpStatusCode.OK_200 })
  197. return this.updateCustomConfig({ ...options, newCustomConfig: merge({}, existing, options.newConfig) })
  198. }
  199. updateCustomSubConfig (options: OverrideCommandOptions & {
  200. newConfig: DeepPartial<CustomConfig>
  201. }) {
  202. const newCustomConfig: CustomConfig = {
  203. instance: {
  204. name: 'PeerTube updated',
  205. shortDescription: 'my short description',
  206. description: 'my super description',
  207. terms: 'my super terms',
  208. codeOfConduct: 'my super coc',
  209. creationReason: 'my super creation reason',
  210. moderationInformation: 'my super moderation information',
  211. administrator: 'Kuja',
  212. maintenanceLifetime: 'forever',
  213. businessModel: 'my super business model',
  214. hardwareInformation: '2vCore 3GB RAM',
  215. languages: [ 'en', 'es' ],
  216. categories: [ 1, 2 ],
  217. isNSFW: true,
  218. defaultNSFWPolicy: 'blur',
  219. defaultClientRoute: '/videos/recently-added',
  220. customizations: {
  221. javascript: 'alert("coucou")',
  222. css: 'body { background-color: red; }'
  223. }
  224. },
  225. theme: {
  226. default: 'default'
  227. },
  228. services: {
  229. twitter: {
  230. username: '@MySuperUsername',
  231. whitelisted: true
  232. }
  233. },
  234. client: {
  235. videos: {
  236. miniature: {
  237. preferAuthorDisplayName: false
  238. }
  239. },
  240. menu: {
  241. login: {
  242. redirectOnSingleExternalAuth: false
  243. }
  244. }
  245. },
  246. cache: {
  247. previews: {
  248. size: 2
  249. },
  250. captions: {
  251. size: 3
  252. },
  253. torrents: {
  254. size: 4
  255. }
  256. },
  257. signup: {
  258. enabled: false,
  259. limit: 5,
  260. requiresEmailVerification: false,
  261. minimumAge: 16
  262. },
  263. admin: {
  264. email: 'superadmin1@example.com'
  265. },
  266. contactForm: {
  267. enabled: true
  268. },
  269. user: {
  270. videoQuota: 5242881,
  271. videoQuotaDaily: 318742
  272. },
  273. videoChannels: {
  274. maxPerUser: 20
  275. },
  276. transcoding: {
  277. enabled: true,
  278. allowAdditionalExtensions: true,
  279. allowAudioFiles: true,
  280. threads: 1,
  281. concurrency: 3,
  282. profile: 'default',
  283. resolutions: {
  284. '0p': false,
  285. '144p': false,
  286. '240p': false,
  287. '360p': true,
  288. '480p': true,
  289. '720p': false,
  290. '1080p': false,
  291. '1440p': false,
  292. '2160p': false
  293. },
  294. alwaysTranscodeOriginalResolution: true,
  295. webtorrent: {
  296. enabled: true
  297. },
  298. hls: {
  299. enabled: false
  300. }
  301. },
  302. live: {
  303. enabled: true,
  304. allowReplay: false,
  305. latencySetting: {
  306. enabled: false
  307. },
  308. maxDuration: -1,
  309. maxInstanceLives: -1,
  310. maxUserLives: 50,
  311. transcoding: {
  312. enabled: true,
  313. threads: 4,
  314. profile: 'default',
  315. resolutions: {
  316. '144p': true,
  317. '240p': true,
  318. '360p': true,
  319. '480p': true,
  320. '720p': true,
  321. '1080p': true,
  322. '1440p': true,
  323. '2160p': true
  324. },
  325. alwaysTranscodeOriginalResolution: true
  326. }
  327. },
  328. videoStudio: {
  329. enabled: false
  330. },
  331. import: {
  332. videos: {
  333. concurrency: 3,
  334. http: {
  335. enabled: false
  336. },
  337. torrent: {
  338. enabled: false
  339. }
  340. },
  341. videoChannelSynchronization: {
  342. enabled: false,
  343. maxPerUser: 10
  344. }
  345. },
  346. trending: {
  347. videos: {
  348. algorithms: {
  349. enabled: [ 'hot', 'most-viewed', 'most-liked' ],
  350. default: 'hot'
  351. }
  352. }
  353. },
  354. autoBlacklist: {
  355. videos: {
  356. ofUsers: {
  357. enabled: false
  358. }
  359. }
  360. },
  361. followers: {
  362. instance: {
  363. enabled: true,
  364. manualApproval: false
  365. }
  366. },
  367. followings: {
  368. instance: {
  369. autoFollowBack: {
  370. enabled: false
  371. },
  372. autoFollowIndex: {
  373. indexUrl: 'https://instances.joinpeertube.org/api/v1/instances/hosts',
  374. enabled: false
  375. }
  376. }
  377. },
  378. broadcastMessage: {
  379. enabled: true,
  380. level: 'warning',
  381. message: 'hello',
  382. dismissable: true
  383. },
  384. search: {
  385. remoteUri: {
  386. users: true,
  387. anonymous: true
  388. },
  389. searchIndex: {
  390. enabled: true,
  391. url: 'https://search.joinpeertube.org',
  392. disableLocalSearch: true,
  393. isDefaultSearch: true
  394. }
  395. }
  396. }
  397. merge(newCustomConfig, options.newConfig)
  398. return this.updateCustomConfig({ ...options, newCustomConfig })
  399. }
  400. }