]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import 'multer' | |
3 | import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../../shared' | |
4 | import { getFormattedObjects } from '../../../helpers/utils' | |
5 | import { CONFIG, MIMETYPES, sequelizeTypescript } from '../../../initializers' | |
6 | import { sendUpdateActor } from '../../../lib/activitypub/send' | |
7 | import { | |
8 | asyncMiddleware, | |
9 | asyncRetryTransactionMiddleware, | |
10 | authenticate, | |
11 | paginationValidator, | |
12 | setDefaultPagination, | |
13 | setDefaultSort, | |
14 | usersUpdateMeValidator, | |
15 | usersVideoRatingValidator | |
16 | } from '../../../middlewares' | |
17 | import { deleteMeValidator, videoImportsSortValidator, videosSortValidator } from '../../../middlewares/validators' | |
18 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
19 | import { UserModel } from '../../../models/account/user' | |
20 | import { VideoModel } from '../../../models/video/video' | |
21 | import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type' | |
22 | import { createReqFiles } from '../../../helpers/express-utils' | |
23 | import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model' | |
24 | import { updateAvatarValidator } from '../../../middlewares/validators/avatar' | |
25 | import { updateActorAvatarFile } from '../../../lib/avatar' | |
26 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' | |
27 | import { VideoImportModel } from '../../../models/video/video-import' | |
28 | import { AccountModel } from '../../../models/account/account' | |
29 | ||
30 | const auditLogger = auditLoggerFactory('users-me') | |
31 | ||
32 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) | |
33 | ||
34 | const meRouter = express.Router() | |
35 | ||
36 | meRouter.get('/me', | |
37 | authenticate, | |
38 | asyncMiddleware(getUserInformation) | |
39 | ) | |
40 | meRouter.delete('/me', | |
41 | authenticate, | |
42 | asyncMiddleware(deleteMeValidator), | |
43 | asyncMiddleware(deleteMe) | |
44 | ) | |
45 | ||
46 | meRouter.get('/me/video-quota-used', | |
47 | authenticate, | |
48 | asyncMiddleware(getUserVideoQuotaUsed) | |
49 | ) | |
50 | ||
51 | meRouter.get('/me/videos/imports', | |
52 | authenticate, | |
53 | paginationValidator, | |
54 | videoImportsSortValidator, | |
55 | setDefaultSort, | |
56 | setDefaultPagination, | |
57 | asyncMiddleware(getUserVideoImports) | |
58 | ) | |
59 | ||
60 | meRouter.get('/me/videos', | |
61 | authenticate, | |
62 | paginationValidator, | |
63 | videosSortValidator, | |
64 | setDefaultSort, | |
65 | setDefaultPagination, | |
66 | asyncMiddleware(getUserVideos) | |
67 | ) | |
68 | ||
69 | meRouter.get('/me/videos/:videoId/rating', | |
70 | authenticate, | |
71 | asyncMiddleware(usersVideoRatingValidator), | |
72 | asyncMiddleware(getUserVideoRating) | |
73 | ) | |
74 | ||
75 | meRouter.put('/me', | |
76 | authenticate, | |
77 | asyncMiddleware(usersUpdateMeValidator), | |
78 | asyncRetryTransactionMiddleware(updateMe) | |
79 | ) | |
80 | ||
81 | meRouter.post('/me/avatar/pick', | |
82 | authenticate, | |
83 | reqAvatarFile, | |
84 | updateAvatarValidator, | |
85 | asyncRetryTransactionMiddleware(updateMyAvatar) | |
86 | ) | |
87 | ||
88 | // --------------------------------------------------------------------------- | |
89 | ||
90 | export { | |
91 | meRouter | |
92 | } | |
93 | ||
94 | // --------------------------------------------------------------------------- | |
95 | ||
96 | async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | |
97 | const user = res.locals.oauth.token.User as UserModel | |
98 | const resultList = await VideoModel.listUserVideosForApi( | |
99 | user.Account.id, | |
100 | req.query.start as number, | |
101 | req.query.count as number, | |
102 | req.query.sort as VideoSortField | |
103 | ) | |
104 | ||
105 | const additionalAttributes = { | |
106 | waitTranscoding: true, | |
107 | state: true, | |
108 | scheduledUpdate: true, | |
109 | blacklistInfo: true | |
110 | } | |
111 | return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes })) | |
112 | } | |
113 | ||
114 | async function getUserVideoImports (req: express.Request, res: express.Response, next: express.NextFunction) { | |
115 | const user = res.locals.oauth.token.User as UserModel | |
116 | const resultList = await VideoImportModel.listUserVideoImportsForApi( | |
117 | user.id, | |
118 | req.query.start as number, | |
119 | req.query.count as number, | |
120 | req.query.sort | |
121 | ) | |
122 | ||
123 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
124 | } | |
125 | ||
126 | async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { | |
127 | // We did not load channels in res.locals.user | |
128 | const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) | |
129 | ||
130 | return res.json(user.toFormattedJSON()) | |
131 | } | |
132 | ||
133 | async function getUserVideoQuotaUsed (req: express.Request, res: express.Response, next: express.NextFunction) { | |
134 | // We did not load channels in res.locals.user | |
135 | const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) | |
136 | const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user) | |
137 | const videoQuotaUsedDaily = await UserModel.getOriginalVideoFileTotalDailyFromUser(user) | |
138 | ||
139 | const data: UserVideoQuota = { | |
140 | videoQuotaUsed, | |
141 | videoQuotaUsedDaily | |
142 | } | |
143 | return res.json(data) | |
144 | } | |
145 | ||
146 | async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) { | |
147 | const videoId = res.locals.video.id | |
148 | const accountId = +res.locals.oauth.token.User.Account.id | |
149 | ||
150 | const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null) | |
151 | const rating = ratingObj ? ratingObj.type : 'none' | |
152 | ||
153 | const json: FormattedUserVideoRate = { | |
154 | videoId, | |
155 | rating | |
156 | } | |
157 | return res.json(json) | |
158 | } | |
159 | ||
160 | async function deleteMe (req: express.Request, res: express.Response) { | |
161 | const user: UserModel = res.locals.oauth.token.User | |
162 | ||
163 | await user.destroy() | |
164 | ||
165 | auditLogger.delete(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON())) | |
166 | ||
167 | return res.sendStatus(204) | |
168 | } | |
169 | ||
170 | async function updateMe (req: express.Request, res: express.Response) { | |
171 | const body: UserUpdateMe = req.body | |
172 | ||
173 | const user: UserModel = res.locals.oauth.token.user | |
174 | const oldUserAuditView = new UserAuditView(user.toFormattedJSON()) | |
175 | ||
176 | if (body.password !== undefined) user.password = body.password | |
177 | if (body.email !== undefined) user.email = body.email | |
178 | if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy | |
179 | if (body.webTorrentEnabled !== undefined) user.webTorrentEnabled = body.webTorrentEnabled | |
180 | if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo | |
181 | if (body.videosHistoryEnabled !== undefined) user.videosHistoryEnabled = body.videosHistoryEnabled | |
182 | ||
183 | await sequelizeTypescript.transaction(async t => { | |
184 | const userAccount = await AccountModel.load(user.Account.id) | |
185 | ||
186 | await user.save({ transaction: t }) | |
187 | ||
188 | if (body.displayName !== undefined) userAccount.name = body.displayName | |
189 | if (body.description !== undefined) userAccount.description = body.description | |
190 | await userAccount.save({ transaction: t }) | |
191 | ||
192 | await sendUpdateActor(userAccount, t) | |
193 | ||
194 | auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON()), oldUserAuditView) | |
195 | }) | |
196 | ||
197 | return res.sendStatus(204) | |
198 | } | |
199 | ||
200 | async function updateMyAvatar (req: express.Request, res: express.Response) { | |
201 | const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] | |
202 | const user: UserModel = res.locals.oauth.token.user | |
203 | const oldUserAuditView = new UserAuditView(user.toFormattedJSON()) | |
204 | ||
205 | const userAccount = await AccountModel.load(user.Account.id) | |
206 | ||
207 | const avatar = await updateActorAvatarFile(avatarPhysicalFile, userAccount) | |
208 | ||
209 | auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON()), oldUserAuditView) | |
210 | ||
211 | return res.json({ avatar: avatar.toFormattedJSON() }) | |
212 | } |