53124f9ec7fd71f5f94a1609f6d83e218d02a9e5
[oweals/peertube.git] / server / initializers / checker-after-init.ts
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 './constants'
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
15 async 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
35 function checkConfig () {
36
37   // Moved configuration keys
38   if (config.has('services.csp-logger')) {
39     logger.warn('services.csp-logger configuration has been renamed to csp.report_uri. Please update your configuration file.')
40   }
41
42   // Email verification
43   if (!Emailer.isEnabled()) {
44     if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
45       return 'Emailer is disabled but you require signup email verification.'
46     }
47
48     if (CONFIG.CONTACT_FORM.ENABLED) {
49       logger.warn('Emailer is disabled so the contact form will not work.')
50     }
51   }
52
53   // NSFW policy
54   const defaultNSFWPolicy = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
55   {
56     const available = [ 'do_not_list', 'blur', 'display' ]
57     if (available.indexOf(defaultNSFWPolicy) === -1) {
58       return 'NSFW policy setting should be ' + available.join(' or ') + ' instead of ' + defaultNSFWPolicy
59     }
60   }
61
62   // Redundancies
63   const redundancyVideos = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
64   if (isArray(redundancyVideos)) {
65     const available = [ 'most-views', 'trending', 'recently-added' ]
66     for (const r of redundancyVideos) {
67       if (available.indexOf(r.strategy) === -1) {
68         return 'Videos redundancy should have ' + available.join(' or ') + ' strategy instead of ' + r.strategy
69       }
70
71       // Lifetime should not be < 10 hours
72       if (!isTestInstance() && r.minLifetime < 1000 * 3600 * 10) {
73         return 'Video redundancy minimum lifetime should be >= 10 hours for strategy ' + r.strategy
74       }
75     }
76
77     const filtered = uniq(redundancyVideos.map(r => r.strategy))
78     if (filtered.length !== redundancyVideos.length) {
79       return 'Redundancy video entries should have unique strategies'
80     }
81
82     const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy
83     if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) {
84       return 'Min views in recently added strategy is not a number'
85     }
86   }
87
88   // Check storage directory locations
89   if (isProdInstance()) {
90     const configStorage = config.get('storage')
91     for (const key of Object.keys(configStorage)) {
92       if (configStorage[key].startsWith('storage/')) {
93         logger.warn(
94           'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.',
95           key
96         )
97       }
98     }
99   }
100
101   return null
102 }
103
104 // We get db by param to not import it in this file (import orders)
105 async function clientsExist () {
106   const totalClients = await OAuthClientModel.countTotal()
107
108   return totalClients !== 0
109 }
110
111 // We get db by param to not import it in this file (import orders)
112 async function usersExist () {
113   const totalUsers = await UserModel.countTotal()
114
115   return totalUsers !== 0
116 }
117
118 // We get db by param to not import it in this file (import orders)
119 async function applicationExist () {
120   const totalApplication = await ApplicationModel.countTotal()
121
122   return totalApplication !== 0
123 }
124
125 // ---------------------------------------------------------------------------
126
127 export {
128   checkConfig,
129   clientsExist,
130   usersExist,
131   applicationExist,
132   checkActivityPubUrls
133 }