Add follow tests
[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   JobState,
10   JobCategory
11 } from '../../shared/models'
12 import { VideoPrivacy } from '../../shared/models/videos/video-privacy.enum'
13 import { FollowState } from '../../shared/models/accounts/follow.model'
14
15 // ---------------------------------------------------------------------------
16
17 const LAST_MIGRATION_VERSION = 95
18
19 // ---------------------------------------------------------------------------
20
21 // API version
22 const API_VERSION = 'v1'
23
24 // Number of results by default for the pagination
25 const PAGINATION_COUNT_DEFAULT = 15
26
27 // Sortable columns per schema
28 const SEARCHABLE_COLUMNS = {
29   VIDEOS: [ 'name', 'magnetUri', 'host', 'account', 'tags' ]
30 }
31
32 // Sortable columns per schema
33 const SORTABLE_COLUMNS = {
34   USERS: [ 'id', 'username', 'createdAt' ],
35   VIDEO_ABUSES: [ 'id', 'createdAt' ],
36   VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
37   VIDEOS: [ 'name', 'duration', 'createdAt', 'views', 'likes' ],
38   BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
39   FOLLOWERS: [ 'createdAt' ],
40   FOLLOWING: [ '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     LOG_DIR: join(root(), config.get<string>('storage.logs')),
63     VIDEOS_DIR: join(root(), config.get<string>('storage.videos')),
64     THUMBNAILS_DIR: join(root(), config.get<string>('storage.thumbnails')),
65     PREVIEWS_DIR: join(root(), config.get<string>('storage.previews')),
66     TORRENTS_DIR: join(root(), config.get<string>('storage.torrents')),
67     CACHE_DIR: join(root(), config.get<string>('storage.cache'))
68   },
69   WEBSERVER: {
70     SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
71     WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
72     HOSTNAME: config.get<string>('webserver.hostname'),
73     PORT: config.get<number>('webserver.port'),
74     URL: '',
75     HOST: ''
76   },
77   ADMIN: {
78     EMAIL: config.get<string>('admin.email')
79   },
80   SIGNUP: {
81     ENABLED: config.get<boolean>('signup.enabled'),
82     LIMIT: config.get<number>('signup.limit')
83   },
84   USER: {
85     VIDEO_QUOTA: config.get<number>('user.video_quota')
86   },
87   TRANSCODING: {
88     ENABLED: config.get<boolean>('transcoding.enabled'),
89     THREADS: config.get<number>('transcoding.threads'),
90     RESOLUTIONS: {
91       '240' : config.get<boolean>('transcoding.resolutions.240p'),
92       '360': config.get<boolean>('transcoding.resolutions.360p'),
93       '480': config.get<boolean>('transcoding.resolutions.480p'),
94       '720': config.get<boolean>('transcoding.resolutions.720p'),
95       '1080': config.get<boolean>('transcoding.resolutions.1080p')
96     }
97   },
98   CACHE: {
99     PREVIEWS: {
100       SIZE: config.get<number>('cache.previews.size')
101     }
102   }
103 }
104 CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
105 CONFIG.WEBSERVER.HOST = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
106
107 // ---------------------------------------------------------------------------
108
109 const CONSTRAINTS_FIELDS = {
110   USERS: {
111     USERNAME: { min: 3, max: 20 }, // Length
112     PASSWORD: { min: 6, max: 255 }, // Length
113     VIDEO_QUOTA: { min: -1 }
114   },
115   VIDEO_ABUSES: {
116     REASON: { min: 2, max: 300 } // Length
117   },
118   VIDEO_CHANNELS: {
119     NAME: { min: 3, max: 120 }, // Length
120     DESCRIPTION: { min: 3, max: 250 }, // Length
121     URL: { min: 3, max: 2000 } // Length
122   },
123   VIDEOS: {
124     NAME: { min: 3, max: 120 }, // Length
125     TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
126     DESCRIPTION: { min: 3, max: 3000 }, // Length
127     EXTNAME: [ '.mp4', '.ogv', '.webm' ],
128     INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
129     DURATION: { min: 1, max: 7200 }, // Number
130     TAGS: { min: 0, max: 5 }, // Number of total tags
131     TAG: { min: 2, max: 30 }, // Length
132     THUMBNAIL: { min: 2, max: 30 },
133     THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
134     VIEWS: { min: 0 },
135     LIKES: { min: 0 },
136     DISLIKES: { min: 0 },
137     FILE_SIZE: { min: 10, max: 1024 * 1024 * 1024 * 10 /* 10Go */ },
138     URL: { min: 3, max: 2000 } // Length
139   },
140   ACCOUNTS: {
141     PUBLIC_KEY: { min: 10, max: 5000 }, // Length
142     PRIVATE_KEY: { min: 10, max: 5000 }, // Length
143     URL: { min: 3, max: 2000 } // Length
144   },
145   VIDEO_EVENTS: {
146     COUNT: { min: 0 }
147   }
148 }
149
150 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
151   LIKE: 'like',
152   DISLIKE: 'dislike'
153 }
154
155 const VIDEO_CATEGORIES = {
156   1: 'Music',
157   2: 'Films',
158   3: 'Vehicles',
159   4: 'Art',
160   5: 'Sports',
161   6: 'Travels',
162   7: 'Gaming',
163   8: 'People',
164   9: 'Comedy',
165   10: 'Entertainment',
166   11: 'News',
167   12: 'How To',
168   13: 'Education',
169   14: 'Activism',
170   15: 'Science & Technology',
171   16: 'Animals',
172   17: 'Kids',
173   18: 'Food'
174 }
175
176 // See https://creativecommons.org/licenses/?lang=en
177 const VIDEO_LICENCES = {
178   1: 'Attribution',
179   2: 'Attribution - Share Alike',
180   3: 'Attribution - No Derivatives',
181   4: 'Attribution - Non Commercial',
182   5: 'Attribution - Non Commercial - Share Alike',
183   6: 'Attribution - Non Commercial - No Derivatives',
184   7: 'Public Domain Dedication'
185 }
186
187 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
188 const VIDEO_LANGUAGES = {
189   1: 'English',
190   2: 'Spanish',
191   3: 'Mandarin',
192   4: 'Hindi',
193   5: 'Arabic',
194   6: 'Portuguese',
195   7: 'Bengali',
196   8: 'Russian',
197   9: 'Japanese',
198   10: 'Punjabi',
199   11: 'German',
200   12: 'Korean',
201   13: 'French',
202   14: 'Italian'
203 }
204
205 const VIDEO_PRIVACIES = {
206   [VideoPrivacy.PUBLIC]: 'Public',
207   [VideoPrivacy.UNLISTED]: 'Unlisted',
208   [VideoPrivacy.PRIVATE]: 'Private'
209 }
210
211 const VIDEO_MIMETYPE_EXT = {
212   'video/webm': '.webm',
213   'video/ogg': '.ogv',
214   'video/mp4': '.mp4'
215 }
216
217 // ---------------------------------------------------------------------------
218
219 // Score a server has when we create it as a friend
220 const FRIEND_SCORE = {
221   BASE: 100,
222   MAX: 1000
223 }
224
225 const SERVER_ACCOUNT_NAME = 'peertube'
226
227 const ACTIVITY_PUB = {
228   ACCEPT_HEADER: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
229   PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
230   COLLECTION_ITEMS_PER_PAGE: 10,
231   URL_MIME_TYPES: {
232     VIDEO: [ 'video/mp4', 'video/webm', 'video/ogg' ], // TODO: Merge with VIDEO_MIMETYPE_EXT
233     TORRENT: [ 'application/x-bittorrent' ],
234     MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
235   }
236 }
237
238 // ---------------------------------------------------------------------------
239
240 // Number of points we add/remove from a friend after a successful/bad request
241 const SERVERS_SCORE = {
242   PENALTY: -10,
243   BONUS: 10
244 }
245
246 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
247   PENDING: 'pending',
248   ACCEPTED: 'accepted'
249 }
250
251 const REMOTE_SCHEME = {
252   HTTP: 'https',
253   WS: 'wss'
254 }
255
256 const JOB_STATES: { [ id: string ]: JobState } = {
257   PENDING: 'pending',
258   PROCESSING: 'processing',
259   ERROR: 'error',
260   SUCCESS: 'success'
261 }
262 const JOB_CATEGORIES: { [ id: string ]: JobCategory } = {
263   TRANSCODING: 'transcoding',
264   ACTIVITYPUB_HTTP: 'activitypub-http'
265 }
266 // How many maximum jobs we fetch from the database per cycle
267 const JOBS_FETCH_LIMIT_PER_CYCLE = {
268   transcoding: 10,
269   httpRequest: 20
270 }
271 // 1 minutes
272 let JOBS_FETCHING_INTERVAL = 60000
273
274 // ---------------------------------------------------------------------------
275
276 const PRIVATE_RSA_KEY_SIZE = 2048
277
278 // Password encryption
279 const BCRYPT_SALT_SIZE = 10
280
281 // ---------------------------------------------------------------------------
282
283 // Express static paths (router)
284 const STATIC_PATHS = {
285   PREVIEWS: '/static/previews/',
286   THUMBNAILS: '/static/thumbnails/',
287   TORRENTS: '/static/torrents/',
288   WEBSEED: '/static/webseed/'
289 }
290
291 // Cache control
292 let STATIC_MAX_AGE = '30d'
293
294 // Videos thumbnail size
295 const THUMBNAILS_SIZE = {
296   width: 200,
297   height: 110
298 }
299 const PREVIEWS_SIZE = {
300   width: 560,
301   height: 315
302 }
303
304 const EMBED_SIZE = {
305   width: 560,
306   height: 315
307 }
308
309 // Sub folders of cache directory
310 const CACHE = {
311   DIRECTORIES: {
312     PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
313   }
314 }
315
316 // ---------------------------------------------------------------------------
317
318 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
319
320 // ---------------------------------------------------------------------------
321
322 // Special constants for a test instance
323 if (isTestInstance() === true) {
324   CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
325   FRIEND_SCORE.BASE = 20
326   JOBS_FETCHING_INTERVAL = 1000
327   REMOTE_SCHEME.HTTP = 'http'
328   REMOTE_SCHEME.WS = 'ws'
329   STATIC_MAX_AGE = '0'
330 }
331
332 // ---------------------------------------------------------------------------
333
334 export {
335   API_VERSION,
336   BCRYPT_SALT_SIZE,
337   CACHE,
338   CONFIG,
339   CONSTRAINTS_FIELDS,
340   EMBED_SIZE,
341   FRIEND_SCORE,
342   JOB_STATES,
343   JOBS_FETCH_LIMIT_PER_CYCLE,
344   JOBS_FETCHING_INTERVAL,
345   JOB_CATEGORIES,
346   LAST_MIGRATION_VERSION,
347   OAUTH_LIFETIME,
348   OPENGRAPH_AND_OEMBED_COMMENT,
349   PAGINATION_COUNT_DEFAULT,
350   SERVERS_SCORE,
351   PREVIEWS_SIZE,
352   REMOTE_SCHEME,
353   FOLLOW_STATES,
354   SEARCHABLE_COLUMNS,
355   SERVER_ACCOUNT_NAME,
356   PRIVATE_RSA_KEY_SIZE,
357   SORTABLE_COLUMNS,
358   STATIC_MAX_AGE,
359   STATIC_PATHS,
360   ACTIVITY_PUB,
361   THUMBNAILS_SIZE,
362   VIDEO_CATEGORIES,
363   VIDEO_LANGUAGES,
364   VIDEO_PRIVACIES,
365   VIDEO_LICENCES,
366   VIDEO_RATE_TYPES,
367   VIDEO_MIMETYPE_EXT
368 }