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