1 import * as express from 'express'
2 import * as cors from 'cors'
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 { getAccountVideoRateValidatorFactory, 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'
27 getVideoCommentsActivityPubUrl,
28 getVideoDislikesActivityPubUrl,
29 getVideoLikesActivityPubUrl,
30 getVideoSharesActivityPubUrl
31 } from '../../lib/activitypub/url'
32 import { VideoCaptionModel } from '../../models/video/video-caption'
33 import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
34 import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
35 import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
36 import { VideoPlaylistModel } from '../../models/video/video-playlist'
37 import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
38 import { MAccountId, MActorId, MVideoAPWithoutCaption, MVideoId, MChannelId } from '@server/typings/models'
39 import { getServerActor } from '@server/models/application/application'
40 import { getRateUrl } from '@server/lib/activitypub/video-rates'
42 const activityPubClientRouter = express.Router()
43 activityPubClientRouter.use(cors())
45 // Intercept ActivityPub client requests
47 activityPubClientRouter.get('/accounts?/:name',
49 asyncMiddleware(localAccountValidator),
52 activityPubClientRouter.get('/accounts?/:name/followers',
54 asyncMiddleware(localAccountValidator),
55 asyncMiddleware(accountFollowersController)
57 activityPubClientRouter.get('/accounts?/:name/following',
59 asyncMiddleware(localAccountValidator),
60 asyncMiddleware(accountFollowingController)
62 activityPubClientRouter.get('/accounts?/:name/playlists',
64 asyncMiddleware(localAccountValidator),
65 asyncMiddleware(accountPlaylistsController)
67 activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
69 asyncMiddleware(getAccountVideoRateValidatorFactory('like')),
70 getAccountVideoRateFactory('like')
72 activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
74 asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')),
75 getAccountVideoRateFactory('dislike')
78 activityPubClientRouter.get('/videos/watch/:id',
80 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)),
81 asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
82 asyncMiddleware(videoController)
84 activityPubClientRouter.get('/videos/watch/:id/activity',
86 asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
87 asyncMiddleware(videoController)
89 activityPubClientRouter.get('/videos/watch/:id/announces',
91 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
92 asyncMiddleware(videoAnnouncesController)
94 activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
96 asyncMiddleware(videosShareValidator),
97 asyncMiddleware(videoAnnounceController)
99 activityPubClientRouter.get('/videos/watch/:id/likes',
100 executeIfActivityPub,
101 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
102 asyncMiddleware(videoLikesController)
104 activityPubClientRouter.get('/videos/watch/:id/dislikes',
105 executeIfActivityPub,
106 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
107 asyncMiddleware(videoDislikesController)
109 activityPubClientRouter.get('/videos/watch/:id/comments',
110 executeIfActivityPub,
111 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
112 asyncMiddleware(videoCommentsController)
114 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
115 executeIfActivityPub,
116 asyncMiddleware(videoCommentGetValidator),
117 asyncMiddleware(videoCommentController)
119 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
120 executeIfActivityPub,
121 asyncMiddleware(videoCommentGetValidator),
122 asyncMiddleware(videoCommentController)
125 activityPubClientRouter.get('/video-channels/:name',
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/:videoId',
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 url = getRateUrl(rateType, byActor, accountVideoRate.Video)
216 const APObject = rateType === 'like'
217 ? buildLikeActivity(url, byActor, accountVideoRate.Video)
218 : buildDislikeActivity(url, byActor, accountVideoRate.Video)
220 return activityPubResponse(activityPubContextify(APObject), res)
224 async function videoController (req: express.Request, res: express.Response) {
225 // We need more attributes
226 const video = await VideoModel.loadForGetAPI({ id: res.locals.onlyVideoWithRights.id }) as MVideoAPWithoutCaption
228 if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url)
230 // We need captions to render AP object
231 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
232 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
234 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
235 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
237 if (req.path.endsWith('/activity')) {
238 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
239 return activityPubResponse(activityPubContextify(data), res)
242 return activityPubResponse(activityPubContextify(videoObject), res)
245 async function videoAnnounceController (req: express.Request, res: express.Response) {
246 const share = res.locals.videoShare
248 if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url)
250 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
252 return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
255 async function videoAnnouncesController (req: express.Request, res: express.Response) {
256 const video = res.locals.onlyImmutableVideo
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(getVideoSharesActivityPubUrl(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
272 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
274 return activityPubResponse(activityPubContextify(json), res)
277 async function videoDislikesController (req: express.Request, res: express.Response) {
278 const video = res.locals.onlyImmutableVideo
279 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
281 return activityPubResponse(activityPubContextify(json), res)
284 async function videoCommentsController (req: express.Request, res: express.Response) {
285 const video = res.locals.onlyImmutableVideo
287 const handler = async (start: number, count: number) => {
288 const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
291 data: result.rows.map(r => r.url)
294 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
296 return activityPubResponse(activityPubContextify(json), res)
299 function videoChannelController (req: express.Request, res: express.Response) {
300 const videoChannel = res.locals.videoChannel
302 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
305 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
306 const videoChannel = res.locals.videoChannel
307 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
309 return activityPubResponse(activityPubContextify(activityPubResult), res)
312 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
313 const videoChannel = res.locals.videoChannel
314 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
316 return activityPubResponse(activityPubContextify(activityPubResult), res)
319 async function videoCommentController (req: express.Request, res: express.Response) {
320 const videoComment = res.locals.videoCommentFull
322 if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url)
324 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
325 const isPublic = true // Comments are always public
326 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
328 if (videoComment.Account) {
329 const audience = getAudience(videoComment.Account.Actor, isPublic)
330 videoCommentObject = audiencify(videoCommentObject, audience)
332 if (req.path.endsWith('/activity')) {
333 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
334 return activityPubResponse(activityPubContextify(data), res)
338 return activityPubResponse(activityPubContextify(videoCommentObject), res)
341 async function videoRedundancyController (req: express.Request, res: express.Response) {
342 const videoRedundancy = res.locals.videoRedundancy
343 if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
345 const serverActor = await getServerActor()
347 const audience = getAudience(serverActor)
348 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
350 if (req.path.endsWith('/activity')) {
351 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
352 return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
355 return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
358 async function videoPlaylistController (req: express.Request, res: express.Response) {
359 const playlist = res.locals.videoPlaylistFull
361 // We need more attributes
362 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
364 const json = await playlist.toActivityPubObject(req.query.page, null)
365 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
366 const object = audiencify(json, audience)
368 return activityPubResponse(activityPubContextify(object), res)
371 function videoPlaylistElementController (req: express.Request, res: express.Response) {
372 const videoPlaylistElement = res.locals.videoPlaylistElementAP
374 const json = videoPlaylistElement.toActivityPubObject()
375 return activityPubResponse(activityPubContextify(json), res)
378 // ---------------------------------------------------------------------------
380 async function actorFollowing (req: express.Request, actor: MActorId) {
381 const handler = (start: number, count: number) => {
382 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
385 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
388 async function actorFollowers (req: express.Request, actor: MActorId) {
389 const handler = (start: number, count: number) => {
390 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
393 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
396 async function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) {
397 const handler = (start: number, count: number) => {
398 return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count)
401 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
404 function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
405 const handler = async (start: number, count: number) => {
406 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
409 data: result.rows.map(r => r.url)
412 return activityPubCollectionPagination(url, handler, req.query.page)