Don't rehost announced video activities
[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 = 185
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   VIDEO_SHARE: {
201     URL: { min: 3, max: 2000 } // Length
202   }
203 }
204
205 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
206   LIKE: 'like',
207   DISLIKE: 'dislike'
208 }
209
210 const VIDEO_CATEGORIES = {
211   1: 'Music',
212   2: 'Films',
213   3: 'Vehicles',
214   4: 'Art',
215   5: 'Sports',
216   6: 'Travels',
217   7: 'Gaming',
218   8: 'People',
219   9: 'Comedy',
220   10: 'Entertainment',
221   11: 'News',
222   12: 'How To',
223   13: 'Education',
224   14: 'Activism',
225   15: 'Science & Technology',
226   16: 'Animals',
227   17: 'Kids',
228   18: 'Food'
229 }
230
231 // See https://creativecommons.org/licenses/?lang=en
232 const VIDEO_LICENCES = {
233   1: 'Attribution',
234   2: 'Attribution - Share Alike',
235   3: 'Attribution - No Derivatives',
236   4: 'Attribution - Non Commercial',
237   5: 'Attribution - Non Commercial - Share Alike',
238   6: 'Attribution - Non Commercial - No Derivatives',
239   7: 'Public Domain Dedication'
240 }
241
242 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
243 const VIDEO_LANGUAGES = {
244   1: 'English',
245   2: 'Spanish',
246   3: 'Mandarin',
247   4: 'Hindi',
248   5: 'Arabic',
249   6: 'Portuguese',
250   7: 'Bengali',
251   8: 'Russian',
252   9: 'Japanese',
253   10: 'Punjabi',
254   11: 'German',
255   12: 'Korean',
256   13: 'French',
257   14: 'Italian'
258 }
259
260 const VIDEO_PRIVACIES = {
261   [VideoPrivacy.PUBLIC]: 'Public',
262   [VideoPrivacy.UNLISTED]: 'Unlisted',
263   [VideoPrivacy.PRIVATE]: 'Private'
264 }
265
266 const VIDEO_MIMETYPE_EXT = {
267   'video/webm': '.webm',
268   'video/ogg': '.ogv',
269   'video/mp4': '.mp4'
270 }
271
272 const AVATAR_MIMETYPE_EXT = {
273   'image/png': '.png',
274   'image/jpg': '.jpg',
275   'image/jpeg': '.jpg'
276 }
277
278 // ---------------------------------------------------------------------------
279
280 const SERVER_ACTOR_NAME = 'peertube'
281
282 const ACTIVITY_PUB = {
283   POTENTIAL_ACCEPT_HEADERS: [
284     'application/activity+json',
285     'application/ld+json',
286     'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
287   ],
288   ACCEPT_HEADER: 'application/activity+json, application/ld+json',
289   PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
290   COLLECTION_ITEMS_PER_PAGE: 10,
291   FETCH_PAGE_LIMIT: 100,
292   URL_MIME_TYPES: {
293     VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
294     TORRENT: [ 'application/x-bittorrent' ],
295     MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
296   },
297   MAX_RECURSION_COMMENTS: 100,
298   ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
299 }
300
301 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
302   GROUP: 'Group',
303   PERSON: 'Person',
304   APPLICATION: 'Application'
305 }
306
307 // ---------------------------------------------------------------------------
308
309 const PRIVATE_RSA_KEY_SIZE = 2048
310
311 // Password encryption
312 const BCRYPT_SALT_SIZE = 10
313
314 // ---------------------------------------------------------------------------
315
316 // Express static paths (router)
317 const STATIC_PATHS = {
318   PREVIEWS: '/static/previews/',
319   THUMBNAILS: '/static/thumbnails/',
320   TORRENTS: '/static/torrents/',
321   WEBSEED: '/static/webseed/',
322   AVATARS: '/static/avatars/'
323 }
324
325 // Cache control
326 let STATIC_MAX_AGE = '30d'
327
328 // Videos thumbnail size
329 const THUMBNAILS_SIZE = {
330   width: 200,
331   height: 110
332 }
333 const PREVIEWS_SIZE = {
334   width: 560,
335   height: 315
336 }
337 const AVATARS_SIZE = {
338   width: 120,
339   height: 120
340 }
341
342 const EMBED_SIZE = {
343   width: 560,
344   height: 315
345 }
346
347 // Sub folders of cache directory
348 const CACHE = {
349   DIRECTORIES: {
350     PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
351   }
352 }
353
354 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
355
356 // ---------------------------------------------------------------------------
357
358 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
359
360 // ---------------------------------------------------------------------------
361
362 // Special constants for a test instance
363 if (isTestInstance() === true) {
364   ACTOR_FOLLOW_SCORE.BASE = 20
365   REMOTE_SCHEME.HTTP = 'http'
366   REMOTE_SCHEME.WS = 'ws'
367   STATIC_MAX_AGE = '0'
368   ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
369   ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
370   CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
371   SCHEDULER_INTERVAL = 10000
372 }
373
374 updateWebserverConfig()
375
376 // ---------------------------------------------------------------------------
377
378 export {
379   API_VERSION,
380   AVATARS_SIZE,
381   ACCEPT_HEADERS,
382   BCRYPT_SALT_SIZE,
383   CACHE,
384   CONFIG,
385   CONSTRAINTS_FIELDS,
386   EMBED_SIZE,
387   JOB_CONCURRENCY,
388   JOB_ATTEMPTS,
389   LAST_MIGRATION_VERSION,
390   OAUTH_LIFETIME,
391   OPENGRAPH_AND_OEMBED_COMMENT,
392   PAGINATION_COUNT_DEFAULT,
393   ACTOR_FOLLOW_SCORE,
394   PREVIEWS_SIZE,
395   REMOTE_SCHEME,
396   FOLLOW_STATES,
397   SERVER_ACTOR_NAME,
398   PRIVATE_RSA_KEY_SIZE,
399   SORTABLE_COLUMNS,
400   STATIC_MAX_AGE,
401   STATIC_PATHS,
402   ACTIVITY_PUB,
403   ACTIVITY_PUB_ACTOR_TYPES,
404   THUMBNAILS_SIZE,
405   VIDEO_CATEGORIES,
406   VIDEO_LANGUAGES,
407   VIDEO_PRIVACIES,
408   VIDEO_LICENCES,
409   VIDEO_RATE_TYPES,
410   VIDEO_MIMETYPE_EXT,
411   AVATAR_MIMETYPE_EXT,
412   SCHEDULER_INTERVAL,
413   JOB_COMPLETED_LIFETIME
414 }
415
416 // ---------------------------------------------------------------------------
417
418 function getLocalConfigFilePath () {
419   const configSources = config.util.getConfigSources()
420   if (configSources.length === 0) throw new Error('Invalid config source.')
421
422   let filename = 'local'
423   if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
424   if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
425
426   return join(dirname(configSources[ 0 ].name), filename + '.json')
427 }
428
429 function updateWebserverConfig () {
430   CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
431   CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
432 }
433
434 export function reloadConfig () {
435
436   function directory () {
437     if (process.env.NODE_CONFIG_DIR) {
438       return process.env.NODE_CONFIG_DIR
439     }
440
441     return join(root(), 'config')
442   }
443
444   function purge () {
445     for (const fileName in require.cache) {
446       if (-1 === fileName.indexOf(directory())) {
447         continue
448       }
449
450       delete require.cache[fileName]
451     }
452
453     delete require.cache[require.resolve('config')]
454   }
455
456   purge()
457
458   config = require('config')
459
460   updateWebserverConfig()
461 }