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