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