]>
Commit | Line | Data |
---|---|---|
e4f97bab C |
1 | // Intercept ActivityPub client requests |
2 | import * as express from 'express' | |
8fffe21a | 3 | import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' |
d6e99e53 | 4 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' |
8fffe21a | 5 | import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers' |
1e7eb25f | 6 | import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send' |
e251f170 | 7 | import { audiencify, getAudience } from '../../lib/activitypub/audience' |
c48e82b5 | 8 | import { buildCreateActivity } from '../../lib/activitypub/send/send-create' |
96f29c0f C |
9 | import { |
10 | asyncMiddleware, | |
11 | executeIfActivityPub, | |
12 | localAccountValidator, | |
13 | localVideoChannelValidator, | |
1e7eb25f C |
14 | videosCustomGetValidator, |
15 | videosShareValidator | |
96f29c0f | 16 | } from '../../middlewares' |
418d092a | 17 | import { getAccountVideoRateValidator, videoCommentGetValidator } from '../../middlewares/validators' |
3fd3ab2d | 18 | import { AccountModel } from '../../models/account/account' |
7006bc63 | 19 | import { ActorModel } from '../../models/activitypub/actor' |
50d6de9c | 20 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
3fd3ab2d C |
21 | import { VideoModel } from '../../models/video/video' |
22 | import { VideoChannelModel } from '../../models/video/video-channel' | |
da854ddd | 23 | import { VideoCommentModel } from '../../models/video/video-comment' |
3fd3ab2d | 24 | import { VideoShareModel } from '../../models/video/video-share' |
98d3324d | 25 | import { cacheRoute } from '../../middlewares/cache' |
8fffe21a C |
26 | import { activityPubResponse } from './utils' |
27 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | |
28 | import { | |
5c6d985f | 29 | getRateUrl, |
8fffe21a C |
30 | getVideoCommentsActivityPubUrl, |
31 | getVideoDislikesActivityPubUrl, | |
32 | getVideoLikesActivityPubUrl, | |
33 | getVideoSharesActivityPubUrl | |
34 | } from '../../lib/activitypub' | |
40e87e9e | 35 | import { VideoCaptionModel } from '../../models/video/video-caption' |
09209296 | 36 | import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy' |
c48e82b5 | 37 | import { getServerActor } from '../../helpers/utils' |
8d1fa36a | 38 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' |
1e7eb25f | 39 | import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike' |
418d092a C |
40 | import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists' |
41 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | |
42 | import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' | |
43 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' | |
e4f97bab C |
44 | |
45 | const activityPubClientRouter = express.Router() | |
46 | ||
108af661 | 47 | activityPubClientRouter.get('/accounts?/:name', |
e65c0c5b C |
48 | executeIfActivityPub, |
49 | asyncMiddleware(localAccountValidator), | |
50 | accountController | |
e4f97bab | 51 | ) |
7006bc63 | 52 | activityPubClientRouter.get('/accounts?/:name/followers', |
e65c0c5b C |
53 | executeIfActivityPub, |
54 | asyncMiddleware(localAccountValidator), | |
55 | asyncMiddleware(accountFollowersController) | |
e4f97bab | 56 | ) |
7006bc63 | 57 | activityPubClientRouter.get('/accounts?/:name/following', |
e65c0c5b C |
58 | executeIfActivityPub, |
59 | asyncMiddleware(localAccountValidator), | |
60 | asyncMiddleware(accountFollowingController) | |
e4f97bab | 61 | ) |
418d092a | 62 | activityPubClientRouter.get('/accounts?/:name/playlists', |
e65c0c5b C |
63 | executeIfActivityPub, |
64 | asyncMiddleware(localAccountValidator), | |
65 | asyncMiddleware(accountPlaylistsController) | |
418d092a | 66 | ) |
5c6d985f | 67 | activityPubClientRouter.get('/accounts?/:name/likes/:videoId', |
e65c0c5b C |
68 | executeIfActivityPub, |
69 | asyncMiddleware(getAccountVideoRateValidator('like')), | |
70 | getAccountVideoRate('like') | |
5c6d985f C |
71 | ) |
72 | activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId', | |
e65c0c5b C |
73 | executeIfActivityPub, |
74 | asyncMiddleware(getAccountVideoRateValidator('dislike')), | |
75 | getAccountVideoRate('dislike') | |
5c6d985f | 76 | ) |
e4f97bab | 77 | |
20494f12 | 78 | activityPubClientRouter.get('/videos/watch/:id', |
e65c0c5b C |
79 | executeIfActivityPub, |
80 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)), | |
81 | asyncMiddleware(videosCustomGetValidator('only-video-with-rights')), | |
82 | asyncMiddleware(videoController) | |
4e50b6a1 | 83 | ) |
296c0905 | 84 | activityPubClientRouter.get('/videos/watch/:id/activity', |
e65c0c5b C |
85 | executeIfActivityPub, |
86 | asyncMiddleware(videosCustomGetValidator('only-video-with-rights')), | |
87 | asyncMiddleware(videoController) | |
296c0905 | 88 | ) |
46531a0a | 89 | activityPubClientRouter.get('/videos/watch/:id/announces', |
e65c0c5b C |
90 | executeIfActivityPub, |
91 | asyncMiddleware(videosCustomGetValidator('only-video')), | |
92 | asyncMiddleware(videoAnnouncesController) | |
46531a0a | 93 | ) |
5c6d985f | 94 | activityPubClientRouter.get('/videos/watch/:id/announces/:actorId', |
e65c0c5b C |
95 | executeIfActivityPub, |
96 | asyncMiddleware(videosShareValidator), | |
97 | asyncMiddleware(videoAnnounceController) | |
20494f12 | 98 | ) |
46531a0a | 99 | activityPubClientRouter.get('/videos/watch/:id/likes', |
e65c0c5b C |
100 | executeIfActivityPub, |
101 | asyncMiddleware(videosCustomGetValidator('only-video')), | |
102 | asyncMiddleware(videoLikesController) | |
46531a0a C |
103 | ) |
104 | activityPubClientRouter.get('/videos/watch/:id/dislikes', | |
e65c0c5b C |
105 | executeIfActivityPub, |
106 | asyncMiddleware(videosCustomGetValidator('only-video')), | |
107 | asyncMiddleware(videoDislikesController) | |
46531a0a C |
108 | ) |
109 | activityPubClientRouter.get('/videos/watch/:id/comments', | |
e65c0c5b C |
110 | executeIfActivityPub, |
111 | asyncMiddleware(videosCustomGetValidator('only-video')), | |
112 | asyncMiddleware(videoCommentsController) | |
46531a0a | 113 | ) |
da854ddd | 114 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', |
e65c0c5b C |
115 | executeIfActivityPub, |
116 | asyncMiddleware(videoCommentGetValidator), | |
117 | asyncMiddleware(videoCommentController) | |
da854ddd | 118 | ) |
296c0905 | 119 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity', |
e65c0c5b C |
120 | executeIfActivityPub, |
121 | asyncMiddleware(videoCommentGetValidator), | |
122 | asyncMiddleware(videoCommentController) | |
296c0905 | 123 | ) |
da854ddd | 124 | |
8a19bee1 | 125 | activityPubClientRouter.get('/video-channels/:name', |
e65c0c5b C |
126 | executeIfActivityPub, |
127 | asyncMiddleware(localVideoChannelValidator), | |
128 | asyncMiddleware(videoChannelController) | |
20494f12 | 129 | ) |
8a19bee1 | 130 | activityPubClientRouter.get('/video-channels/:name/followers', |
e65c0c5b C |
131 | executeIfActivityPub, |
132 | asyncMiddleware(localVideoChannelValidator), | |
133 | asyncMiddleware(videoChannelFollowersController) | |
7006bc63 | 134 | ) |
8a19bee1 | 135 | activityPubClientRouter.get('/video-channels/:name/following', |
e65c0c5b C |
136 | executeIfActivityPub, |
137 | asyncMiddleware(localVideoChannelValidator), | |
138 | asyncMiddleware(videoChannelFollowingController) | |
7006bc63 | 139 | ) |
20494f12 | 140 | |
c48e82b5 | 141 | activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?', |
e65c0c5b C |
142 | executeIfActivityPub, |
143 | asyncMiddleware(videoFileRedundancyGetValidator), | |
144 | asyncMiddleware(videoRedundancyController) | |
09209296 | 145 | ) |
9c6ca37f | 146 | activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId', |
e65c0c5b C |
147 | executeIfActivityPub, |
148 | asyncMiddleware(videoPlaylistRedundancyGetValidator), | |
149 | asyncMiddleware(videoRedundancyController) | |
c48e82b5 C |
150 | ) |
151 | ||
418d092a | 152 | activityPubClientRouter.get('/video-playlists/:playlistId', |
e65c0c5b C |
153 | executeIfActivityPub, |
154 | asyncMiddleware(videoPlaylistsGetValidator), | |
155 | asyncMiddleware(videoPlaylistController) | |
418d092a C |
156 | ) |
157 | activityPubClientRouter.get('/video-playlists/:playlistId/:videoId', | |
e65c0c5b C |
158 | executeIfActivityPub, |
159 | asyncMiddleware(videoPlaylistElementAPGetValidator), | |
160 | asyncMiddleware(videoPlaylistElementController) | |
418d092a C |
161 | ) |
162 | ||
e4f97bab C |
163 | // --------------------------------------------------------------------------- |
164 | ||
165 | export { | |
166 | activityPubClientRouter | |
167 | } | |
168 | ||
169 | // --------------------------------------------------------------------------- | |
170 | ||
418d092a | 171 | function accountController (req: express.Request, res: express.Response) { |
dae86118 | 172 | const account = res.locals.account |
e4f97bab | 173 | |
4b8f09fa | 174 | return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res) |
e4f97bab C |
175 | } |
176 | ||
418d092a | 177 | async function accountFollowersController (req: express.Request, res: express.Response) { |
dae86118 | 178 | const account = res.locals.account |
7006bc63 | 179 | const activityPubResult = await actorFollowers(req, account.Actor) |
e4f97bab | 180 | |
4b8f09fa | 181 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
e4f97bab C |
182 | } |
183 | ||
418d092a | 184 | async function accountFollowingController (req: express.Request, res: express.Response) { |
dae86118 | 185 | const account = res.locals.account |
7006bc63 | 186 | const activityPubResult = await actorFollowing(req, account.Actor) |
e4f97bab | 187 | |
4b8f09fa | 188 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
e4f97bab | 189 | } |
20494f12 | 190 | |
418d092a | 191 | async function accountPlaylistsController (req: express.Request, res: express.Response) { |
dae86118 | 192 | const account = res.locals.account |
418d092a C |
193 | const activityPubResult = await actorPlaylists(req, account) |
194 | ||
195 | return activityPubResponse(activityPubContextify(activityPubResult), res) | |
196 | } | |
197 | ||
5c6d985f C |
198 | function getAccountVideoRate (rateType: VideoRateType) { |
199 | return (req: express.Request, res: express.Response) => { | |
dae86118 | 200 | const accountVideoRate = res.locals.accountVideoRate |
5c6d985f C |
201 | |
202 | const byActor = accountVideoRate.Account.Actor | |
203 | const url = getRateUrl(rateType, byActor, accountVideoRate.Video) | |
204 | const APObject = rateType === 'like' | |
205 | ? buildLikeActivity(url, byActor, accountVideoRate.Video) | |
1e7eb25f | 206 | : buildDislikeActivity(url, byActor, accountVideoRate.Video) |
5c6d985f C |
207 | |
208 | return activityPubResponse(activityPubContextify(APObject), res) | |
209 | } | |
210 | } | |
211 | ||
1a8dd4da | 212 | async function videoController (req: express.Request, res: express.Response) { |
09209296 | 213 | // We need more attributes |
dae86118 | 214 | const video = await VideoModel.loadForGetAPI(res.locals.video.id) |
20494f12 | 215 | |
1a8dd4da | 216 | if (video.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(video.url) |
8d1fa36a | 217 | |
40e87e9e C |
218 | // We need captions to render AP object |
219 | video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id) | |
220 | ||
2186386c | 221 | const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC) |
8fffe21a | 222 | const videoObject = audiencify(video.toActivityPubObject(), audience) |
2ccaeeb3 | 223 | |
296c0905 | 224 | if (req.path.endsWith('/activity')) { |
c48e82b5 | 225 | const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience) |
4b8f09fa | 226 | return activityPubResponse(activityPubContextify(data), res) |
296c0905 C |
227 | } |
228 | ||
4b8f09fa | 229 | return activityPubResponse(activityPubContextify(videoObject), res) |
20494f12 C |
230 | } |
231 | ||
1a8dd4da | 232 | async function videoAnnounceController (req: express.Request, res: express.Response) { |
dae86118 | 233 | const share = res.locals.videoShare |
8d1fa36a | 234 | |
1a8dd4da | 235 | if (share.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(share.url) |
8d1fa36a | 236 | |
c48e82b5 | 237 | const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined) |
4e50b6a1 | 238 | |
c48e82b5 | 239 | return activityPubResponse(activityPubContextify(activity), res) |
4e50b6a1 C |
240 | } |
241 | ||
1a8dd4da | 242 | async function videoAnnouncesController (req: express.Request, res: express.Response) { |
dae86118 | 243 | const video = res.locals.video |
46531a0a | 244 | |
8fffe21a C |
245 | const handler = async (start: number, count: number) => { |
246 | const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count) | |
247 | return { | |
248 | total: result.count, | |
249 | data: result.rows.map(r => r.url) | |
250 | } | |
251 | } | |
252 | const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page) | |
46531a0a | 253 | |
8fffe21a | 254 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
255 | } |
256 | ||
1a8dd4da | 257 | async function videoLikesController (req: express.Request, res: express.Response) { |
dae86118 | 258 | const video = res.locals.video |
8fffe21a | 259 | const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video)) |
46531a0a | 260 | |
8fffe21a | 261 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
262 | } |
263 | ||
1a8dd4da | 264 | async function videoDislikesController (req: express.Request, res: express.Response) { |
dae86118 | 265 | const video = res.locals.video |
8fffe21a | 266 | const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video)) |
46531a0a | 267 | |
8fffe21a | 268 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
269 | } |
270 | ||
1a8dd4da | 271 | async function videoCommentsController (req: express.Request, res: express.Response) { |
dae86118 | 272 | const video = res.locals.video |
46531a0a | 273 | |
8fffe21a C |
274 | const handler = async (start: number, count: number) => { |
275 | const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count) | |
276 | return { | |
277 | total: result.count, | |
278 | data: result.rows.map(r => r.url) | |
279 | } | |
280 | } | |
281 | const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page) | |
46531a0a | 282 | |
8fffe21a | 283 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
284 | } |
285 | ||
1a8dd4da | 286 | async function videoChannelController (req: express.Request, res: express.Response) { |
dae86118 | 287 | const videoChannel = res.locals.videoChannel |
20494f12 | 288 | |
4b8f09fa | 289 | return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res) |
20494f12 | 290 | } |
da854ddd | 291 | |
1a8dd4da | 292 | async function videoChannelFollowersController (req: express.Request, res: express.Response) { |
dae86118 | 293 | const videoChannel = res.locals.videoChannel |
7006bc63 C |
294 | const activityPubResult = await actorFollowers(req, videoChannel.Actor) |
295 | ||
4b8f09fa | 296 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
7006bc63 C |
297 | } |
298 | ||
1a8dd4da | 299 | async function videoChannelFollowingController (req: express.Request, res: express.Response) { |
dae86118 | 300 | const videoChannel = res.locals.videoChannel |
7006bc63 C |
301 | const activityPubResult = await actorFollowing(req, videoChannel.Actor) |
302 | ||
4b8f09fa | 303 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
7006bc63 C |
304 | } |
305 | ||
1a8dd4da | 306 | async function videoCommentController (req: express.Request, res: express.Response) { |
dae86118 | 307 | const videoComment = res.locals.videoComment |
da854ddd | 308 | |
1a8dd4da | 309 | if (videoComment.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoComment.url) |
8d1fa36a | 310 | |
d7e70384 | 311 | const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) |
d6e99e53 | 312 | const isPublic = true // Comments are always public |
2186386c | 313 | const audience = getAudience(videoComment.Account.Actor, isPublic) |
d6e99e53 C |
314 | |
315 | const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience) | |
316 | ||
296c0905 | 317 | if (req.path.endsWith('/activity')) { |
c48e82b5 | 318 | const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience) |
4b8f09fa | 319 | return activityPubResponse(activityPubContextify(data), res) |
296c0905 C |
320 | } |
321 | ||
4b8f09fa | 322 | return activityPubResponse(activityPubContextify(videoCommentObject), res) |
da854ddd | 323 | } |
7006bc63 | 324 | |
c48e82b5 | 325 | async function videoRedundancyController (req: express.Request, res: express.Response) { |
dae86118 | 326 | const videoRedundancy = res.locals.videoRedundancy |
1a8dd4da | 327 | if (videoRedundancy.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url) |
8d1fa36a | 328 | |
c48e82b5 C |
329 | const serverActor = await getServerActor() |
330 | ||
331 | const audience = getAudience(serverActor) | |
332 | const object = audiencify(videoRedundancy.toActivityPubObject(), audience) | |
333 | ||
334 | if (req.path.endsWith('/activity')) { | |
335 | const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience) | |
336 | return activityPubResponse(activityPubContextify(data), res) | |
337 | } | |
338 | ||
339 | return activityPubResponse(activityPubContextify(object), res) | |
340 | } | |
341 | ||
418d092a | 342 | async function videoPlaylistController (req: express.Request, res: express.Response) { |
dae86118 | 343 | const playlist = res.locals.videoPlaylist |
418d092a | 344 | |
df0b219d C |
345 | // We need more attributes |
346 | playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId) | |
347 | ||
348 | const json = await playlist.toActivityPubObject(req.query.page, null) | |
418d092a C |
349 | const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC) |
350 | const object = audiencify(json, audience) | |
351 | ||
352 | return activityPubResponse(activityPubContextify(object), res) | |
353 | } | |
354 | ||
355 | async function videoPlaylistElementController (req: express.Request, res: express.Response) { | |
dae86118 | 356 | const videoPlaylistElement = res.locals.videoPlaylistElement |
418d092a C |
357 | |
358 | const json = videoPlaylistElement.toActivityPubObject() | |
359 | return activityPubResponse(activityPubContextify(json), res) | |
360 | } | |
361 | ||
7006bc63 C |
362 | // --------------------------------------------------------------------------- |
363 | ||
364 | async function actorFollowing (req: express.Request, actor: ActorModel) { | |
8fffe21a C |
365 | const handler = (start: number, count: number) => { |
366 | return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) | |
367 | } | |
7006bc63 | 368 | |
babecc3c | 369 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page) |
7006bc63 C |
370 | } |
371 | ||
372 | async function actorFollowers (req: express.Request, actor: ActorModel) { | |
8fffe21a | 373 | const handler = (start: number, count: number) => { |
418d092a C |
374 | return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count) |
375 | } | |
376 | ||
377 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page) | |
378 | } | |
379 | ||
380 | async function actorPlaylists (req: express.Request, account: AccountModel) { | |
381 | const handler = (start: number, count: number) => { | |
0b16f5f2 | 382 | return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count) |
8fffe21a | 383 | } |
7006bc63 | 384 | |
babecc3c | 385 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page) |
7006bc63 | 386 | } |
4b8f09fa | 387 | |
8fffe21a C |
388 | function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) { |
389 | const handler = async (start: number, count: number) => { | |
390 | const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) | |
391 | return { | |
392 | total: result.count, | |
5c6d985f | 393 | data: result.rows.map(r => r.url) |
8fffe21a C |
394 | } |
395 | } | |
396 | return activityPubCollectionPagination(url, handler, req.query.page) | |
4b8f09fa | 397 | } |