1 import * as cors from 'cors'
2 import * as 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'
22 localAccountValidator,
23 localVideoChannelValidator,
24 videosCustomGetValidator,
26 } from '../../middlewares'
27 import { cacheRoute } from '../../middlewares/cache'
28 import { getAccountVideoRateValidatorFactory, videoCommentGetValidator } from '../../middlewares/validators'
29 import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
30 import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
31 import { AccountModel } from '../../models/account/account'
32 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
33 import { ActorFollowModel } from '../../models/actor/actor-follow'
34 import { VideoCaptionModel } from '../../models/video/video-caption'
35 import { VideoCommentModel } from '../../models/video/video-comment'
36 import { VideoPlaylistModel } from '../../models/video/video-playlist'
37 import { VideoShareModel } from '../../models/video/video-share'
38 import { activityPubResponse } from './utils'
40 const activityPubClientRouter = express.Router()
41 activityPubClientRouter.use(cors())
43 // Intercept ActivityPub client requests
45 activityPubClientRouter.get(
46 [ '/accounts?/:name', '/accounts?/:name/video-channels' ],
48 asyncMiddleware(localAccountValidator),
51 activityPubClientRouter.get('/accounts?/:name/followers',
53 asyncMiddleware(localAccountValidator),
54 asyncMiddleware(accountFollowersController)
56 activityPubClientRouter.get('/accounts?/:name/following',
58 asyncMiddleware(localAccountValidator),
59 asyncMiddleware(accountFollowingController)
61 activityPubClientRouter.get('/accounts?/:name/playlists',
63 asyncMiddleware(localAccountValidator),
64 asyncMiddleware(accountPlaylistsController)
66 activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
68 asyncMiddleware(getAccountVideoRateValidatorFactory('like')),
69 getAccountVideoRateFactory('like')
71 activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
73 asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')),
74 getAccountVideoRateFactory('dislike')
77 activityPubClientRouter.get('/videos/watch/:id',
79 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)),
80 asyncMiddleware(videosCustomGetValidator('all')),
81 asyncMiddleware(videoController)
83 activityPubClientRouter.get('/videos/watch/:id/activity',
85 asyncMiddleware(videosCustomGetValidator('all')),
86 asyncMiddleware(videoController)
88 activityPubClientRouter.get('/videos/watch/:id/announces',
90 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
91 asyncMiddleware(videoAnnouncesController)
93 activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
95 asyncMiddleware(videosShareValidator),
96 asyncMiddleware(videoAnnounceController)
98 activityPubClientRouter.get('/videos/watch/:id/likes',
100 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
101 asyncMiddleware(videoLikesController)
103 activityPubClientRouter.get('/videos/watch/:id/dislikes',
104 executeIfActivityPub,
105 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
106 asyncMiddleware(videoDislikesController)
108 activityPubClientRouter.get('/videos/watch/:id/comments',
109 executeIfActivityPub,
110 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
111 asyncMiddleware(videoCommentsController)
113 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
114 executeIfActivityPub,
115 asyncMiddleware(videoCommentGetValidator),
116 asyncMiddleware(videoCommentController)
118 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
119 executeIfActivityPub,
120 asyncMiddleware(videoCommentGetValidator),
121 asyncMiddleware(videoCommentController)
124 activityPubClientRouter.get(
125 [ '/video-channels/:name', '/video-channels/:name/videos' ],
126 executeIfActivityPub,
127 asyncMiddleware(localVideoChannelValidator),
128 videoChannelController
130 activityPubClientRouter.get('/video-channels/:name/followers',
131 executeIfActivityPub,
132 asyncMiddleware(localVideoChannelValidator),
133 asyncMiddleware(videoChannelFollowersController)
135 activityPubClientRouter.get('/video-channels/:name/following',
136 executeIfActivityPub,
137 asyncMiddleware(localVideoChannelValidator),
138 asyncMiddleware(videoChannelFollowingController)
140 activityPubClientRouter.get('/video-channels/:name/playlists',
141 executeIfActivityPub,
142 asyncMiddleware(localVideoChannelValidator),
143 asyncMiddleware(videoChannelPlaylistsController)
146 activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
147 executeIfActivityPub,
148 asyncMiddleware(videoFileRedundancyGetValidator),
149 asyncMiddleware(videoRedundancyController)
151 activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
152 executeIfActivityPub,
153 asyncMiddleware(videoPlaylistRedundancyGetValidator),
154 asyncMiddleware(videoRedundancyController)
157 activityPubClientRouter.get('/video-playlists/:playlistId',
158 executeIfActivityPub,
159 asyncMiddleware(videoPlaylistsGetValidator('all')),
160 asyncMiddleware(videoPlaylistController)
162 activityPubClientRouter.get('/video-playlists/:playlistId/videos/:playlistElementId',
163 executeIfActivityPub,
164 asyncMiddleware(videoPlaylistElementAPGetValidator),
165 videoPlaylistElementController
168 // ---------------------------------------------------------------------------
171 activityPubClientRouter
174 // ---------------------------------------------------------------------------
176 function accountController (req: express.Request, res: express.Response) {
177 const account = res.locals.account
179 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
182 async function accountFollowersController (req: express.Request, res: express.Response) {
183 const account = res.locals.account
184 const activityPubResult = await actorFollowers(req, account.Actor)
186 return activityPubResponse(activityPubContextify(activityPubResult), res)
189 async function accountFollowingController (req: express.Request, res: express.Response) {
190 const account = res.locals.account
191 const activityPubResult = await actorFollowing(req, account.Actor)
193 return activityPubResponse(activityPubContextify(activityPubResult), res)
196 async function accountPlaylistsController (req: express.Request, res: express.Response) {
197 const account = res.locals.account
198 const activityPubResult = await actorPlaylists(req, { account })
200 return activityPubResponse(activityPubContextify(activityPubResult), res)
203 async function videoChannelPlaylistsController (req: express.Request, res: express.Response) {
204 const channel = res.locals.videoChannel
205 const activityPubResult = await actorPlaylists(req, { channel })
207 return activityPubResponse(activityPubContextify(activityPubResult), res)
210 function getAccountVideoRateFactory (rateType: VideoRateType) {
211 return (req: express.Request, res: express.Response) => {
212 const accountVideoRate = res.locals.accountVideoRate
214 const byActor = accountVideoRate.Account.Actor
215 const APObject = rateType === 'like'
216 ? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
217 : buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
219 return activityPubResponse(activityPubContextify(APObject), res)
223 async function videoController (req: express.Request, res: express.Response) {
224 const video = res.locals.videoAll
226 if (redirectIfNotOwned(video.url, res)) return
228 // We need captions to render AP object
229 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
230 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
232 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
233 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
235 if (req.path.endsWith('/activity')) {
236 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
237 return activityPubResponse(activityPubContextify(data), res)
240 return activityPubResponse(activityPubContextify(videoObject), res)
243 async function videoAnnounceController (req: express.Request, res: express.Response) {
244 const share = res.locals.videoShare
246 if (redirectIfNotOwned(share.url, res)) return
248 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
250 return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
253 async function videoAnnouncesController (req: express.Request, res: express.Response) {
254 const video = res.locals.onlyImmutableVideo
256 if (redirectIfNotOwned(video.url, res)) return
258 const handler = async (start: number, count: number) => {
259 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
262 data: result.rows.map(r => r.url)
265 const json = await activityPubCollectionPagination(getLocalVideoSharesActivityPubUrl(video), handler, req.query.page)
267 return activityPubResponse(activityPubContextify(json), res)
270 async function videoLikesController (req: express.Request, res: express.Response) {
271 const video = res.locals.onlyImmutableVideo
273 if (redirectIfNotOwned(video.url, res)) return
275 const json = await videoRates(req, 'like', video, getLocalVideoLikesActivityPubUrl(video))
277 return activityPubResponse(activityPubContextify(json), res)
280 async function videoDislikesController (req: express.Request, res: express.Response) {
281 const video = res.locals.onlyImmutableVideo
283 if (redirectIfNotOwned(video.url, res)) return
285 const json = await videoRates(req, 'dislike', video, getLocalVideoDislikesActivityPubUrl(video))
287 return activityPubResponse(activityPubContextify(json), res)
290 async function videoCommentsController (req: express.Request, res: express.Response) {
291 const video = res.locals.onlyImmutableVideo
293 if (redirectIfNotOwned(video.url, res)) return
295 const handler = async (start: number, count: number) => {
296 const result = await VideoCommentModel.listAndCountByVideoForAP(video, start, count)
299 data: result.rows.map(r => r.url)
302 const json = await activityPubCollectionPagination(getLocalVideoCommentsActivityPubUrl(video), handler, req.query.page)
304 return activityPubResponse(activityPubContextify(json), res)
307 function videoChannelController (req: express.Request, res: express.Response) {
308 const videoChannel = res.locals.videoChannel
310 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
313 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
314 const videoChannel = res.locals.videoChannel
315 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
317 return activityPubResponse(activityPubContextify(activityPubResult), res)
320 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
321 const videoChannel = res.locals.videoChannel
322 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
324 return activityPubResponse(activityPubContextify(activityPubResult), res)
327 async function videoCommentController (req: express.Request, res: express.Response) {
328 const videoComment = res.locals.videoCommentFull
330 if (redirectIfNotOwned(videoComment.url, res)) return
332 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
333 const isPublic = true // Comments are always public
334 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
336 if (videoComment.Account) {
337 const audience = getAudience(videoComment.Account.Actor, isPublic)
338 videoCommentObject = audiencify(videoCommentObject, audience)
340 if (req.path.endsWith('/activity')) {
341 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
342 return activityPubResponse(activityPubContextify(data), res)
346 return activityPubResponse(activityPubContextify(videoCommentObject), res)
349 async function videoRedundancyController (req: express.Request, res: express.Response) {
350 const videoRedundancy = res.locals.videoRedundancy
352 if (redirectIfNotOwned(videoRedundancy.url, res)) return
354 const serverActor = await getServerActor()
356 const audience = getAudience(serverActor)
357 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
359 if (req.path.endsWith('/activity')) {
360 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
361 return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
364 return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
367 async function videoPlaylistController (req: express.Request, res: express.Response) {
368 const playlist = res.locals.videoPlaylistFull
370 if (redirectIfNotOwned(playlist.url, res)) return
372 // We need more attributes
373 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
375 const json = await playlist.toActivityPubObject(req.query.page, null)
376 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
377 const object = audiencify(json, audience)
379 return activityPubResponse(activityPubContextify(object), res)
382 function videoPlaylistElementController (req: express.Request, res: express.Response) {
383 const videoPlaylistElement = res.locals.videoPlaylistElementAP
385 if (redirectIfNotOwned(videoPlaylistElement.url, res)) return
387 const json = videoPlaylistElement.toActivityPubObject()
388 return activityPubResponse(activityPubContextify(json), res)
391 // ---------------------------------------------------------------------------
393 async function actorFollowing (req: express.Request, actor: MActorId) {
394 const handler = (start: number, count: number) => {
395 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
398 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
401 async function actorFollowers (req: express.Request, actor: MActorId) {
402 const handler = (start: number, count: number) => {
403 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
406 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
409 async function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) {
410 const handler = (start: number, count: number) => {
411 return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count)
414 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
417 function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
418 const handler = async (start: number, count: number) => {
419 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
422 data: result.rows.map(r => r.url)
425 return activityPubCollectionPagination(url, handler, req.query.page)
428 function redirectIfNotOwned (url: string, res: express.Response) {
429 if (url.startsWith(WEBSERVER.URL) === false) {