Refractor audit user identifier
[oweals/peertube.git] / server / controllers / api / config.ts
1 import * as express from 'express'
2 import { omit } from 'lodash'
3 import { ServerConfig, UserRight } from '../../../shared'
4 import { About } from '../../../shared/models/server/about.model'
5 import { CustomConfig } from '../../../shared/models/server/custom-config.model'
6 import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
7 import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
8 import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
9 import { customConfigUpdateValidator } from '../../middlewares/validators/config'
10 import { ClientHtml } from '../../lib/client-html'
11 import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger'
12 import { remove, writeJSON } from 'fs-extra'
13
14 const packageJSON = require('../../../../package.json')
15 const configRouter = express.Router()
16
17 const auditLogger = auditLoggerFactory('config')
18
19 configRouter.get('/about', getAbout)
20 configRouter.get('/',
21   asyncMiddleware(getConfig)
22 )
23
24 configRouter.get('/custom',
25   authenticate,
26   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
27   asyncMiddleware(getCustomConfig)
28 )
29 configRouter.put('/custom',
30   authenticate,
31   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
32   asyncMiddleware(customConfigUpdateValidator),
33   asyncMiddleware(updateCustomConfig)
34 )
35 configRouter.delete('/custom',
36   authenticate,
37   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
38   asyncMiddleware(deleteCustomConfig)
39 )
40
41 async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
42   const allowed = await isSignupAllowed()
43   const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
44
45   const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
46    .filter(key => CONFIG.TRANSCODING.ENABLED === CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
47    .map(r => parseInt(r, 10))
48
49   const json: ServerConfig = {
50     instance: {
51       name: CONFIG.INSTANCE.NAME,
52       shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
53       defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
54       defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
55       customizations: {
56         javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
57         css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
58       }
59     },
60     serverVersion: packageJSON.version,
61     signup: {
62       allowed,
63       allowedForCurrentIP,
64       requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
65     },
66     transcoding: {
67       enabledResolutions
68     },
69     import: {
70       videos: {
71         http: {
72           enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
73         },
74         torrent: {
75           enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
76         }
77       }
78     },
79     avatar: {
80       file: {
81         size: {
82           max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
83         },
84         extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
85       }
86     },
87     video: {
88       image: {
89         extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
90         size: {
91           max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
92         }
93       },
94       file: {
95         extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
96       }
97     },
98     videoCaption: {
99       file: {
100         size: {
101           max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
102         },
103         extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
104       }
105     },
106     user: {
107       videoQuota: CONFIG.USER.VIDEO_QUOTA,
108       videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
109     }
110   }
111
112   return res.json(json)
113 }
114
115 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
116   const about: About = {
117     instance: {
118       name: CONFIG.INSTANCE.NAME,
119       shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
120       description: CONFIG.INSTANCE.DESCRIPTION,
121       terms: CONFIG.INSTANCE.TERMS
122     }
123   }
124
125   return res.json(about).end()
126 }
127
128 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
129   const data = customConfig()
130
131   return res.json(data).end()
132 }
133
134 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
135   await remove(CONFIG.CUSTOM_FILE)
136
137   auditLogger.delete(getAuditIdFromRes(res), new CustomConfigAuditView(customConfig()))
138
139   reloadConfig()
140   ClientHtml.invalidCache()
141
142   const data = customConfig()
143
144   return res.json(data).end()
145 }
146
147 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
148   const toUpdate: CustomConfig = req.body
149   const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
150
151   // Force number conversion
152   toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
153   toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
154   toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
155   toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
156   toUpdate.user.videoQuotaDaily = parseInt('' + toUpdate.user.videoQuotaDaily, 10)
157   toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
158
159   // camelCase to snake_case key
160   const toUpdateJSON = omit(
161     toUpdate,
162     'user.videoQuota',
163     'instance.defaultClientRoute',
164     'instance.shortDescription',
165     'cache.videoCaptions',
166     'signup.requiresEmailVerification'
167   )
168   toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
169   toUpdateJSON.user['video_quota_daily'] = toUpdate.user.videoQuotaDaily
170   toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
171   toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
172   toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
173   toUpdateJSON.signup['requires_email_verification'] = toUpdate.signup.requiresEmailVerification
174
175   await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
176
177   reloadConfig()
178   ClientHtml.invalidCache()
179
180   const data = customConfig()
181
182   auditLogger.update(
183     getAuditIdFromRes(res),
184     new CustomConfigAuditView(data),
185     oldCustomConfigAuditKeys
186   )
187
188   return res.json(data).end()
189 }
190
191 // ---------------------------------------------------------------------------
192
193 export {
194   configRouter
195 }
196
197 // ---------------------------------------------------------------------------
198
199 function customConfig (): CustomConfig {
200   return {
201     instance: {
202       name: CONFIG.INSTANCE.NAME,
203       shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
204       description: CONFIG.INSTANCE.DESCRIPTION,
205       terms: CONFIG.INSTANCE.TERMS,
206       defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
207       defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
208       customizations: {
209         css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
210         javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
211       }
212     },
213     services: {
214       twitter: {
215         username: CONFIG.SERVICES.TWITTER.USERNAME,
216         whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
217       }
218     },
219     cache: {
220       previews: {
221         size: CONFIG.CACHE.PREVIEWS.SIZE
222       },
223       captions: {
224         size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
225       }
226     },
227     signup: {
228       enabled: CONFIG.SIGNUP.ENABLED,
229       limit: CONFIG.SIGNUP.LIMIT,
230       requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
231     },
232     admin: {
233       email: CONFIG.ADMIN.EMAIL
234     },
235     user: {
236       videoQuota: CONFIG.USER.VIDEO_QUOTA,
237       videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
238     },
239     transcoding: {
240       enabled: CONFIG.TRANSCODING.ENABLED,
241       threads: CONFIG.TRANSCODING.THREADS,
242       resolutions: {
243         '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
244         '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
245         '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
246         '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
247         '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
248       }
249     },
250     import: {
251       videos: {
252         http: {
253           enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
254         },
255         torrent: {
256           enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
257         }
258       }
259     }
260   }
261 }