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