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 { videoRedundancyGetValidator } 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(videosGetValidator)),
67 executeIfActivityPub(asyncMiddleware(videoController))
69 activityPubClientRouter.get('/videos/watch/:id/activity',
70 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
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(videoRedundancyGetValidator)),
117 executeIfActivityPub(asyncMiddleware(videoRedundancyController))
120 // ---------------------------------------------------------------------------
123 activityPubClientRouter
126 // ---------------------------------------------------------------------------
128 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
129 const account: AccountModel = res.locals.account
131 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
134 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
135 const account: AccountModel = res.locals.account
136 const activityPubResult = await actorFollowers(req, account.Actor)
138 return activityPubResponse(activityPubContextify(activityPubResult), res)
141 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
142 const account: AccountModel = res.locals.account
143 const activityPubResult = await actorFollowing(req, account.Actor)
145 return activityPubResponse(activityPubContextify(activityPubResult), res)
148 function getAccountVideoRate (rateType: VideoRateType) {
149 return (req: express.Request, res: express.Response) => {
150 const accountVideoRate: AccountVideoRateModel = res.locals.accountVideoRate
152 const byActor = accountVideoRate.Account.Actor
153 const url = getRateUrl(rateType, byActor, accountVideoRate.Video)
154 const APObject = rateType === 'like'
155 ? buildLikeActivity(url, byActor, accountVideoRate.Video)
156 : buildDislikeActivity(url, byActor, accountVideoRate.Video)
158 return activityPubResponse(activityPubContextify(APObject), res)
162 async function videoController (req: express.Request, res: express.Response) {
163 const video: VideoModel = res.locals.video
165 if (video.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(video.url)
167 // We need captions to render AP object
168 video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id)
170 const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC)
171 const videoObject = audiencify(video.toActivityPubObject(), audience)
173 if (req.path.endsWith('/activity')) {
174 const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience)
175 return activityPubResponse(activityPubContextify(data), res)
178 return activityPubResponse(activityPubContextify(videoObject), res)
181 async function videoAnnounceController (req: express.Request, res: express.Response) {
182 const share = res.locals.videoShare as VideoShareModel
184 if (share.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(share.url)
186 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined)
188 return activityPubResponse(activityPubContextify(activity), res)
191 async function videoAnnouncesController (req: express.Request, res: express.Response) {
192 const video: VideoModel = res.locals.video
194 const handler = async (start: number, count: number) => {
195 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
198 data: result.rows.map(r => r.url)
201 const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
203 return activityPubResponse(activityPubContextify(json), res)
206 async function videoLikesController (req: express.Request, res: express.Response) {
207 const video: VideoModel = res.locals.video
208 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
210 return activityPubResponse(activityPubContextify(json), res)
213 async function videoDislikesController (req: express.Request, res: express.Response) {
214 const video: VideoModel = res.locals.video
215 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
217 return activityPubResponse(activityPubContextify(json), res)
220 async function videoCommentsController (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 VideoCommentModel.listAndCountByVideoId(video.id, start, count)
227 data: result.rows.map(r => r.url)
230 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
232 return activityPubResponse(activityPubContextify(json), res)
235 async function videoChannelController (req: express.Request, res: express.Response) {
236 const videoChannel: VideoChannelModel = res.locals.videoChannel
238 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
241 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
242 const videoChannel: VideoChannelModel = res.locals.videoChannel
243 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
245 return activityPubResponse(activityPubContextify(activityPubResult), res)
248 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
249 const videoChannel: VideoChannelModel = res.locals.videoChannel
250 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
252 return activityPubResponse(activityPubContextify(activityPubResult), res)
255 async function videoCommentController (req: express.Request, res: express.Response) {
256 const videoComment: VideoCommentModel = res.locals.videoComment
258 if (videoComment.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoComment.url)
260 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
261 const isPublic = true // Comments are always public
262 const audience = getAudience(videoComment.Account.Actor, isPublic)
264 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
266 if (req.path.endsWith('/activity')) {
267 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
268 return activityPubResponse(activityPubContextify(data), res)
271 return activityPubResponse(activityPubContextify(videoCommentObject), res)
274 async function videoRedundancyController (req: express.Request, res: express.Response) {
275 const videoRedundancy: VideoRedundancyModel = res.locals.videoRedundancy
276 if (videoRedundancy.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
278 const serverActor = await getServerActor()
280 const audience = getAudience(serverActor)
281 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
283 if (req.path.endsWith('/activity')) {
284 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
285 return activityPubResponse(activityPubContextify(data), res)
288 return activityPubResponse(activityPubContextify(object), res)
291 // ---------------------------------------------------------------------------
293 async function actorFollowing (req: express.Request, actor: ActorModel) {
294 const handler = (start: number, count: number) => {
295 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
298 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page)
301 async function actorFollowers (req: express.Request, actor: ActorModel) {
302 const handler = (start: number, count: number) => {
303 return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
306 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page)
309 function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) {
310 const handler = async (start: number, count: number) => {
311 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
314 data: result.rows.map(r => r.url)
317 return activityPubCollectionPagination(url, handler, req.query.page)