]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/users/me.ts
Handle email update on server
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users / me.ts
CommitLineData
d03cd8bb
C
1import * as express from 'express'
2import 'multer'
3import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../../shared'
4import { getFormattedObjects } from '../../../helpers/utils'
74dc3bca 5import { MIMETYPES } from '../../../initializers/constants'
d03cd8bb
C
6import { sendUpdateActor } from '../../../lib/activitypub/send'
7import {
993cef4b
C
8 asyncMiddleware,
9 asyncRetryTransactionMiddleware,
d03cd8bb
C
10 authenticate,
11 paginationValidator,
12 setDefaultPagination,
13 setDefaultSort,
14 usersUpdateMeValidator,
15 usersVideoRatingValidator
16} from '../../../middlewares'
cf405589 17import { deleteMeValidator, videoImportsSortValidator, videosSortValidator } from '../../../middlewares/validators'
d03cd8bb
C
18import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
19import { UserModel } from '../../../models/account/user'
20import { VideoModel } from '../../../models/video/video'
21import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type'
cf405589 22import { createReqFiles } from '../../../helpers/express-utils'
d03cd8bb
C
23import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model'
24import { updateAvatarValidator } from '../../../middlewares/validators/avatar'
25import { updateActorAvatarFile } from '../../../lib/avatar'
993cef4b 26import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger'
d03cd8bb 27import { VideoImportModel } from '../../../models/video/video-import'
91411dba 28import { AccountModel } from '../../../models/account/account'
6dd9de95 29import { CONFIG } from '../../../initializers/config'
74dc3bca 30import { sequelizeTypescript } from '../../../initializers/database'
d1ab89de 31import { sendVerifyUserEmail } from '../../../lib/user'
d03cd8bb
C
32
33const auditLogger = auditLoggerFactory('users-me')
34
14e2014a 35const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR })
d03cd8bb
C
36
37const meRouter = express.Router()
38
39meRouter.get('/me',
40 authenticate,
41 asyncMiddleware(getUserInformation)
42)
43meRouter.delete('/me',
44 authenticate,
45 asyncMiddleware(deleteMeValidator),
46 asyncMiddleware(deleteMe)
47)
48
49meRouter.get('/me/video-quota-used',
50 authenticate,
51 asyncMiddleware(getUserVideoQuotaUsed)
52)
53
54meRouter.get('/me/videos/imports',
55 authenticate,
56 paginationValidator,
57 videoImportsSortValidator,
58 setDefaultSort,
59 setDefaultPagination,
60 asyncMiddleware(getUserVideoImports)
61)
62
63meRouter.get('/me/videos',
64 authenticate,
65 paginationValidator,
66 videosSortValidator,
67 setDefaultSort,
68 setDefaultPagination,
69 asyncMiddleware(getUserVideos)
70)
71
72meRouter.get('/me/videos/:videoId/rating',
73 authenticate,
74 asyncMiddleware(usersVideoRatingValidator),
75 asyncMiddleware(getUserVideoRating)
76)
77
78meRouter.put('/me',
79 authenticate,
a890d1e0 80 asyncMiddleware(usersUpdateMeValidator),
176e2114 81 asyncRetryTransactionMiddleware(updateMe)
d03cd8bb
C
82)
83
84meRouter.post('/me/avatar/pick',
85 authenticate,
86 reqAvatarFile,
87 updateAvatarValidator,
176e2114 88 asyncRetryTransactionMiddleware(updateMyAvatar)
d03cd8bb
C
89)
90
91// ---------------------------------------------------------------------------
92
93export {
94 meRouter
95}
96
97// ---------------------------------------------------------------------------
98
dae86118
C
99async function getUserVideos (req: express.Request, res: express.Response) {
100 const user = res.locals.oauth.token.User
d03cd8bb
C
101 const resultList = await VideoModel.listUserVideosForApi(
102 user.Account.id,
103 req.query.start as number,
104 req.query.count as number,
105 req.query.sort as VideoSortField
106 )
107
108 const additionalAttributes = {
109 waitTranscoding: true,
110 state: true,
111 scheduledUpdate: true,
112 blacklistInfo: true
113 }
114 return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes }))
115}
116
dae86118
C
117async function getUserVideoImports (req: express.Request, res: express.Response) {
118 const user = res.locals.oauth.token.User
d03cd8bb
C
119 const resultList = await VideoImportModel.listUserVideoImportsForApi(
120 user.id,
121 req.query.start as number,
122 req.query.count as number,
123 req.query.sort
124 )
125
126 return res.json(getFormattedObjects(resultList.data, resultList.total))
127}
128
dae86118 129async function getUserInformation (req: express.Request, res: express.Response) {
d03cd8bb
C
130 // We did not load channels in res.locals.user
131 const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
132
1eddc9a7 133 return res.json(user.toFormattedJSON({}))
d03cd8bb
C
134}
135
dae86118 136async function getUserVideoQuotaUsed (req: express.Request, res: express.Response) {
d03cd8bb
C
137 // We did not load channels in res.locals.user
138 const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
139 const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user)
bee0abff 140 const videoQuotaUsedDaily = await UserModel.getOriginalVideoFileTotalDailyFromUser(user)
d03cd8bb
C
141
142 const data: UserVideoQuota = {
bee0abff
FA
143 videoQuotaUsed,
144 videoQuotaUsedDaily
d03cd8bb
C
145 }
146 return res.json(data)
147}
148
dae86118 149async function getUserVideoRating (req: express.Request, res: express.Response) {
627621c1 150 const videoId = res.locals.video.id
d03cd8bb
C
151 const accountId = +res.locals.oauth.token.User.Account.id
152
153 const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null)
154 const rating = ratingObj ? ratingObj.type : 'none'
155
156 const json: FormattedUserVideoRate = {
157 videoId,
158 rating
159 }
06a05d5f 160 return res.json(json)
d03cd8bb
C
161}
162
163async function deleteMe (req: express.Request, res: express.Response) {
dae86118 164 const user = res.locals.oauth.token.User
d03cd8bb
C
165
166 await user.destroy()
167
1eddc9a7 168 auditLogger.delete(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON({})))
d03cd8bb
C
169
170 return res.sendStatus(204)
171}
172
b426edd4 173async function updateMe (req: express.Request, res: express.Response) {
d03cd8bb 174 const body: UserUpdateMe = req.body
d1ab89de 175 let sendVerificationEmail = false
d03cd8bb 176
dae86118 177 const user = res.locals.oauth.token.user
1eddc9a7 178 const oldUserAuditView = new UserAuditView(user.toFormattedJSON({}))
d03cd8bb
C
179
180 if (body.password !== undefined) user.password = body.password
d03cd8bb 181 if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy
ed638e53 182 if (body.webTorrentEnabled !== undefined) user.webTorrentEnabled = body.webTorrentEnabled
d03cd8bb 183 if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
8b9a525a 184 if (body.videosHistoryEnabled !== undefined) user.videosHistoryEnabled = body.videosHistoryEnabled
d03cd8bb 185
d1ab89de
C
186 if (body.email !== undefined) {
187 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
188 user.pendingEmail = body.email
189 sendVerificationEmail = true
190 } else {
191 user.email = body.email
192 }
193 }
194
d03cd8bb 195 await sequelizeTypescript.transaction(async t => {
91411dba
C
196 const userAccount = await AccountModel.load(user.Account.id)
197
d03cd8bb
C
198 await user.save({ transaction: t })
199
91411dba
C
200 if (body.displayName !== undefined) userAccount.name = body.displayName
201 if (body.description !== undefined) userAccount.description = body.description
202 await userAccount.save({ transaction: t })
d03cd8bb 203
91411dba 204 await sendUpdateActor(userAccount, t)
d03cd8bb 205
1eddc9a7 206 auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON({})), oldUserAuditView)
d03cd8bb
C
207 })
208
d1ab89de
C
209 if (sendVerificationEmail === true) {
210 await sendVerifyUserEmail(user, true)
211 }
212
d03cd8bb
C
213 return res.sendStatus(204)
214}
215
6040f87d 216async function updateMyAvatar (req: express.Request, res: express.Response) {
d03cd8bb 217 const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ]
dae86118 218 const user = res.locals.oauth.token.user
1eddc9a7 219 const oldUserAuditView = new UserAuditView(user.toFormattedJSON({}))
d03cd8bb 220
91411dba 221 const userAccount = await AccountModel.load(user.Account.id)
d03cd8bb 222
f201a749 223 const avatar = await updateActorAvatarFile(avatarPhysicalFile, userAccount)
91411dba 224
1eddc9a7 225 auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON({})), oldUserAuditView)
d03cd8bb 226
06a05d5f 227 return res.json({ avatar: avatar.toFormattedJSON() })
d03cd8bb 228}