Server: add endpoint in requests
[oweals/peertube.git] / server / initializers / constants.js
1 'use strict'
2
3 const config = require('config')
4 const maxBy = require('lodash/maxBy')
5 const path = require('path')
6
7 // ---------------------------------------------------------------------------
8
9 // API version
10 const API_VERSION = 'v1'
11
12 // Number of results by default for the pagination
13 const PAGINATION_COUNT_DEFAULT = 15
14
15 // Sortable columns per schema
16 const SEARCHABLE_COLUMNS = {
17   VIDEOS: [ 'name', 'magnetUri', 'podUrl', 'author', 'tags' ]
18 }
19
20 // Sortable columns per schema
21 const SORTABLE_COLUMNS = {
22   USERS: [ 'username', '-username', 'createdDate', '-createdDate' ],
23   VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ]
24 }
25
26 const OAUTH_LIFETIME = {
27   ACCESS_TOKEN: 3600 * 4, // 4 hours
28   REFRESH_TOKEN: 1209600 // 2 weeks
29 }
30
31 // ---------------------------------------------------------------------------
32
33 const CONFIG = {
34   DATABASE: {
35     DBNAME: 'peertube' + config.get('database.suffix'),
36     HOSTNAME: config.get('database.hostname'),
37     PORT: config.get('database.port')
38   },
39   STORAGE: {
40     CERT_DIR: path.join(__dirname, '..', '..', config.get('storage.certs')),
41     LOG_DIR: path.join(__dirname, '..', '..', config.get('storage.logs')),
42     VIDEOS_DIR: path.join(__dirname, '..', '..', config.get('storage.videos')),
43     THUMBNAILS_DIR: path.join(__dirname, '..', '..', config.get('storage.thumbnails')),
44     TORRENTS_DIR: path.join(__dirname, '..', '..', config.get('storage.torrents'))
45   },
46   WEBSERVER: {
47     SCHEME: config.get('webserver.https') === true ? 'https' : 'http',
48     WS: config.get('webserver.https') === true ? 'wss' : 'ws',
49     HOSTNAME: config.get('webserver.hostname'),
50     PORT: config.get('webserver.port')
51   }
52 }
53 CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
54
55 // ---------------------------------------------------------------------------
56
57 const CONSTRAINTS_FIELDS = {
58   USERS: {
59     USERNAME: { min: 3, max: 20 }, // Length
60     PASSWORD: { min: 6, max: 255 } // Length
61   },
62   VIDEOS: {
63     NAME: { min: 3, max: 50 }, // Length
64     DESCRIPTION: { min: 3, max: 250 }, // Length
65     MAGNET_URI: { min: 10 }, // Length
66     DURATION: { min: 1, max: 7200 }, // Number
67     TAGS: { min: 1, max: 3 }, // Number of total tags
68     TAG: { min: 2, max: 10 }, // Length
69     THUMBNAIL: { min: 2, max: 30 },
70     THUMBNAIL64: { min: 0, max: 20000 } // Bytes
71   }
72 }
73
74 // ---------------------------------------------------------------------------
75
76 // Score a pod has when we create it as a friend
77 const FRIEND_SCORE = {
78   BASE: 100,
79   MAX: 1000
80 }
81
82 // ---------------------------------------------------------------------------
83
84 const MONGO_MIGRATION_SCRIPTS = [
85   {
86     script: '0005-create-application',
87     version: 5
88   },
89   {
90     script: '0010-users-password',
91     version: 10
92   },
93   {
94     script: '0015-admin-role',
95     version: 15
96   },
97   {
98     script: '0020-requests-endpoint',
99     version: 20
100   }
101 ]
102 const LAST_MONGO_SCHEMA_VERSION = (maxBy(MONGO_MIGRATION_SCRIPTS, 'version'))['version']
103
104 // ---------------------------------------------------------------------------
105
106 // Number of points we add/remove from a friend after a successful/bad request
107 const PODS_SCORE = {
108   MALUS: -10,
109   BONUS: 10
110 }
111
112 // Time to wait between requests to the friends (10 min)
113 let REQUESTS_INTERVAL = 600000
114
115 // Number of requests in parallel we can make
116 const REQUESTS_IN_PARALLEL = 10
117
118 // How many requests we put in request
119 const REQUESTS_LIMIT = 10
120
121 // Number of requests to retry for replay requests module
122 const RETRY_REQUESTS = 5
123
124 const REQUEST_ENDPOINTS = {
125   VIDEOS: 'videos'
126 }
127
128 // ---------------------------------------------------------------------------
129
130 // Password encryption
131 const BCRYPT_SALT_SIZE = 10
132
133 // Express static paths (router)
134 const STATIC_PATHS = {
135   THUMBNAILS: '/static/thumbnails',
136   TORRENTS: '/static/torrents/',
137   WEBSEED: '/static/webseed/'
138 }
139
140 // Cache control
141 let STATIC_MAX_AGE = '30d'
142
143 // Videos thumbnail size
144 const THUMBNAILS_SIZE = '200x110'
145
146 const USER_ROLES = {
147   ADMIN: 'admin',
148   USER: 'user'
149 }
150
151 // ---------------------------------------------------------------------------
152
153 // Special constants for a test instance
154 if (isTestInstance() === true) {
155   CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
156   FRIEND_SCORE.BASE = 20
157   REQUESTS_INTERVAL = 10000
158   STATIC_MAX_AGE = 0
159 }
160
161 // ---------------------------------------------------------------------------
162
163 module.exports = {
164   API_VERSION,
165   BCRYPT_SALT_SIZE,
166   CONFIG,
167   CONSTRAINTS_FIELDS,
168   FRIEND_SCORE,
169   LAST_MONGO_SCHEMA_VERSION,
170   MONGO_MIGRATION_SCRIPTS,
171   OAUTH_LIFETIME,
172   PAGINATION_COUNT_DEFAULT,
173   PODS_SCORE,
174   REQUEST_ENDPOINTS,
175   REQUESTS_IN_PARALLEL,
176   REQUESTS_INTERVAL,
177   REQUESTS_LIMIT,
178   RETRY_REQUESTS,
179   SEARCHABLE_COLUMNS,
180   SORTABLE_COLUMNS,
181   STATIC_MAX_AGE,
182   STATIC_PATHS,
183   THUMBNAILS_SIZE,
184   USER_ROLES
185 }
186
187 // ---------------------------------------------------------------------------
188
189 function isTestInstance () {
190   return (process.env.NODE_ENV === 'test')
191 }