]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { getServerActor } from '@server/models/application/application' | |
3 | import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils' | |
4 | import { getFormattedObjects } from '../../helpers/utils' | |
5 | import { JobQueue } from '../../lib/job-queue' | |
6 | import { | |
7 | asyncMiddleware, | |
8 | authenticate, | |
9 | commonVideosFiltersValidator, | |
10 | optionalAuthenticate, | |
11 | paginationValidator, | |
12 | setDefaultPagination, | |
13 | setDefaultSort, | |
14 | setDefaultVideosSort, | |
15 | videoPlaylistsSortValidator, | |
16 | videoRatesSortValidator, | |
17 | videoRatingValidator | |
18 | } from '../../middlewares' | |
19 | import { | |
20 | accountNameWithHostGetValidator, | |
21 | accountsSortValidator, | |
22 | ensureAuthUserOwnsAccountValidator, | |
23 | videoChannelsSortValidator, | |
24 | videoChannelStatsValidator, | |
25 | videosSortValidator | |
26 | } from '../../middlewares/validators' | |
27 | import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists' | |
28 | import { AccountModel } from '../../models/account/account' | |
29 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | |
30 | import { VideoModel } from '../../models/video/video' | |
31 | import { VideoChannelModel } from '../../models/video/video-channel' | |
32 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | |
33 | ||
34 | const accountsRouter = express.Router() | |
35 | ||
36 | accountsRouter.get('/', | |
37 | paginationValidator, | |
38 | accountsSortValidator, | |
39 | setDefaultSort, | |
40 | setDefaultPagination, | |
41 | asyncMiddleware(listAccounts) | |
42 | ) | |
43 | ||
44 | accountsRouter.get('/:accountName', | |
45 | asyncMiddleware(accountNameWithHostGetValidator), | |
46 | getAccount | |
47 | ) | |
48 | ||
49 | accountsRouter.get('/:accountName/videos', | |
50 | asyncMiddleware(accountNameWithHostGetValidator), | |
51 | paginationValidator, | |
52 | videosSortValidator, | |
53 | setDefaultVideosSort, | |
54 | setDefaultPagination, | |
55 | optionalAuthenticate, | |
56 | commonVideosFiltersValidator, | |
57 | asyncMiddleware(listAccountVideos) | |
58 | ) | |
59 | ||
60 | accountsRouter.get('/:accountName/video-channels', | |
61 | asyncMiddleware(accountNameWithHostGetValidator), | |
62 | videoChannelStatsValidator, | |
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 | } | |
108 | ||
109 | return res.json(account.toFormattedJSON()) | |
110 | } | |
111 | ||
112 | async function listAccounts (req: express.Request, res: express.Response) { | |
113 | const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort) | |
114 | ||
115 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
116 | } | |
117 | ||
118 | async function listAccountChannels (req: express.Request, res: express.Response) { | |
119 | const options = { | |
120 | accountId: res.locals.account.id, | |
121 | start: req.query.start, | |
122 | count: req.query.count, | |
123 | sort: req.query.sort, | |
124 | withStats: req.query.withStats, | |
125 | search: req.query.search | |
126 | } | |
127 | ||
128 | const resultList = await VideoChannelModel.listByAccount(options) | |
129 | ||
130 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
131 | } | |
132 | ||
133 | async function listAccountPlaylists (req: express.Request, res: express.Response) { | |
134 | const serverActor = await getServerActor() | |
135 | ||
136 | // Allow users to see their private/unlisted video playlists | |
137 | let listMyPlaylists = false | |
138 | if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) { | |
139 | listMyPlaylists = true | |
140 | } | |
141 | ||
142 | const resultList = await VideoPlaylistModel.listForApi({ | |
143 | search: req.query.search, | |
144 | followerActorId: serverActor.id, | |
145 | start: req.query.start, | |
146 | count: req.query.count, | |
147 | sort: req.query.sort, | |
148 | accountId: res.locals.account.id, | |
149 | listMyPlaylists, | |
150 | type: req.query.playlistType | |
151 | }) | |
152 | ||
153 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
154 | } | |
155 | ||
156 | async function listAccountVideos (req: express.Request, res: express.Response) { | |
157 | const account = res.locals.account | |
158 | const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined | |
159 | const countVideos = getCountVideos(req) | |
160 | ||
161 | const resultList = await VideoModel.listForApi({ | |
162 | followerActorId, | |
163 | start: req.query.start, | |
164 | count: req.query.count, | |
165 | sort: req.query.sort, | |
166 | includeLocalVideos: true, | |
167 | categoryOneOf: req.query.categoryOneOf, | |
168 | licenceOneOf: req.query.licenceOneOf, | |
169 | languageOneOf: req.query.languageOneOf, | |
170 | tagsOneOf: req.query.tagsOneOf, | |
171 | tagsAllOf: req.query.tagsAllOf, | |
172 | filter: req.query.filter, | |
173 | nsfw: buildNSFWFilter(res, req.query.nsfw), | |
174 | withFiles: false, | |
175 | accountId: account.id, | |
176 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined, | |
177 | countVideos | |
178 | }) | |
179 | ||
180 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
181 | } | |
182 | ||
183 | async function listAccountRatings (req: express.Request, res: express.Response) { | |
184 | const account = res.locals.account | |
185 | ||
186 | const resultList = await AccountVideoRateModel.listByAccountForApi({ | |
187 | accountId: account.id, | |
188 | start: req.query.start, | |
189 | count: req.query.count, | |
190 | sort: req.query.sort, | |
191 | type: req.query.rating | |
192 | }) | |
193 | return res.json(getFormattedObjects(resultList.rows, resultList.count)) | |
194 | } |