checker-after-init.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import * as config from 'config'
  2. import { isProdInstance, isTestInstance } from '../helpers/core-utils'
  3. import { UserModel } from '../models/account/user'
  4. import { ApplicationModel } from '../models/application/application'
  5. import { OAuthClientModel } from '../models/oauth/oauth-client'
  6. import { parse } from 'url'
  7. import { CONFIG } from './config'
  8. import { logger } from '../helpers/logger'
  9. import { getServerActor } from '../helpers/utils'
  10. import { RecentlyAddedStrategy } from '../../shared/models/redundancy'
  11. import { isArray } from '../helpers/custom-validators/misc'
  12. import { uniq } from 'lodash'
  13. import { Emailer } from '../lib/emailer'
  14. import { WEBSERVER } from './constants'
  15. async function checkActivityPubUrls () {
  16. const actor = await getServerActor()
  17. const parsed = parse(actor.url)
  18. if (WEBSERVER.HOST !== parsed.host) {
  19. const NODE_ENV = config.util.getEnv('NODE_ENV')
  20. const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR')
  21. logger.warn(
  22. 'It seems PeerTube was started (and created some data) with another domain name. ' +
  23. 'This means you will not be able to federate! ' +
  24. 'Please use %s %s npm run update-host to fix this.',
  25. NODE_CONFIG_DIR ? `NODE_CONFIG_DIR=${NODE_CONFIG_DIR}` : '',
  26. NODE_ENV ? `NODE_ENV=${NODE_ENV}` : ''
  27. )
  28. }
  29. }
  30. // Some checks on configuration files
  31. // Return an error message, or null if everything is okay
  32. function checkConfig () {
  33. // Moved configuration keys
  34. if (config.has('services.csp-logger')) {
  35. logger.warn('services.csp-logger configuration has been renamed to csp.report_uri. Please update your configuration file.')
  36. }
  37. // Email verification
  38. if (!Emailer.isEnabled()) {
  39. if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
  40. return 'Emailer is disabled but you require signup email verification.'
  41. }
  42. if (CONFIG.CONTACT_FORM.ENABLED) {
  43. logger.warn('Emailer is disabled so the contact form will not work.')
  44. }
  45. }
  46. // NSFW policy
  47. const defaultNSFWPolicy = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
  48. {
  49. const available = [ 'do_not_list', 'blur', 'display' ]
  50. if (available.indexOf(defaultNSFWPolicy) === -1) {
  51. return 'NSFW policy setting should be ' + available.join(' or ') + ' instead of ' + defaultNSFWPolicy
  52. }
  53. }
  54. // Redundancies
  55. const redundancyVideos = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
  56. if (isArray(redundancyVideos)) {
  57. const available = [ 'most-views', 'trending', 'recently-added' ]
  58. for (const r of redundancyVideos) {
  59. if (available.indexOf(r.strategy) === -1) {
  60. return 'Videos redundancy should have ' + available.join(' or ') + ' strategy instead of ' + r.strategy
  61. }
  62. // Lifetime should not be < 10 hours
  63. if (!isTestInstance() && r.minLifetime < 1000 * 3600 * 10) {
  64. return 'Video redundancy minimum lifetime should be >= 10 hours for strategy ' + r.strategy
  65. }
  66. }
  67. const filtered = uniq(redundancyVideos.map(r => r.strategy))
  68. if (filtered.length !== redundancyVideos.length) {
  69. return 'Redundancy video entries should have unique strategies'
  70. }
  71. const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy
  72. if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) {
  73. return 'Min views in recently added strategy is not a number'
  74. }
  75. } else {
  76. return 'Videos redundancy should be an array (you must uncomment lines containing - too)'
  77. }
  78. // Check storage directory locations
  79. if (isProdInstance()) {
  80. const configStorage = config.get('storage')
  81. for (const key of Object.keys(configStorage)) {
  82. if (configStorage[key].startsWith('storage/')) {
  83. logger.warn(
  84. 'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.',
  85. key
  86. )
  87. }
  88. }
  89. }
  90. return null
  91. }
  92. // We get db by param to not import it in this file (import orders)
  93. async function clientsExist () {
  94. const totalClients = await OAuthClientModel.countTotal()
  95. return totalClients !== 0
  96. }
  97. // We get db by param to not import it in this file (import orders)
  98. async function usersExist () {
  99. const totalUsers = await UserModel.countTotal()
  100. return totalUsers !== 0
  101. }
  102. // We get db by param to not import it in this file (import orders)
  103. async function applicationExist () {
  104. const totalApplication = await ApplicationModel.countTotal()
  105. return totalApplication !== 0
  106. }
  107. // ---------------------------------------------------------------------------
  108. export {
  109. checkConfig,
  110. clientsExist,
  111. usersExist,
  112. applicationExist,
  113. checkActivityPubUrls
  114. }