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