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