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 { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers'
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 { ActorModel } from '../../models/activitypub/actor'
20 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
21 import { VideoModel } from '../../models/video/video'
22 import { VideoChannelModel } from '../../models/video/video-channel'
23 import { VideoCommentModel } from '../../models/video/video-comment'
24 import { VideoShareModel } from '../../models/video/video-share'
25 import { cacheRoute } from '../../middlewares/cache'
26 import { activityPubResponse } from './utils'
27 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
30 getVideoCommentsActivityPubUrl,
31 getVideoDislikesActivityPubUrl,
32 getVideoLikesActivityPubUrl,
33 getVideoSharesActivityPubUrl
34 } from '../../lib/activitypub'
35 import { VideoCaptionModel } from '../../models/video/video-caption'
36 import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
37 import { getServerActor } from '../../helpers/utils'
38 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
39 import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
40 import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
41 import { VideoPlaylistModel } from '../../models/video/video-playlist'
42 import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element'
43 import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
45 const activityPubClientRouter = express.Router()
47 activityPubClientRouter.get('/accounts?/:name',
48 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
49 executeIfActivityPub(accountController)
51 activityPubClientRouter.get('/accounts?/:name/followers',
52 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
53 executeIfActivityPub(asyncMiddleware(accountFollowersController))
55 activityPubClientRouter.get('/accounts?/:name/following',
56 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
57 executeIfActivityPub(asyncMiddleware(accountFollowingController))
59 activityPubClientRouter.get('/accounts?/:name/playlists',
60 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
61 executeIfActivityPub(asyncMiddleware(accountPlaylistsController))
63 activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
64 executeIfActivityPub(asyncMiddleware(getAccountVideoRateValidator('like'))),
65 executeIfActivityPub(getAccountVideoRate('like'))
67 activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
68 executeIfActivityPub(asyncMiddleware(getAccountVideoRateValidator('dislike'))),
69 executeIfActivityPub(getAccountVideoRate('dislike'))
72 activityPubClientRouter.get('/videos/watch/:id',
73 executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
74 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video-with-rights'))),
75 executeIfActivityPub(asyncMiddleware(videoController))
77 activityPubClientRouter.get('/videos/watch/:id/activity',
78 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video-with-rights'))),
79 executeIfActivityPub(asyncMiddleware(videoController))
81 activityPubClientRouter.get('/videos/watch/:id/announces',
82 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
83 executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
85 activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
86 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
87 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
89 activityPubClientRouter.get('/videos/watch/:id/likes',
90 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
91 executeIfActivityPub(asyncMiddleware(videoLikesController))
93 activityPubClientRouter.get('/videos/watch/:id/dislikes',
94 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
95 executeIfActivityPub(asyncMiddleware(videoDislikesController))
97 activityPubClientRouter.get('/videos/watch/:id/comments',
98 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
99 executeIfActivityPub(asyncMiddleware(videoCommentsController))
101 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
102 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
103 executeIfActivityPub(asyncMiddleware(videoCommentController))
105 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
106 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
107 executeIfActivityPub(asyncMiddleware(videoCommentController))
110 activityPubClientRouter.get('/video-channels/:name',
111 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
112 executeIfActivityPub(asyncMiddleware(videoChannelController))
114 activityPubClientRouter.get('/video-channels/:name/followers',
115 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
116 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
118 activityPubClientRouter.get('/video-channels/:name/following',
119 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
120 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
123 activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
124 executeIfActivityPub(asyncMiddleware(videoFileRedundancyGetValidator)),
125 executeIfActivityPub(asyncMiddleware(videoRedundancyController))
127 activityPubClientRouter.get('/redundancy/video-playlists/:streamingPlaylistType/:videoId',
128 executeIfActivityPub(asyncMiddleware(videoPlaylistRedundancyGetValidator)),
129 executeIfActivityPub(asyncMiddleware(videoRedundancyController))
132 activityPubClientRouter.get('/video-playlists/:playlistId',
133 executeIfActivityPub(asyncMiddleware(videoPlaylistsGetValidator)),
134 executeIfActivityPub(asyncMiddleware(videoPlaylistController))
136 activityPubClientRouter.get('/video-playlists/:playlistId/:videoId',
137 executeIfActivityPub(asyncMiddleware(videoPlaylistElementAPGetValidator)),
138 executeIfActivityPub(asyncMiddleware(videoPlaylistElementController))
141 // ---------------------------------------------------------------------------
144 activityPubClientRouter
147 // ---------------------------------------------------------------------------
149 function accountController (req: express.Request, res: express.Response) {
150 const account: AccountModel = res.locals.account
152 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
155 async function accountFollowersController (req: express.Request, res: express.Response) {
156 const account: AccountModel = res.locals.account
157 const activityPubResult = await actorFollowers(req, account.Actor)
159 return activityPubResponse(activityPubContextify(activityPubResult), res)
162 async function accountFollowingController (req: express.Request, res: express.Response) {
163 const account: AccountModel = res.locals.account
164 const activityPubResult = await actorFollowing(req, account.Actor)
166 return activityPubResponse(activityPubContextify(activityPubResult), res)
169 async function accountPlaylistsController (req: express.Request, res: express.Response) {
170 const account: AccountModel = res.locals.account
171 const activityPubResult = await actorPlaylists(req, account)
173 return activityPubResponse(activityPubContextify(activityPubResult), res)
176 function getAccountVideoRate (rateType: VideoRateType) {
177 return (req: express.Request, res: express.Response) => {
178 const accountVideoRate: AccountVideoRateModel = res.locals.accountVideoRate
180 const byActor = accountVideoRate.Account.Actor
181 const url = getRateUrl(rateType, byActor, accountVideoRate.Video)
182 const APObject = rateType === 'like'
183 ? buildLikeActivity(url, byActor, accountVideoRate.Video)
184 : buildDislikeActivity(url, byActor, accountVideoRate.Video)
186 return activityPubResponse(activityPubContextify(APObject), res)
190 async function videoController (req: express.Request, res: express.Response) {
191 // We need more attributes
192 const video: VideoModel = await VideoModel.loadForGetAPI(res.locals.video.id)
194 if (video.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(video.url)
196 // We need captions to render AP object
197 video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id)
199 const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC)
200 const videoObject = audiencify(video.toActivityPubObject(), audience)
202 if (req.path.endsWith('/activity')) {
203 const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience)
204 return activityPubResponse(activityPubContextify(data), res)
207 return activityPubResponse(activityPubContextify(videoObject), res)
210 async function videoAnnounceController (req: express.Request, res: express.Response) {
211 const share = res.locals.videoShare as VideoShareModel
213 if (share.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(share.url)
215 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined)
217 return activityPubResponse(activityPubContextify(activity), res)
220 async function videoAnnouncesController (req: express.Request, res: express.Response) {
221 const video: VideoModel = res.locals.video
223 const handler = async (start: number, count: number) => {
224 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
227 data: result.rows.map(r => r.url)
230 const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
232 return activityPubResponse(activityPubContextify(json), res)
235 async function videoLikesController (req: express.Request, res: express.Response) {
236 const video: VideoModel = res.locals.video
237 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
239 return activityPubResponse(activityPubContextify(json), res)
242 async function videoDislikesController (req: express.Request, res: express.Response) {
243 const video: VideoModel = res.locals.video
244 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
246 return activityPubResponse(activityPubContextify(json), res)
249 async function videoCommentsController (req: express.Request, res: express.Response) {
250 const video: VideoModel = res.locals.video
252 const handler = async (start: number, count: number) => {
253 const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
256 data: result.rows.map(r => r.url)
259 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
261 return activityPubResponse(activityPubContextify(json), res)
264 async function videoChannelController (req: express.Request, res: express.Response) {
265 const videoChannel: VideoChannelModel = res.locals.videoChannel
267 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
270 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
271 const videoChannel: VideoChannelModel = res.locals.videoChannel
272 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
274 return activityPubResponse(activityPubContextify(activityPubResult), res)
277 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
278 const videoChannel: VideoChannelModel = res.locals.videoChannel
279 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
281 return activityPubResponse(activityPubContextify(activityPubResult), res)
284 async function videoCommentController (req: express.Request, res: express.Response) {
285 const videoComment: VideoCommentModel = res.locals.videoComment
287 if (videoComment.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoComment.url)
289 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
290 const isPublic = true // Comments are always public
291 const audience = getAudience(videoComment.Account.Actor, isPublic)
293 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
295 if (req.path.endsWith('/activity')) {
296 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
297 return activityPubResponse(activityPubContextify(data), res)
300 return activityPubResponse(activityPubContextify(videoCommentObject), res)
303 async function videoRedundancyController (req: express.Request, res: express.Response) {
304 const videoRedundancy: VideoRedundancyModel = res.locals.videoRedundancy
305 if (videoRedundancy.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
307 const serverActor = await getServerActor()
309 const audience = getAudience(serverActor)
310 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
312 if (req.path.endsWith('/activity')) {
313 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
314 return activityPubResponse(activityPubContextify(data), res)
317 return activityPubResponse(activityPubContextify(object), res)
320 async function videoPlaylistController (req: express.Request, res: express.Response) {
321 const playlist: VideoPlaylistModel = res.locals.videoPlaylist
323 // We need more attributes
324 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
326 const json = await playlist.toActivityPubObject(req.query.page, null)
327 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
328 const object = audiencify(json, audience)
330 return activityPubResponse(activityPubContextify(object), res)
333 async function videoPlaylistElementController (req: express.Request, res: express.Response) {
334 const videoPlaylistElement: VideoPlaylistElementModel = res.locals.videoPlaylistElement
336 const json = videoPlaylistElement.toActivityPubObject()
337 return activityPubResponse(activityPubContextify(json), res)
340 // ---------------------------------------------------------------------------
342 async function actorFollowing (req: express.Request, actor: ActorModel) {
343 const handler = (start: number, count: number) => {
344 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
347 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page)
350 async function actorFollowers (req: express.Request, actor: ActorModel) {
351 const handler = (start: number, count: number) => {
352 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
355 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page)
358 async function actorPlaylists (req: express.Request, account: AccountModel) {
359 const handler = (start: number, count: number) => {
360 return VideoPlaylistModel.listUrlsOfForAP(account.id, start, count)
363 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page)
366 function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) {
367 const handler = async (start: number, count: number) => {
368 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
371 data: result.rows.map(r => r.url)
374 return activityPubCollectionPagination(url, handler, req.query.page)