]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/checker-after-init.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / initializers / checker-after-init.ts
CommitLineData
e5565833 1import * as config from 'config'
d1105b97 2import { isProdInstance, isTestInstance } from '../helpers/core-utils'
e5565833
C
3import { UserModel } from '../models/account/user'
4import { ApplicationModel } from '../models/application/application'
5import { OAuthClientModel } from '../models/oauth/oauth-client'
a1587156 6import { URL } from 'url'
6dd9de95 7import { CONFIG } from './config'
e5565833
C
8import { logger } from '../helpers/logger'
9import { getServerActor } from '../helpers/utils'
d1105b97 10import { RecentlyAddedStrategy } from '../../shared/models/redundancy'
e5565833
C
11import { isArray } from '../helpers/custom-validators/misc'
12import { uniq } from 'lodash'
d3e56c0c 13import { Emailer } from '../lib/emailer'
6dd9de95 14import { WEBSERVER } from './constants'
e5565833
C
15
16async function checkActivityPubUrls () {
17 const actor = await getServerActor()
18
a1587156 19 const parsed = new URL(actor.url)
6dd9de95 20 if (WEBSERVER.HOST !== parsed.host) {
e5565833
C
21 const NODE_ENV = config.util.getEnv('NODE_ENV')
22 const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR')
23
24 logger.warn(
25 'It seems PeerTube was started (and created some data) with another domain name. ' +
26 'This means you will not be able to federate! ' +
27 'Please use %s %s npm run update-host to fix this.',
28 NODE_CONFIG_DIR ? `NODE_CONFIG_DIR=${NODE_CONFIG_DIR}` : '',
29 NODE_ENV ? `NODE_ENV=${NODE_ENV}` : ''
30 )
31 }
32}
33
34// Some checks on configuration files
35// Return an error message, or null if everything is okay
36function checkConfig () {
d3e56c0c 37
539d3f4f
C
38 // Moved configuration keys
39 if (config.has('services.csp-logger')) {
40 logger.warn('services.csp-logger configuration has been renamed to csp.report_uri. Please update your configuration file.')
41 }
42
43 // Email verification
d3e56c0c
C
44 if (!Emailer.isEnabled()) {
45 if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
46 return 'Emailer is disabled but you require signup email verification.'
47 }
48
49 if (CONFIG.CONTACT_FORM.ENABLED) {
50 logger.warn('Emailer is disabled so the contact form will not work.')
51 }
52 }
e5565833
C
53
54 // NSFW policy
d3e56c0c 55 const defaultNSFWPolicy = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
e5565833
C
56 {
57 const available = [ 'do_not_list', 'blur', 'display' ]
58 if (available.indexOf(defaultNSFWPolicy) === -1) {
59 return 'NSFW policy setting should be ' + available.join(' or ') + ' instead of ' + defaultNSFWPolicy
60 }
61 }
62
63 // Redundancies
64 const redundancyVideos = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
65 if (isArray(redundancyVideos)) {
66 const available = [ 'most-views', 'trending', 'recently-added' ]
67 for (const r of redundancyVideos) {
68 if (available.indexOf(r.strategy) === -1) {
69 return 'Videos redundancy should have ' + available.join(' or ') + ' strategy instead of ' + r.strategy
70 }
71
72 // Lifetime should not be < 10 hours
73 if (!isTestInstance() && r.minLifetime < 1000 * 3600 * 10) {
74 return 'Video redundancy minimum lifetime should be >= 10 hours for strategy ' + r.strategy
75 }
76 }
77
78 const filtered = uniq(redundancyVideos.map(r => r.strategy))
79 if (filtered.length !== redundancyVideos.length) {
80 return 'Redundancy video entries should have unique strategies'
81 }
82
83 const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy
84 if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) {
85 return 'Min views in recently added strategy is not a number'
86 }
d85798c4
C
87 } else {
88 return 'Videos redundancy should be an array (you must uncomment lines containing - too)'
e5565833
C
89 }
90
d3e56c0c 91 // Check storage directory locations
e5565833
C
92 if (isProdInstance()) {
93 const configStorage = config.get('storage')
94 for (const key of Object.keys(configStorage)) {
95 if (configStorage[key].startsWith('storage/')) {
96 logger.warn(
97 'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.',
98 key
99 )
100 }
101 }
102 }
103
d7a25329
C
104 // Transcoding
105 if (CONFIG.TRANSCODING.ENABLED) {
106 if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false && CONFIG.TRANSCODING.HLS.ENABLED === false) {
107 return 'You need to enable at least WebTorrent transcoding or HLS transcoding.'
108 }
109 }
110
2034c3aa
C
111 if (CONFIG.STORAGE.VIDEOS_DIR === CONFIG.STORAGE.REDUNDANCY_DIR) {
112 logger.warn('Redundancy directory should be different than the videos folder.')
113 }
114
e5565833
C
115 return null
116}
117
118// We get db by param to not import it in this file (import orders)
119async function clientsExist () {
120 const totalClients = await OAuthClientModel.countTotal()
121
122 return totalClients !== 0
123}
124
125// We get db by param to not import it in this file (import orders)
126async function usersExist () {
127 const totalUsers = await UserModel.countTotal()
128
129 return totalUsers !== 0
130}
131
132// We get db by param to not import it in this file (import orders)
133async function applicationExist () {
134 const totalApplication = await ApplicationModel.countTotal()
135
136 return totalApplication !== 0
137}
138
139// ---------------------------------------------------------------------------
140
141export {
142 checkConfig,
143 clientsExist,
144 usersExist,
145 applicationExist,
146 checkActivityPubUrls
147}