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