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/activitypub/actor-follow'
34 import { VideoModel } from '../../models/video/video'
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('/accounts?/:name',
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('only-video-with-rights')),
81 asyncMiddleware(videoController)
83 activityPubClientRouter.get('/videos/watch/:id/activity',
85 asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
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('/video-channels/:name',
125 executeIfActivityPub,
126 asyncMiddleware(localVideoChannelValidator),
127 videoChannelController
129 activityPubClientRouter.get('/video-channels/:name/followers',
130 executeIfActivityPub,
131 asyncMiddleware(localVideoChannelValidator),
132 asyncMiddleware(videoChannelFollowersController)
134 activityPubClientRouter.get('/video-channels/:name/following',
135 executeIfActivityPub,
136 asyncMiddleware(localVideoChannelValidator),
137 asyncMiddleware(videoChannelFollowingController)
139 activityPubClientRouter.get('/video-channels/:name/playlists',
140 executeIfActivityPub,
141 asyncMiddleware(localVideoChannelValidator),
142 asyncMiddleware(videoChannelPlaylistsController)
145 activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
146 executeIfActivityPub,
147 asyncMiddleware(videoFileRedundancyGetValidator),
148 asyncMiddleware(videoRedundancyController)
150 activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
151 executeIfActivityPub,
152 asyncMiddleware(videoPlaylistRedundancyGetValidator),
153 asyncMiddleware(videoRedundancyController)
156 activityPubClientRouter.get('/video-playlists/:playlistId',
157 executeIfActivityPub,
158 asyncMiddleware(videoPlaylistsGetValidator('all')),
159 asyncMiddleware(videoPlaylistController)
161 activityPubClientRouter.get('/video-playlists/:playlistId/videos/:playlistElementId',
162 executeIfActivityPub,
163 asyncMiddleware(videoPlaylistElementAPGetValidator),
164 videoPlaylistElementController
167 // ---------------------------------------------------------------------------
170 activityPubClientRouter
173 // ---------------------------------------------------------------------------
175 function accountController (req: express.Request, res: express.Response) {
176 const account = res.locals.account
178 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
181 async function accountFollowersController (req: express.Request, res: express.Response) {
182 const account = res.locals.account
183 const activityPubResult = await actorFollowers(req, account.Actor)
185 return activityPubResponse(activityPubContextify(activityPubResult), res)
188 async function accountFollowingController (req: express.Request, res: express.Response) {
189 const account = res.locals.account
190 const activityPubResult = await actorFollowing(req, account.Actor)
192 return activityPubResponse(activityPubContextify(activityPubResult), res)
195 async function accountPlaylistsController (req: express.Request, res: express.Response) {
196 const account = res.locals.account
197 const activityPubResult = await actorPlaylists(req, { account })
199 return activityPubResponse(activityPubContextify(activityPubResult), res)
202 async function videoChannelPlaylistsController (req: express.Request, res: express.Response) {
203 const channel = res.locals.videoChannel
204 const activityPubResult = await actorPlaylists(req, { channel })
206 return activityPubResponse(activityPubContextify(activityPubResult), res)
209 function getAccountVideoRateFactory (rateType: VideoRateType) {
210 return (req: express.Request, res: express.Response) => {
211 const accountVideoRate = res.locals.accountVideoRate
213 const byActor = accountVideoRate.Account.Actor
214 const APObject = rateType === 'like'
215 ? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
216 : buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
218 return activityPubResponse(activityPubContextify(APObject), res)
222 async function videoController (req: express.Request, res: express.Response) {
223 // We need more attributes
224 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(res.locals.onlyVideoWithRights.id)
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) {