2 import * as express from 'express'
3 import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate, VideoSortField } from '../../../../shared'
4 import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model'
5 import { createReqFiles } from '../../../helpers/express-utils'
6 import { getFormattedObjects } from '../../../helpers/utils'
7 import { CONFIG } from '../../../initializers/config'
8 import { MIMETYPES } from '../../../initializers/constants'
9 import { sequelizeTypescript } from '../../../initializers/database'
10 import { sendUpdateActor } from '../../../lib/activitypub/send'
11 import { updateActorAvatarFile } from '../../../lib/avatar'
12 import { getOriginalVideoFileTotalDailyFromUser, getOriginalVideoFileTotalFromUser, sendVerifyUserEmail } from '../../../lib/user'
15 asyncRetryTransactionMiddleware,
21 usersUpdateMeValidator,
22 usersVideoRatingValidator
23 } from '../../../middlewares'
24 import { deleteMeValidator, videoImportsSortValidator, videosSortValidator } from '../../../middlewares/validators'
25 import { updateAvatarValidator } from '../../../middlewares/validators/avatar'
26 import { AccountModel } from '../../../models/account/account'
27 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
28 import { UserModel } from '../../../models/account/user'
29 import { VideoModel } from '../../../models/video/video'
30 import { VideoImportModel } from '../../../models/video/video-import'
31 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
33 const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR })
35 const meRouter = express.Router()
39 asyncMiddleware(getUserInformation)
41 meRouter.delete('/me',
44 asyncMiddleware(deleteMe)
47 meRouter.get('/me/video-quota-used',
49 asyncMiddleware(getUserVideoQuotaUsed)
52 meRouter.get('/me/videos/imports',
55 videoImportsSortValidator,
58 asyncMiddleware(getUserVideoImports)
61 meRouter.get('/me/videos',
67 asyncMiddleware(getUserVideos)
70 meRouter.get('/me/videos/:videoId/rating',
72 asyncMiddleware(usersVideoRatingValidator),
73 asyncMiddleware(getUserVideoRating)
78 asyncMiddleware(usersUpdateMeValidator),
79 asyncRetryTransactionMiddleware(updateMe)
82 meRouter.post('/me/avatar/pick',
85 updateAvatarValidator,
86 asyncRetryTransactionMiddleware(updateMyAvatar)
89 // ---------------------------------------------------------------------------
95 // ---------------------------------------------------------------------------
97 async function getUserVideos (req: express.Request, res: express.Response) {
98 const user = res.locals.oauth.token.User
99 const resultList = await VideoModel.listUserVideosForApi(
101 req.query.start as number,
102 req.query.count as number,
103 req.query.sort as VideoSortField,
104 req.query.search as string
107 const additionalAttributes = {
108 waitTranscoding: true,
110 scheduledUpdate: true,
113 return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes }))
116 async function getUserVideoImports (req: express.Request, res: express.Response) {
117 const user = res.locals.oauth.token.User
118 const resultList = await VideoImportModel.listUserVideoImportsForApi(
120 req.query.start as number,
121 req.query.count as number,
125 return res.json(getFormattedObjects(resultList.data, resultList.total))
128 async function getUserInformation (req: express.Request, res: express.Response) {
129 // We did not load channels in res.locals.user
130 const user = await UserModel.loadForMeAPI(res.locals.oauth.token.user.username)
132 return res.json(user.toMeFormattedJSON())
135 async function getUserVideoQuotaUsed (req: express.Request, res: express.Response) {
136 const user = res.locals.oauth.token.user
137 const videoQuotaUsed = await getOriginalVideoFileTotalFromUser(user)
138 const videoQuotaUsedDaily = await getOriginalVideoFileTotalDailyFromUser(user)
140 const data: UserVideoQuota = {
144 return res.json(data)
147 async function getUserVideoRating (req: express.Request, res: express.Response) {
148 const videoId = res.locals.videoId.id
149 const accountId = +res.locals.oauth.token.User.Account.id
151 const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null)
152 const rating = ratingObj ? ratingObj.type : 'none'
154 const json: FormattedUserVideoRate = {
158 return res.json(json)
161 async function deleteMe (req: express.Request, res: express.Response) {
162 const user = res.locals.oauth.token.User
166 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
169 async function updateMe (req: express.Request, res: express.Response) {
170 const body: UserUpdateMe = req.body
171 let sendVerificationEmail = false
173 const user = res.locals.oauth.token.user
175 if (body.password !== undefined) user.password = body.password
176 if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy
177 if (body.webTorrentEnabled !== undefined) user.webTorrentEnabled = body.webTorrentEnabled
178 if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
179 if (body.autoPlayNextVideo !== undefined) user.autoPlayNextVideo = body.autoPlayNextVideo
180 if (body.autoPlayNextVideoPlaylist !== undefined) user.autoPlayNextVideoPlaylist = body.autoPlayNextVideoPlaylist
181 if (body.videosHistoryEnabled !== undefined) user.videosHistoryEnabled = body.videosHistoryEnabled
182 if (body.videoLanguages !== undefined) user.videoLanguages = body.videoLanguages
183 if (body.theme !== undefined) user.theme = body.theme
184 if (body.noInstanceConfigWarningModal !== undefined) user.noInstanceConfigWarningModal = body.noInstanceConfigWarningModal
185 if (body.noWelcomeModal !== undefined) user.noWelcomeModal = body.noWelcomeModal
187 if (body.email !== undefined) {
188 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
189 user.pendingEmail = body.email
190 sendVerificationEmail = true
192 user.email = body.email
196 await sequelizeTypescript.transaction(async t => {
197 await user.save({ transaction: t })
199 if (body.displayName !== undefined || body.description !== undefined) {
200 const userAccount = await AccountModel.load(user.Account.id, t)
202 if (body.displayName !== undefined) userAccount.name = body.displayName
203 if (body.description !== undefined) userAccount.description = body.description
204 await userAccount.save({ transaction: t })
206 await sendUpdateActor(userAccount, t)
210 if (sendVerificationEmail === true) {
211 await sendVerifyUserEmail(user, true)
214 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
217 async function updateMyAvatar (req: express.Request, res: express.Response) {
218 const avatarPhysicalFile = req.files['avatarfile'][0]
219 const user = res.locals.oauth.token.user
221 const userAccount = await AccountModel.load(user.Account.id)
223 const avatar = await updateActorAvatarFile(avatarPhysicalFile, userAccount)
225 return res.json({ avatar: avatar.toFormattedJSON() })