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