]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/checker-after-init.ts
Merge branch 'develop' into pr/1217
[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'
6import { parse } from 'url'
7import { CONFIG } from './constants'
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'
e5565833
C
14
15async function checkActivityPubUrls () {
16 const actor = await getServerActor()
17
18 const parsed = parse(actor.url)
19 if (CONFIG.WEBSERVER.HOST !== parsed.host) {
20 const NODE_ENV = config.util.getEnv('NODE_ENV')
21 const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR')
22
23 logger.warn(
24 'It seems PeerTube was started (and created some data) with another domain name. ' +
25 'This means you will not be able to federate! ' +
26 'Please use %s %s npm run update-host to fix this.',
27 NODE_CONFIG_DIR ? `NODE_CONFIG_DIR=${NODE_CONFIG_DIR}` : '',
28 NODE_ENV ? `NODE_ENV=${NODE_ENV}` : ''
29 )
30 }
31}
32
33// Some checks on configuration files
34// Return an error message, or null if everything is okay
35function checkConfig () {
d3e56c0c
C
36
37 if (!Emailer.isEnabled()) {
38 if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
39 return 'Emailer is disabled but you require signup email verification.'
40 }
41
42 if (CONFIG.CONTACT_FORM.ENABLED) {
43 logger.warn('Emailer is disabled so the contact form will not work.')
44 }
45 }
e5565833
C
46
47 // NSFW policy
d3e56c0c 48 const defaultNSFWPolicy = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
e5565833
C
49 {
50 const available = [ 'do_not_list', 'blur', 'display' ]
51 if (available.indexOf(defaultNSFWPolicy) === -1) {
52 return 'NSFW policy setting should be ' + available.join(' or ') + ' instead of ' + defaultNSFWPolicy
53 }
54 }
55
56 // Redundancies
57 const redundancyVideos = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
58 if (isArray(redundancyVideos)) {
59 const available = [ 'most-views', 'trending', 'recently-added' ]
60 for (const r of redundancyVideos) {
61 if (available.indexOf(r.strategy) === -1) {
62 return 'Videos redundancy should have ' + available.join(' or ') + ' strategy instead of ' + r.strategy
63 }
64
65 // Lifetime should not be < 10 hours
66 if (!isTestInstance() && r.minLifetime < 1000 * 3600 * 10) {
67 return 'Video redundancy minimum lifetime should be >= 10 hours for strategy ' + r.strategy
68 }
69 }
70
71 const filtered = uniq(redundancyVideos.map(r => r.strategy))
72 if (filtered.length !== redundancyVideos.length) {
73 return 'Redundancy video entries should have unique strategies'
74 }
75
76 const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy
77 if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) {
78 return 'Min views in recently added strategy is not a number'
79 }
80 }
81
d3e56c0c 82 // Check storage directory locations
e5565833
C
83 if (isProdInstance()) {
84 const configStorage = config.get('storage')
85 for (const key of Object.keys(configStorage)) {
86 if (configStorage[key].startsWith('storage/')) {
87 logger.warn(
88 'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.',
89 key
90 )
91 }
92 }
93 }
94
95 return null
96}
97
98// We get db by param to not import it in this file (import orders)
99async function clientsExist () {
100 const totalClients = await OAuthClientModel.countTotal()
101
102 return totalClients !== 0
103}
104
105// We get db by param to not import it in this file (import orders)
106async function usersExist () {
107 const totalUsers = await UserModel.countTotal()
108
109 return totalUsers !== 0
110}
111
112// We get db by param to not import it in this file (import orders)
113async function applicationExist () {
114 const totalApplication = await ApplicationModel.countTotal()
115
116 return totalApplication !== 0
117}
118
119// ---------------------------------------------------------------------------
120
121export {
122 checkConfig,
123 clientsExist,
124 usersExist,
125 applicationExist,
126 checkActivityPubUrls
127}