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