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