]>
Commit | Line | Data |
---|---|---|
1 | import 'multer' | |
2 | import express from 'express' | |
3 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '@server/helpers/audit-logger' | |
4 | import { getBiggestActorImage } from '@server/lib/actor-image' | |
5 | import { Hooks } from '@server/lib/plugins/hooks' | |
6 | import { pick } from '@shared/core-utils' | |
7 | import { ActorImageType, HttpStatusCode, UserUpdateMe, UserVideoQuota, UserVideoRate as FormattedUserVideoRate } from '@shared/models' | |
8 | import { AttributesOnly } from '@shared/typescript-utils' | |
9 | import { createReqFiles } from '../../../helpers/express-utils' | |
10 | import { getFormattedObjects } from '../../../helpers/utils' | |
11 | import { CONFIG } from '../../../initializers/config' | |
12 | import { MIMETYPES } from '../../../initializers/constants' | |
13 | import { sequelizeTypescript } from '../../../initializers/database' | |
14 | import { sendUpdateActor } from '../../../lib/activitypub/send' | |
15 | import { deleteLocalActorImageFile, updateLocalActorImageFiles } from '../../../lib/local-actor' | |
16 | import { getOriginalVideoFileTotalDailyFromUser, getOriginalVideoFileTotalFromUser, sendVerifyUserEmail } from '../../../lib/user' | |
17 | import { | |
18 | asyncMiddleware, | |
19 | asyncRetryTransactionMiddleware, | |
20 | authenticate, | |
21 | paginationValidator, | |
22 | setDefaultPagination, | |
23 | setDefaultSort, | |
24 | setDefaultVideosSort, | |
25 | usersUpdateMeValidator, | |
26 | usersVideoRatingValidator | |
27 | } from '../../../middlewares' | |
28 | import { | |
29 | deleteMeValidator, | |
30 | getMyVideoImportsValidator, | |
31 | usersVideosValidator, | |
32 | videoImportsSortValidator, | |
33 | videosSortValidator | |
34 | } from '../../../middlewares/validators' | |
35 | import { updateAvatarValidator } from '../../../middlewares/validators/actor-image' | |
36 | import { AccountModel } from '../../../models/account/account' | |
37 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
38 | import { UserModel } from '../../../models/user/user' | |
39 | import { VideoModel } from '../../../models/video/video' | |
40 | import { VideoImportModel } from '../../../models/video/video-import' | |
41 | ||
42 | const auditLogger = auditLoggerFactory('users') | |
43 | ||
44 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) | |
45 | ||
46 | const meRouter = express.Router() | |
47 | ||
48 | meRouter.get('/me', | |
49 | authenticate, | |
50 | asyncMiddleware(getUserInformation) | |
51 | ) | |
52 | meRouter.delete('/me', | |
53 | authenticate, | |
54 | deleteMeValidator, | |
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, | |
69 | getMyVideoImportsValidator, | |
70 | asyncMiddleware(getUserVideoImports) | |
71 | ) | |
72 | ||
73 | meRouter.get('/me/videos', | |
74 | authenticate, | |
75 | paginationValidator, | |
76 | videosSortValidator, | |
77 | setDefaultVideosSort, | |
78 | setDefaultPagination, | |
79 | asyncMiddleware(usersVideosValidator), | |
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, | |
91 | asyncMiddleware(usersUpdateMeValidator), | |
92 | asyncRetryTransactionMiddleware(updateMe) | |
93 | ) | |
94 | ||
95 | meRouter.post('/me/avatar/pick', | |
96 | authenticate, | |
97 | reqAvatarFile, | |
98 | updateAvatarValidator, | |
99 | asyncRetryTransactionMiddleware(updateMyAvatar) | |
100 | ) | |
101 | ||
102 | meRouter.delete('/me/avatar', | |
103 | authenticate, | |
104 | asyncRetryTransactionMiddleware(deleteMyAvatar) | |
105 | ) | |
106 | ||
107 | // --------------------------------------------------------------------------- | |
108 | ||
109 | export { | |
110 | meRouter | |
111 | } | |
112 | ||
113 | // --------------------------------------------------------------------------- | |
114 | ||
115 | async function getUserVideos (req: express.Request, res: express.Response) { | |
116 | const user = res.locals.oauth.token.User | |
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, | |
123 | search: req.query.search, | |
124 | channelId: res.locals.videoChannel?.id, | |
125 | isLive: req.query.isLive | |
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' | |
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 | ||
143 | async function getUserVideoImports (req: express.Request, res: express.Response) { | |
144 | const user = res.locals.oauth.token.User | |
145 | const resultList = await VideoImportModel.listUserVideoImportsForApi({ | |
146 | userId: user.id, | |
147 | ||
148 | ...pick(req.query, [ 'targetUrl', 'start', 'count', 'sort', 'search', 'videoChannelSyncId' ]) | |
149 | }) | |
150 | ||
151 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
152 | } | |
153 | ||
154 | async function getUserInformation (req: express.Request, res: express.Response) { | |
155 | // We did not load channels in res.locals.user | |
156 | const user = await UserModel.loadForMeAPI(res.locals.oauth.token.user.id) | |
157 | ||
158 | return res.json(user.toMeFormattedJSON()) | |
159 | } | |
160 | ||
161 | async function getUserVideoQuotaUsed (req: express.Request, res: express.Response) { | |
162 | const user = res.locals.oauth.token.user | |
163 | const videoQuotaUsed = await getOriginalVideoFileTotalFromUser(user) | |
164 | const videoQuotaUsedDaily = await getOriginalVideoFileTotalDailyFromUser(user) | |
165 | ||
166 | const data: UserVideoQuota = { | |
167 | videoQuotaUsed, | |
168 | videoQuotaUsedDaily | |
169 | } | |
170 | return res.json(data) | |
171 | } | |
172 | ||
173 | async function getUserVideoRating (req: express.Request, res: express.Response) { | |
174 | const videoId = res.locals.videoId.id | |
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 | } | |
184 | return res.json(json) | |
185 | } | |
186 | ||
187 | async function deleteMe (req: express.Request, res: express.Response) { | |
188 | const user = await UserModel.loadByIdWithChannels(res.locals.oauth.token.User.id) | |
189 | ||
190 | auditLogger.delete(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON())) | |
191 | ||
192 | await user.destroy() | |
193 | ||
194 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
195 | } | |
196 | ||
197 | async function updateMe (req: express.Request, res: express.Response) { | |
198 | const body: UserUpdateMe = req.body | |
199 | let sendVerificationEmail = false | |
200 | ||
201 | const user = res.locals.oauth.token.user | |
202 | ||
203 | const keysToUpdate: (keyof UserUpdateMe & keyof AttributesOnly<UserModel>)[] = [ | |
204 | 'password', | |
205 | 'nsfwPolicy', | |
206 | 'p2pEnabled', | |
207 | 'autoPlayVideo', | |
208 | 'autoPlayNextVideo', | |
209 | 'autoPlayNextVideoPlaylist', | |
210 | 'videosHistoryEnabled', | |
211 | 'videoLanguages', | |
212 | 'theme', | |
213 | 'noInstanceConfigWarningModal', | |
214 | 'noAccountSetupWarningModal', | |
215 | 'noWelcomeModal', | |
216 | 'emailPublic' | |
217 | ] | |
218 | ||
219 | for (const key of keysToUpdate) { | |
220 | if (body[key] !== undefined) user.set(key, body[key]) | |
221 | } | |
222 | ||
223 | if (body.p2pEnabled !== undefined) { | |
224 | user.set('p2pEnabled', body.p2pEnabled) | |
225 | } else if (body.webTorrentEnabled !== undefined) { // FIXME: deprecated in 4.1 | |
226 | user.set('p2pEnabled', body.webTorrentEnabled) | |
227 | } | |
228 | ||
229 | if (body.email !== undefined) { | |
230 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) { | |
231 | user.pendingEmail = body.email | |
232 | sendVerificationEmail = true | |
233 | } else { | |
234 | user.email = body.email | |
235 | } | |
236 | } | |
237 | ||
238 | await sequelizeTypescript.transaction(async t => { | |
239 | await user.save({ transaction: t }) | |
240 | ||
241 | if (body.displayName === undefined && body.description === undefined) return | |
242 | ||
243 | const userAccount = await AccountModel.load(user.Account.id, t) | |
244 | ||
245 | if (body.displayName !== undefined) userAccount.name = body.displayName | |
246 | if (body.description !== undefined) userAccount.description = body.description | |
247 | await userAccount.save({ transaction: t }) | |
248 | ||
249 | await sendUpdateActor(userAccount, t) | |
250 | }) | |
251 | ||
252 | if (sendVerificationEmail === true) { | |
253 | await sendVerifyUserEmail(user, true) | |
254 | } | |
255 | ||
256 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
257 | } | |
258 | ||
259 | async function updateMyAvatar (req: express.Request, res: express.Response) { | |
260 | const avatarPhysicalFile = req.files['avatarfile'][0] | |
261 | const user = res.locals.oauth.token.user | |
262 | ||
263 | const userAccount = await AccountModel.load(user.Account.id) | |
264 | ||
265 | const avatars = await updateLocalActorImageFiles( | |
266 | userAccount, | |
267 | avatarPhysicalFile, | |
268 | ActorImageType.AVATAR | |
269 | ) | |
270 | ||
271 | return res.json({ | |
272 | // TODO: remove, deprecated in 4.2 | |
273 | avatar: getBiggestActorImage(avatars).toFormattedJSON(), | |
274 | avatars: avatars.map(avatar => avatar.toFormattedJSON()) | |
275 | }) | |
276 | } | |
277 | ||
278 | async function deleteMyAvatar (req: express.Request, res: express.Response) { | |
279 | const user = res.locals.oauth.token.user | |
280 | ||
281 | const userAccount = await AccountModel.load(user.Account.id) | |
282 | await deleteLocalActorImageFile(userAccount, ActorImageType.AVATAR) | |
283 | ||
284 | return res.json({ avatars: [] }) | |
285 | } |