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