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