1 import cors from 'cors'
2 import express from 'express'
3 import { getServerActor } from '@server/models/application/application'
4 import { MAccountId, MActorId, MChannelId, MVideoId } from '@server/types/models'
5 import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
6 import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
7 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
8 import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants'
9 import { audiencify, getAudience } from '../../lib/activitypub/audience'
10 import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send'
11 import { buildCreateActivity } from '../../lib/activitypub/send/send-create'
12 import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
14 getLocalVideoCommentsActivityPubUrl,
15 getLocalVideoDislikesActivityPubUrl,
16 getLocalVideoLikesActivityPubUrl,
17 getLocalVideoSharesActivityPubUrl
18 } from '../../lib/activitypub/url'
23 localAccountValidator,
24 videoChannelsNameWithHostValidator,
25 videosCustomGetValidator,
27 } from '../../middlewares'
28 import { cacheRoute } from '../../middlewares/cache/cache'
29 import { getAccountVideoRateValidatorFactory, videoCommentGetValidator } from '../../middlewares/validators'
30 import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
31 import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
32 import { AccountModel } from '../../models/account/account'
33 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
34 import { ActorFollowModel } from '../../models/actor/actor-follow'
35 import { VideoCaptionModel } from '../../models/video/video-caption'
36 import { VideoCommentModel } from '../../models/video/video-comment'
37 import { VideoPlaylistModel } from '../../models/video/video-playlist'
38 import { VideoShareModel } from '../../models/video/video-share'
39 import { activityPubResponse } from './utils'
41 const activityPubClientRouter = express.Router()
42 activityPubClientRouter.use(cors())
44 // Intercept ActivityPub client requests
46 activityPubClientRouter.get(
47 [ '/accounts?/:name', '/accounts?/:name/video-channels', '/a/:name', '/a/:name/video-channels' ],
49 asyncMiddleware(localAccountValidator),
52 activityPubClientRouter.get('/accounts?/:name/followers',
54 asyncMiddleware(localAccountValidator),
55 asyncMiddleware(accountFollowersController)
57 activityPubClientRouter.get('/accounts?/:name/following',
59 asyncMiddleware(localAccountValidator),
60 asyncMiddleware(accountFollowingController)
62 activityPubClientRouter.get('/accounts?/:name/playlists',
64 asyncMiddleware(localAccountValidator),
65 asyncMiddleware(accountPlaylistsController)
67 activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
69 cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
70 asyncMiddleware(getAccountVideoRateValidatorFactory('like')),
71 getAccountVideoRateFactory('like')
73 activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
75 cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
76 asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')),
77 getAccountVideoRateFactory('dislike')
80 activityPubClientRouter.get(
81 [ '/videos/watch/:id', '/w/:id' ],
83 cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
84 asyncMiddleware(videosCustomGetValidator('all')),
85 asyncMiddleware(videoController)
87 activityPubClientRouter.get('/videos/watch/:id/activity',
89 asyncMiddleware(videosCustomGetValidator('all')),
90 asyncMiddleware(videoController)
92 activityPubClientRouter.get('/videos/watch/:id/announces',
94 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
95 asyncMiddleware(videoAnnouncesController)
97 activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
99 asyncMiddleware(videosShareValidator),
100 asyncMiddleware(videoAnnounceController)
102 activityPubClientRouter.get('/videos/watch/:id/likes',
103 executeIfActivityPub,
104 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
105 asyncMiddleware(videoLikesController)
107 activityPubClientRouter.get('/videos/watch/:id/dislikes',
108 executeIfActivityPub,
109 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
110 asyncMiddleware(videoDislikesController)
112 activityPubClientRouter.get('/videos/watch/:id/comments',
113 executeIfActivityPub,
114 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
115 asyncMiddleware(videoCommentsController)
117 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
118 executeIfActivityPub,
119 asyncMiddleware(videoCommentGetValidator),
120 asyncMiddleware(videoCommentController)
122 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
123 executeIfActivityPub,
124 asyncMiddleware(videoCommentGetValidator),
125 asyncMiddleware(videoCommentController)
128 activityPubClientRouter.get(
129 [ '/video-channels/:nameWithHost', '/video-channels/:nameWithHost/videos', '/c/:nameWithHost', '/c/:nameWithHost/videos' ],
130 executeIfActivityPub,
131 asyncMiddleware(videoChannelsNameWithHostValidator),
132 ensureIsLocalChannel,
133 videoChannelController
135 activityPubClientRouter.get('/video-channels/:nameWithHost/followers',
136 executeIfActivityPub,
137 asyncMiddleware(videoChannelsNameWithHostValidator),
138 ensureIsLocalChannel,
139 asyncMiddleware(videoChannelFollowersController)
141 activityPubClientRouter.get('/video-channels/:nameWithHost/following',
142 executeIfActivityPub,
143 asyncMiddleware(videoChannelsNameWithHostValidator),
144 ensureIsLocalChannel,
145 asyncMiddleware(videoChannelFollowingController)
147 activityPubClientRouter.get('/video-channels/:nameWithHost/playlists',
148 executeIfActivityPub,
149 asyncMiddleware(videoChannelsNameWithHostValidator),
150 ensureIsLocalChannel,
151 asyncMiddleware(videoChannelPlaylistsController)
154 activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
155 executeIfActivityPub,
156 asyncMiddleware(videoFileRedundancyGetValidator),
157 asyncMiddleware(videoRedundancyController)
159 activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
160 executeIfActivityPub,
161 asyncMiddleware(videoPlaylistRedundancyGetValidator),
162 asyncMiddleware(videoRedundancyController)
165 activityPubClientRouter.get(
166 [ '/video-playlists/:playlistId', '/videos/watch/playlist/:playlistId', '/w/p/:playlistId' ],
167 executeIfActivityPub,
168 asyncMiddleware(videoPlaylistsGetValidator('all')),
169 asyncMiddleware(videoPlaylistController)
171 activityPubClientRouter.get('/video-playlists/:playlistId/videos/:playlistElementId',
172 executeIfActivityPub,
173 asyncMiddleware(videoPlaylistElementAPGetValidator),
174 videoPlaylistElementController
177 // ---------------------------------------------------------------------------
180 activityPubClientRouter
183 // ---------------------------------------------------------------------------
185 function accountController (req: express.Request, res: express.Response) {
186 const account = res.locals.account
188 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
191 async function accountFollowersController (req: express.Request, res: express.Response) {
192 const account = res.locals.account
193 const activityPubResult = await actorFollowers(req, account.Actor)
195 return activityPubResponse(activityPubContextify(activityPubResult), res)
198 async function accountFollowingController (req: express.Request, res: express.Response) {
199 const account = res.locals.account
200 const activityPubResult = await actorFollowing(req, account.Actor)
202 return activityPubResponse(activityPubContextify(activityPubResult), res)
205 async function accountPlaylistsController (req: express.Request, res: express.Response) {
206 const account = res.locals.account
207 const activityPubResult = await actorPlaylists(req, { account })
209 return activityPubResponse(activityPubContextify(activityPubResult), res)
212 async function videoChannelPlaylistsController (req: express.Request, res: express.Response) {
213 const channel = res.locals.videoChannel
214 const activityPubResult = await actorPlaylists(req, { channel })
216 return activityPubResponse(activityPubContextify(activityPubResult), res)
219 function getAccountVideoRateFactory (rateType: VideoRateType) {
220 return (req: express.Request, res: express.Response) => {
221 const accountVideoRate = res.locals.accountVideoRate
223 const byActor = accountVideoRate.Account.Actor
224 const APObject = rateType === 'like'
225 ? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
226 : buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
228 return activityPubResponse(activityPubContextify(APObject), res)
232 async function videoController (req: express.Request, res: express.Response) {
233 const video = res.locals.videoAll
235 if (redirectIfNotOwned(video.url, res)) return
237 // We need captions to render AP object
238 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
239 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
241 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
242 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
244 if (req.path.endsWith('/activity')) {
245 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
246 return activityPubResponse(activityPubContextify(data), res)
249 return activityPubResponse(activityPubContextify(videoObject), res)
252 async function videoAnnounceController (req: express.Request, res: express.Response) {
253 const share = res.locals.videoShare
255 if (redirectIfNotOwned(share.url, res)) return
257 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
259 return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
262 async function videoAnnouncesController (req: express.Request, res: express.Response) {
263 const video = res.locals.onlyImmutableVideo
265 if (redirectIfNotOwned(video.url, res)) return
267 const handler = async (start: number, count: number) => {
268 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
271 data: result.data.map(r => r.url)
274 const json = await activityPubCollectionPagination(getLocalVideoSharesActivityPubUrl(video), handler, req.query.page)
276 return activityPubResponse(activityPubContextify(json), res)
279 async function videoLikesController (req: express.Request, res: express.Response) {
280 const video = res.locals.onlyImmutableVideo
282 if (redirectIfNotOwned(video.url, res)) return
284 const json = await videoRates(req, 'like', video, getLocalVideoLikesActivityPubUrl(video))
286 return activityPubResponse(activityPubContextify(json), res)
289 async function videoDislikesController (req: express.Request, res: express.Response) {
290 const video = res.locals.onlyImmutableVideo
292 if (redirectIfNotOwned(video.url, res)) return
294 const json = await videoRates(req, 'dislike', video, getLocalVideoDislikesActivityPubUrl(video))
296 return activityPubResponse(activityPubContextify(json), res)
299 async function videoCommentsController (req: express.Request, res: express.Response) {
300 const video = res.locals.onlyImmutableVideo
302 if (redirectIfNotOwned(video.url, res)) return
304 const handler = async (start: number, count: number) => {
305 const result = await VideoCommentModel.listAndCountByVideoForAP(video, start, count)
309 data: result.data.map(r => r.url)
312 const json = await activityPubCollectionPagination(getLocalVideoCommentsActivityPubUrl(video), handler, req.query.page)
314 return activityPubResponse(activityPubContextify(json), res)
317 function videoChannelController (req: express.Request, res: express.Response) {
318 const videoChannel = res.locals.videoChannel
320 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
323 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
324 const videoChannel = res.locals.videoChannel
325 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
327 return activityPubResponse(activityPubContextify(activityPubResult), res)
330 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
331 const videoChannel = res.locals.videoChannel
332 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
334 return activityPubResponse(activityPubContextify(activityPubResult), res)
337 async function videoCommentController (req: express.Request, res: express.Response) {
338 const videoComment = res.locals.videoCommentFull
340 if (redirectIfNotOwned(videoComment.url, res)) return
342 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
343 const isPublic = true // Comments are always public
344 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
346 if (videoComment.Account) {
347 const audience = getAudience(videoComment.Account.Actor, isPublic)
348 videoCommentObject = audiencify(videoCommentObject, audience)
350 if (req.path.endsWith('/activity')) {
351 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
352 return activityPubResponse(activityPubContextify(data), res)
356 return activityPubResponse(activityPubContextify(videoCommentObject), res)
359 async function videoRedundancyController (req: express.Request, res: express.Response) {
360 const videoRedundancy = res.locals.videoRedundancy
362 if (redirectIfNotOwned(videoRedundancy.url, res)) return
364 const serverActor = await getServerActor()
366 const audience = getAudience(serverActor)
367 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
369 if (req.path.endsWith('/activity')) {
370 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
371 return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
374 return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
377 async function videoPlaylistController (req: express.Request, res: express.Response) {
378 const playlist = res.locals.videoPlaylistFull
380 if (redirectIfNotOwned(playlist.url, res)) return
382 // We need more attributes
383 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
385 const json = await playlist.toActivityPubObject(req.query.page, null)
386 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
387 const object = audiencify(json, audience)
389 return activityPubResponse(activityPubContextify(object), res)
392 function videoPlaylistElementController (req: express.Request, res: express.Response) {
393 const videoPlaylistElement = res.locals.videoPlaylistElementAP
395 if (redirectIfNotOwned(videoPlaylistElement.url, res)) return
397 const json = videoPlaylistElement.toActivityPubObject()
398 return activityPubResponse(activityPubContextify(json), res)
401 // ---------------------------------------------------------------------------
403 async function actorFollowing (req: express.Request, actor: MActorId) {
404 const handler = (start: number, count: number) => {
405 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
408 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
411 async function actorFollowers (req: express.Request, actor: MActorId) {
412 const handler = (start: number, count: number) => {
413 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
416 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
419 async function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) {
420 const handler = (start: number, count: number) => {
421 return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count)
424 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
427 function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
428 const handler = async (start: number, count: number) => {
429 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
432 data: result.data.map(r => r.url)
435 return activityPubCollectionPagination(url, handler, req.query.page)
438 function redirectIfNotOwned (url: string, res: express.Response) {
439 if (url.startsWith(WEBSERVER.URL) === false) {