]>
Commit | Line | Data |
---|---|---|
d03cd8bb | 1 | import 'multer' |
41fb13c3 | 2 | import express from 'express' |
2dbc170d | 3 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '@server/helpers/audit-logger' |
d0800f76 | 4 | import { getBiggestActorImage } from '@server/lib/actor-image' |
a4d2ca07 | 5 | import { Hooks } from '@server/lib/plugins/hooks' |
d0800f76 | 6 | import { pick } from '@shared/core-utils' |
d17c7b4e | 7 | import { ActorImageType, HttpStatusCode, UserUpdateMe, UserVideoQuota, UserVideoRate as FormattedUserVideoRate } from '@shared/models' |
6b5f72be | 8 | import { AttributesOnly } from '@shared/typescript-utils' |
67ed6552 | 9 | import { createReqFiles } from '../../../helpers/express-utils' |
d03cd8bb | 10 | import { getFormattedObjects } from '../../../helpers/utils' |
67ed6552 | 11 | import { CONFIG } from '../../../initializers/config' |
74dc3bca | 12 | import { MIMETYPES } from '../../../initializers/constants' |
67ed6552 | 13 | import { sequelizeTypescript } from '../../../initializers/database' |
d03cd8bb | 14 | import { sendUpdateActor } from '../../../lib/activitypub/send' |
d0800f76 | 15 | import { deleteLocalActorImageFile, updateLocalActorImageFiles } from '../../../lib/local-actor' |
fb719404 | 16 | import { getOriginalVideoFileTotalDailyFromUser, getOriginalVideoFileTotalFromUser, sendVerifyUserEmail } from '../../../lib/user' |
d03cd8bb | 17 | import { |
993cef4b C |
18 | asyncMiddleware, |
19 | asyncRetryTransactionMiddleware, | |
d03cd8bb C |
20 | authenticate, |
21 | paginationValidator, | |
22 | setDefaultPagination, | |
23 | setDefaultSort, | |
8054669f | 24 | setDefaultVideosSort, |
d03cd8bb C |
25 | usersUpdateMeValidator, |
26 | usersVideoRatingValidator | |
27 | } from '../../../middlewares' | |
a3b472a1 C |
28 | import { |
29 | deleteMeValidator, | |
30 | getMyVideoImportsValidator, | |
31 | usersVideosValidator, | |
32 | videoImportsSortValidator, | |
33 | videosSortValidator | |
34 | } from '../../../middlewares/validators' | |
213e30ef | 35 | import { updateAvatarValidator } from '../../../middlewares/validators/actor-image' |
67ed6552 | 36 | import { AccountModel } from '../../../models/account/account' |
d03cd8bb | 37 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
7d9ba5c0 | 38 | import { UserModel } from '../../../models/user/user' |
d03cd8bb | 39 | import { VideoModel } from '../../../models/video/video' |
d03cd8bb | 40 | import { VideoImportModel } from '../../../models/video/video-import' |
2dbc170d C |
41 | |
42 | const auditLogger = auditLoggerFactory('users') | |
d03cd8bb | 43 | |
d3d3deaa | 44 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) |
d03cd8bb C |
45 | |
46 | const meRouter = express.Router() | |
47 | ||
48 | meRouter.get('/me', | |
49 | authenticate, | |
50 | asyncMiddleware(getUserInformation) | |
51 | ) | |
52 | meRouter.delete('/me', | |
53 | authenticate, | |
a1587156 | 54 | deleteMeValidator, |
d03cd8bb C |
55 | asyncMiddleware(deleteMe) |
56 | ) | |
57 | ||
58 | meRouter.get('/me/video-quota-used', | |
59 | authenticate, | |
60 | asyncMiddleware(getUserVideoQuotaUsed) | |
61 | ) | |
62 | ||
63 | meRouter.get('/me/videos/imports', | |
64 | authenticate, | |
65 | paginationValidator, | |
66 | videoImportsSortValidator, | |
67 | setDefaultSort, | |
68 | setDefaultPagination, | |
a3b472a1 | 69 | getMyVideoImportsValidator, |
d03cd8bb C |
70 | asyncMiddleware(getUserVideoImports) |
71 | ) | |
72 | ||
73 | meRouter.get('/me/videos', | |
74 | authenticate, | |
75 | paginationValidator, | |
76 | videosSortValidator, | |
8054669f | 77 | setDefaultVideosSort, |
d03cd8bb | 78 | setDefaultPagination, |
978c87e7 | 79 | asyncMiddleware(usersVideosValidator), |
d03cd8bb C |
80 | asyncMiddleware(getUserVideos) |
81 | ) | |
82 | ||
83 | meRouter.get('/me/videos/:videoId/rating', | |
84 | authenticate, | |
85 | asyncMiddleware(usersVideoRatingValidator), | |
86 | asyncMiddleware(getUserVideoRating) | |
87 | ) | |
88 | ||
89 | meRouter.put('/me', | |
90 | authenticate, | |
a890d1e0 | 91 | asyncMiddleware(usersUpdateMeValidator), |
176e2114 | 92 | asyncRetryTransactionMiddleware(updateMe) |
d03cd8bb C |
93 | ) |
94 | ||
95 | meRouter.post('/me/avatar/pick', | |
96 | authenticate, | |
97 | reqAvatarFile, | |
98 | updateAvatarValidator, | |
176e2114 | 99 | asyncRetryTransactionMiddleware(updateMyAvatar) |
d03cd8bb C |
100 | ) |
101 | ||
1ea7da81 RK |
102 | meRouter.delete('/me/avatar', |
103 | authenticate, | |
104 | asyncRetryTransactionMiddleware(deleteMyAvatar) | |
105 | ) | |
106 | ||
d03cd8bb C |
107 | // --------------------------------------------------------------------------- |
108 | ||
109 | export { | |
110 | meRouter | |
111 | } | |
112 | ||
113 | // --------------------------------------------------------------------------- | |
114 | ||
dae86118 C |
115 | async function getUserVideos (req: express.Request, res: express.Response) { |
116 | const user = res.locals.oauth.token.User | |
a4d2ca07 C |
117 | |
118 | const apiOptions = await Hooks.wrapObject({ | |
119 | accountId: user.Account.id, | |
120 | start: req.query.start, | |
121 | count: req.query.count, | |
122 | sort: req.query.sort, | |
1fd61899 | 123 | search: req.query.search, |
978c87e7 | 124 | channelId: res.locals.videoChannel?.id, |
1fd61899 | 125 | isLive: req.query.isLive |
a4d2ca07 C |
126 | }, 'filter:api.user.me.videos.list.params') |
127 | ||
128 | const resultList = await Hooks.wrapPromiseFun( | |
129 | VideoModel.listUserVideosForApi, | |
130 | apiOptions, | |
131 | 'filter:api.user.me.videos.list.result' | |
d03cd8bb C |
132 | ) |
133 | ||
134 | const additionalAttributes = { | |
135 | waitTranscoding: true, | |
136 | state: true, | |
137 | scheduledUpdate: true, | |
138 | blacklistInfo: true | |
139 | } | |
140 | return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes })) | |
141 | } | |
142 | ||
dae86118 C |
143 | async function getUserVideoImports (req: express.Request, res: express.Response) { |
144 | const user = res.locals.oauth.token.User | |
d511df28 C |
145 | const resultList = await VideoImportModel.listUserVideoImportsForApi({ |
146 | userId: user.id, | |
147 | ||
a3b472a1 | 148 | ...pick(req.query, [ 'targetUrl', 'start', 'count', 'sort', 'search', 'videoChannelSyncId' ]) |
d511df28 | 149 | }) |
d03cd8bb C |
150 | |
151 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
152 | } | |
153 | ||
dae86118 | 154 | async function getUserInformation (req: express.Request, res: express.Response) { |
d03cd8bb | 155 | // We did not load channels in res.locals.user |
1acb9475 | 156 | const user = await UserModel.loadForMeAPI(res.locals.oauth.token.user.id) |
d03cd8bb | 157 | |
ac0868bc | 158 | return res.json(user.toMeFormattedJSON()) |
d03cd8bb C |
159 | } |
160 | ||
dae86118 | 161 | async function getUserVideoQuotaUsed (req: express.Request, res: express.Response) { |
ac0868bc | 162 | const user = res.locals.oauth.token.user |
fb719404 C |
163 | const videoQuotaUsed = await getOriginalVideoFileTotalFromUser(user) |
164 | const videoQuotaUsedDaily = await getOriginalVideoFileTotalDailyFromUser(user) | |
d03cd8bb C |
165 | |
166 | const data: UserVideoQuota = { | |
bee0abff FA |
167 | videoQuotaUsed, |
168 | videoQuotaUsedDaily | |
d03cd8bb C |
169 | } |
170 | return res.json(data) | |
171 | } | |
172 | ||
dae86118 | 173 | async function getUserVideoRating (req: express.Request, res: express.Response) { |
453e83ea | 174 | const videoId = res.locals.videoId.id |
d03cd8bb C |
175 | const accountId = +res.locals.oauth.token.User.Account.id |
176 | ||
177 | const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null) | |
178 | const rating = ratingObj ? ratingObj.type : 'none' | |
179 | ||
180 | const json: FormattedUserVideoRate = { | |
181 | videoId, | |
182 | rating | |
183 | } | |
06a05d5f | 184 | return res.json(json) |
d03cd8bb C |
185 | } |
186 | ||
187 | async function deleteMe (req: express.Request, res: express.Response) { | |
2dbc170d C |
188 | const user = await UserModel.loadByIdWithChannels(res.locals.oauth.token.User.id) |
189 | ||
190 | auditLogger.delete(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON())) | |
d03cd8bb C |
191 | |
192 | await user.destroy() | |
193 | ||
76148b27 | 194 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
d03cd8bb C |
195 | } |
196 | ||
b426edd4 | 197 | async function updateMe (req: express.Request, res: express.Response) { |
d03cd8bb | 198 | const body: UserUpdateMe = req.body |
d1ab89de | 199 | let sendVerificationEmail = false |
d03cd8bb | 200 | |
dae86118 | 201 | const user = res.locals.oauth.token.user |
d03cd8bb | 202 | |
c158a5fa C |
203 | const keysToUpdate: (keyof UserUpdateMe & keyof AttributesOnly<UserModel>)[] = [ |
204 | 'password', | |
205 | 'nsfwPolicy', | |
a9bfa85d | 206 | 'p2pEnabled', |
c158a5fa C |
207 | 'autoPlayVideo', |
208 | 'autoPlayNextVideo', | |
209 | 'autoPlayNextVideoPlaylist', | |
210 | 'videosHistoryEnabled', | |
211 | 'videoLanguages', | |
212 | 'theme', | |
213 | 'noInstanceConfigWarningModal', | |
8f581725 | 214 | 'noAccountSetupWarningModal', |
c158a5fa C |
215 | 'noWelcomeModal' |
216 | ] | |
217 | ||
218 | for (const key of keysToUpdate) { | |
219 | if (body[key] !== undefined) user.set(key, body[key]) | |
220 | } | |
d03cd8bb | 221 | |
a9bfa85d C |
222 | if (body.p2pEnabled !== undefined) { |
223 | user.set('p2pEnabled', body.p2pEnabled) | |
224 | } else if (body.webTorrentEnabled !== undefined) { // FIXME: deprecated in 4.1 | |
225 | user.set('p2pEnabled', body.webTorrentEnabled) | |
226 | } | |
227 | ||
d1ab89de C |
228 | if (body.email !== undefined) { |
229 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) { | |
230 | user.pendingEmail = body.email | |
231 | sendVerificationEmail = true | |
232 | } else { | |
233 | user.email = body.email | |
234 | } | |
235 | } | |
236 | ||
589d9f55 C |
237 | await sequelizeTypescript.transaction(async t => { |
238 | await user.save({ transaction: t }) | |
91411dba | 239 | |
c158a5fa | 240 | if (body.displayName === undefined && body.description === undefined) return |
d03cd8bb | 241 | |
c158a5fa | 242 | const userAccount = await AccountModel.load(user.Account.id, t) |
d03cd8bb | 243 | |
c158a5fa C |
244 | if (body.displayName !== undefined) userAccount.name = body.displayName |
245 | if (body.description !== undefined) userAccount.description = body.description | |
246 | await userAccount.save({ transaction: t }) | |
247 | ||
248 | await sendUpdateActor(userAccount, t) | |
589d9f55 | 249 | }) |
d03cd8bb | 250 | |
d1ab89de C |
251 | if (sendVerificationEmail === true) { |
252 | await sendVerifyUserEmail(user, true) | |
253 | } | |
254 | ||
76148b27 | 255 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
d03cd8bb C |
256 | } |
257 | ||
6040f87d | 258 | async function updateMyAvatar (req: express.Request, res: express.Response) { |
a1587156 | 259 | const avatarPhysicalFile = req.files['avatarfile'][0] |
dae86118 | 260 | const user = res.locals.oauth.token.user |
d03cd8bb | 261 | |
91411dba | 262 | const userAccount = await AccountModel.load(user.Account.id) |
d03cd8bb | 263 | |
d0800f76 | 264 | const avatars = await updateLocalActorImageFiles( |
265 | userAccount, | |
266 | avatarPhysicalFile, | |
267 | ActorImageType.AVATAR | |
268 | ) | |
91411dba | 269 | |
d0800f76 | 270 | return res.json({ |
271 | // TODO: remove, deprecated in 4.2 | |
272 | avatar: getBiggestActorImage(avatars).toFormattedJSON(), | |
273 | avatars: avatars.map(avatar => avatar.toFormattedJSON()) | |
274 | }) | |
d03cd8bb | 275 | } |
1ea7da81 RK |
276 | |
277 | async function deleteMyAvatar (req: express.Request, res: express.Response) { | |
278 | const user = res.locals.oauth.token.user | |
279 | ||
280 | const userAccount = await AccountModel.load(user.Account.id) | |
2cb03dc1 | 281 | await deleteLocalActorImageFile(userAccount, ActorImageType.AVATAR) |
1ea7da81 | 282 | |
d0800f76 | 283 | return res.json({ avatars: [] }) |
1ea7da81 | 284 | } |