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, videosGetValidator } 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'
41 const activityPubClientRouter = express.Router()
43 activityPubClientRouter.get('/accounts?/:name',
44 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
45 executeIfActivityPub(accountController)
47 activityPubClientRouter.get('/accounts?/:name/followers',
48 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
49 executeIfActivityPub(asyncMiddleware(accountFollowersController))
51 activityPubClientRouter.get('/accounts?/:name/following',
52 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
53 executeIfActivityPub(asyncMiddleware(accountFollowingController))
55 activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
56 executeIfActivityPub(asyncMiddleware(getAccountVideoRateValidator('like'))),
57 executeIfActivityPub(getAccountVideoRate('like'))
59 activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
60 executeIfActivityPub(asyncMiddleware(getAccountVideoRateValidator('dislike'))),
61 executeIfActivityPub(getAccountVideoRate('dislike'))
64 activityPubClientRouter.get('/videos/watch/:id',
65 executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
66 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video-with-rights'))),
67 executeIfActivityPub(asyncMiddleware(videoController))
69 activityPubClientRouter.get('/videos/watch/:id/activity',
70 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video-with-rights'))),
71 executeIfActivityPub(asyncMiddleware(videoController))
73 activityPubClientRouter.get('/videos/watch/:id/announces',
74 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
75 executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
77 activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
78 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
79 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
81 activityPubClientRouter.get('/videos/watch/:id/likes',
82 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
83 executeIfActivityPub(asyncMiddleware(videoLikesController))
85 activityPubClientRouter.get('/videos/watch/:id/dislikes',
86 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
87 executeIfActivityPub(asyncMiddleware(videoDislikesController))
89 activityPubClientRouter.get('/videos/watch/:id/comments',
90 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
91 executeIfActivityPub(asyncMiddleware(videoCommentsController))
93 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
94 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
95 executeIfActivityPub(asyncMiddleware(videoCommentController))
97 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
98 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
99 executeIfActivityPub(asyncMiddleware(videoCommentController))
102 activityPubClientRouter.get('/video-channels/:name',
103 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
104 executeIfActivityPub(asyncMiddleware(videoChannelController))
106 activityPubClientRouter.get('/video-channels/:name/followers',
107 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
108 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
110 activityPubClientRouter.get('/video-channels/:name/following',
111 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
112 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
115 activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
116 executeIfActivityPub(asyncMiddleware(videoFileRedundancyGetValidator)),
117 executeIfActivityPub(asyncMiddleware(videoRedundancyController))
119 activityPubClientRouter.get('/redundancy/video-playlists/:streamingPlaylistType/:videoId',
120 executeIfActivityPub(asyncMiddleware(videoPlaylistRedundancyGetValidator)),
121 executeIfActivityPub(asyncMiddleware(videoRedundancyController))
124 // ---------------------------------------------------------------------------
127 activityPubClientRouter
130 // ---------------------------------------------------------------------------
132 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
133 const account: AccountModel = res.locals.account
135 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
138 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
139 const account: AccountModel = res.locals.account
140 const activityPubResult = await actorFollowers(req, account.Actor)
142 return activityPubResponse(activityPubContextify(activityPubResult), res)
145 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
146 const account: AccountModel = res.locals.account
147 const activityPubResult = await actorFollowing(req, account.Actor)
149 return activityPubResponse(activityPubContextify(activityPubResult), res)
152 function getAccountVideoRate (rateType: VideoRateType) {
153 return (req: express.Request, res: express.Response) => {
154 const accountVideoRate: AccountVideoRateModel = res.locals.accountVideoRate
156 const byActor = accountVideoRate.Account.Actor
157 const url = getRateUrl(rateType, byActor, accountVideoRate.Video)
158 const APObject = rateType === 'like'
159 ? buildLikeActivity(url, byActor, accountVideoRate.Video)
160 : buildDislikeActivity(url, byActor, accountVideoRate.Video)
162 return activityPubResponse(activityPubContextify(APObject), res)
166 async function videoController (req: express.Request, res: express.Response) {
167 // We need more attributes
168 const video: VideoModel = await VideoModel.loadForGetAPI(res.locals.video.id)
170 if (video.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(video.url)
172 // We need captions to render AP object
173 video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id)
175 const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC)
176 const videoObject = audiencify(video.toActivityPubObject(), audience)
178 if (req.path.endsWith('/activity')) {
179 const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience)
180 return activityPubResponse(activityPubContextify(data), res)
183 return activityPubResponse(activityPubContextify(videoObject), res)
186 async function videoAnnounceController (req: express.Request, res: express.Response) {
187 const share = res.locals.videoShare as VideoShareModel
189 if (share.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(share.url)
191 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined)
193 return activityPubResponse(activityPubContextify(activity), res)
196 async function videoAnnouncesController (req: express.Request, res: express.Response) {
197 const video: VideoModel = res.locals.video
199 const handler = async (start: number, count: number) => {
200 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
203 data: result.rows.map(r => r.url)
206 const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
208 return activityPubResponse(activityPubContextify(json), res)
211 async function videoLikesController (req: express.Request, res: express.Response) {
212 const video: VideoModel = res.locals.video
213 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
215 return activityPubResponse(activityPubContextify(json), res)
218 async function videoDislikesController (req: express.Request, res: express.Response) {
219 const video: VideoModel = res.locals.video
220 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
222 return activityPubResponse(activityPubContextify(json), res)
225 async function videoCommentsController (req: express.Request, res: express.Response) {
226 const video: VideoModel = res.locals.video
228 const handler = async (start: number, count: number) => {
229 const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
232 data: result.rows.map(r => r.url)
235 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
237 return activityPubResponse(activityPubContextify(json), res)
240 async function videoChannelController (req: express.Request, res: express.Response) {
241 const videoChannel: VideoChannelModel = res.locals.videoChannel
243 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
246 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
247 const videoChannel: VideoChannelModel = res.locals.videoChannel
248 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
250 return activityPubResponse(activityPubContextify(activityPubResult), res)
253 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
254 const videoChannel: VideoChannelModel = res.locals.videoChannel
255 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
257 return activityPubResponse(activityPubContextify(activityPubResult), res)
260 async function videoCommentController (req: express.Request, res: express.Response) {
261 const videoComment: VideoCommentModel = res.locals.videoComment
263 if (videoComment.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoComment.url)
265 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
266 const isPublic = true // Comments are always public
267 const audience = getAudience(videoComment.Account.Actor, isPublic)
269 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
271 if (req.path.endsWith('/activity')) {
272 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
273 return activityPubResponse(activityPubContextify(data), res)
276 return activityPubResponse(activityPubContextify(videoCommentObject), res)
279 async function videoRedundancyController (req: express.Request, res: express.Response) {
280 const videoRedundancy: VideoRedundancyModel = res.locals.videoRedundancy
281 if (videoRedundancy.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
283 const serverActor = await getServerActor()
285 const audience = getAudience(serverActor)
286 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
288 if (req.path.endsWith('/activity')) {
289 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
290 return activityPubResponse(activityPubContextify(data), res)
293 return activityPubResponse(activityPubContextify(object), res)
296 // ---------------------------------------------------------------------------
298 async function actorFollowing (req: express.Request, actor: ActorModel) {
299 const handler = (start: number, count: number) => {
300 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
303 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page)
306 async function actorFollowers (req: express.Request, actor: ActorModel) {
307 const handler = (start: number, count: number) => {
308 return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
311 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page)
314 function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) {
315 const handler = async (start: number, count: number) => {
316 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
319 data: result.rows.map(r => r.url)
322 return activityPubCollectionPagination(url, handler, req.query.page)