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