]>
Commit | Line | Data |
---|---|---|
265ba139 | 1 | import * as express from 'express' |
cc918ac3 | 2 | import { getFormattedObjects } from '../../helpers/utils' |
48dce1c9 | 3 | import { |
7ad9b984 C |
4 | asyncMiddleware, |
5 | commonVideosFiltersValidator, | |
48dce1c9 C |
6 | listVideoAccountChannelsValidator, |
7 | optionalAuthenticate, | |
8 | paginationValidator, | |
9 | setDefaultPagination, | |
cc918ac3 | 10 | setDefaultSort |
48dce1c9 | 11 | } from '../../middlewares' |
ad9e39fb | 12 | import { accountsNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators' |
265ba139 | 13 | import { AccountModel } from '../../models/account/account' |
0626e7af | 14 | import { VideoModel } from '../../models/video/video' |
687d638c | 15 | import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' |
48dce1c9 | 16 | import { VideoChannelModel } from '../../models/video/video-channel' |
744d0eca C |
17 | import { JobQueue } from '../../lib/job-queue' |
18 | import { logger } from '../../helpers/logger' | |
265ba139 C |
19 | |
20 | const accountsRouter = express.Router() | |
21 | ||
22 | accountsRouter.get('/', | |
23 | paginationValidator, | |
24 | accountsSortValidator, | |
1174a847 | 25 | setDefaultSort, |
f05a1c30 | 26 | setDefaultPagination, |
265ba139 C |
27 | asyncMiddleware(listAccounts) |
28 | ) | |
29 | ||
ad9e39fb C |
30 | accountsRouter.get('/:accountName', |
31 | asyncMiddleware(accountsNameWithHostGetValidator), | |
265ba139 C |
32 | getAccount |
33 | ) | |
34 | ||
ad9e39fb C |
35 | accountsRouter.get('/:accountName/videos', |
36 | asyncMiddleware(accountsNameWithHostGetValidator), | |
0626e7af C |
37 | paginationValidator, |
38 | videosSortValidator, | |
39 | setDefaultSort, | |
40 | setDefaultPagination, | |
41 | optionalAuthenticate, | |
d525fc39 | 42 | commonVideosFiltersValidator, |
48dce1c9 C |
43 | asyncMiddleware(listAccountVideos) |
44 | ) | |
45 | ||
ad9e39fb | 46 | accountsRouter.get('/:accountName/video-channels', |
48dce1c9 C |
47 | asyncMiddleware(listVideoAccountChannelsValidator), |
48 | asyncMiddleware(listVideoAccountChannels) | |
49 | ) | |
50 | ||
265ba139 C |
51 | // --------------------------------------------------------------------------- |
52 | ||
53 | export { | |
54 | accountsRouter | |
55 | } | |
56 | ||
57 | // --------------------------------------------------------------------------- | |
58 | ||
59 | function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) { | |
0626e7af C |
60 | const account: AccountModel = res.locals.account |
61 | ||
744d0eca C |
62 | if (account.isOutdated()) { |
63 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } }) | |
64 | .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err })) | |
65 | } | |
66 | ||
0626e7af | 67 | return res.json(account.toFormattedJSON()) |
265ba139 C |
68 | } |
69 | ||
70 | async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) { | |
71 | const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort) | |
72 | ||
73 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
74 | } | |
0626e7af | 75 | |
48dce1c9 C |
76 | async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) { |
77 | const resultList = await VideoChannelModel.listByAccount(res.locals.account.id) | |
78 | ||
79 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
80 | } | |
81 | ||
48dce1c9 | 82 | async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
0626e7af | 83 | const account: AccountModel = res.locals.account |
4e74e803 | 84 | const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined |
0626e7af | 85 | |
48dce1c9 | 86 | const resultList = await VideoModel.listForApi({ |
4e74e803 | 87 | followerActorId, |
48dce1c9 C |
88 | start: req.query.start, |
89 | count: req.query.count, | |
90 | sort: req.query.sort, | |
8a19bee1 | 91 | includeLocalVideos: true, |
d525fc39 C |
92 | categoryOneOf: req.query.categoryOneOf, |
93 | licenceOneOf: req.query.licenceOneOf, | |
94 | languageOneOf: req.query.languageOneOf, | |
95 | tagsOneOf: req.query.tagsOneOf, | |
96 | tagsAllOf: req.query.tagsAllOf, | |
1cd3facc | 97 | filter: req.query.filter, |
d525fc39 | 98 | nsfw: buildNSFWFilter(res, req.query.nsfw), |
48dce1c9 | 99 | withFiles: false, |
1cd3facc | 100 | accountId: account.id, |
7ad9b984 | 101 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined |
48dce1c9 | 102 | }) |
0626e7af C |
103 | |
104 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
105 | } |