Support roles with rights and add moderator role
[oweals/peertube.git] / server / initializers / constants.ts
1 import * as config from 'config'
2 import { join } from 'path'
3
4 // Do not use barrels, remain constants as independent as possible
5 import { root, isTestInstance } from '../helpers/core-utils'
6
7 import {
8   VideoRateType,
9   RequestEndpoint,
10   RequestVideoEventType,
11   RequestVideoQaduType,
12   RemoteVideoRequestType,
13   JobState
14 } from '../../shared/models'
15
16 // ---------------------------------------------------------------------------
17
18 const LAST_MIGRATION_VERSION = 85
19
20 // ---------------------------------------------------------------------------
21
22 // API version
23 const API_VERSION = 'v1'
24
25 // Number of results by default for the pagination
26 const PAGINATION_COUNT_DEFAULT = 15
27
28 // Sortable columns per schema
29 const SEARCHABLE_COLUMNS = {
30   VIDEOS: [ 'name', 'magnetUri', 'host', 'author', 'tags' ]
31 }
32
33 // Sortable columns per schema
34 const SORTABLE_COLUMNS = {
35   PODS: [ 'id', 'host', 'score', 'createdAt' ],
36   USERS: [ 'id', 'username', 'createdAt' ],
37   VIDEO_ABUSES: [ 'id', 'createdAt' ],
38   VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
39   VIDEOS: [ 'name', 'duration', 'createdAt', 'views', 'likes' ],
40   BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ]
41 }
42
43 const OAUTH_LIFETIME = {
44   ACCESS_TOKEN: 3600 * 4, // 4 hours
45   REFRESH_TOKEN: 1209600 // 2 weeks
46 }
47
48 // ---------------------------------------------------------------------------
49
50 const CONFIG = {
51   LISTEN: {
52     PORT: config.get<number>('listen.port')
53   },
54   DATABASE: {
55     DBNAME: 'peertube' + config.get<string>('database.suffix'),
56     HOSTNAME: config.get<string>('database.hostname'),
57     PORT: config.get<number>('database.port'),
58     USERNAME: config.get<string>('database.username'),
59     PASSWORD: config.get<string>('database.password')
60   },
61   STORAGE: {
62     CERT_DIR: join(root(), config.get<string>('storage.certs')),
63     LOG_DIR: join(root(), config.get<string>('storage.logs')),
64     VIDEOS_DIR: join(root(), config.get<string>('storage.videos')),
65     THUMBNAILS_DIR: join(root(), config.get<string>('storage.thumbnails')),
66     PREVIEWS_DIR: join(root(), config.get<string>('storage.previews')),
67     TORRENTS_DIR: join(root(), config.get<string>('storage.torrents')),
68     CACHE_DIR: join(root(), config.get<string>('storage.cache'))
69   },
70   WEBSERVER: {
71     SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
72     WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
73     HOSTNAME: config.get<string>('webserver.hostname'),
74     PORT: config.get<number>('webserver.port'),
75     URL: '',
76     HOST: ''
77   },
78   ADMIN: {
79     EMAIL: config.get<string>('admin.email')
80   },
81   SIGNUP: {
82     ENABLED: config.get<boolean>('signup.enabled'),
83     LIMIT: config.get<number>('signup.limit')
84   },
85   USER: {
86     VIDEO_QUOTA: config.get<number>('user.video_quota')
87   },
88   TRANSCODING: {
89     ENABLED: config.get<boolean>('transcoding.enabled'),
90     THREADS: config.get<number>('transcoding.threads'),
91     RESOLUTIONS: {
92       '240' : config.get<boolean>('transcoding.resolutions.240p'),
93       '360': config.get<boolean>('transcoding.resolutions.360p'),
94       '480': config.get<boolean>('transcoding.resolutions.480p'),
95       '720': config.get<boolean>('transcoding.resolutions.720p'),
96       '1080': config.get<boolean>('transcoding.resolutions.1080p')
97     }
98   },
99   CACHE: {
100     PREVIEWS: {
101       SIZE: config.get<number>('cache.previews.size')
102     }
103   }
104 }
105 CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
106 CONFIG.WEBSERVER.HOST = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
107
108 // ---------------------------------------------------------------------------
109
110 const CONSTRAINTS_FIELDS = {
111   USERS: {
112     USERNAME: { min: 3, max: 20 }, // Length
113     PASSWORD: { min: 6, max: 255 }, // Length
114     VIDEO_QUOTA: { min: -1 }
115   },
116   VIDEO_ABUSES: {
117     REASON: { min: 2, max: 300 } // Length
118   },
119   VIDEO_CHANNELS: {
120     NAME: { min: 3, max: 120 }, // Length
121     DESCRIPTION: { min: 3, max: 250 } // Length
122   },
123   VIDEOS: {
124     NAME: { min: 3, max: 120 }, // Length
125     DESCRIPTION: { min: 3, max: 250 }, // Length
126     EXTNAME: [ '.mp4', '.ogv', '.webm' ],
127     INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
128     DURATION: { min: 1, max: 7200 }, // Number
129     TAGS: { min: 0, max: 5 }, // Number of total tags
130     TAG: { min: 2, max: 30 }, // Length
131     THUMBNAIL: { min: 2, max: 30 },
132     THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
133     VIEWS: { min: 0 },
134     LIKES: { min: 0 },
135     DISLIKES: { min: 0 },
136     FILE_SIZE: { min: 10, max: 1024 * 1024 * 1024 * 3 /* 3Go */ }
137   },
138   VIDEO_EVENTS: {
139     COUNT: { min: 0 }
140   }
141 }
142
143 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
144   LIKE: 'like',
145   DISLIKE: 'dislike'
146 }
147
148 const VIDEO_CATEGORIES = {
149   1: 'Music',
150   2: 'Films',
151   3: 'Vehicles',
152   4: 'Art',
153   5: 'Sports',
154   6: 'Travels',
155   7: 'Gaming',
156   8: 'People',
157   9: 'Comedy',
158   10: 'Entertainment',
159   11: 'News',
160   12: 'How To',
161   13: 'Education',
162   14: 'Activism',
163   15: 'Science & Technology',
164   16: 'Animals',
165   17: 'Kids',
166   18: 'Food'
167 }
168
169 // See https://creativecommons.org/licenses/?lang=en
170 const VIDEO_LICENCES = {
171   1: 'Attribution',
172   2: 'Attribution - Share Alike',
173   3: 'Attribution - No Derivatives',
174   4: 'Attribution - Non Commercial',
175   5: 'Attribution - Non Commercial - Share Alike',
176   6: 'Attribution - Non Commercial - No Derivatives',
177   7: 'Public Domain Dedication'
178 }
179
180 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
181 const VIDEO_LANGUAGES = {
182   1: 'English',
183   2: 'Spanish',
184   3: 'Mandarin',
185   4: 'Hindi',
186   5: 'Arabic',
187   6: 'Portuguese',
188   7: 'Bengali',
189   8: 'Russian',
190   9: 'Japanese',
191   10: 'Punjabi',
192   11: 'German',
193   12: 'Korean',
194   13: 'French',
195   14: 'Italian'
196 }
197
198 // ---------------------------------------------------------------------------
199
200 // Score a pod has when we create it as a friend
201 const FRIEND_SCORE = {
202   BASE: 100,
203   MAX: 1000
204 }
205
206 // ---------------------------------------------------------------------------
207
208 // Number of points we add/remove from a friend after a successful/bad request
209 const PODS_SCORE = {
210   PENALTY: -10,
211   BONUS: 10
212 }
213
214 // Time to wait between requests to the friends (10 min)
215 let REQUESTS_INTERVAL = 600000
216
217 // Number of requests in parallel we can make
218 const REQUESTS_IN_PARALLEL = 10
219
220 // To how many pods we send requests
221 const REQUESTS_LIMIT_PODS = 10
222 // How many requests we send to a pod per interval
223 const REQUESTS_LIMIT_PER_POD = 5
224
225 const REQUESTS_VIDEO_QADU_LIMIT_PODS = 10
226 // The QADU requests are not big
227 const REQUESTS_VIDEO_QADU_LIMIT_PER_POD = 50
228
229 const REQUESTS_VIDEO_EVENT_LIMIT_PODS = 10
230 // The EVENTS requests are not big
231 const REQUESTS_VIDEO_EVENT_LIMIT_PER_POD = 50
232
233 // Number of requests to retry for replay requests module
234 const RETRY_REQUESTS = 5
235
236 const REQUEST_ENDPOINTS: { [ id: string ]: RequestEndpoint } = {
237   VIDEOS: 'videos'
238 }
239
240 const REQUEST_ENDPOINT_ACTIONS: {
241   [ id: string ]: {
242     [ id: string ]: RemoteVideoRequestType
243   }
244 } = {}
245 REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] = {
246   ADD_VIDEO: 'add-video',
247   UPDATE_VIDEO: 'update-video',
248   REMOVE_VIDEO: 'remove-video',
249   ADD_CHANNEL: 'add-channel',
250   UPDATE_CHANNEL: 'update-channel',
251   REMOVE_CHANNEL: 'remove-channel',
252   ADD_AUTHOR: 'add-author',
253   REMOVE_AUTHOR: 'remove-author',
254   REPORT_ABUSE: 'report-abuse'
255 }
256
257 const REQUEST_VIDEO_QADU_ENDPOINT = 'videos/qadu'
258 const REQUEST_VIDEO_EVENT_ENDPOINT = 'videos/events'
259
260 const REQUEST_VIDEO_QADU_TYPES: { [ id: string ]: RequestVideoQaduType } = {
261   LIKES: 'likes',
262   DISLIKES: 'dislikes',
263   VIEWS: 'views'
264 }
265
266 const REQUEST_VIDEO_EVENT_TYPES: { [ id: string ]: RequestVideoEventType } = {
267   LIKES: 'likes',
268   DISLIKES: 'dislikes',
269   VIEWS: 'views'
270 }
271
272 const REMOTE_SCHEME = {
273   HTTP: 'https',
274   WS: 'wss'
275 }
276
277 const JOB_STATES: { [ id: string ]: JobState } = {
278   PENDING: 'pending',
279   PROCESSING: 'processing',
280   ERROR: 'error',
281   SUCCESS: 'success'
282 }
283 // How many maximum jobs we fetch from the database per cycle
284 const JOBS_FETCH_LIMIT_PER_CYCLE = 10
285 // 1 minutes
286 let JOBS_FETCHING_INTERVAL = 60000
287
288 // ---------------------------------------------------------------------------
289
290 const PRIVATE_CERT_NAME = 'peertube.key.pem'
291 const PUBLIC_CERT_NAME = 'peertube.pub'
292 const SIGNATURE_ALGORITHM = 'RSA-SHA256'
293 const SIGNATURE_ENCODING = 'hex'
294
295 // Password encryption
296 const BCRYPT_SALT_SIZE = 10
297
298 // ---------------------------------------------------------------------------
299
300 // Express static paths (router)
301 const STATIC_PATHS = {
302   PREVIEWS: '/static/previews/',
303   THUMBNAILS: '/static/thumbnails/',
304   TORRENTS: '/static/torrents/',
305   WEBSEED: '/static/webseed/'
306 }
307
308 // Cache control
309 let STATIC_MAX_AGE = '30d'
310
311 // Videos thumbnail size
312 const THUMBNAILS_SIZE = {
313   width: 200,
314   height: 110
315 }
316 const PREVIEWS_SIZE = {
317   width: 560,
318   height: 315
319 }
320
321 const EMBED_SIZE = {
322   width: 560,
323   height: 315
324 }
325
326 // Sub folders of cache directory
327 const CACHE = {
328   DIRECTORIES: {
329     PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
330   }
331 }
332
333 // ---------------------------------------------------------------------------
334
335 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
336
337 // ---------------------------------------------------------------------------
338
339 // Special constants for a test instance
340 if (isTestInstance() === true) {
341   CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
342   FRIEND_SCORE.BASE = 20
343   REQUESTS_INTERVAL = 10000
344   JOBS_FETCHING_INTERVAL = 10000
345   REMOTE_SCHEME.HTTP = 'http'
346   REMOTE_SCHEME.WS = 'ws'
347   STATIC_MAX_AGE = '0'
348 }
349
350 // ---------------------------------------------------------------------------
351
352 export {
353   API_VERSION,
354   BCRYPT_SALT_SIZE,
355   CACHE,
356   CONFIG,
357   CONSTRAINTS_FIELDS,
358   EMBED_SIZE,
359   FRIEND_SCORE,
360   JOB_STATES,
361   JOBS_FETCH_LIMIT_PER_CYCLE,
362   JOBS_FETCHING_INTERVAL,
363   LAST_MIGRATION_VERSION,
364   OAUTH_LIFETIME,
365   OPENGRAPH_AND_OEMBED_COMMENT,
366   PAGINATION_COUNT_DEFAULT,
367   PODS_SCORE,
368   PREVIEWS_SIZE,
369   PRIVATE_CERT_NAME,
370   PUBLIC_CERT_NAME,
371   REMOTE_SCHEME,
372   REQUEST_ENDPOINT_ACTIONS,
373   REQUEST_ENDPOINTS,
374   REQUEST_VIDEO_EVENT_ENDPOINT,
375   REQUEST_VIDEO_EVENT_TYPES,
376   REQUEST_VIDEO_QADU_ENDPOINT,
377   REQUEST_VIDEO_QADU_TYPES,
378   REQUESTS_IN_PARALLEL,
379   REQUESTS_INTERVAL,
380   REQUESTS_LIMIT_PER_POD,
381   REQUESTS_LIMIT_PODS,
382   REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
383   REQUESTS_VIDEO_EVENT_LIMIT_PODS,
384   REQUESTS_VIDEO_QADU_LIMIT_PER_POD,
385   REQUESTS_VIDEO_QADU_LIMIT_PODS,
386   RETRY_REQUESTS,
387   SEARCHABLE_COLUMNS,
388   SIGNATURE_ALGORITHM,
389   SIGNATURE_ENCODING,
390   SORTABLE_COLUMNS,
391   STATIC_MAX_AGE,
392   STATIC_PATHS,
393   THUMBNAILS_SIZE,
394   VIDEO_CATEGORIES,
395   VIDEO_LANGUAGES,
396   VIDEO_LICENCES,
397   VIDEO_RATE_TYPES
398 }