e531c4c394fd7377dcbbef9a6dde98906e72ad2f
[oweals/peertube.git] / server / initializers / constants.ts
1 import { IConfig } from 'config'
2 import { dirname, join } from 'path'
3 import { JobType, VideoRateType } from '../../shared/models'
4 import { ActivityPubActorType } from '../../shared/models/activitypub'
5 import { FollowState } from '../../shared/models/actors'
6 import { VideoPrivacy } from '../../shared/models/videos'
7 // Do not use barrels, remain constants as independent as possible
8 import { buildPath, isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
9
10 // Use a variable to reload the configuration if we need
11 let config: IConfig = require('config')
12
13 // ---------------------------------------------------------------------------
14
15 const LAST_MIGRATION_VERSION = 190
16
17 // ---------------------------------------------------------------------------
18
19 // API version
20 const API_VERSION = 'v1'
21
22 // Number of results by default for the pagination
23 const PAGINATION_COUNT_DEFAULT = 15
24
25 // Sortable columns per schema
26 const SORTABLE_COLUMNS = {
27   USERS: [ 'id', 'username', 'createdAt' ],
28   ACCOUNTS: [ 'createdAt' ],
29   JOBS: [ 'createdAt' ],
30   VIDEO_ABUSES: [ 'id', 'createdAt' ],
31   VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
32   VIDEOS: [ 'name', 'duration', 'createdAt', 'views', 'likes' ],
33   VIDEO_COMMENT_THREADS: [ 'createdAt' ],
34   BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
35   FOLLOWERS: [ 'createdAt' ],
36   FOLLOWING: [ 'createdAt' ]
37 }
38
39 const OAUTH_LIFETIME = {
40   ACCESS_TOKEN: 3600 * 4, // 4 hours
41   REFRESH_TOKEN: 1209600 // 2 weeks
42 }
43
44 // ---------------------------------------------------------------------------
45
46 // Number of points we add/remove after a successful/bad request
47 const ACTOR_FOLLOW_SCORE = {
48   PENALTY: -10,
49   BONUS: 10,
50   BASE: 1000,
51   MAX: 10000
52 }
53
54 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
55   PENDING: 'pending',
56   ACCEPTED: 'accepted'
57 }
58
59 const REMOTE_SCHEME = {
60   HTTP: 'https',
61   WS: 'wss'
62 }
63
64 const JOB_ATTEMPTS: { [ id in JobType ]: number } = {
65   'activitypub-http-broadcast': 5,
66   'activitypub-http-unicast': 5,
67   'activitypub-http-fetcher': 5,
68   'video-file': 1,
69   'email': 5
70 }
71 const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
72   'activitypub-http-broadcast': 1,
73   'activitypub-http-unicast': 5,
74   'activitypub-http-fetcher': 1,
75   'video-file': 1,
76   'email': 5
77 }
78 // 2 days
79 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2
80
81 // 1 hour
82 let SCHEDULER_INTERVAL = 60000 * 60
83
84 // ---------------------------------------------------------------------------
85
86 const CONFIG = {
87   CUSTOM_FILE: getLocalConfigFilePath(),
88   LISTEN: {
89     PORT: config.get<number>('listen.port')
90   },
91   DATABASE: {
92     DBNAME: 'peertube' + config.get<string>('database.suffix'),
93     HOSTNAME: config.get<string>('database.hostname'),
94     PORT: config.get<number>('database.port'),
95     USERNAME: config.get<string>('database.username'),
96     PASSWORD: config.get<string>('database.password')
97   },
98   REDIS: {
99     HOSTNAME: config.get<string>('redis.hostname'),
100     PORT: config.get<number>('redis.port'),
101     AUTH: config.get<string>('redis.auth')
102   },
103   SMTP: {
104     HOSTNAME: config.get<string>('smtp.hostname'),
105     PORT: config.get<number>('smtp.port'),
106     USERNAME: config.get<string>('smtp.username'),
107     PASSWORD: config.get<string>('smtp.password'),
108     TLS: config.get<boolean>('smtp.tls'),
109     CA_FILE: config.get<string>('smtp.ca_file'),
110     FROM_ADDRESS: config.get<string>('smtp.from_address')
111   },
112   STORAGE: {
113     AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
114     LOG_DIR: buildPath(config.get<string>('storage.logs')),
115     VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
116     THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
117     PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
118     TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
119     CACHE_DIR: buildPath(config.get<string>('storage.cache'))
120   },
121   WEBSERVER: {
122     SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
123     WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
124     HOSTNAME: config.get<string>('webserver.hostname'),
125     PORT: config.get<number>('webserver.port'),
126     URL: '',
127     HOST: ''
128   },
129   LOG: {
130     LEVEL: config.get<string>('log.level')
131   },
132   ADMIN: {
133     get EMAIL () { return config.get<string>('admin.email') }
134   },
135   SIGNUP: {
136     get ENABLED () { return config.get<boolean>('signup.enabled') },
137     get LIMIT () { return config.get<number>('signup.limit') }
138   },
139   USER: {
140     get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
141   },
142   TRANSCODING: {
143     get ENABLED () { return config.get<boolean>('transcoding.enabled') },
144     get THREADS () { return config.get<number>('transcoding.threads') },
145     RESOLUTIONS: {
146       get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
147       get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
148       get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
149       get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
150       get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
151     }
152   },
153   CACHE: {
154     PREVIEWS: {
155       get SIZE () { return config.get<number>('cache.previews.size') }
156     }
157   },
158   INSTANCE: {
159     get NAME () { return config.get<string>('instance.name') },
160     get DESCRIPTION () { return config.get<string>('instance.description') },
161     get TERMS () { return config.get<string>('instance.terms') }
162   }
163 }
164
165 // ---------------------------------------------------------------------------
166
167 const CONSTRAINTS_FIELDS = {
168   USERS: {
169     USERNAME: { min: 3, max: 20 }, // Length
170     PASSWORD: { min: 6, max: 255 }, // Length
171     VIDEO_QUOTA: { min: -1 }
172   },
173   VIDEO_ABUSES: {
174     REASON: { min: 2, max: 300 } // Length
175   },
176   VIDEO_CHANNELS: {
177     NAME: { min: 3, max: 120 }, // Length
178     DESCRIPTION: { min: 3, max: 250 }, // Length
179     URL: { min: 3, max: 2000 } // Length
180   },
181   VIDEOS: {
182     NAME: { min: 3, max: 120 }, // Length
183     TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
184     DESCRIPTION: { min: 3, max: 3000 }, // Length
185     EXTNAME: [ '.mp4', '.ogv', '.webm' ],
186     INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
187     DURATION: { min: 1 }, // Number
188     TAGS: { min: 0, max: 5 }, // Number of total tags
189     TAG: { min: 2, max: 30 }, // Length
190     THUMBNAIL: { min: 2, max: 30 },
191     THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
192     VIEWS: { min: 0 },
193     LIKES: { min: 0 },
194     DISLIKES: { min: 0 },
195     FILE_SIZE: { min: 10 },
196     URL: { min: 3, max: 2000 } // Length
197   },
198   ACTORS: {
199     PUBLIC_KEY: { min: 10, max: 5000 }, // Length
200     PRIVATE_KEY: { min: 10, max: 5000 }, // Length
201     URL: { min: 3, max: 2000 }, // Length
202     AVATAR: {
203       EXTNAME: [ '.png', '.jpeg', '.jpg' ],
204       FILE_SIZE: {
205         max: 2 * 1024 * 1024 // 2MB
206       }
207     }
208   },
209   VIDEO_EVENTS: {
210     COUNT: { min: 0 }
211   },
212   VIDEO_COMMENTS: {
213     TEXT: { min: 2, max: 3000 }, // Length
214     URL: { min: 3, max: 2000 } // Length
215   },
216   VIDEO_SHARE: {
217     URL: { min: 3, max: 2000 } // Length
218   }
219 }
220
221 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
222   LIKE: 'like',
223   DISLIKE: 'dislike'
224 }
225
226 const VIDEO_CATEGORIES = {
227   1: 'Music',
228   2: 'Films',
229   3: 'Vehicles',
230   4: 'Art',
231   5: 'Sports',
232   6: 'Travels',
233   7: 'Gaming',
234   8: 'People',
235   9: 'Comedy',
236   10: 'Entertainment',
237   11: 'News',
238   12: 'How To',
239   13: 'Education',
240   14: 'Activism',
241   15: 'Science & Technology',
242   16: 'Animals',
243   17: 'Kids',
244   18: 'Food'
245 }
246
247 // See https://creativecommons.org/licenses/?lang=en
248 const VIDEO_LICENCES = {
249   1: 'Attribution',
250   2: 'Attribution - Share Alike',
251   3: 'Attribution - No Derivatives',
252   4: 'Attribution - Non Commercial',
253   5: 'Attribution - Non Commercial - Share Alike',
254   6: 'Attribution - Non Commercial - No Derivatives',
255   7: 'Public Domain Dedication'
256 }
257
258 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
259 const VIDEO_LANGUAGES = {
260   1: 'English',
261   2: 'Spanish',
262   3: 'Mandarin',
263   4: 'Hindi',
264   5: 'Arabic',
265   6: 'Portuguese',
266   7: 'Bengali',
267   8: 'Russian',
268   9: 'Japanese',
269   10: 'Punjabi',
270   11: 'German',
271   12: 'Korean',
272   13: 'French',
273   14: 'Italian'
274 }
275
276 const VIDEO_PRIVACIES = {
277   [VideoPrivacy.PUBLIC]: 'Public',
278   [VideoPrivacy.UNLISTED]: 'Unlisted',
279   [VideoPrivacy.PRIVATE]: 'Private'
280 }
281
282 const VIDEO_MIMETYPE_EXT = {
283   'video/webm': '.webm',
284   'video/ogg': '.ogv',
285   'video/mp4': '.mp4'
286 }
287
288 const AVATAR_MIMETYPE_EXT = {
289   'image/png': '.png',
290   'image/jpg': '.jpg',
291   'image/jpeg': '.jpg'
292 }
293
294 // ---------------------------------------------------------------------------
295
296 const SERVER_ACTOR_NAME = 'peertube'
297
298 const ACTIVITY_PUB = {
299   POTENTIAL_ACCEPT_HEADERS: [
300     'application/activity+json',
301     'application/ld+json',
302     'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
303   ],
304   ACCEPT_HEADER: 'application/activity+json, application/ld+json',
305   PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
306   COLLECTION_ITEMS_PER_PAGE: 10,
307   FETCH_PAGE_LIMIT: 100,
308   URL_MIME_TYPES: {
309     VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
310     TORRENT: [ 'application/x-bittorrent' ],
311     MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
312   },
313   MAX_RECURSION_COMMENTS: 100,
314   ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
315 }
316
317 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
318   GROUP: 'Group',
319   PERSON: 'Person',
320   APPLICATION: 'Application'
321 }
322
323 // ---------------------------------------------------------------------------
324
325 const PRIVATE_RSA_KEY_SIZE = 2048
326
327 // Password encryption
328 const BCRYPT_SALT_SIZE = 10
329
330 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
331
332 // ---------------------------------------------------------------------------
333
334 // Express static paths (router)
335 const STATIC_PATHS = {
336   PREVIEWS: '/static/previews/',
337   THUMBNAILS: '/static/thumbnails/',
338   TORRENTS: '/static/torrents/',
339   WEBSEED: '/static/webseed/',
340   AVATARS: '/static/avatars/'
341 }
342
343 // Cache control
344 let STATIC_MAX_AGE = '30d'
345
346 // Videos thumbnail size
347 const THUMBNAILS_SIZE = {
348   width: 200,
349   height: 110
350 }
351 const PREVIEWS_SIZE = {
352   width: 560,
353   height: 315
354 }
355 const AVATARS_SIZE = {
356   width: 120,
357   height: 120
358 }
359
360 const EMBED_SIZE = {
361   width: 560,
362   height: 315
363 }
364
365 // Sub folders of cache directory
366 const CACHE = {
367   DIRECTORIES: {
368     PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
369   }
370 }
371
372 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
373
374 // ---------------------------------------------------------------------------
375
376 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
377
378 // ---------------------------------------------------------------------------
379
380 // Special constants for a test instance
381 if (isTestInstance() === true) {
382   ACTOR_FOLLOW_SCORE.BASE = 20
383   REMOTE_SCHEME.HTTP = 'http'
384   REMOTE_SCHEME.WS = 'ws'
385   STATIC_MAX_AGE = '0'
386   ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
387   ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
388   CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
389   SCHEDULER_INTERVAL = 10000
390 }
391
392 updateWebserverConfig()
393
394 // ---------------------------------------------------------------------------
395
396 export {
397   API_VERSION,
398   AVATARS_SIZE,
399   ACCEPT_HEADERS,
400   BCRYPT_SALT_SIZE,
401   CACHE,
402   CONFIG,
403   CONSTRAINTS_FIELDS,
404   EMBED_SIZE,
405   JOB_CONCURRENCY,
406   JOB_ATTEMPTS,
407   LAST_MIGRATION_VERSION,
408   OAUTH_LIFETIME,
409   OPENGRAPH_AND_OEMBED_COMMENT,
410   PAGINATION_COUNT_DEFAULT,
411   ACTOR_FOLLOW_SCORE,
412   PREVIEWS_SIZE,
413   REMOTE_SCHEME,
414   FOLLOW_STATES,
415   SERVER_ACTOR_NAME,
416   PRIVATE_RSA_KEY_SIZE,
417   SORTABLE_COLUMNS,
418   STATIC_MAX_AGE,
419   STATIC_PATHS,
420   ACTIVITY_PUB,
421   ACTIVITY_PUB_ACTOR_TYPES,
422   THUMBNAILS_SIZE,
423   VIDEO_CATEGORIES,
424   VIDEO_LANGUAGES,
425   VIDEO_PRIVACIES,
426   VIDEO_LICENCES,
427   VIDEO_RATE_TYPES,
428   VIDEO_MIMETYPE_EXT,
429   USER_PASSWORD_RESET_LIFETIME,
430   AVATAR_MIMETYPE_EXT,
431   SCHEDULER_INTERVAL,
432   JOB_COMPLETED_LIFETIME
433 }
434
435 // ---------------------------------------------------------------------------
436
437 function getLocalConfigFilePath () {
438   const configSources = config.util.getConfigSources()
439   if (configSources.length === 0) throw new Error('Invalid config source.')
440
441   let filename = 'local'
442   if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
443   if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
444
445   return join(dirname(configSources[ 0 ].name), filename + '.json')
446 }
447
448 function updateWebserverConfig () {
449   CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
450   CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
451 }
452
453 export function reloadConfig () {
454
455   function directory () {
456     if (process.env.NODE_CONFIG_DIR) {
457       return process.env.NODE_CONFIG_DIR
458     }
459
460     return join(root(), 'config')
461   }
462
463   function purge () {
464     for (const fileName in require.cache) {
465       if (-1 === fileName.indexOf(directory())) {
466         continue
467       }
468
469       delete require.cache[fileName]
470     }
471
472     delete require.cache[require.resolve('config')]
473   }
474
475   purge()
476
477   config = require('config')
478
479   updateWebserverConfig()
480 }