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