]>
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, IMAGE_MIMETYPE_EXT, sequelizeTypescript } from '../../../initializers' | |
6 | import { sendUpdateActor } from '../../../lib/activitypub/send' | |
7 | import { | |
8 | asyncMiddleware, | |
9 | authenticate, | |
10 | commonVideosFiltersValidator, | |
11 | paginationValidator, | |
12 | setDefaultPagination, | |
13 | setDefaultSort, | |
14 | userSubscriptionAddValidator, | |
15 | userSubscriptionGetValidator, | |
16 | usersUpdateMeValidator, | |
17 | usersVideoRatingValidator | |
18 | } from '../../../middlewares' | |
19 | import { | |
20 | deleteMeValidator, | |
21 | userSubscriptionsSortValidator, | |
22 | videoImportsSortValidator, | |
23 | videosSortValidator, | |
24 | areSubscriptionsExistValidator | |
25 | } from '../../../middlewares/validators' | |
26 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
27 | import { UserModel } from '../../../models/account/user' | |
28 | import { VideoModel } from '../../../models/video/video' | |
29 | import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type' | |
30 | import { buildNSFWFilter, createReqFiles } from '../../../helpers/express-utils' | |
31 | import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model' | |
32 | import { updateAvatarValidator } from '../../../middlewares/validators/avatar' | |
33 | import { updateActorAvatarFile } from '../../../lib/avatar' | |
34 | import { auditLoggerFactory, UserAuditView } from '../../../helpers/audit-logger' | |
35 | import { VideoImportModel } from '../../../models/video/video-import' | |
36 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' | |
37 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
38 | import { JobQueue } from '../../../lib/job-queue' | |
39 | import { logger } from '../../../helpers/logger' | |
40 | ||
41 | const auditLogger = auditLoggerFactory('users-me') | |
42 | ||
43 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR }) | |
44 | ||
45 | const meRouter = express.Router() | |
46 | ||
47 | meRouter.get('/me', | |
48 | authenticate, | |
49 | asyncMiddleware(getUserInformation) | |
50 | ) | |
51 | meRouter.delete('/me', | |
52 | authenticate, | |
53 | asyncMiddleware(deleteMeValidator), | |
54 | asyncMiddleware(deleteMe) | |
55 | ) | |
56 | ||
57 | meRouter.get('/me/video-quota-used', | |
58 | authenticate, | |
59 | asyncMiddleware(getUserVideoQuotaUsed) | |
60 | ) | |
61 | ||
62 | meRouter.get('/me/videos/imports', | |
63 | authenticate, | |
64 | paginationValidator, | |
65 | videoImportsSortValidator, | |
66 | setDefaultSort, | |
67 | setDefaultPagination, | |
68 | asyncMiddleware(getUserVideoImports) | |
69 | ) | |
70 | ||
71 | meRouter.get('/me/videos', | |
72 | authenticate, | |
73 | paginationValidator, | |
74 | videosSortValidator, | |
75 | setDefaultSort, | |
76 | setDefaultPagination, | |
77 | asyncMiddleware(getUserVideos) | |
78 | ) | |
79 | ||
80 | meRouter.get('/me/videos/:videoId/rating', | |
81 | authenticate, | |
82 | asyncMiddleware(usersVideoRatingValidator), | |
83 | asyncMiddleware(getUserVideoRating) | |
84 | ) | |
85 | ||
86 | meRouter.put('/me', | |
87 | authenticate, | |
88 | usersUpdateMeValidator, | |
89 | asyncMiddleware(updateMe) | |
90 | ) | |
91 | ||
92 | meRouter.post('/me/avatar/pick', | |
93 | authenticate, | |
94 | reqAvatarFile, | |
95 | updateAvatarValidator, | |
96 | asyncMiddleware(updateMyAvatar) | |
97 | ) | |
98 | ||
99 | // ##### Subscriptions part ##### | |
100 | ||
101 | meRouter.get('/me/subscriptions/videos', | |
102 | authenticate, | |
103 | paginationValidator, | |
104 | videosSortValidator, | |
105 | setDefaultSort, | |
106 | setDefaultPagination, | |
107 | commonVideosFiltersValidator, | |
108 | asyncMiddleware(getUserSubscriptionVideos) | |
109 | ) | |
110 | ||
111 | meRouter.get('/me/subscriptions/exist', | |
112 | authenticate, | |
113 | areSubscriptionsExistValidator, | |
114 | asyncMiddleware(areSubscriptionsExist) | |
115 | ) | |
116 | ||
117 | meRouter.get('/me/subscriptions', | |
118 | authenticate, | |
119 | paginationValidator, | |
120 | userSubscriptionsSortValidator, | |
121 | setDefaultSort, | |
122 | setDefaultPagination, | |
123 | asyncMiddleware(getUserSubscriptions) | |
124 | ) | |
125 | ||
126 | meRouter.post('/me/subscriptions', | |
127 | authenticate, | |
128 | userSubscriptionAddValidator, | |
129 | asyncMiddleware(addUserSubscription) | |
130 | ) | |
131 | ||
132 | meRouter.get('/me/subscriptions/:uri', | |
133 | authenticate, | |
134 | userSubscriptionGetValidator, | |
135 | getUserSubscription | |
136 | ) | |
137 | ||
138 | meRouter.delete('/me/subscriptions/:uri', | |
139 | authenticate, | |
140 | userSubscriptionGetValidator, | |
141 | asyncMiddleware(deleteUserSubscription) | |
142 | ) | |
143 | ||
144 | // --------------------------------------------------------------------------- | |
145 | ||
146 | export { | |
147 | meRouter | |
148 | } | |
149 | ||
150 | // --------------------------------------------------------------------------- | |
151 | ||
152 | async function areSubscriptionsExist (req: express.Request, res: express.Response) { | |
153 | const uris = req.query.uris as string[] | |
154 | const user = res.locals.oauth.token.User as UserModel | |
155 | ||
156 | const handles = uris.map(u => { | |
157 | let [ name, host ] = u.split('@') | |
158 | if (host === CONFIG.WEBSERVER.HOST) host = null | |
159 | ||
160 | return { name, host, uri: u } | |
161 | }) | |
162 | ||
163 | const results = await ActorFollowModel.listSubscribedIn(user.Account.Actor.id, handles) | |
164 | ||
165 | const existObject: { [id: string ]: boolean } = {} | |
166 | for (const handle of handles) { | |
167 | const obj = results.find(r => { | |
168 | const server = r.ActorFollowing.Server | |
169 | ||
170 | return r.ActorFollowing.preferredUsername === handle.name && | |
171 | ( | |
172 | (!server && !handle.host) || | |
173 | (server.host === handle.host) | |
174 | ) | |
175 | }) | |
176 | ||
177 | existObject[handle.uri] = obj !== undefined | |
178 | } | |
179 | ||
180 | return res.json(existObject) | |
181 | } | |
182 | ||
183 | async function addUserSubscription (req: express.Request, res: express.Response) { | |
184 | const user = res.locals.oauth.token.User as UserModel | |
185 | const [ name, host ] = req.body.uri.split('@') | |
186 | ||
187 | const payload = { | |
188 | name, | |
189 | host, | |
190 | followerActorId: user.Account.Actor.id | |
191 | } | |
192 | ||
193 | JobQueue.Instance.createJob({ type: 'activitypub-follow', payload }) | |
194 | .catch(err => logger.error('Cannot create follow job for subscription %s.', req.body.uri, err)) | |
195 | ||
196 | return res.status(204).end() | |
197 | } | |
198 | ||
199 | function getUserSubscription (req: express.Request, res: express.Response) { | |
200 | const subscription: ActorFollowModel = res.locals.subscription | |
201 | ||
202 | return res.json(subscription.ActorFollowing.VideoChannel.toFormattedJSON()) | |
203 | } | |
204 | ||
205 | async function deleteUserSubscription (req: express.Request, res: express.Response) { | |
206 | const subscription: ActorFollowModel = res.locals.subscription | |
207 | ||
208 | await sequelizeTypescript.transaction(async t => { | |
209 | return subscription.destroy({ transaction: t }) | |
210 | }) | |
211 | ||
212 | return res.type('json').status(204).end() | |
213 | } | |
214 | ||
215 | async function getUserSubscriptions (req: express.Request, res: express.Response) { | |
216 | const user = res.locals.oauth.token.User as UserModel | |
217 | const actorId = user.Account.Actor.id | |
218 | ||
219 | const resultList = await ActorFollowModel.listSubscriptionsForApi(actorId, req.query.start, req.query.count, req.query.sort) | |
220 | ||
221 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
222 | } | |
223 | ||
224 | async function getUserSubscriptionVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | |
225 | const user = res.locals.oauth.token.User as UserModel | |
226 | const resultList = await VideoModel.listForApi({ | |
227 | start: req.query.start, | |
228 | count: req.query.count, | |
229 | sort: req.query.sort, | |
230 | includeLocalVideos: false, | |
231 | categoryOneOf: req.query.categoryOneOf, | |
232 | licenceOneOf: req.query.licenceOneOf, | |
233 | languageOneOf: req.query.languageOneOf, | |
234 | tagsOneOf: req.query.tagsOneOf, | |
235 | tagsAllOf: req.query.tagsAllOf, | |
236 | nsfw: buildNSFWFilter(res, req.query.nsfw), | |
237 | filter: req.query.filter as VideoFilter, | |
238 | withFiles: false, | |
239 | actorId: user.Account.Actor.id | |
240 | }) | |
241 | ||
242 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
243 | } | |
244 | ||
245 | async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | |
246 | const user = res.locals.oauth.token.User as UserModel | |
247 | const resultList = await VideoModel.listUserVideosForApi( | |
248 | user.Account.id, | |
249 | req.query.start as number, | |
250 | req.query.count as number, | |
251 | req.query.sort as VideoSortField | |
252 | ) | |
253 | ||
254 | const additionalAttributes = { | |
255 | waitTranscoding: true, | |
256 | state: true, | |
257 | scheduledUpdate: true, | |
258 | blacklistInfo: true | |
259 | } | |
260 | return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes })) | |
261 | } | |
262 | ||
263 | async function getUserVideoImports (req: express.Request, res: express.Response, next: express.NextFunction) { | |
264 | const user = res.locals.oauth.token.User as UserModel | |
265 | const resultList = await VideoImportModel.listUserVideoImportsForApi( | |
266 | user.id, | |
267 | req.query.start as number, | |
268 | req.query.count as number, | |
269 | req.query.sort | |
270 | ) | |
271 | ||
272 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
273 | } | |
274 | ||
275 | async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { | |
276 | // We did not load channels in res.locals.user | |
277 | const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) | |
278 | ||
279 | return res.json(user.toFormattedJSON()) | |
280 | } | |
281 | ||
282 | async function getUserVideoQuotaUsed (req: express.Request, res: express.Response, next: express.NextFunction) { | |
283 | // We did not load channels in res.locals.user | |
284 | const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) | |
285 | const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user) | |
286 | ||
287 | const data: UserVideoQuota = { | |
288 | videoQuotaUsed | |
289 | } | |
290 | return res.json(data) | |
291 | } | |
292 | ||
293 | async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) { | |
294 | const videoId = +req.params.videoId | |
295 | const accountId = +res.locals.oauth.token.User.Account.id | |
296 | ||
297 | const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null) | |
298 | const rating = ratingObj ? ratingObj.type : 'none' | |
299 | ||
300 | const json: FormattedUserVideoRate = { | |
301 | videoId, | |
302 | rating | |
303 | } | |
304 | return res.json(json) | |
305 | } | |
306 | ||
307 | async function deleteMe (req: express.Request, res: express.Response) { | |
308 | const user: UserModel = res.locals.oauth.token.User | |
309 | ||
310 | await user.destroy() | |
311 | ||
312 | auditLogger.delete(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new UserAuditView(user.toFormattedJSON())) | |
313 | ||
314 | return res.sendStatus(204) | |
315 | } | |
316 | ||
317 | async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) { | |
318 | const body: UserUpdateMe = req.body | |
319 | ||
320 | const user: UserModel = res.locals.oauth.token.user | |
321 | const oldUserAuditView = new UserAuditView(user.toFormattedJSON()) | |
322 | ||
323 | if (body.password !== undefined) user.password = body.password | |
324 | if (body.email !== undefined) user.email = body.email | |
325 | if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy | |
326 | if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo | |
327 | ||
328 | await sequelizeTypescript.transaction(async t => { | |
329 | await user.save({ transaction: t }) | |
330 | ||
331 | if (body.displayName !== undefined) user.Account.name = body.displayName | |
332 | if (body.description !== undefined) user.Account.description = body.description | |
333 | await user.Account.save({ transaction: t }) | |
334 | ||
335 | await sendUpdateActor(user.Account, t) | |
336 | ||
337 | auditLogger.update( | |
338 | res.locals.oauth.token.User.Account.Actor.getIdentifier(), | |
339 | new UserAuditView(user.toFormattedJSON()), | |
340 | oldUserAuditView | |
341 | ) | |
342 | }) | |
343 | ||
344 | return res.sendStatus(204) | |
345 | } | |
346 | ||
347 | async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { | |
348 | const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] | |
349 | const user: UserModel = res.locals.oauth.token.user | |
350 | const oldUserAuditView = new UserAuditView(user.toFormattedJSON()) | |
351 | const account = user.Account | |
352 | ||
353 | const avatar = await updateActorAvatarFile(avatarPhysicalFile, account.Actor, account) | |
354 | ||
355 | auditLogger.update( | |
356 | res.locals.oauth.token.User.Account.Actor.getIdentifier(), | |
357 | new UserAuditView(user.toFormattedJSON()), | |
358 | oldUserAuditView | |
359 | ) | |
360 | ||
361 | return res.json({ avatar: avatar.toFormattedJSON() }) | |
362 | } |