2 import express from 'express'
3 import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '@server/helpers/audit-logger'
4 import { getBiggestActorImage } from '@server/lib/actor-image'
5 import { Hooks } from '@server/lib/plugins/hooks'
6 import { pick } from '@shared/core-utils'
7 import { ActorImageType, HttpStatusCode, UserUpdateMe, UserVideoQuota, UserVideoRate as FormattedUserVideoRate } from '@shared/models'
8 import { AttributesOnly } from '@shared/typescript-utils'
9 import { createReqFiles } from '../../../helpers/express-utils'
10 import { getFormattedObjects } from '../../../helpers/utils'
11 import { CONFIG } from '../../../initializers/config'
12 import { MIMETYPES } from '../../../initializers/constants'
13 import { sequelizeTypescript } from '../../../initializers/database'
14 import { sendUpdateActor } from '../../../lib/activitypub/send'
15 import { deleteLocalActorImageFile, updateLocalActorImageFiles } from '../../../lib/local-actor'
16 import { getOriginalVideoFileTotalDailyFromUser, getOriginalVideoFileTotalFromUser, sendVerifyUserEmail } from '../../../lib/user'
19 asyncRetryTransactionMiddleware,
25 usersUpdateMeValidator,
26 usersVideoRatingValidator
27 } from '../../../middlewares'
28 import { deleteMeValidator, usersVideosValidator, videoImportsSortValidator, videosSortValidator } from '../../../middlewares/validators'
29 import { updateAvatarValidator } from '../../../middlewares/validators/actor-image'
30 import { AccountModel } from '../../../models/account/account'
31 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
32 import { UserModel } from '../../../models/user/user'
33 import { VideoModel } from '../../../models/video/video'
34 import { VideoImportModel } from '../../../models/video/video-import'
36 const auditLogger = auditLoggerFactory('users')
38 const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
40 const meRouter = express.Router()
44 asyncMiddleware(getUserInformation)
46 meRouter.delete('/me',
49 asyncMiddleware(deleteMe)
52 meRouter.get('/me/video-quota-used',
54 asyncMiddleware(getUserVideoQuotaUsed)
57 meRouter.get('/me/videos/imports',
60 videoImportsSortValidator,
63 asyncMiddleware(getUserVideoImports)
66 meRouter.get('/me/videos',
72 asyncMiddleware(usersVideosValidator),
73 asyncMiddleware(getUserVideos)
76 meRouter.get('/me/videos/:videoId/rating',
78 asyncMiddleware(usersVideoRatingValidator),
79 asyncMiddleware(getUserVideoRating)
84 asyncMiddleware(usersUpdateMeValidator),
85 asyncRetryTransactionMiddleware(updateMe)
88 meRouter.post('/me/avatar/pick',
91 updateAvatarValidator,
92 asyncRetryTransactionMiddleware(updateMyAvatar)
95 meRouter.delete('/me/avatar',
97 asyncRetryTransactionMiddleware(deleteMyAvatar)
100 // ---------------------------------------------------------------------------
106 // ---------------------------------------------------------------------------
108 async function getUserVideos (req: express.Request, res: express.Response) {
109 const user = res.locals.oauth.token.User
111 const apiOptions = await Hooks.wrapObject({
112 accountId: user.Account.id,
113 start: req.query.start,
114 count: req.query.count,
115 sort: req.query.sort,
116 search: req.query.search,
117 channelId: res.locals.videoChannel?.id,
118 isLive: req.query.isLive
119 }, 'filter:api.user.me.videos.list.params')
121 const resultList = await Hooks.wrapPromiseFun(
122 VideoModel.listUserVideosForApi,
124 'filter:api.user.me.videos.list.result'
127 const additionalAttributes = {
128 waitTranscoding: true,
130 scheduledUpdate: true,
133 return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes }))
136 async function getUserVideoImports (req: express.Request, res: express.Response) {
137 const user = res.locals.oauth.token.User
138 const resultList = await VideoImportModel.listUserVideoImportsForApi({
141 ...pick(req.query, [ 'targetUrl', 'start', 'count', 'sort' ])
144 return res.json(getFormattedObjects(resultList.data, resultList.total))
147 async function getUserInformation (req: express.Request, res: express.Response) {
148 // We did not load channels in res.locals.user
149 const user = await UserModel.loadForMeAPI(res.locals.oauth.token.user.id)
151 return res.json(user.toMeFormattedJSON())
154 async function getUserVideoQuotaUsed (req: express.Request, res: express.Response) {
155 const user = res.locals.oauth.token.user
156 const videoQuotaUsed = await getOriginalVideoFileTotalFromUser(user)
157 const videoQuotaUsedDaily = await getOriginalVideoFileTotalDailyFromUser(user)
159 const data: UserVideoQuota = {
163 return res.json(data)
166 async function getUserVideoRating (req: express.Request, res: express.Response) {
167 const videoId = res.locals.videoId.id
168 const accountId = +res.locals.oauth.token.User.Account.id
170 const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null)
171 const rating = ratingObj ? ratingObj.type : 'none'
173 const json: FormattedUserVideoRate = {
177 return res.json(json)
180 async function deleteMe (req: express.Request, res: express.Response) {
181 const user = await UserModel.loadByIdWithChannels(res.locals.oauth.token.User.id)
183 auditLogger.delete(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON()))
187 return res.status(HttpStatusCode.NO_CONTENT_204).end()
190 async function updateMe (req: express.Request, res: express.Response) {
191 const body: UserUpdateMe = req.body
192 let sendVerificationEmail = false
194 const user = res.locals.oauth.token.user
196 const keysToUpdate: (keyof UserUpdateMe & keyof AttributesOnly<UserModel>)[] = [
202 'autoPlayNextVideoPlaylist',
203 'videosHistoryEnabled',
206 'noInstanceConfigWarningModal',
207 'noAccountSetupWarningModal',
211 for (const key of keysToUpdate) {
212 if (body[key] !== undefined) user.set(key, body[key])
215 if (body.p2pEnabled !== undefined) {
216 user.set('p2pEnabled', body.p2pEnabled)
217 } else if (body.webTorrentEnabled !== undefined) { // FIXME: deprecated in 4.1
218 user.set('p2pEnabled', body.webTorrentEnabled)
221 if (body.email !== undefined) {
222 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
223 user.pendingEmail = body.email
224 sendVerificationEmail = true
226 user.email = body.email
230 await sequelizeTypescript.transaction(async t => {
231 await user.save({ transaction: t })
233 if (body.displayName === undefined && body.description === undefined) return
235 const userAccount = await AccountModel.load(user.Account.id, t)
237 if (body.displayName !== undefined) userAccount.name = body.displayName
238 if (body.description !== undefined) userAccount.description = body.description
239 await userAccount.save({ transaction: t })
241 await sendUpdateActor(userAccount, t)
244 if (sendVerificationEmail === true) {
245 await sendVerifyUserEmail(user, true)
248 return res.status(HttpStatusCode.NO_CONTENT_204).end()
251 async function updateMyAvatar (req: express.Request, res: express.Response) {
252 const avatarPhysicalFile = req.files['avatarfile'][0]
253 const user = res.locals.oauth.token.user
255 const userAccount = await AccountModel.load(user.Account.id)
257 const avatars = await updateLocalActorImageFiles(
260 ActorImageType.AVATAR
264 // TODO: remove, deprecated in 4.2
265 avatar: getBiggestActorImage(avatars).toFormattedJSON(),
266 avatars: avatars.map(avatar => avatar.toFormattedJSON())
270 async function deleteMyAvatar (req: express.Request, res: express.Response) {
271 const user = res.locals.oauth.token.user
273 const userAccount = await AccountModel.load(user.Account.id)
274 await deleteLocalActorImageFile(userAccount, ActorImageType.AVATAR)
276 return res.json({ avatars: [] })