Limit comment length to 1 character, fixes #394 (#399)
[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 = 195
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     DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
110     CA_FILE: config.get<string>('smtp.ca_file'),
111     FROM_ADDRESS: config.get<string>('smtp.from_address')
112   },
113   STORAGE: {
114     AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
115     LOG_DIR: buildPath(config.get<string>('storage.logs')),
116     VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
117     THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
118     PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
119     TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
120     CACHE_DIR: buildPath(config.get<string>('storage.cache'))
121   },
122   WEBSERVER: {
123     SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
124     WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
125     HOSTNAME: config.get<string>('webserver.hostname'),
126     PORT: config.get<number>('webserver.port'),
127     URL: '',
128     HOST: ''
129   },
130   LOG: {
131     LEVEL: config.get<string>('log.level')
132   },
133   ADMIN: {
134     get EMAIL () { return config.get<string>('admin.email') }
135   },
136   SIGNUP: {
137     get ENABLED () { return config.get<boolean>('signup.enabled') },
138     get LIMIT () { return config.get<number>('signup.limit') }
139   },
140   USER: {
141     get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
142   },
143   TRANSCODING: {
144     get ENABLED () { return config.get<boolean>('transcoding.enabled') },
145     get THREADS () { return config.get<number>('transcoding.threads') },
146     RESOLUTIONS: {
147       get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
148       get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
149       get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
150       get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
151       get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
152     }
153   },
154   CACHE: {
155     PREVIEWS: {
156       get SIZE () { return config.get<number>('cache.previews.size') }
157     }
158   },
159   INSTANCE: {
160     get NAME () { return config.get<string>('instance.name') },
161     get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
162     get DESCRIPTION () { return config.get<string>('instance.description') },
163     get TERMS () { return config.get<string>('instance.terms') },
164     get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
165     CUSTOMIZATIONS: {
166       get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
167       get CSS () { return config.get<string>('instance.customizations.css') }
168     }
169   }
170 }
171
172 // ---------------------------------------------------------------------------
173
174 const CONSTRAINTS_FIELDS = {
175   USERS: {
176     USERNAME: { min: 3, max: 20 }, // Length
177     PASSWORD: { min: 6, max: 255 }, // Length
178     DESCRIPTION: { min: 3, max: 250 }, // Length
179     VIDEO_QUOTA: { min: -1 }
180   },
181   VIDEO_ABUSES: {
182     REASON: { min: 2, max: 300 } // Length
183   },
184   VIDEO_CHANNELS: {
185     NAME: { min: 3, max: 120 }, // Length
186     DESCRIPTION: { min: 3, max: 250 }, // Length
187     SUPPORT: { min: 3, max: 300 }, // Length
188     URL: { min: 3, max: 2000 } // Length
189   },
190   VIDEOS: {
191     NAME: { min: 3, max: 120 }, // Length
192     TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
193     DESCRIPTION: { min: 3, max: 10000 }, // Length
194     SUPPORT: { min: 3, max: 300 }, // Length
195     IMAGE: {
196       EXTNAME: [ '.jpg', '.jpeg' ],
197       FILE_SIZE: {
198         max: 2 * 1024 * 1024 // 2MB
199       }
200     },
201     EXTNAME: [ '.mp4', '.ogv', '.webm' ],
202     INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
203     DURATION: { min: 1 }, // Number
204     TAGS: { min: 0, max: 5 }, // Number of total tags
205     TAG: { min: 2, max: 30 }, // Length
206     THUMBNAIL: { min: 2, max: 30 },
207     THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
208     VIEWS: { min: 0 },
209     LIKES: { min: 0 },
210     DISLIKES: { min: 0 },
211     FILE_SIZE: { min: 10 },
212     URL: { min: 3, max: 2000 } // Length
213   },
214   ACTORS: {
215     PUBLIC_KEY: { min: 10, max: 5000 }, // Length
216     PRIVATE_KEY: { min: 10, max: 5000 }, // Length
217     URL: { min: 3, max: 2000 }, // Length
218     AVATAR: {
219       EXTNAME: [ '.png', '.jpeg', '.jpg' ],
220       FILE_SIZE: {
221         max: 2 * 1024 * 1024 // 2MB
222       }
223     }
224   },
225   VIDEO_EVENTS: {
226     COUNT: { min: 0 }
227   },
228   VIDEO_COMMENTS: {
229     TEXT: { min: 1, max: 3000 }, // Length
230     URL: { min: 3, max: 2000 } // Length
231   },
232   VIDEO_SHARE: {
233     URL: { min: 3, max: 2000 } // Length
234   }
235 }
236
237 let VIDEO_VIEW_LIFETIME = 60000 * 60 // 1 hour
238 const VIDEO_TRANSCODING_FPS = {
239   MIN: 10,
240   MAX: 30
241 }
242
243 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
244   LIKE: 'like',
245   DISLIKE: 'dislike'
246 }
247
248 const VIDEO_CATEGORIES = {
249   1: 'Music',
250   2: 'Films',
251   3: 'Vehicles',
252   4: 'Art',
253   5: 'Sports',
254   6: 'Travels',
255   7: 'Gaming',
256   8: 'People',
257   9: 'Comedy',
258   10: 'Entertainment',
259   11: 'News',
260   12: 'How To',
261   13: 'Education',
262   14: 'Activism',
263   15: 'Science & Technology',
264   16: 'Animals',
265   17: 'Kids',
266   18: 'Food'
267 }
268
269 // See https://creativecommons.org/licenses/?lang=en
270 const VIDEO_LICENCES = {
271   1: 'Attribution',
272   2: 'Attribution - Share Alike',
273   3: 'Attribution - No Derivatives',
274   4: 'Attribution - Non Commercial',
275   5: 'Attribution - Non Commercial - Share Alike',
276   6: 'Attribution - Non Commercial - No Derivatives',
277   7: 'Public Domain Dedication'
278 }
279
280 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
281 const VIDEO_LANGUAGES = {
282   1: 'English',
283   2: 'Spanish',
284   3: 'Mandarin',
285   4: 'Hindi',
286   5: 'Arabic',
287   6: 'Portuguese',
288   7: 'Bengali',
289   8: 'Russian',
290   9: 'Japanese',
291   10: 'Punjabi',
292   11: 'German',
293   12: 'Korean',
294   13: 'French',
295   14: 'Italian'
296 }
297
298 const VIDEO_PRIVACIES = {
299   [VideoPrivacy.PUBLIC]: 'Public',
300   [VideoPrivacy.UNLISTED]: 'Unlisted',
301   [VideoPrivacy.PRIVATE]: 'Private'
302 }
303
304 const VIDEO_MIMETYPE_EXT = {
305   'video/webm': '.webm',
306   'video/ogg': '.ogv',
307   'video/mp4': '.mp4'
308 }
309
310 const IMAGE_MIMETYPE_EXT = {
311   'image/png': '.png',
312   'image/jpg': '.jpg',
313   'image/jpeg': '.jpg'
314 }
315
316 // ---------------------------------------------------------------------------
317
318 const SERVER_ACTOR_NAME = 'peertube'
319
320 const ACTIVITY_PUB = {
321   POTENTIAL_ACCEPT_HEADERS: [
322     'application/activity+json',
323     'application/ld+json',
324     'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
325   ],
326   ACCEPT_HEADER: 'application/activity+json, application/ld+json',
327   PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
328   COLLECTION_ITEMS_PER_PAGE: 10,
329   FETCH_PAGE_LIMIT: 100,
330   URL_MIME_TYPES: {
331     VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
332     TORRENT: [ 'application/x-bittorrent' ],
333     MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
334   },
335   MAX_RECURSION_COMMENTS: 100,
336   ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
337 }
338
339 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
340   GROUP: 'Group',
341   PERSON: 'Person',
342   APPLICATION: 'Application'
343 }
344
345 // ---------------------------------------------------------------------------
346
347 const PRIVATE_RSA_KEY_SIZE = 2048
348
349 // Password encryption
350 const BCRYPT_SALT_SIZE = 10
351
352 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
353
354 // ---------------------------------------------------------------------------
355
356 // Express static paths (router)
357 const STATIC_PATHS = {
358   PREVIEWS: '/static/previews/',
359   THUMBNAILS: '/static/thumbnails/',
360   TORRENTS: '/static/torrents/',
361   WEBSEED: '/static/webseed/',
362   AVATARS: '/static/avatars/'
363 }
364
365 // Cache control
366 let STATIC_MAX_AGE = '30d'
367
368 // Videos thumbnail size
369 const THUMBNAILS_SIZE = {
370   width: 200,
371   height: 110
372 }
373 const PREVIEWS_SIZE = {
374   width: 560,
375   height: 315
376 }
377 const AVATARS_SIZE = {
378   width: 120,
379   height: 120
380 }
381
382 const EMBED_SIZE = {
383   width: 560,
384   height: 315
385 }
386
387 // Sub folders of cache directory
388 const CACHE = {
389   DIRECTORIES: {
390     PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
391   }
392 }
393
394 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
395
396 // ---------------------------------------------------------------------------
397
398 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
399
400 // ---------------------------------------------------------------------------
401
402 // Special constants for a test instance
403 if (isTestInstance() === true) {
404   ACTOR_FOLLOW_SCORE.BASE = 20
405   REMOTE_SCHEME.HTTP = 'http'
406   REMOTE_SCHEME.WS = 'ws'
407   STATIC_MAX_AGE = '0'
408   ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
409   ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
410   CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
411   SCHEDULER_INTERVAL = 10000
412   VIDEO_VIEW_LIFETIME = 1000 // 1 second
413 }
414
415 updateWebserverConfig()
416
417 // ---------------------------------------------------------------------------
418
419 export {
420   API_VERSION,
421   AVATARS_SIZE,
422   ACCEPT_HEADERS,
423   BCRYPT_SALT_SIZE,
424   CACHE,
425   CONFIG,
426   CONSTRAINTS_FIELDS,
427   EMBED_SIZE,
428   JOB_CONCURRENCY,
429   JOB_ATTEMPTS,
430   LAST_MIGRATION_VERSION,
431   OAUTH_LIFETIME,
432   OPENGRAPH_AND_OEMBED_COMMENT,
433   PAGINATION_COUNT_DEFAULT,
434   ACTOR_FOLLOW_SCORE,
435   PREVIEWS_SIZE,
436   REMOTE_SCHEME,
437   FOLLOW_STATES,
438   SERVER_ACTOR_NAME,
439   PRIVATE_RSA_KEY_SIZE,
440   SORTABLE_COLUMNS,
441   STATIC_MAX_AGE,
442   STATIC_PATHS,
443   ACTIVITY_PUB,
444   ACTIVITY_PUB_ACTOR_TYPES,
445   THUMBNAILS_SIZE,
446   VIDEO_CATEGORIES,
447   VIDEO_LANGUAGES,
448   VIDEO_PRIVACIES,
449   VIDEO_LICENCES,
450   VIDEO_RATE_TYPES,
451   VIDEO_MIMETYPE_EXT,
452   VIDEO_TRANSCODING_FPS,
453   USER_PASSWORD_RESET_LIFETIME,
454   IMAGE_MIMETYPE_EXT,
455   SCHEDULER_INTERVAL,
456   JOB_COMPLETED_LIFETIME,
457   VIDEO_VIEW_LIFETIME
458 }
459
460 // ---------------------------------------------------------------------------
461
462 function getLocalConfigFilePath () {
463   const configSources = config.util.getConfigSources()
464   if (configSources.length === 0) throw new Error('Invalid config source.')
465
466   let filename = 'local'
467   if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
468   if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
469
470   return join(dirname(configSources[ 0 ].name), filename + '.json')
471 }
472
473 function updateWebserverConfig () {
474   CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
475   CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
476 }
477
478 export function reloadConfig () {
479
480   function directory () {
481     if (process.env.NODE_CONFIG_DIR) {
482       return process.env.NODE_CONFIG_DIR
483     }
484
485     return join(root(), 'config')
486   }
487
488   function purge () {
489     for (const fileName in require.cache) {
490       if (-1 === fileName.indexOf(directory())) {
491         continue
492       }
493
494       delete require.cache[fileName]
495     }
496
497     delete require.cache[require.resolve('config')]
498   }
499
500   purge()
501
502   config = require('config')
503
504   updateWebserverConfig()
505 }