Server: add config endpoint
[oweals/peertube.git] / server / initializers / constants.js
1 'use strict'
2
3 const config = require('config')
4 const path = require('path')
5
6 // ---------------------------------------------------------------------------
7
8 const LAST_MIGRATION_VERSION = 25
9
10 // ---------------------------------------------------------------------------
11
12 // API version
13 const API_VERSION = 'v1'
14
15 // Number of results by default for the pagination
16 const PAGINATION_COUNT_DEFAULT = 15
17
18 // Sortable columns per schema
19 const SEARCHABLE_COLUMNS = {
20   VIDEOS: [ 'name', 'magnetUri', 'host', 'author', 'tags' ]
21 }
22
23 // Sortable columns per schema
24 const SORTABLE_COLUMNS = {
25   USERS: [ 'id', 'username', 'createdAt' ],
26   VIDEO_ABUSES: [ 'id', 'createdAt' ],
27   VIDEOS: [ 'name', 'duration', 'createdAt', 'views', 'likes' ]
28 }
29
30 const OAUTH_LIFETIME = {
31   ACCESS_TOKEN: 3600 * 4, // 4 hours
32   REFRESH_TOKEN: 1209600 // 2 weeks
33 }
34
35 // ---------------------------------------------------------------------------
36
37 const CONFIG = {
38   LISTEN: {
39     PORT: config.get('listen.port')
40   },
41   DATABASE: {
42     DBNAME: 'peertube' + config.get('database.suffix'),
43     HOSTNAME: config.get('database.hostname'),
44     PORT: config.get('database.port'),
45     USERNAME: config.get('database.username'),
46     PASSWORD: config.get('database.password')
47   },
48   STORAGE: {
49     CERT_DIR: path.join(__dirname, '..', '..', config.get('storage.certs')),
50     LOG_DIR: path.join(__dirname, '..', '..', config.get('storage.logs')),
51     VIDEOS_DIR: path.join(__dirname, '..', '..', config.get('storage.videos')),
52     THUMBNAILS_DIR: path.join(__dirname, '..', '..', config.get('storage.thumbnails')),
53     PREVIEWS_DIR: path.join(__dirname, '..', '..', config.get('storage.previews')),
54     TORRENTS_DIR: path.join(__dirname, '..', '..', config.get('storage.torrents'))
55   },
56   WEBSERVER: {
57     SCHEME: config.get('webserver.https') === true ? 'https' : 'http',
58     WS: config.get('webserver.https') === true ? 'wss' : 'ws',
59     HOSTNAME: config.get('webserver.hostname'),
60     PORT: config.get('webserver.port')
61   },
62   ADMIN: {
63     EMAIL: config.get('admin.email')
64   },
65   SIGNUP: {
66     ENABLED: config.get('signup.enabled')
67   }
68 }
69 CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
70 CONFIG.WEBSERVER.HOST = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
71
72 // ---------------------------------------------------------------------------
73
74 const CONSTRAINTS_FIELDS = {
75   USERS: {
76     USERNAME: { min: 3, max: 20 }, // Length
77     PASSWORD: { min: 6, max: 255 } // Length
78   },
79   VIDEO_ABUSES: {
80     REASON: { min: 2, max: 300 } // Length
81   },
82   VIDEOS: {
83     NAME: { min: 3, max: 50 }, // Length
84     DESCRIPTION: { min: 3, max: 250 }, // Length
85     EXTNAME: [ '.mp4', '.ogv', '.webm' ],
86     INFO_HASH: { min: 40, max: 40 }, // Length, infohash is 20 bytes length but we represent it in hexa so 20 * 2
87     DURATION: { min: 1, max: 7200 }, // Number
88     TAGS: { min: 1, max: 3 }, // Number of total tags
89     TAG: { min: 2, max: 10 }, // Length
90     THUMBNAIL: { min: 2, max: 30 },
91     THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
92     VIEWS: { min: 0 },
93     LIKES: { min: 0 },
94     DISLIKES: { min: 0 }
95   },
96   VIDEO_EVENTS: {
97     COUNT: { min: 0 }
98   }
99 }
100
101 const VIDEO_RATE_TYPES = {
102   LIKE: 'like',
103   DISLIKE: 'dislike'
104 }
105
106 // ---------------------------------------------------------------------------
107
108 // Score a pod has when we create it as a friend
109 const FRIEND_SCORE = {
110   BASE: 100,
111   MAX: 1000
112 }
113
114 // ---------------------------------------------------------------------------
115
116 // Number of points we add/remove from a friend after a successful/bad request
117 const PODS_SCORE = {
118   MALUS: -10,
119   BONUS: 10
120 }
121
122 // Time to wait between requests to the friends (10 min)
123 let REQUESTS_INTERVAL = 600000
124
125 // Number of requests in parallel we can make
126 const REQUESTS_IN_PARALLEL = 10
127
128 // To how many pods we send requests
129 const REQUESTS_LIMIT_PODS = 10
130 // How many requests we send to a pod per interval
131 const REQUESTS_LIMIT_PER_POD = 5
132
133 const REQUESTS_VIDEO_QADU_LIMIT_PODS = 10
134 // The QADU requests are not big
135 const REQUESTS_VIDEO_QADU_LIMIT_PER_POD = 50
136
137 const REQUESTS_VIDEO_EVENT_LIMIT_PODS = 10
138 // The EVENTS requests are not big
139 const REQUESTS_VIDEO_EVENT_LIMIT_PER_POD = 50
140
141 // Number of requests to retry for replay requests module
142 const RETRY_REQUESTS = 5
143
144 const REQUEST_ENDPOINTS = {
145   VIDEOS: 'videos'
146 }
147
148 const REQUEST_ENDPOINT_ACTIONS = {}
149 REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] = {
150   ADD: 'add',
151   UPDATE: 'update',
152   REMOVE: 'remove',
153   REPORT_ABUSE: 'report-abuse'
154 }
155
156 const REQUEST_VIDEO_QADU_ENDPOINT = 'videos/qadu'
157 const REQUEST_VIDEO_EVENT_ENDPOINT = 'videos/events'
158
159 const REQUEST_VIDEO_QADU_TYPES = {
160   LIKES: 'likes',
161   DISLIKES: 'dislikes',
162   VIEWS: 'views'
163 }
164
165 const REQUEST_VIDEO_EVENT_TYPES = {
166   LIKES: 'likes',
167   DISLIKES: 'dislikes',
168   VIEWS: 'views'
169 }
170
171 const REMOTE_SCHEME = {
172   HTTP: 'https',
173   WS: 'wss'
174 }
175
176 // ---------------------------------------------------------------------------
177
178 const PRIVATE_CERT_NAME = 'peertube.key.pem'
179 const PUBLIC_CERT_NAME = 'peertube.pub'
180 const SIGNATURE_ALGORITHM = 'RSA-SHA256'
181 const SIGNATURE_ENCODING = 'hex'
182
183 // Password encryption
184 const BCRYPT_SALT_SIZE = 10
185
186 // ---------------------------------------------------------------------------
187
188 // Express static paths (router)
189 const STATIC_PATHS = {
190   PREVIEWS: '/static/previews/',
191   THUMBNAILS: '/static/thumbnails/',
192   TORRENTS: '/static/torrents/',
193   WEBSEED: '/static/webseed/'
194 }
195
196 // Cache control
197 let STATIC_MAX_AGE = '30d'
198
199 // Videos thumbnail size
200 const THUMBNAILS_SIZE = '200x110'
201 const PREVIEWS_SIZE = '640x480'
202
203 // ---------------------------------------------------------------------------
204
205 const USER_ROLES = {
206   ADMIN: 'admin',
207   USER: 'user'
208 }
209
210 // ---------------------------------------------------------------------------
211
212 // Special constants for a test instance
213 if (isTestInstance() === true) {
214   CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
215   FRIEND_SCORE.BASE = 20
216   REQUESTS_INTERVAL = 10000
217   REMOTE_SCHEME.HTTP = 'http'
218   REMOTE_SCHEME.WS = 'ws'
219   STATIC_MAX_AGE = 0
220 }
221
222 // ---------------------------------------------------------------------------
223
224 module.exports = {
225   API_VERSION,
226   BCRYPT_SALT_SIZE,
227   CONFIG,
228   CONSTRAINTS_FIELDS,
229   FRIEND_SCORE,
230   LAST_MIGRATION_VERSION,
231   OAUTH_LIFETIME,
232   PAGINATION_COUNT_DEFAULT,
233   PODS_SCORE,
234   PREVIEWS_SIZE,
235   PRIVATE_CERT_NAME,
236   PUBLIC_CERT_NAME,
237   REMOTE_SCHEME,
238   REQUEST_ENDPOINT_ACTIONS,
239   REQUEST_ENDPOINTS,
240   REQUEST_VIDEO_EVENT_ENDPOINT,
241   REQUEST_VIDEO_EVENT_TYPES,
242   REQUEST_VIDEO_QADU_ENDPOINT,
243   REQUEST_VIDEO_QADU_TYPES,
244   REQUESTS_IN_PARALLEL,
245   REQUESTS_INTERVAL,
246   REQUESTS_LIMIT_PER_POD,
247   REQUESTS_LIMIT_PODS,
248   REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
249   REQUESTS_VIDEO_EVENT_LIMIT_PODS,
250   REQUESTS_VIDEO_QADU_LIMIT_PER_POD,
251   REQUESTS_VIDEO_QADU_LIMIT_PODS,
252   RETRY_REQUESTS,
253   SEARCHABLE_COLUMNS,
254   SIGNATURE_ALGORITHM,
255   SIGNATURE_ENCODING,
256   SORTABLE_COLUMNS,
257   STATIC_MAX_AGE,
258   STATIC_PATHS,
259   THUMBNAILS_SIZE,
260   USER_ROLES,
261   VIDEO_RATE_TYPES
262 }
263
264 // ---------------------------------------------------------------------------
265
266 // This method exists in utils module but we want to let the constants module independent
267 function isTestInstance () {
268   return (process.env.NODE_ENV === 'test')
269 }