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