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