1 import * as express from 'express'
2 import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
3 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
4 import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants'
5 import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send'
6 import { audiencify, getAudience } from '../../lib/activitypub/audience'
7 import { buildCreateActivity } from '../../lib/activitypub/send/send-create'
11 localAccountValidator,
12 localVideoChannelValidator,
13 videosCustomGetValidator,
15 } from '../../middlewares'
16 import { getAccountVideoRateValidatorFactory, videoCommentGetValidator } from '../../middlewares/validators'
17 import { AccountModel } from '../../models/account/account'
18 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
19 import { VideoModel } from '../../models/video/video'
20 import { VideoCommentModel } from '../../models/video/video-comment'
21 import { VideoShareModel } from '../../models/video/video-share'
22 import { cacheRoute } from '../../middlewares/cache'
23 import { activityPubResponse } from './utils'
24 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
27 getVideoCommentsActivityPubUrl,
28 getVideoDislikesActivityPubUrl,
29 getVideoLikesActivityPubUrl,
30 getVideoSharesActivityPubUrl
31 } from '../../lib/activitypub'
32 import { VideoCaptionModel } from '../../models/video/video-caption'
33 import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
34 import { getServerActor } from '../../helpers/utils'
35 import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
36 import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
37 import { VideoPlaylistModel } from '../../models/video/video-playlist'
38 import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
39 import { MAccountId, MActorId, MVideoAPWithoutCaption, MVideoId } from '@server/typings/models'
41 const activityPubClientRouter = express.Router()
43 // Intercept ActivityPub client requests
45 activityPubClientRouter.get('/accounts?/:name',
47 asyncMiddleware(localAccountValidator),
50 activityPubClientRouter.get('/accounts?/:name/followers',
52 asyncMiddleware(localAccountValidator),
53 asyncMiddleware(accountFollowersController)
55 activityPubClientRouter.get('/accounts?/:name/following',
57 asyncMiddleware(localAccountValidator),
58 asyncMiddleware(accountFollowingController)
60 activityPubClientRouter.get('/accounts?/:name/playlists',
62 asyncMiddleware(localAccountValidator),
63 asyncMiddleware(accountPlaylistsController)
65 activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
67 asyncMiddleware(getAccountVideoRateValidatorFactory('like')),
68 getAccountVideoRateFactory('like')
70 activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
72 asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')),
73 getAccountVideoRateFactory('dislike')
76 activityPubClientRouter.get('/videos/watch/:id',
78 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)),
79 asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
80 asyncMiddleware(videoController)
82 activityPubClientRouter.get('/videos/watch/:id/activity',
84 asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
85 asyncMiddleware(videoController)
87 activityPubClientRouter.get('/videos/watch/:id/announces',
89 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
90 asyncMiddleware(videoAnnouncesController)
92 activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
94 asyncMiddleware(videosShareValidator),
95 asyncMiddleware(videoAnnounceController)
97 activityPubClientRouter.get('/videos/watch/:id/likes',
99 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
100 asyncMiddleware(videoLikesController)
102 activityPubClientRouter.get('/videos/watch/:id/dislikes',
103 executeIfActivityPub,
104 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
105 asyncMiddleware(videoDislikesController)
107 activityPubClientRouter.get('/videos/watch/:id/comments',
108 executeIfActivityPub,
109 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
110 asyncMiddleware(videoCommentsController)
112 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
113 executeIfActivityPub,
114 asyncMiddleware(videoCommentGetValidator),
115 asyncMiddleware(videoCommentController)
117 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
118 executeIfActivityPub,
119 asyncMiddleware(videoCommentGetValidator),
120 asyncMiddleware(videoCommentController)
123 activityPubClientRouter.get('/video-channels/:name',
124 executeIfActivityPub,
125 asyncMiddleware(localVideoChannelValidator),
126 videoChannelController
128 activityPubClientRouter.get('/video-channels/:name/followers',
129 executeIfActivityPub,
130 asyncMiddleware(localVideoChannelValidator),
131 asyncMiddleware(videoChannelFollowersController)
133 activityPubClientRouter.get('/video-channels/:name/following',
134 executeIfActivityPub,
135 asyncMiddleware(localVideoChannelValidator),
136 asyncMiddleware(videoChannelFollowingController)
139 activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
140 executeIfActivityPub,
141 asyncMiddleware(videoFileRedundancyGetValidator),
142 asyncMiddleware(videoRedundancyController)
144 activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
145 executeIfActivityPub,
146 asyncMiddleware(videoPlaylistRedundancyGetValidator),
147 asyncMiddleware(videoRedundancyController)
150 activityPubClientRouter.get('/video-playlists/:playlistId',
151 executeIfActivityPub,
152 asyncMiddleware(videoPlaylistsGetValidator('all')),
153 asyncMiddleware(videoPlaylistController)
155 activityPubClientRouter.get('/video-playlists/:playlistId/:videoId',
156 executeIfActivityPub,
157 asyncMiddleware(videoPlaylistElementAPGetValidator),
158 videoPlaylistElementController
161 // ---------------------------------------------------------------------------
164 activityPubClientRouter
167 // ---------------------------------------------------------------------------
169 function accountController (req: express.Request, res: express.Response) {
170 const account = res.locals.account
172 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
175 async function accountFollowersController (req: express.Request, res: express.Response) {
176 const account = res.locals.account
177 const activityPubResult = await actorFollowers(req, account.Actor)
179 return activityPubResponse(activityPubContextify(activityPubResult), res)
182 async function accountFollowingController (req: express.Request, res: express.Response) {
183 const account = res.locals.account
184 const activityPubResult = await actorFollowing(req, account.Actor)
186 return activityPubResponse(activityPubContextify(activityPubResult), res)
189 async function accountPlaylistsController (req: express.Request, res: express.Response) {
190 const account = res.locals.account
191 const activityPubResult = await actorPlaylists(req, account)
193 return activityPubResponse(activityPubContextify(activityPubResult), res)
196 function getAccountVideoRateFactory (rateType: VideoRateType) {
197 return (req: express.Request, res: express.Response) => {
198 const accountVideoRate = res.locals.accountVideoRate
200 const byActor = accountVideoRate.Account.Actor
201 const url = getRateUrl(rateType, byActor, accountVideoRate.Video)
202 const APObject = rateType === 'like'
203 ? buildLikeActivity(url, byActor, accountVideoRate.Video)
204 : buildDislikeActivity(url, byActor, accountVideoRate.Video)
206 return activityPubResponse(activityPubContextify(APObject), res)
210 async function videoController (req: express.Request, res: express.Response) {
211 // We need more attributes
212 const video = await VideoModel.loadForGetAPI({ id: res.locals.onlyVideoWithRights.id }) as MVideoAPWithoutCaption
214 if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url)
216 // We need captions to render AP object
217 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
218 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
220 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
221 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
223 if (req.path.endsWith('/activity')) {
224 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
225 return activityPubResponse(activityPubContextify(data), res)
228 return activityPubResponse(activityPubContextify(videoObject), res)
231 async function videoAnnounceController (req: express.Request, res: express.Response) {
232 const share = res.locals.videoShare
234 if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url)
236 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
238 return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
241 async function videoAnnouncesController (req: express.Request, res: express.Response) {
242 const video = res.locals.onlyImmutableVideo
244 const handler = async (start: number, count: number) => {
245 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
248 data: result.rows.map(r => r.url)
251 const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
253 return activityPubResponse(activityPubContextify(json), res)
256 async function videoLikesController (req: express.Request, res: express.Response) {
257 const video = res.locals.onlyImmutableVideo
258 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
260 return activityPubResponse(activityPubContextify(json), res)
263 async function videoDislikesController (req: express.Request, res: express.Response) {
264 const video = res.locals.onlyImmutableVideo
265 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
267 return activityPubResponse(activityPubContextify(json), res)
270 async function videoCommentsController (req: express.Request, res: express.Response) {
271 const video = res.locals.onlyImmutableVideo
273 const handler = async (start: number, count: number) => {
274 const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
277 data: result.rows.map(r => r.url)
280 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
282 return activityPubResponse(activityPubContextify(json), res)
285 function videoChannelController (req: express.Request, res: express.Response) {
286 const videoChannel = res.locals.videoChannel
288 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
291 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
292 const videoChannel = res.locals.videoChannel
293 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
295 return activityPubResponse(activityPubContextify(activityPubResult), res)
298 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
299 const videoChannel = res.locals.videoChannel
300 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
302 return activityPubResponse(activityPubContextify(activityPubResult), res)
305 async function videoCommentController (req: express.Request, res: express.Response) {
306 const videoComment = res.locals.videoCommentFull
308 if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url)
310 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
311 const isPublic = true // Comments are always public
312 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
314 if (videoComment.Account) {
315 const audience = getAudience(videoComment.Account.Actor, isPublic)
316 videoCommentObject = audiencify(videoCommentObject, audience)
318 if (req.path.endsWith('/activity')) {
319 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
320 return activityPubResponse(activityPubContextify(data), res)
324 return activityPubResponse(activityPubContextify(videoCommentObject), res)
327 async function videoRedundancyController (req: express.Request, res: express.Response) {
328 const videoRedundancy = res.locals.videoRedundancy
329 if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
331 const serverActor = await getServerActor()
333 const audience = getAudience(serverActor)
334 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
336 if (req.path.endsWith('/activity')) {
337 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
338 return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
341 return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
344 async function videoPlaylistController (req: express.Request, res: express.Response) {
345 const playlist = res.locals.videoPlaylistFull
347 // We need more attributes
348 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
350 const json = await playlist.toActivityPubObject(req.query.page, null)
351 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
352 const object = audiencify(json, audience)
354 return activityPubResponse(activityPubContextify(object), res)
357 function videoPlaylistElementController (req: express.Request, res: express.Response) {
358 const videoPlaylistElement = res.locals.videoPlaylistElementAP
360 const json = videoPlaylistElement.toActivityPubObject()
361 return activityPubResponse(activityPubContextify(json), res)
364 // ---------------------------------------------------------------------------
366 async function actorFollowing (req: express.Request, actor: MActorId) {
367 const handler = (start: number, count: number) => {
368 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
371 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
374 async function actorFollowers (req: express.Request, actor: MActorId) {
375 const handler = (start: number, count: number) => {
376 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
379 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
382 async function actorPlaylists (req: express.Request, account: MAccountId) {
383 const handler = (start: number, count: number) => {
384 return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count)
387 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
390 function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
391 const handler = async (start: number, count: number) => {
392 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
395 data: result.rows.map(r => r.url)
398 return activityPubCollectionPagination(url, handler, req.query.page)