]>
Commit | Line | Data |
---|---|---|
1 | import 'multer' | |
2 | import express from 'express' | |
3 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '@server/helpers/audit-logger' | |
4 | import { Hooks } from '@server/lib/plugins/hooks' | |
5 | import { AttributesOnly } from '@shared/core-utils' | |
6 | import { ActorImageType, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../../shared' | |
7 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' | |
8 | import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model' | |
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, updateLocalActorImageFile } 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 { deleteMeValidator, usersVideosValidator, videoImportsSortValidator, videosSortValidator } from '../../../middlewares/validators' | |
29 | import { updateAvatarValidator } from '../../../middlewares/validators/actor-image' | |
30 | import { AccountModel } from '../../../models/account/account' | |
31 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
32 | import { UserModel } from '../../../models/user/user' | |
33 | import { VideoModel } from '../../../models/video/video' | |
34 | import { VideoImportModel } from '../../../models/video/video-import' | |
35 | ||
36 | const auditLogger = auditLoggerFactory('users') | |
37 | ||
38 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) | |
39 | ||
40 | const meRouter = express.Router() | |
41 | ||
42 | meRouter.get('/me', | |
43 | authenticate, | |
44 | asyncMiddleware(getUserInformation) | |
45 | ) | |
46 | meRouter.delete('/me', | |
47 | authenticate, | |
48 | deleteMeValidator, | |
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, | |
70 | setDefaultVideosSort, | |
71 | setDefaultPagination, | |
72 | asyncMiddleware(usersVideosValidator), | |
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, | |
84 | asyncMiddleware(usersUpdateMeValidator), | |
85 | asyncRetryTransactionMiddleware(updateMe) | |
86 | ) | |
87 | ||
88 | meRouter.post('/me/avatar/pick', | |
89 | authenticate, | |
90 | reqAvatarFile, | |
91 | updateAvatarValidator, | |
92 | asyncRetryTransactionMiddleware(updateMyAvatar) | |
93 | ) | |
94 | ||
95 | meRouter.delete('/me/avatar', | |
96 | authenticate, | |
97 | asyncRetryTransactionMiddleware(deleteMyAvatar) | |
98 | ) | |
99 | ||
100 | // --------------------------------------------------------------------------- | |
101 | ||
102 | export { | |
103 | meRouter | |
104 | } | |
105 | ||
106 | // --------------------------------------------------------------------------- | |
107 | ||
108 | async function getUserVideos (req: express.Request, res: express.Response) { | |
109 | const user = res.locals.oauth.token.User | |
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, | |
116 | search: req.query.search, | |
117 | channelId: res.locals.videoChannel?.id, | |
118 | isLive: req.query.isLive | |
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' | |
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 | ||
136 | async function getUserVideoImports (req: express.Request, res: express.Response) { | |
137 | const user = res.locals.oauth.token.User | |
138 | const resultList = await VideoImportModel.listUserVideoImportsForApi( | |
139 | user.id, | |
140 | req.query.start as number, | |
141 | req.query.count as number, | |
142 | req.query.sort | |
143 | ) | |
144 | ||
145 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
146 | } | |
147 | ||
148 | async function getUserInformation (req: express.Request, res: express.Response) { | |
149 | // We did not load channels in res.locals.user | |
150 | const user = await UserModel.loadForMeAPI(res.locals.oauth.token.user.id) | |
151 | ||
152 | return res.json(user.toMeFormattedJSON()) | |
153 | } | |
154 | ||
155 | async function getUserVideoQuotaUsed (req: express.Request, res: express.Response) { | |
156 | const user = res.locals.oauth.token.user | |
157 | const videoQuotaUsed = await getOriginalVideoFileTotalFromUser(user) | |
158 | const videoQuotaUsedDaily = await getOriginalVideoFileTotalDailyFromUser(user) | |
159 | ||
160 | const data: UserVideoQuota = { | |
161 | videoQuotaUsed, | |
162 | videoQuotaUsedDaily | |
163 | } | |
164 | return res.json(data) | |
165 | } | |
166 | ||
167 | async function getUserVideoRating (req: express.Request, res: express.Response) { | |
168 | const videoId = res.locals.videoId.id | |
169 | const accountId = +res.locals.oauth.token.User.Account.id | |
170 | ||
171 | const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null) | |
172 | const rating = ratingObj ? ratingObj.type : 'none' | |
173 | ||
174 | const json: FormattedUserVideoRate = { | |
175 | videoId, | |
176 | rating | |
177 | } | |
178 | return res.json(json) | |
179 | } | |
180 | ||
181 | async function deleteMe (req: express.Request, res: express.Response) { | |
182 | const user = await UserModel.loadByIdWithChannels(res.locals.oauth.token.User.id) | |
183 | ||
184 | auditLogger.delete(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON())) | |
185 | ||
186 | await user.destroy() | |
187 | ||
188 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
189 | } | |
190 | ||
191 | async function updateMe (req: express.Request, res: express.Response) { | |
192 | const body: UserUpdateMe = req.body | |
193 | let sendVerificationEmail = false | |
194 | ||
195 | const user = res.locals.oauth.token.user | |
196 | ||
197 | const keysToUpdate: (keyof UserUpdateMe & keyof AttributesOnly<UserModel>)[] = [ | |
198 | 'password', | |
199 | 'nsfwPolicy', | |
200 | 'webTorrentEnabled', | |
201 | 'autoPlayVideo', | |
202 | 'autoPlayNextVideo', | |
203 | 'autoPlayNextVideoPlaylist', | |
204 | 'videosHistoryEnabled', | |
205 | 'videoLanguages', | |
206 | 'theme', | |
207 | 'noInstanceConfigWarningModal', | |
208 | 'noAccountSetupWarningModal', | |
209 | 'noWelcomeModal' | |
210 | ] | |
211 | ||
212 | for (const key of keysToUpdate) { | |
213 | if (body[key] !== undefined) user.set(key, body[key]) | |
214 | } | |
215 | ||
216 | if (body.email !== undefined) { | |
217 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) { | |
218 | user.pendingEmail = body.email | |
219 | sendVerificationEmail = true | |
220 | } else { | |
221 | user.email = body.email | |
222 | } | |
223 | } | |
224 | ||
225 | await sequelizeTypescript.transaction(async t => { | |
226 | await user.save({ transaction: t }) | |
227 | ||
228 | if (body.displayName === undefined && body.description === undefined) return | |
229 | ||
230 | const userAccount = await AccountModel.load(user.Account.id, t) | |
231 | ||
232 | if (body.displayName !== undefined) userAccount.name = body.displayName | |
233 | if (body.description !== undefined) userAccount.description = body.description | |
234 | await userAccount.save({ transaction: t }) | |
235 | ||
236 | await sendUpdateActor(userAccount, t) | |
237 | }) | |
238 | ||
239 | if (sendVerificationEmail === true) { | |
240 | await sendVerifyUserEmail(user, true) | |
241 | } | |
242 | ||
243 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
244 | } | |
245 | ||
246 | async function updateMyAvatar (req: express.Request, res: express.Response) { | |
247 | const avatarPhysicalFile = req.files['avatarfile'][0] | |
248 | const user = res.locals.oauth.token.user | |
249 | ||
250 | const userAccount = await AccountModel.load(user.Account.id) | |
251 | ||
252 | const avatar = await updateLocalActorImageFile(userAccount, avatarPhysicalFile, ActorImageType.AVATAR) | |
253 | ||
254 | return res.json({ avatar: avatar.toFormattedJSON() }) | |
255 | } | |
256 | ||
257 | async function deleteMyAvatar (req: express.Request, res: express.Response) { | |
258 | const user = res.locals.oauth.token.user | |
259 | ||
260 | const userAccount = await AccountModel.load(user.Account.id) | |
261 | await deleteLocalActorImageFile(userAccount, ActorImageType.AVATAR) | |
262 | ||
263 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
264 | } |