]>
Commit | Line | Data |
---|---|---|
265ba139 | 1 | import * as express from 'express' |
418d092a | 2 | import { getFormattedObjects, getServerActor } from '../../helpers/utils' |
48dce1c9 | 3 | import { |
7ad9b984 C |
4 | asyncMiddleware, |
5 | commonVideosFiltersValidator, | |
48dce1c9 C |
6 | optionalAuthenticate, |
7 | paginationValidator, | |
8 | setDefaultPagination, | |
418d092a C |
9 | setDefaultSort, |
10 | videoPlaylistsSortValidator | |
48dce1c9 | 11 | } from '../../middlewares' |
418d092a | 12 | import { accountNameWithHostGetValidator, 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' | |
418d092a | 19 | import { VideoPlaylistModel } from '../../models/video/video-playlist' |
df0b219d | 20 | import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists' |
265ba139 C |
21 | |
22 | const accountsRouter = express.Router() | |
23 | ||
24 | accountsRouter.get('/', | |
25 | paginationValidator, | |
26 | accountsSortValidator, | |
1174a847 | 27 | setDefaultSort, |
f05a1c30 | 28 | setDefaultPagination, |
265ba139 C |
29 | asyncMiddleware(listAccounts) |
30 | ) | |
31 | ||
ad9e39fb | 32 | accountsRouter.get('/:accountName', |
418d092a | 33 | asyncMiddleware(accountNameWithHostGetValidator), |
265ba139 C |
34 | getAccount |
35 | ) | |
36 | ||
ad9e39fb | 37 | accountsRouter.get('/:accountName/videos', |
418d092a | 38 | asyncMiddleware(accountNameWithHostGetValidator), |
0626e7af C |
39 | paginationValidator, |
40 | videosSortValidator, | |
41 | setDefaultSort, | |
42 | setDefaultPagination, | |
43 | optionalAuthenticate, | |
d525fc39 | 44 | commonVideosFiltersValidator, |
48dce1c9 C |
45 | asyncMiddleware(listAccountVideos) |
46 | ) | |
47 | ||
ad9e39fb | 48 | accountsRouter.get('/:accountName/video-channels', |
418d092a C |
49 | asyncMiddleware(accountNameWithHostGetValidator), |
50 | asyncMiddleware(listAccountChannels) | |
51 | ) | |
52 | ||
53 | accountsRouter.get('/:accountName/video-playlists', | |
54 | optionalAuthenticate, | |
55 | asyncMiddleware(accountNameWithHostGetValidator), | |
56 | paginationValidator, | |
57 | videoPlaylistsSortValidator, | |
58 | setDefaultSort, | |
59 | setDefaultPagination, | |
df0b219d | 60 | commonVideoPlaylistFiltersValidator, |
418d092a | 61 | asyncMiddleware(listAccountPlaylists) |
48dce1c9 C |
62 | ) |
63 | ||
265ba139 C |
64 | // --------------------------------------------------------------------------- |
65 | ||
66 | export { | |
67 | accountsRouter | |
68 | } | |
69 | ||
70 | // --------------------------------------------------------------------------- | |
71 | ||
418d092a | 72 | function getAccount (req: express.Request, res: express.Response) { |
dae86118 | 73 | const account = res.locals.account |
0626e7af | 74 | |
744d0eca C |
75 | if (account.isOutdated()) { |
76 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } }) | |
77 | .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err })) | |
78 | } | |
79 | ||
0626e7af | 80 | return res.json(account.toFormattedJSON()) |
265ba139 C |
81 | } |
82 | ||
418d092a | 83 | async function listAccounts (req: express.Request, res: express.Response) { |
265ba139 C |
84 | const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort) |
85 | ||
86 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
87 | } | |
0626e7af | 88 | |
418d092a | 89 | async function listAccountChannels (req: express.Request, res: express.Response) { |
48dce1c9 C |
90 | const resultList = await VideoChannelModel.listByAccount(res.locals.account.id) |
91 | ||
92 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
93 | } | |
94 | ||
418d092a C |
95 | async function listAccountPlaylists (req: express.Request, res: express.Response) { |
96 | const serverActor = await getServerActor() | |
97 | ||
98 | // Allow users to see their private/unlisted video playlists | |
99 | let privateAndUnlisted = false | |
dae86118 | 100 | if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) { |
418d092a C |
101 | privateAndUnlisted = true |
102 | } | |
103 | ||
104 | const resultList = await VideoPlaylistModel.listForApi({ | |
105 | followerActorId: serverActor.id, | |
106 | start: req.query.start, | |
107 | count: req.query.count, | |
108 | sort: req.query.sort, | |
109 | accountId: res.locals.account.id, | |
df0b219d C |
110 | privateAndUnlisted, |
111 | type: req.query.playlistType | |
418d092a C |
112 | }) |
113 | ||
114 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
115 | } | |
116 | ||
117 | async function listAccountVideos (req: express.Request, res: express.Response) { | |
dae86118 | 118 | const account = res.locals.account |
4e74e803 | 119 | const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined |
0626e7af | 120 | |
48dce1c9 | 121 | const resultList = await VideoModel.listForApi({ |
4e74e803 | 122 | followerActorId, |
48dce1c9 C |
123 | start: req.query.start, |
124 | count: req.query.count, | |
125 | sort: req.query.sort, | |
8a19bee1 | 126 | includeLocalVideos: true, |
d525fc39 C |
127 | categoryOneOf: req.query.categoryOneOf, |
128 | licenceOneOf: req.query.licenceOneOf, | |
129 | languageOneOf: req.query.languageOneOf, | |
130 | tagsOneOf: req.query.tagsOneOf, | |
131 | tagsAllOf: req.query.tagsAllOf, | |
1cd3facc | 132 | filter: req.query.filter, |
d525fc39 | 133 | nsfw: buildNSFWFilter(res, req.query.nsfw), |
48dce1c9 | 134 | withFiles: false, |
1cd3facc | 135 | accountId: account.id, |
7ad9b984 | 136 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined |
48dce1c9 | 137 | }) |
0626e7af C |
138 | |
139 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
140 | } |