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