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 } 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
15 } from '../../middlewares'
16 import { videosGetValidator, videosShareValidator } from '../../middlewares/validators'
17 import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
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'
29 getVideoCommentsActivityPubUrl,
30 getVideoDislikesActivityPubUrl,
31 getVideoLikesActivityPubUrl,
32 getVideoSharesActivityPubUrl
33 } from '../../lib/activitypub'
34 import { VideoCaptionModel } from '../../models/video/video-caption'
35 import { videoRedundancyGetValidator } from '../../middlewares/validators/redundancy'
36 import { getServerActor } from '../../helpers/utils'
38 const activityPubClientRouter = express.Router()
40 activityPubClientRouter.get('/accounts?/:name',
41 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
42 executeIfActivityPub(accountController)
44 activityPubClientRouter.get('/accounts?/:name/followers',
45 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
46 executeIfActivityPub(asyncMiddleware(accountFollowersController))
48 activityPubClientRouter.get('/accounts?/:name/following',
49 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
50 executeIfActivityPub(asyncMiddleware(accountFollowingController))
53 activityPubClientRouter.get('/videos/watch/:id',
54 executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
55 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
56 executeIfActivityPub(asyncMiddleware(videoController))
58 activityPubClientRouter.get('/videos/watch/:id/activity',
59 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
60 executeIfActivityPub(asyncMiddleware(videoController))
62 activityPubClientRouter.get('/videos/watch/:id/announces',
63 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
64 executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
66 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
67 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
68 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
70 activityPubClientRouter.get('/videos/watch/:id/likes',
71 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
72 executeIfActivityPub(asyncMiddleware(videoLikesController))
74 activityPubClientRouter.get('/videos/watch/:id/dislikes',
75 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
76 executeIfActivityPub(asyncMiddleware(videoDislikesController))
78 activityPubClientRouter.get('/videos/watch/:id/comments',
79 executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))),
80 executeIfActivityPub(asyncMiddleware(videoCommentsController))
82 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
83 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
84 executeIfActivityPub(asyncMiddleware(videoCommentController))
86 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
87 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
88 executeIfActivityPub(asyncMiddleware(videoCommentController))
91 activityPubClientRouter.get('/video-channels/:name',
92 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
93 executeIfActivityPub(asyncMiddleware(videoChannelController))
95 activityPubClientRouter.get('/video-channels/:name/followers',
96 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
97 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
99 activityPubClientRouter.get('/video-channels/:name/following',
100 executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
101 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
104 activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
105 executeIfActivityPub(asyncMiddleware(videoRedundancyGetValidator)),
106 executeIfActivityPub(asyncMiddleware(videoRedundancyController))
109 // ---------------------------------------------------------------------------
112 activityPubClientRouter
115 // ---------------------------------------------------------------------------
117 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
118 const account: AccountModel = res.locals.account
120 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
123 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
124 const account: AccountModel = res.locals.account
125 const activityPubResult = await actorFollowers(req, account.Actor)
127 return activityPubResponse(activityPubContextify(activityPubResult), res)
130 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
131 const account: AccountModel = res.locals.account
132 const activityPubResult = await actorFollowing(req, account.Actor)
134 return activityPubResponse(activityPubContextify(activityPubResult), res)
137 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
138 const video: VideoModel = res.locals.video
140 // We need captions to render AP object
141 video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id)
143 const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC)
144 const videoObject = audiencify(video.toActivityPubObject(), audience)
146 if (req.path.endsWith('/activity')) {
147 const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience)
148 return activityPubResponse(activityPubContextify(data), res)
151 return activityPubResponse(activityPubContextify(videoObject), res)
154 async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
155 const share = res.locals.videoShare as VideoShareModel
156 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined)
158 return activityPubResponse(activityPubContextify(activity), res)
161 async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) {
162 const video: VideoModel = res.locals.video
164 const handler = async (start: number, count: number) => {
165 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
168 data: result.rows.map(r => r.url)
171 const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
173 return activityPubResponse(activityPubContextify(json), res)
176 async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
177 const video: VideoModel = res.locals.video
178 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
180 return activityPubResponse(activityPubContextify(json), res)
183 async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
184 const video: VideoModel = res.locals.video
185 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
187 return activityPubResponse(activityPubContextify(json), res)
190 async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) {
191 const video: VideoModel = res.locals.video
193 const handler = async (start: number, count: number) => {
194 const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
197 data: result.rows.map(r => r.url)
200 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
202 return activityPubResponse(activityPubContextify(json), res)
205 async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
206 const videoChannel: VideoChannelModel = res.locals.videoChannel
208 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
211 async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
212 const videoChannel: VideoChannelModel = res.locals.videoChannel
213 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
215 return activityPubResponse(activityPubContextify(activityPubResult), res)
218 async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
219 const videoChannel: VideoChannelModel = res.locals.videoChannel
220 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
222 return activityPubResponse(activityPubContextify(activityPubResult), res)
225 async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
226 const videoComment: VideoCommentModel = res.locals.videoComment
228 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
229 const isPublic = true // Comments are always public
230 const audience = getAudience(videoComment.Account.Actor, isPublic)
232 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
234 if (req.path.endsWith('/activity')) {
235 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
236 return activityPubResponse(activityPubContextify(data), res)
239 return activityPubResponse(activityPubContextify(videoCommentObject), res)
242 async function videoRedundancyController (req: express.Request, res: express.Response) {
243 const videoRedundancy = res.locals.videoRedundancy
244 const serverActor = await getServerActor()
246 const audience = getAudience(serverActor)
247 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
249 if (req.path.endsWith('/activity')) {
250 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
251 return activityPubResponse(activityPubContextify(data), res)
254 return activityPubResponse(activityPubContextify(object), res)
257 // ---------------------------------------------------------------------------
259 async function actorFollowing (req: express.Request, actor: ActorModel) {
260 const handler = (start: number, count: number) => {
261 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
264 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page)
267 async function actorFollowers (req: express.Request, actor: ActorModel) {
268 const handler = (start: number, count: number) => {
269 return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
272 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page)
275 function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) {
276 const handler = async (start: number, count: number) => {
277 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
280 data: result.rows.map(r => r.Account.Actor.url)
283 return activityPubCollectionPagination(url, handler, req.query.page)