1 // Intercept ActivityPub client requests
2 import * as express from 'express'
3 import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
4 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
5 import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants'
6 import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send'
7 import { audiencify, getAudience } from '../../lib/activitypub/audience'
8 import { buildCreateActivity } from '../../lib/activitypub/send/send-create'
12 localAccountValidator,
13 localVideoChannelValidator,
14 videosCustomGetValidator,
16 } from '../../middlewares'
17 import { getAccountVideoRateValidator, videoCommentGetValidator } from '../../middlewares/validators'
18 import { AccountModel } from '../../models/account/account'
19 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
20 import { VideoModel } from '../../models/video/video'
21 import { VideoCommentModel } from '../../models/video/video-comment'
22 import { VideoShareModel } from '../../models/video/video-share'
23 import { cacheRoute } from '../../middlewares/cache'
24 import { activityPubResponse } from './utils'
25 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
28 getVideoCommentsActivityPubUrl,
29 getVideoDislikesActivityPubUrl,
30 getVideoLikesActivityPubUrl,
31 getVideoSharesActivityPubUrl
32 } from '../../lib/activitypub'
33 import { VideoCaptionModel } from '../../models/video/video-caption'
34 import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
35 import { getServerActor } from '../../helpers/utils'
36 import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
37 import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
38 import { VideoPlaylistModel } from '../../models/video/video-playlist'
39 import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
40 import { MAccountId, MActorId, MVideo, MVideoAPWithoutCaption } from '@server/typings/models'
42 const activityPubClientRouter = express.Router()
44 activityPubClientRouter.get('/accounts?/:name',
46 asyncMiddleware(localAccountValidator),
49 activityPubClientRouter.get('/accounts?/:name/followers',
51 asyncMiddleware(localAccountValidator),
52 asyncMiddleware(accountFollowersController)
54 activityPubClientRouter.get('/accounts?/:name/following',
56 asyncMiddleware(localAccountValidator),
57 asyncMiddleware(accountFollowingController)
59 activityPubClientRouter.get('/accounts?/:name/playlists',
61 asyncMiddleware(localAccountValidator),
62 asyncMiddleware(accountPlaylistsController)
64 activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
66 asyncMiddleware(getAccountVideoRateValidator('like')),
67 getAccountVideoRate('like')
69 activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
71 asyncMiddleware(getAccountVideoRateValidator('dislike')),
72 getAccountVideoRate('dislike')
75 activityPubClientRouter.get('/videos/watch/:id',
77 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)),
78 asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
79 asyncMiddleware(videoController)
81 activityPubClientRouter.get('/videos/watch/:id/activity',
83 asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
84 asyncMiddleware(videoController)
86 activityPubClientRouter.get('/videos/watch/:id/announces',
88 asyncMiddleware(videosCustomGetValidator('only-video')),
89 asyncMiddleware(videoAnnouncesController)
91 activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
93 asyncMiddleware(videosShareValidator),
94 asyncMiddleware(videoAnnounceController)
96 activityPubClientRouter.get('/videos/watch/:id/likes',
98 asyncMiddleware(videosCustomGetValidator('only-video')),
99 asyncMiddleware(videoLikesController)
101 activityPubClientRouter.get('/videos/watch/:id/dislikes',
102 executeIfActivityPub,
103 asyncMiddleware(videosCustomGetValidator('only-video')),
104 asyncMiddleware(videoDislikesController)
106 activityPubClientRouter.get('/videos/watch/:id/comments',
107 executeIfActivityPub,
108 asyncMiddleware(videosCustomGetValidator('only-video')),
109 asyncMiddleware(videoCommentsController)
111 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
112 executeIfActivityPub,
113 asyncMiddleware(videoCommentGetValidator),
114 asyncMiddleware(videoCommentController)
116 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
117 executeIfActivityPub,
118 asyncMiddleware(videoCommentGetValidator),
119 asyncMiddleware(videoCommentController)
122 activityPubClientRouter.get('/video-channels/:name',
123 executeIfActivityPub,
124 asyncMiddleware(localVideoChannelValidator),
125 asyncMiddleware(videoChannelController)
127 activityPubClientRouter.get('/video-channels/:name/followers',
128 executeIfActivityPub,
129 asyncMiddleware(localVideoChannelValidator),
130 asyncMiddleware(videoChannelFollowersController)
132 activityPubClientRouter.get('/video-channels/:name/following',
133 executeIfActivityPub,
134 asyncMiddleware(localVideoChannelValidator),
135 asyncMiddleware(videoChannelFollowingController)
138 activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
139 executeIfActivityPub,
140 asyncMiddleware(videoFileRedundancyGetValidator),
141 asyncMiddleware(videoRedundancyController)
143 activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
144 executeIfActivityPub,
145 asyncMiddleware(videoPlaylistRedundancyGetValidator),
146 asyncMiddleware(videoRedundancyController)
149 activityPubClientRouter.get('/video-playlists/:playlistId',
150 executeIfActivityPub,
151 asyncMiddleware(videoPlaylistsGetValidator('all')),
152 asyncMiddleware(videoPlaylistController)
154 activityPubClientRouter.get('/video-playlists/:playlistId/:videoId',
155 executeIfActivityPub,
156 asyncMiddleware(videoPlaylistElementAPGetValidator),
157 asyncMiddleware(videoPlaylistElementController)
160 // ---------------------------------------------------------------------------
163 activityPubClientRouter
166 // ---------------------------------------------------------------------------
168 function accountController (req: express.Request, res: express.Response) {
169 const account = res.locals.account
171 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
174 async function accountFollowersController (req: express.Request, res: express.Response) {
175 const account = res.locals.account
176 const activityPubResult = await actorFollowers(req, account.Actor)
178 return activityPubResponse(activityPubContextify(activityPubResult), res)
181 async function accountFollowingController (req: express.Request, res: express.Response) {
182 const account = res.locals.account
183 const activityPubResult = await actorFollowing(req, account.Actor)
185 return activityPubResponse(activityPubContextify(activityPubResult), res)
188 async function accountPlaylistsController (req: express.Request, res: express.Response) {
189 const account = res.locals.account
190 const activityPubResult = await actorPlaylists(req, account)
192 return activityPubResponse(activityPubContextify(activityPubResult), res)
195 function getAccountVideoRate (rateType: VideoRateType) {
196 return (req: express.Request, res: express.Response) => {
197 const accountVideoRate = res.locals.accountVideoRate
199 const byActor = accountVideoRate.Account.Actor
200 const url = getRateUrl(rateType, byActor, accountVideoRate.Video)
201 const APObject = rateType === 'like'
202 ? buildLikeActivity(url, byActor, accountVideoRate.Video)
203 : buildDislikeActivity(url, byActor, accountVideoRate.Video)
205 return activityPubResponse(activityPubContextify(APObject), res)
209 async function videoController (req: express.Request, res: express.Response) {
210 // We need more attributes
211 const video = await VideoModel.loadForGetAPI({ id: res.locals.onlyVideoWithRights.id }) as MVideoAPWithoutCaption
213 if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url)
215 // We need captions to render AP object
216 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
217 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
219 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
220 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
222 if (req.path.endsWith('/activity')) {
223 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
224 return activityPubResponse(activityPubContextify(data), res)
227 return activityPubResponse(activityPubContextify(videoObject), res)
230 async function videoAnnounceController (req: express.Request, res: express.Response) {
231 const share = res.locals.videoShare
233 if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url)
235 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
237 return activityPubResponse(activityPubContextify(activity), res)
240 async function videoAnnouncesController (req: express.Request, res: express.Response) {
241 const video = res.locals.onlyVideo
243 const handler = async (start: number, count: number) => {
244 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
247 data: result.rows.map(r => r.url)
250 const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
252 return activityPubResponse(activityPubContextify(json), res)
255 async function videoLikesController (req: express.Request, res: express.Response) {
256 const video = res.locals.onlyVideo
257 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
259 return activityPubResponse(activityPubContextify(json), res)
262 async function videoDislikesController (req: express.Request, res: express.Response) {
263 const video = res.locals.onlyVideo
264 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
266 return activityPubResponse(activityPubContextify(json), res)
269 async function videoCommentsController (req: express.Request, res: express.Response) {
270 const video = res.locals.onlyVideo
272 const handler = async (start: number, count: number) => {
273 const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
276 data: result.rows.map(r => r.url)
279 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
281 return activityPubResponse(activityPubContextify(json), res)
284 async function videoChannelController (req: express.Request, res: express.Response) {
285 const videoChannel = res.locals.videoChannel
287 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
290 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
291 const videoChannel = res.locals.videoChannel
292 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
294 return activityPubResponse(activityPubContextify(activityPubResult), res)
297 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
298 const videoChannel = res.locals.videoChannel
299 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
301 return activityPubResponse(activityPubContextify(activityPubResult), res)
304 async function videoCommentController (req: express.Request, res: express.Response) {
305 const videoComment = res.locals.videoCommentFull
307 if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url)
309 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
310 const isPublic = true // Comments are always public
311 const audience = getAudience(videoComment.Account.Actor, isPublic)
313 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
315 if (req.path.endsWith('/activity')) {
316 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
317 return activityPubResponse(activityPubContextify(data), res)
320 return activityPubResponse(activityPubContextify(videoCommentObject), res)
323 async function videoRedundancyController (req: express.Request, res: express.Response) {
324 const videoRedundancy = res.locals.videoRedundancy
325 if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
327 const serverActor = await getServerActor()
329 const audience = getAudience(serverActor)
330 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
332 if (req.path.endsWith('/activity')) {
333 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
334 return activityPubResponse(activityPubContextify(data), res)
337 return activityPubResponse(activityPubContextify(object), res)
340 async function videoPlaylistController (req: express.Request, res: express.Response) {
341 const playlist = res.locals.videoPlaylistFull
343 // We need more attributes
344 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
346 const json = await playlist.toActivityPubObject(req.query.page, null)
347 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
348 const object = audiencify(json, audience)
350 return activityPubResponse(activityPubContextify(object), res)
353 async function videoPlaylistElementController (req: express.Request, res: express.Response) {
354 const videoPlaylistElement = res.locals.videoPlaylistElementAP
356 const json = videoPlaylistElement.toActivityPubObject()
357 return activityPubResponse(activityPubContextify(json), res)
360 // ---------------------------------------------------------------------------
362 async function actorFollowing (req: express.Request, actor: MActorId) {
363 const handler = (start: number, count: number) => {
364 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
367 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
370 async function actorFollowers (req: express.Request, actor: MActorId) {
371 const handler = (start: number, count: number) => {
372 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
375 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
378 async function actorPlaylists (req: express.Request, account: MAccountId) {
379 const handler = (start: number, count: number) => {
380 return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count)
383 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
386 function videoRates (req: express.Request, rateType: VideoRateType, video: MVideo, url: string) {
387 const handler = async (start: number, count: number) => {
388 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
391 data: result.rows.map(r => r.url)
394 return activityPubCollectionPagination(url, handler, req.query.page)