]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { getFormattedObjects, getServerActor } from '../../helpers/utils' | |
3 | import { | |
4 | asyncMiddleware, | |
5 | authenticate, | |
6 | commonVideosFiltersValidator, | |
7 | optionalAuthenticate, | |
8 | paginationValidator, | |
9 | setDefaultPagination, | |
10 | setDefaultSort, | |
11 | videoPlaylistsSortValidator, | |
12 | videoRatesSortValidator, | |
13 | videoRatingValidator | |
14 | } from '../../middlewares' | |
15 | import { | |
16 | accountNameWithHostGetValidator, | |
17 | accountsSortValidator, | |
18 | ensureAuthUserOwnsAccountValidator, | |
19 | videosSortValidator, | |
20 | videoChannelsSortValidator | |
21 | } from '../../middlewares/validators' | |
22 | import { AccountModel } from '../../models/account/account' | |
23 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | |
24 | import { VideoModel } from '../../models/video/video' | |
25 | import { buildNSFWFilter, isUserAbleToSearchRemoteURI, getCountVideos } from '../../helpers/express-utils' | |
26 | import { VideoChannelModel } from '../../models/video/video-channel' | |
27 | import { JobQueue } from '../../lib/job-queue' | |
28 | import { logger } from '../../helpers/logger' | |
29 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | |
30 | import { | |
31 | commonVideoPlaylistFiltersValidator, | |
32 | videoPlaylistsSearchValidator | |
33 | } from '../../middlewares/validators/videos/video-playlists' | |
34 | ||
35 | const accountsRouter = express.Router() | |
36 | ||
37 | accountsRouter.get('/', | |
38 | paginationValidator, | |
39 | accountsSortValidator, | |
40 | setDefaultSort, | |
41 | setDefaultPagination, | |
42 | asyncMiddleware(listAccounts) | |
43 | ) | |
44 | ||
45 | accountsRouter.get('/:accountName', | |
46 | asyncMiddleware(accountNameWithHostGetValidator), | |
47 | getAccount | |
48 | ) | |
49 | ||
50 | accountsRouter.get('/:accountName/videos', | |
51 | asyncMiddleware(accountNameWithHostGetValidator), | |
52 | paginationValidator, | |
53 | videosSortValidator, | |
54 | setDefaultSort, | |
55 | setDefaultPagination, | |
56 | optionalAuthenticate, | |
57 | commonVideosFiltersValidator, | |
58 | asyncMiddleware(listAccountVideos) | |
59 | ) | |
60 | ||
61 | accountsRouter.get('/:accountName/video-channels', | |
62 | asyncMiddleware(accountNameWithHostGetValidator), | |
63 | paginationValidator, | |
64 | videoChannelsSortValidator, | |
65 | setDefaultSort, | |
66 | setDefaultPagination, | |
67 | asyncMiddleware(listAccountChannels) | |
68 | ) | |
69 | ||
70 | accountsRouter.get('/:accountName/video-playlists', | |
71 | optionalAuthenticate, | |
72 | asyncMiddleware(accountNameWithHostGetValidator), | |
73 | paginationValidator, | |
74 | videoPlaylistsSortValidator, | |
75 | setDefaultSort, | |
76 | setDefaultPagination, | |
77 | commonVideoPlaylistFiltersValidator, | |
78 | videoPlaylistsSearchValidator, | |
79 | asyncMiddleware(listAccountPlaylists) | |
80 | ) | |
81 | ||
82 | accountsRouter.get('/:accountName/ratings', | |
83 | authenticate, | |
84 | asyncMiddleware(accountNameWithHostGetValidator), | |
85 | ensureAuthUserOwnsAccountValidator, | |
86 | paginationValidator, | |
87 | videoRatesSortValidator, | |
88 | setDefaultSort, | |
89 | setDefaultPagination, | |
90 | videoRatingValidator, | |
91 | asyncMiddleware(listAccountRatings) | |
92 | ) | |
93 | ||
94 | // --------------------------------------------------------------------------- | |
95 | ||
96 | export { | |
97 | accountsRouter | |
98 | } | |
99 | ||
100 | // --------------------------------------------------------------------------- | |
101 | ||
102 | function getAccount (req: express.Request, res: express.Response) { | |
103 | const account = res.locals.account | |
104 | ||
105 | if (account.isOutdated()) { | |
106 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } }) | |
107 | .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err })) | |
108 | } | |
109 | ||
110 | return res.json(account.toFormattedJSON()) | |
111 | } | |
112 | ||
113 | async function listAccounts (req: express.Request, res: express.Response) { | |
114 | const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort) | |
115 | ||
116 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
117 | } | |
118 | ||
119 | async function listAccountChannels (req: express.Request, res: express.Response) { | |
120 | const options = { | |
121 | accountId: res.locals.account.id, | |
122 | start: req.query.start, | |
123 | count: req.query.count, | |
124 | sort: req.query.sort | |
125 | } | |
126 | ||
127 | const resultList = await VideoChannelModel.listByAccount(options) | |
128 | ||
129 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
130 | } | |
131 | ||
132 | async function listAccountPlaylists (req: express.Request, res: express.Response) { | |
133 | const serverActor = await getServerActor() | |
134 | ||
135 | // Allow users to see their private/unlisted video playlists | |
136 | let listMyPlaylists = false | |
137 | if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) { | |
138 | listMyPlaylists = true | |
139 | } | |
140 | ||
141 | const resultList = await VideoPlaylistModel.listForApi({ | |
142 | search: req.query.search, | |
143 | followerActorId: serverActor.id, | |
144 | start: req.query.start, | |
145 | count: req.query.count, | |
146 | sort: req.query.sort, | |
147 | accountId: res.locals.account.id, | |
148 | listMyPlaylists, | |
149 | type: req.query.playlistType | |
150 | }) | |
151 | ||
152 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
153 | } | |
154 | ||
155 | async function listAccountVideos (req: express.Request, res: express.Response) { | |
156 | const account = res.locals.account | |
157 | const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined | |
158 | const countVideos = getCountVideos(req) | |
159 | ||
160 | const resultList = await VideoModel.listForApi({ | |
161 | followerActorId, | |
162 | start: req.query.start, | |
163 | count: req.query.count, | |
164 | sort: req.query.sort, | |
165 | includeLocalVideos: true, | |
166 | categoryOneOf: req.query.categoryOneOf, | |
167 | licenceOneOf: req.query.licenceOneOf, | |
168 | languageOneOf: req.query.languageOneOf, | |
169 | tagsOneOf: req.query.tagsOneOf, | |
170 | tagsAllOf: req.query.tagsAllOf, | |
171 | filter: req.query.filter, | |
172 | nsfw: buildNSFWFilter(res, req.query.nsfw), | |
173 | withFiles: false, | |
174 | accountId: account.id, | |
175 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined, | |
176 | countVideos | |
177 | }) | |
178 | ||
179 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
180 | } | |
181 | ||
182 | async function listAccountRatings (req: express.Request, res: express.Response) { | |
183 | const account = res.locals.account | |
184 | ||
185 | const resultList = await AccountVideoRateModel.listByAccountForApi({ | |
186 | accountId: account.id, | |
187 | start: req.query.start, | |
188 | count: req.query.count, | |
189 | sort: req.query.sort, | |
190 | type: req.query.rating | |
191 | }) | |
192 | return res.json(getFormattedObjects(resultList.rows, resultList.count)) | |
193 | } |