Server: reorganize constant file
[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 // API version
9 const API_VERSION = 'v1'
10
11 // Number of results by default for the pagination
12 const PAGINATION_COUNT_DEFAULT = 15
13
14 // Sortable columns per schema
15 const SEARCHABLE_COLUMNS = {
16   VIDEOS: [ 'name', 'magnetUri', 'podUrl', 'author', 'tags' ]
17 }
18
19 // Sortable columns per schema
20 const SORTABLE_COLUMNS = {
21   USERS: [ 'username', '-username', 'createdDate', '-createdDate' ],
22   VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ]
23 }
24
25 const OAUTH_LIFETIME = {
26   ACCESS_TOKEN: 3600 * 4, // 4 hours
27   REFRESH_TOKEN: 1209600 // 2 weeks
28 }
29
30 // ---------------------------------------------------------------------------
31
32 const CONFIG = {
33   DATABASE: {
34     DBNAME: 'peertube' + config.get('database.suffix'),
35     HOST: config.get('database.host'),
36     PORT: config.get('database.port')
37   },
38   ELECTRON: {
39     DEBUG: config.get('electron.debug')
40   },
41   STORAGE: {
42     CERT_DIR: path.join(__dirname, '..', '..', config.get('storage.certs')),
43     LOG_DIR: path.join(__dirname, '..', '..', config.get('storage.logs')),
44     UPLOAD_DIR: path.join(__dirname, '..', '..', config.get('storage.uploads')),
45     THUMBNAILS_DIR: path.join(__dirname, '..', '..', config.get('storage.thumbnails'))
46   },
47   WEBSERVER: {
48     SCHEME: config.get('webserver.https') === true ? 'https' : 'http',
49     HOST: config.get('webserver.host'),
50     PORT: config.get('webserver.port')
51   }
52 }
53 CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOST + ':' + 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 const LAST_MONGO_SCHEMA_VERSION = 15
99
100 // ---------------------------------------------------------------------------
101
102 // Number of points we add/remove from a friend after a successful/bad request
103 const PODS_SCORE = {
104   MALUS: -10,
105   BONUS: 10
106 }
107
108 // Time to wait between requests to the friends (10 min)
109 let REQUESTS_INTERVAL = 600000
110
111 // Number of requests in parallel we can make
112 const REQUESTS_IN_PARALLEL = 10
113
114 // How many requests we put in request
115 const REQUESTS_LIMIT = 10
116
117 // Number of requests to retry for replay requests module
118 const RETRY_REQUESTS = 5
119
120 // ---------------------------------------------------------------------------
121
122 // Password encryption
123 const BCRYPT_SALT_SIZE = 10
124
125 // Videos thumbnail size
126 const THUMBNAILS_SIZE = '200x110'
127
128 // Path for access to thumbnails with express router
129 const THUMBNAILS_STATIC_PATH = '/static/thumbnails'
130
131 const USER_ROLES = {
132   ADMIN: 'admin',
133   USER: 'user'
134 }
135
136 // Seeds in parallel we send to electron when "seed all"
137 // Once a video is in seeding state we seed another video etc
138 const SEEDS_IN_PARALLEL = 3
139
140 // ---------------------------------------------------------------------------
141
142 // Special constants for a test instance
143 if (isTestInstance() === true) {
144   CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
145   FRIEND_SCORE.BASE = 20
146   REQUESTS_INTERVAL = 10000
147 }
148
149 // ---------------------------------------------------------------------------
150
151 module.exports = {
152   API_VERSION,
153   BCRYPT_SALT_SIZE,
154   CONFIG,
155   CONSTRAINTS_FIELDS,
156   FRIEND_SCORE,
157   LAST_MONGO_SCHEMA_VERSION,
158   MONGO_MIGRATION_SCRIPTS,
159   OAUTH_LIFETIME,
160   PAGINATION_COUNT_DEFAULT,
161   PODS_SCORE,
162   REQUESTS_IN_PARALLEL,
163   REQUESTS_INTERVAL,
164   REQUESTS_LIMIT,
165   RETRY_REQUESTS,
166   SEARCHABLE_COLUMNS,
167   SEEDS_IN_PARALLEL,
168   SORTABLE_COLUMNS,
169   THUMBNAILS_SIZE,
170   THUMBNAILS_STATIC_PATH,
171   USER_ROLES
172 }
173
174 // ---------------------------------------------------------------------------
175
176 function isTestInstance () {
177   return (process.env.NODE_ENV === 'test')
178 }