]>
Commit | Line | Data |
---|---|---|
d03cd8bb | 1 | import 'multer' |
67ed6552 | 2 | import * as express from 'express' |
2dbc170d | 3 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '@server/helpers/audit-logger' |
a4d2ca07 C |
4 | import { Hooks } from '@server/lib/plugins/hooks' |
5 | import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../../shared' | |
2dbc170d | 6 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
67ed6552 C |
7 | import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model' |
8 | import { createReqFiles } from '../../../helpers/express-utils' | |
d03cd8bb | 9 | import { getFormattedObjects } from '../../../helpers/utils' |
67ed6552 | 10 | import { CONFIG } from '../../../initializers/config' |
74dc3bca | 11 | import { MIMETYPES } from '../../../initializers/constants' |
67ed6552 | 12 | import { sequelizeTypescript } from '../../../initializers/database' |
d03cd8bb | 13 | import { sendUpdateActor } from '../../../lib/activitypub/send' |
e08ff02a | 14 | import { deleteLocalActorAvatarFile, updateLocalActorAvatarFile } from '../../../lib/avatar' |
fb719404 | 15 | import { getOriginalVideoFileTotalDailyFromUser, getOriginalVideoFileTotalFromUser, sendVerifyUserEmail } from '../../../lib/user' |
d03cd8bb | 16 | import { |
993cef4b C |
17 | asyncMiddleware, |
18 | asyncRetryTransactionMiddleware, | |
d03cd8bb C |
19 | authenticate, |
20 | paginationValidator, | |
21 | setDefaultPagination, | |
22 | setDefaultSort, | |
8054669f | 23 | setDefaultVideosSort, |
d03cd8bb C |
24 | usersUpdateMeValidator, |
25 | usersVideoRatingValidator | |
26 | } from '../../../middlewares' | |
cf405589 | 27 | import { deleteMeValidator, videoImportsSortValidator, videosSortValidator } from '../../../middlewares/validators' |
67ed6552 C |
28 | import { updateAvatarValidator } from '../../../middlewares/validators/avatar' |
29 | import { AccountModel } from '../../../models/account/account' | |
d03cd8bb C |
30 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
31 | import { UserModel } from '../../../models/account/user' | |
32 | import { VideoModel } from '../../../models/video/video' | |
d03cd8bb | 33 | import { VideoImportModel } from '../../../models/video/video-import' |
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 C |
70 | setDefaultPagination, |
71 | asyncMiddleware(getUserVideos) | |
72 | ) | |
73 | ||
74 | meRouter.get('/me/videos/:videoId/rating', | |
75 | authenticate, | |
76 | asyncMiddleware(usersVideoRatingValidator), | |
77 | asyncMiddleware(getUserVideoRating) | |
78 | ) | |
79 | ||
80 | meRouter.put('/me', | |
81 | authenticate, | |
a890d1e0 | 82 | asyncMiddleware(usersUpdateMeValidator), |
176e2114 | 83 | asyncRetryTransactionMiddleware(updateMe) |
d03cd8bb C |
84 | ) |
85 | ||
86 | meRouter.post('/me/avatar/pick', | |
87 | authenticate, | |
88 | reqAvatarFile, | |
89 | updateAvatarValidator, | |
176e2114 | 90 | asyncRetryTransactionMiddleware(updateMyAvatar) |
d03cd8bb C |
91 | ) |
92 | ||
1ea7da81 RK |
93 | meRouter.delete('/me/avatar', |
94 | authenticate, | |
95 | asyncRetryTransactionMiddleware(deleteMyAvatar) | |
96 | ) | |
97 | ||
d03cd8bb C |
98 | // --------------------------------------------------------------------------- |
99 | ||
100 | export { | |
101 | meRouter | |
102 | } | |
103 | ||
104 | // --------------------------------------------------------------------------- | |
105 | ||
dae86118 C |
106 | async function getUserVideos (req: express.Request, res: express.Response) { |
107 | const user = res.locals.oauth.token.User | |
a4d2ca07 C |
108 | |
109 | const apiOptions = await Hooks.wrapObject({ | |
110 | accountId: user.Account.id, | |
111 | start: req.query.start, | |
112 | count: req.query.count, | |
113 | sort: req.query.sort, | |
114 | search: req.query.search | |
115 | }, 'filter:api.user.me.videos.list.params') | |
116 | ||
117 | const resultList = await Hooks.wrapPromiseFun( | |
118 | VideoModel.listUserVideosForApi, | |
119 | apiOptions, | |
120 | 'filter:api.user.me.videos.list.result' | |
d03cd8bb C |
121 | ) |
122 | ||
123 | const additionalAttributes = { | |
124 | waitTranscoding: true, | |
125 | state: true, | |
126 | scheduledUpdate: true, | |
127 | blacklistInfo: true | |
128 | } | |
129 | return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes })) | |
130 | } | |
131 | ||
dae86118 C |
132 | async function getUserVideoImports (req: express.Request, res: express.Response) { |
133 | const user = res.locals.oauth.token.User | |
d03cd8bb C |
134 | const resultList = await VideoImportModel.listUserVideoImportsForApi( |
135 | user.id, | |
136 | req.query.start as number, | |
137 | req.query.count as number, | |
138 | req.query.sort | |
139 | ) | |
140 | ||
141 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
142 | } | |
143 | ||
dae86118 | 144 | async function getUserInformation (req: express.Request, res: express.Response) { |
d03cd8bb | 145 | // We did not load channels in res.locals.user |
1acb9475 | 146 | const user = await UserModel.loadForMeAPI(res.locals.oauth.token.user.id) |
d03cd8bb | 147 | |
ac0868bc | 148 | return res.json(user.toMeFormattedJSON()) |
d03cd8bb C |
149 | } |
150 | ||
dae86118 | 151 | async function getUserVideoQuotaUsed (req: express.Request, res: express.Response) { |
ac0868bc | 152 | const user = res.locals.oauth.token.user |
fb719404 C |
153 | const videoQuotaUsed = await getOriginalVideoFileTotalFromUser(user) |
154 | const videoQuotaUsedDaily = await getOriginalVideoFileTotalDailyFromUser(user) | |
d03cd8bb C |
155 | |
156 | const data: UserVideoQuota = { | |
bee0abff FA |
157 | videoQuotaUsed, |
158 | videoQuotaUsedDaily | |
d03cd8bb C |
159 | } |
160 | return res.json(data) | |
161 | } | |
162 | ||
dae86118 | 163 | async function getUserVideoRating (req: express.Request, res: express.Response) { |
453e83ea | 164 | const videoId = res.locals.videoId.id |
d03cd8bb C |
165 | const accountId = +res.locals.oauth.token.User.Account.id |
166 | ||
167 | const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null) | |
168 | const rating = ratingObj ? ratingObj.type : 'none' | |
169 | ||
170 | const json: FormattedUserVideoRate = { | |
171 | videoId, | |
172 | rating | |
173 | } | |
06a05d5f | 174 | return res.json(json) |
d03cd8bb C |
175 | } |
176 | ||
177 | async function deleteMe (req: express.Request, res: express.Response) { | |
2dbc170d C |
178 | const user = await UserModel.loadByIdWithChannels(res.locals.oauth.token.User.id) |
179 | ||
180 | auditLogger.delete(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON())) | |
d03cd8bb C |
181 | |
182 | await user.destroy() | |
183 | ||
2d53be02 | 184 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) |
d03cd8bb C |
185 | } |
186 | ||
b426edd4 | 187 | async function updateMe (req: express.Request, res: express.Response) { |
d03cd8bb | 188 | const body: UserUpdateMe = req.body |
d1ab89de | 189 | let sendVerificationEmail = false |
d03cd8bb | 190 | |
dae86118 | 191 | const user = res.locals.oauth.token.user |
d03cd8bb C |
192 | |
193 | if (body.password !== undefined) user.password = body.password | |
d03cd8bb | 194 | if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy |
ed638e53 | 195 | if (body.webTorrentEnabled !== undefined) user.webTorrentEnabled = body.webTorrentEnabled |
d03cd8bb | 196 | if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo |
6aa54148 | 197 | if (body.autoPlayNextVideo !== undefined) user.autoPlayNextVideo = body.autoPlayNextVideo |
bee29df8 | 198 | if (body.autoPlayNextVideoPlaylist !== undefined) user.autoPlayNextVideoPlaylist = body.autoPlayNextVideoPlaylist |
8b9a525a | 199 | if (body.videosHistoryEnabled !== undefined) user.videosHistoryEnabled = body.videosHistoryEnabled |
3caf77d3 | 200 | if (body.videoLanguages !== undefined) user.videoLanguages = body.videoLanguages |
7cd4d2ba | 201 | if (body.theme !== undefined) user.theme = body.theme |
43d0ea7f C |
202 | if (body.noInstanceConfigWarningModal !== undefined) user.noInstanceConfigWarningModal = body.noInstanceConfigWarningModal |
203 | if (body.noWelcomeModal !== undefined) user.noWelcomeModal = body.noWelcomeModal | |
d03cd8bb | 204 | |
d1ab89de C |
205 | if (body.email !== undefined) { |
206 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) { | |
207 | user.pendingEmail = body.email | |
208 | sendVerificationEmail = true | |
209 | } else { | |
210 | user.email = body.email | |
211 | } | |
212 | } | |
213 | ||
589d9f55 C |
214 | await sequelizeTypescript.transaction(async t => { |
215 | await user.save({ transaction: t }) | |
91411dba | 216 | |
589d9f55 C |
217 | if (body.displayName !== undefined || body.description !== undefined) { |
218 | const userAccount = await AccountModel.load(user.Account.id, t) | |
d03cd8bb | 219 | |
43d0ea7f C |
220 | if (body.displayName !== undefined) userAccount.name = body.displayName |
221 | if (body.description !== undefined) userAccount.description = body.description | |
222 | await userAccount.save({ transaction: t }) | |
d03cd8bb | 223 | |
43d0ea7f | 224 | await sendUpdateActor(userAccount, t) |
589d9f55 C |
225 | } |
226 | }) | |
d03cd8bb | 227 | |
d1ab89de C |
228 | if (sendVerificationEmail === true) { |
229 | await sendVerifyUserEmail(user, true) | |
230 | } | |
231 | ||
2d53be02 | 232 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) |
d03cd8bb C |
233 | } |
234 | ||
6040f87d | 235 | async function updateMyAvatar (req: express.Request, res: express.Response) { |
a1587156 | 236 | const avatarPhysicalFile = req.files['avatarfile'][0] |
dae86118 | 237 | const user = res.locals.oauth.token.user |
d03cd8bb | 238 | |
91411dba | 239 | const userAccount = await AccountModel.load(user.Account.id) |
d03cd8bb | 240 | |
e08ff02a | 241 | const avatar = await updateLocalActorAvatarFile(userAccount, avatarPhysicalFile) |
91411dba | 242 | |
06a05d5f | 243 | return res.json({ avatar: avatar.toFormattedJSON() }) |
d03cd8bb | 244 | } |
1ea7da81 RK |
245 | |
246 | async function deleteMyAvatar (req: express.Request, res: express.Response) { | |
247 | const user = res.locals.oauth.token.user | |
248 | ||
249 | const userAccount = await AccountModel.load(user.Account.id) | |
e08ff02a | 250 | await deleteLocalActorAvatarFile(userAccount) |
1ea7da81 RK |
251 | |
252 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
253 | } |