]>
Commit | Line | Data |
---|---|---|
41fb13c3 C |
1 | import cors from 'cors' |
2 | import express from 'express' | |
7e98a7df C |
3 | import { activityPubCollectionPagination } from '@server/lib/activitypub/collection' |
4 | import { activityPubContextify } from '@server/lib/activitypub/context' | |
053aed43 | 5 | import { getServerActor } from '@server/models/application/application' |
55d5fbc5 | 6 | import { MAccountId, MActorId, MChannelId, MVideoId } from '@server/types/models' |
8fffe21a | 7 | import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' |
053aed43 | 8 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' |
74dc3bca | 9 | import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants' |
e251f170 | 10 | import { audiencify, getAudience } from '../../lib/activitypub/audience' |
053aed43 | 11 | import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send' |
c48e82b5 | 12 | import { buildCreateActivity } from '../../lib/activitypub/send/send-create' |
053aed43 C |
13 | import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike' |
14 | import { | |
de94ac86 C |
15 | getLocalVideoCommentsActivityPubUrl, |
16 | getLocalVideoDislikesActivityPubUrl, | |
17 | getLocalVideoLikesActivityPubUrl, | |
18 | getLocalVideoSharesActivityPubUrl | |
053aed43 | 19 | } from '../../lib/activitypub/url' |
96f29c0f C |
20 | import { |
21 | asyncMiddleware, | |
d0800f76 | 22 | ensureIsLocalChannel, |
96f29c0f C |
23 | executeIfActivityPub, |
24 | localAccountValidator, | |
a37e9e74 | 25 | videoChannelsNameWithHostValidator, |
1e7eb25f C |
26 | videosCustomGetValidator, |
27 | videosShareValidator | |
96f29c0f | 28 | } from '../../middlewares' |
20bafcb6 | 29 | import { cacheRoute } from '../../middlewares/cache/cache' |
b2111066 | 30 | import { getAccountVideoRateValidatorFactory, getVideoLocalViewerValidator, videoCommentGetValidator } from '../../middlewares/validators' |
053aed43 C |
31 | import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy' |
32 | import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists' | |
3fd3ab2d | 33 | import { AccountModel } from '../../models/account/account' |
053aed43 | 34 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' |
7d9ba5c0 | 35 | import { ActorFollowModel } from '../../models/actor/actor-follow' |
053aed43 | 36 | import { VideoCaptionModel } from '../../models/video/video-caption' |
da854ddd | 37 | import { VideoCommentModel } from '../../models/video/video-comment' |
053aed43 | 38 | import { VideoPlaylistModel } from '../../models/video/video-playlist' |
3fd3ab2d | 39 | import { VideoShareModel } from '../../models/video/video-share' |
8fffe21a | 40 | import { activityPubResponse } from './utils' |
e4f97bab C |
41 | |
42 | const activityPubClientRouter = express.Router() | |
670e955c | 43 | activityPubClientRouter.use(cors()) |
e4f97bab | 44 | |
2c8776fc C |
45 | // Intercept ActivityPub client requests |
46 | ||
70fdff3d | 47 | activityPubClientRouter.get( |
559985b5 | 48 | [ '/accounts?/:name', '/accounts?/:name/video-channels', '/a/:name', '/a/:name/video-channels' ], |
e65c0c5b C |
49 | executeIfActivityPub, |
50 | asyncMiddleware(localAccountValidator), | |
51 | accountController | |
e4f97bab | 52 | ) |
7006bc63 | 53 | activityPubClientRouter.get('/accounts?/:name/followers', |
e65c0c5b C |
54 | executeIfActivityPub, |
55 | asyncMiddleware(localAccountValidator), | |
56 | asyncMiddleware(accountFollowersController) | |
e4f97bab | 57 | ) |
7006bc63 | 58 | activityPubClientRouter.get('/accounts?/:name/following', |
e65c0c5b C |
59 | executeIfActivityPub, |
60 | asyncMiddleware(localAccountValidator), | |
61 | asyncMiddleware(accountFollowingController) | |
e4f97bab | 62 | ) |
418d092a | 63 | activityPubClientRouter.get('/accounts?/:name/playlists', |
e65c0c5b C |
64 | executeIfActivityPub, |
65 | asyncMiddleware(localAccountValidator), | |
66 | asyncMiddleware(accountPlaylistsController) | |
418d092a | 67 | ) |
5c6d985f | 68 | activityPubClientRouter.get('/accounts?/:name/likes/:videoId', |
e65c0c5b | 69 | executeIfActivityPub, |
57e4e1c1 | 70 | cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS), |
75ba887d C |
71 | asyncMiddleware(getAccountVideoRateValidatorFactory('like')), |
72 | getAccountVideoRateFactory('like') | |
5c6d985f C |
73 | ) |
74 | activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId', | |
e65c0c5b | 75 | executeIfActivityPub, |
57e4e1c1 | 76 | cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS), |
75ba887d C |
77 | asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')), |
78 | getAccountVideoRateFactory('dislike') | |
5c6d985f | 79 | ) |
e4f97bab | 80 | |
559985b5 C |
81 | activityPubClientRouter.get( |
82 | [ '/videos/watch/:id', '/w/:id' ], | |
e65c0c5b | 83 | executeIfActivityPub, |
20bafcb6 | 84 | cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS), |
71d4af1e | 85 | asyncMiddleware(videosCustomGetValidator('all')), |
e65c0c5b | 86 | asyncMiddleware(videoController) |
4e50b6a1 | 87 | ) |
296c0905 | 88 | activityPubClientRouter.get('/videos/watch/:id/activity', |
e65c0c5b | 89 | executeIfActivityPub, |
71d4af1e | 90 | asyncMiddleware(videosCustomGetValidator('all')), |
e65c0c5b | 91 | asyncMiddleware(videoController) |
296c0905 | 92 | ) |
46531a0a | 93 | activityPubClientRouter.get('/videos/watch/:id/announces', |
e65c0c5b | 94 | executeIfActivityPub, |
7eba5e1f | 95 | asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), |
e65c0c5b | 96 | asyncMiddleware(videoAnnouncesController) |
46531a0a | 97 | ) |
5c6d985f | 98 | activityPubClientRouter.get('/videos/watch/:id/announces/:actorId', |
e65c0c5b C |
99 | executeIfActivityPub, |
100 | asyncMiddleware(videosShareValidator), | |
101 | asyncMiddleware(videoAnnounceController) | |
20494f12 | 102 | ) |
46531a0a | 103 | activityPubClientRouter.get('/videos/watch/:id/likes', |
e65c0c5b | 104 | executeIfActivityPub, |
7eba5e1f | 105 | asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), |
e65c0c5b | 106 | asyncMiddleware(videoLikesController) |
46531a0a C |
107 | ) |
108 | activityPubClientRouter.get('/videos/watch/:id/dislikes', | |
e65c0c5b | 109 | executeIfActivityPub, |
7eba5e1f | 110 | asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), |
e65c0c5b | 111 | asyncMiddleware(videoDislikesController) |
46531a0a C |
112 | ) |
113 | activityPubClientRouter.get('/videos/watch/:id/comments', | |
e65c0c5b | 114 | executeIfActivityPub, |
7eba5e1f | 115 | asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), |
e65c0c5b | 116 | asyncMiddleware(videoCommentsController) |
46531a0a | 117 | ) |
da854ddd | 118 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', |
e65c0c5b C |
119 | executeIfActivityPub, |
120 | asyncMiddleware(videoCommentGetValidator), | |
121 | asyncMiddleware(videoCommentController) | |
da854ddd | 122 | ) |
296c0905 | 123 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity', |
e65c0c5b C |
124 | executeIfActivityPub, |
125 | asyncMiddleware(videoCommentGetValidator), | |
126 | asyncMiddleware(videoCommentController) | |
296c0905 | 127 | ) |
da854ddd | 128 | |
70fdff3d | 129 | activityPubClientRouter.get( |
a37e9e74 | 130 | [ '/video-channels/:nameWithHost', '/video-channels/:nameWithHost/videos', '/c/:nameWithHost', '/c/:nameWithHost/videos' ], |
e65c0c5b | 131 | executeIfActivityPub, |
a37e9e74 | 132 | asyncMiddleware(videoChannelsNameWithHostValidator), |
133 | ensureIsLocalChannel, | |
a1587156 | 134 | videoChannelController |
20494f12 | 135 | ) |
a37e9e74 | 136 | activityPubClientRouter.get('/video-channels/:nameWithHost/followers', |
e65c0c5b | 137 | executeIfActivityPub, |
a37e9e74 | 138 | asyncMiddleware(videoChannelsNameWithHostValidator), |
139 | ensureIsLocalChannel, | |
e65c0c5b | 140 | asyncMiddleware(videoChannelFollowersController) |
7006bc63 | 141 | ) |
a37e9e74 | 142 | activityPubClientRouter.get('/video-channels/:nameWithHost/following', |
e65c0c5b | 143 | executeIfActivityPub, |
a37e9e74 | 144 | asyncMiddleware(videoChannelsNameWithHostValidator), |
145 | ensureIsLocalChannel, | |
e65c0c5b | 146 | asyncMiddleware(videoChannelFollowingController) |
7006bc63 | 147 | ) |
a37e9e74 | 148 | activityPubClientRouter.get('/video-channels/:nameWithHost/playlists', |
7405b6ba | 149 | executeIfActivityPub, |
a37e9e74 | 150 | asyncMiddleware(videoChannelsNameWithHostValidator), |
151 | ensureIsLocalChannel, | |
7405b6ba C |
152 | asyncMiddleware(videoChannelPlaylistsController) |
153 | ) | |
20494f12 | 154 | |
c48e82b5 | 155 | activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?', |
e65c0c5b C |
156 | executeIfActivityPub, |
157 | asyncMiddleware(videoFileRedundancyGetValidator), | |
158 | asyncMiddleware(videoRedundancyController) | |
09209296 | 159 | ) |
9c6ca37f | 160 | activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId', |
e65c0c5b C |
161 | executeIfActivityPub, |
162 | asyncMiddleware(videoPlaylistRedundancyGetValidator), | |
163 | asyncMiddleware(videoRedundancyController) | |
c48e82b5 C |
164 | ) |
165 | ||
37a44fc9 C |
166 | activityPubClientRouter.get( |
167 | [ '/video-playlists/:playlistId', '/videos/watch/playlist/:playlistId', '/w/p/:playlistId' ], | |
e65c0c5b | 168 | executeIfActivityPub, |
453e83ea | 169 | asyncMiddleware(videoPlaylistsGetValidator('all')), |
e65c0c5b | 170 | asyncMiddleware(videoPlaylistController) |
418d092a | 171 | ) |
37190663 | 172 | activityPubClientRouter.get('/video-playlists/:playlistId/videos/:playlistElementId', |
e65c0c5b C |
173 | executeIfActivityPub, |
174 | asyncMiddleware(videoPlaylistElementAPGetValidator), | |
a1587156 | 175 | videoPlaylistElementController |
418d092a C |
176 | ) |
177 | ||
b2111066 C |
178 | activityPubClientRouter.get('/videos/local-viewer/:localViewerId', |
179 | executeIfActivityPub, | |
180 | asyncMiddleware(getVideoLocalViewerValidator), | |
181 | getVideoLocalViewerController | |
182 | ) | |
183 | ||
e4f97bab C |
184 | // --------------------------------------------------------------------------- |
185 | ||
186 | export { | |
187 | activityPubClientRouter | |
188 | } | |
189 | ||
190 | // --------------------------------------------------------------------------- | |
191 | ||
418d092a | 192 | function accountController (req: express.Request, res: express.Response) { |
dae86118 | 193 | const account = res.locals.account |
e4f97bab | 194 | |
a219c910 | 195 | return activityPubResponse(activityPubContextify(account.toActivityPubObject(), 'Actor'), res) |
e4f97bab C |
196 | } |
197 | ||
418d092a | 198 | async function accountFollowersController (req: express.Request, res: express.Response) { |
dae86118 | 199 | const account = res.locals.account |
7006bc63 | 200 | const activityPubResult = await actorFollowers(req, account.Actor) |
e4f97bab | 201 | |
a219c910 | 202 | return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res) |
e4f97bab C |
203 | } |
204 | ||
418d092a | 205 | async function accountFollowingController (req: express.Request, res: express.Response) { |
dae86118 | 206 | const account = res.locals.account |
7006bc63 | 207 | const activityPubResult = await actorFollowing(req, account.Actor) |
e4f97bab | 208 | |
a219c910 | 209 | return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res) |
e4f97bab | 210 | } |
20494f12 | 211 | |
418d092a | 212 | async function accountPlaylistsController (req: express.Request, res: express.Response) { |
dae86118 | 213 | const account = res.locals.account |
7405b6ba C |
214 | const activityPubResult = await actorPlaylists(req, { account }) |
215 | ||
a219c910 | 216 | return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res) |
7405b6ba C |
217 | } |
218 | ||
219 | async function videoChannelPlaylistsController (req: express.Request, res: express.Response) { | |
220 | const channel = res.locals.videoChannel | |
221 | const activityPubResult = await actorPlaylists(req, { channel }) | |
418d092a | 222 | |
a219c910 | 223 | return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res) |
418d092a C |
224 | } |
225 | ||
75ba887d | 226 | function getAccountVideoRateFactory (rateType: VideoRateType) { |
5c6d985f | 227 | return (req: express.Request, res: express.Response) => { |
dae86118 | 228 | const accountVideoRate = res.locals.accountVideoRate |
5c6d985f C |
229 | |
230 | const byActor = accountVideoRate.Account.Actor | |
5c6d985f | 231 | const APObject = rateType === 'like' |
de94ac86 C |
232 | ? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video) |
233 | : buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video) | |
5c6d985f | 234 | |
a219c910 | 235 | return activityPubResponse(activityPubContextify(APObject, 'Rate'), res) |
5c6d985f C |
236 | } |
237 | } | |
238 | ||
1a8dd4da | 239 | async function videoController (req: express.Request, res: express.Response) { |
71d4af1e | 240 | const video = res.locals.videoAll |
20494f12 | 241 | |
de94ac86 | 242 | if (redirectIfNotOwned(video.url, res)) return |
8d1fa36a | 243 | |
40e87e9e | 244 | // We need captions to render AP object |
453e83ea | 245 | const captions = await VideoCaptionModel.listVideoCaptions(video.id) |
b5fecbf4 | 246 | const videoWithCaptions = Object.assign(video, { VideoCaptions: captions }) |
40e87e9e | 247 | |
453e83ea C |
248 | const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC) |
249 | const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience) | |
2ccaeeb3 | 250 | |
296c0905 | 251 | if (req.path.endsWith('/activity')) { |
453e83ea | 252 | const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience) |
a219c910 | 253 | return activityPubResponse(activityPubContextify(data, 'Video'), res) |
296c0905 C |
254 | } |
255 | ||
a219c910 | 256 | return activityPubResponse(activityPubContextify(videoObject, 'Video'), res) |
20494f12 C |
257 | } |
258 | ||
1a8dd4da | 259 | async function videoAnnounceController (req: express.Request, res: express.Response) { |
dae86118 | 260 | const share = res.locals.videoShare |
8d1fa36a | 261 | |
de94ac86 | 262 | if (redirectIfNotOwned(share.url, res)) return |
8d1fa36a | 263 | |
453e83ea | 264 | const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined) |
4e50b6a1 | 265 | |
598edb8a | 266 | return activityPubResponse(activityPubContextify(activity, 'Announce'), res) |
4e50b6a1 C |
267 | } |
268 | ||
1a8dd4da | 269 | async function videoAnnouncesController (req: express.Request, res: express.Response) { |
7eba5e1f | 270 | const video = res.locals.onlyImmutableVideo |
46531a0a | 271 | |
de94ac86 C |
272 | if (redirectIfNotOwned(video.url, res)) return |
273 | ||
8fffe21a C |
274 | const handler = async (start: number, count: number) => { |
275 | const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count) | |
276 | return { | |
d0800f76 | 277 | total: result.total, |
278 | data: result.data.map(r => r.url) | |
8fffe21a C |
279 | } |
280 | } | |
de94ac86 | 281 | const json = await activityPubCollectionPagination(getLocalVideoSharesActivityPubUrl(video), handler, req.query.page) |
46531a0a | 282 | |
a219c910 | 283 | return activityPubResponse(activityPubContextify(json, 'Collection'), res) |
46531a0a C |
284 | } |
285 | ||
1a8dd4da | 286 | async function videoLikesController (req: express.Request, res: express.Response) { |
7eba5e1f | 287 | const video = res.locals.onlyImmutableVideo |
de94ac86 C |
288 | |
289 | if (redirectIfNotOwned(video.url, res)) return | |
290 | ||
291 | const json = await videoRates(req, 'like', video, getLocalVideoLikesActivityPubUrl(video)) | |
46531a0a | 292 | |
a219c910 | 293 | return activityPubResponse(activityPubContextify(json, 'Collection'), res) |
46531a0a C |
294 | } |
295 | ||
1a8dd4da | 296 | async function videoDislikesController (req: express.Request, res: express.Response) { |
7eba5e1f | 297 | const video = res.locals.onlyImmutableVideo |
de94ac86 C |
298 | |
299 | if (redirectIfNotOwned(video.url, res)) return | |
300 | ||
301 | const json = await videoRates(req, 'dislike', video, getLocalVideoDislikesActivityPubUrl(video)) | |
46531a0a | 302 | |
a219c910 | 303 | return activityPubResponse(activityPubContextify(json, 'Collection'), res) |
46531a0a C |
304 | } |
305 | ||
1a8dd4da | 306 | async function videoCommentsController (req: express.Request, res: express.Response) { |
7eba5e1f | 307 | const video = res.locals.onlyImmutableVideo |
46531a0a | 308 | |
de94ac86 C |
309 | if (redirectIfNotOwned(video.url, res)) return |
310 | ||
8fffe21a | 311 | const handler = async (start: number, count: number) => { |
696d83fd | 312 | const result = await VideoCommentModel.listAndCountByVideoForAP(video, start, count) |
d0800f76 | 313 | |
8fffe21a | 314 | return { |
d0800f76 | 315 | total: result.total, |
316 | data: result.data.map(r => r.url) | |
8fffe21a C |
317 | } |
318 | } | |
de94ac86 | 319 | const json = await activityPubCollectionPagination(getLocalVideoCommentsActivityPubUrl(video), handler, req.query.page) |
46531a0a | 320 | |
a219c910 | 321 | return activityPubResponse(activityPubContextify(json, 'Collection'), res) |
46531a0a C |
322 | } |
323 | ||
a1587156 | 324 | function videoChannelController (req: express.Request, res: express.Response) { |
dae86118 | 325 | const videoChannel = res.locals.videoChannel |
20494f12 | 326 | |
a219c910 | 327 | return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject(), 'Actor'), res) |
20494f12 | 328 | } |
da854ddd | 329 | |
1a8dd4da | 330 | async function videoChannelFollowersController (req: express.Request, res: express.Response) { |
dae86118 | 331 | const videoChannel = res.locals.videoChannel |
7006bc63 C |
332 | const activityPubResult = await actorFollowers(req, videoChannel.Actor) |
333 | ||
a219c910 | 334 | return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res) |
7006bc63 C |
335 | } |
336 | ||
1a8dd4da | 337 | async function videoChannelFollowingController (req: express.Request, res: express.Response) { |
dae86118 | 338 | const videoChannel = res.locals.videoChannel |
7006bc63 C |
339 | const activityPubResult = await actorFollowing(req, videoChannel.Actor) |
340 | ||
a219c910 | 341 | return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res) |
7006bc63 C |
342 | } |
343 | ||
1a8dd4da | 344 | async function videoCommentController (req: express.Request, res: express.Response) { |
453e83ea | 345 | const videoComment = res.locals.videoCommentFull |
da854ddd | 346 | |
de94ac86 | 347 | if (redirectIfNotOwned(videoComment.url, res)) return |
8d1fa36a | 348 | |
d7e70384 | 349 | const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) |
d6e99e53 | 350 | const isPublic = true // Comments are always public |
69222afa | 351 | let videoCommentObject = videoComment.toActivityPubObject(threadParentComments) |
d6e99e53 | 352 | |
69222afa JM |
353 | if (videoComment.Account) { |
354 | const audience = getAudience(videoComment.Account.Actor, isPublic) | |
355 | videoCommentObject = audiencify(videoCommentObject, audience) | |
d6e99e53 | 356 | |
69222afa JM |
357 | if (req.path.endsWith('/activity')) { |
358 | const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience) | |
a219c910 | 359 | return activityPubResponse(activityPubContextify(data, 'Comment'), res) |
69222afa | 360 | } |
296c0905 C |
361 | } |
362 | ||
a219c910 | 363 | return activityPubResponse(activityPubContextify(videoCommentObject, 'Comment'), res) |
da854ddd | 364 | } |
7006bc63 | 365 | |
c48e82b5 | 366 | async function videoRedundancyController (req: express.Request, res: express.Response) { |
dae86118 | 367 | const videoRedundancy = res.locals.videoRedundancy |
de94ac86 C |
368 | |
369 | if (redirectIfNotOwned(videoRedundancy.url, res)) return | |
8d1fa36a | 370 | |
c48e82b5 C |
371 | const serverActor = await getServerActor() |
372 | ||
373 | const audience = getAudience(serverActor) | |
374 | const object = audiencify(videoRedundancy.toActivityPubObject(), audience) | |
375 | ||
376 | if (req.path.endsWith('/activity')) { | |
377 | const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience) | |
084a2cd0 | 378 | return activityPubResponse(activityPubContextify(data, 'CacheFile'), res) |
c48e82b5 C |
379 | } |
380 | ||
084a2cd0 | 381 | return activityPubResponse(activityPubContextify(object, 'CacheFile'), res) |
c48e82b5 C |
382 | } |
383 | ||
418d092a | 384 | async function videoPlaylistController (req: express.Request, res: express.Response) { |
453e83ea | 385 | const playlist = res.locals.videoPlaylistFull |
418d092a | 386 | |
de94ac86 C |
387 | if (redirectIfNotOwned(playlist.url, res)) return |
388 | ||
df0b219d C |
389 | // We need more attributes |
390 | playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId) | |
391 | ||
392 | const json = await playlist.toActivityPubObject(req.query.page, null) | |
418d092a C |
393 | const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC) |
394 | const object = audiencify(json, audience) | |
395 | ||
a219c910 | 396 | return activityPubResponse(activityPubContextify(object, 'Playlist'), res) |
418d092a C |
397 | } |
398 | ||
a1587156 | 399 | function videoPlaylistElementController (req: express.Request, res: express.Response) { |
b5fecbf4 | 400 | const videoPlaylistElement = res.locals.videoPlaylistElementAP |
418d092a | 401 | |
de94ac86 C |
402 | if (redirectIfNotOwned(videoPlaylistElement.url, res)) return |
403 | ||
418d092a | 404 | const json = videoPlaylistElement.toActivityPubObject() |
a219c910 | 405 | return activityPubResponse(activityPubContextify(json, 'Playlist'), res) |
418d092a C |
406 | } |
407 | ||
b2111066 C |
408 | function getVideoLocalViewerController (req: express.Request, res: express.Response) { |
409 | const localViewer = res.locals.localViewerFull | |
410 | ||
411 | return activityPubResponse(activityPubContextify(localViewer.toActivityPubObject(), 'WatchAction'), res) | |
412 | } | |
413 | ||
7006bc63 C |
414 | // --------------------------------------------------------------------------- |
415 | ||
7e98a7df | 416 | function actorFollowing (req: express.Request, actor: MActorId) { |
8fffe21a C |
417 | const handler = (start: number, count: number) => { |
418 | return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) | |
419 | } | |
7006bc63 | 420 | |
6dd9de95 | 421 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) |
7006bc63 C |
422 | } |
423 | ||
7e98a7df | 424 | function actorFollowers (req: express.Request, actor: MActorId) { |
8fffe21a | 425 | const handler = (start: number, count: number) => { |
418d092a C |
426 | return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count) |
427 | } | |
428 | ||
6dd9de95 | 429 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) |
418d092a C |
430 | } |
431 | ||
7e98a7df | 432 | function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) { |
418d092a | 433 | const handler = (start: number, count: number) => { |
7405b6ba | 434 | return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count) |
8fffe21a | 435 | } |
7006bc63 | 436 | |
6dd9de95 | 437 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) |
7006bc63 | 438 | } |
4b8f09fa | 439 | |
7eba5e1f | 440 | function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) { |
8fffe21a C |
441 | const handler = async (start: number, count: number) => { |
442 | const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) | |
443 | return { | |
d0800f76 | 444 | total: result.total, |
445 | data: result.data.map(r => r.url) | |
8fffe21a C |
446 | } |
447 | } | |
448 | return activityPubCollectionPagination(url, handler, req.query.page) | |
4b8f09fa | 449 | } |
de94ac86 C |
450 | |
451 | function redirectIfNotOwned (url: string, res: express.Response) { | |
452 | if (url.startsWith(WEBSERVER.URL) === false) { | |
453 | res.redirect(url) | |
454 | return true | |
455 | } | |
456 | ||
457 | return false | |
458 | } |