1 // Intercept ActivityPub client requests
2 import * as express from 'express'
3 import { VideoPrivacy } from '../../../shared/models/videos'
4 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
5 import { pageToStartAndCount } from '../../helpers/core-utils'
6 import { ACTIVITY_PUB, CONFIG } from '../../initializers'
7 import { buildVideoAnnounceToFollowers } from '../../lib/activitypub/send'
8 import { audiencify, getAudience } from '../../lib/activitypub/send/misc'
9 import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares'
10 import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators'
11 import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
12 import { AccountModel } from '../../models/account/account'
13 import { ActorModel } from '../../models/activitypub/actor'
14 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
15 import { VideoModel } from '../../models/video/video'
16 import { VideoChannelModel } from '../../models/video/video-channel'
17 import { VideoCommentModel } from '../../models/video/video-comment'
18 import { VideoShareModel } from '../../models/video/video-share'
20 const activityPubClientRouter = express.Router()
22 activityPubClientRouter.get('/accounts?/:name',
23 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
24 executeIfActivityPub(accountController)
26 activityPubClientRouter.get('/accounts?/:name/followers',
27 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
28 executeIfActivityPub(asyncMiddleware(accountFollowersController))
30 activityPubClientRouter.get('/accounts?/:name/following',
31 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
32 executeIfActivityPub(asyncMiddleware(accountFollowingController))
35 activityPubClientRouter.get('/videos/watch/:id',
36 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
37 executeIfActivityPub(asyncMiddleware(videoController))
39 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
40 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
41 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
43 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
44 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
45 executeIfActivityPub(asyncMiddleware(videoCommentController))
48 activityPubClientRouter.get('/video-channels/:id',
49 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
50 executeIfActivityPub(asyncMiddleware(videoChannelController))
52 activityPubClientRouter.get('/video-channels/:id/followers',
53 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
54 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
56 activityPubClientRouter.get('/video-channels/:id/following',
57 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
58 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
61 // ---------------------------------------------------------------------------
64 activityPubClientRouter
67 // ---------------------------------------------------------------------------
69 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
70 const account: AccountModel = res.locals.account
72 return res.json(activityPubContextify(account.toActivityPubObject()))
76 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
77 const account: AccountModel = res.locals.account
78 const activityPubResult = await actorFollowers(req, account.Actor)
80 return res.json(activityPubContextify(activityPubResult))
83 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
84 const account: AccountModel = res.locals.account
85 const activityPubResult = await actorFollowing(req, account.Actor)
87 return res.json(activityPubContextify(activityPubResult))
90 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
91 const video: VideoModel = res.locals.video
93 // We need more attributes
94 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
95 const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC)
96 const videoObject = audiencify(videoAll.toActivityPubObject(), audience)
98 return res.json(activityPubContextify(videoObject))
101 async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
102 const share = res.locals.videoShare as VideoShareModel
103 const object = await buildVideoAnnounceToFollowers(share.Actor, res.locals.video, undefined)
105 return res.json(activityPubContextify(object))
108 async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
109 const videoChannel: VideoChannelModel = res.locals.videoChannel
111 return res.json(activityPubContextify(videoChannel.toActivityPubObject()))
114 async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
115 const videoChannel: VideoChannelModel = res.locals.videoChannel
116 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
118 return res.json(activityPubContextify(activityPubResult))
121 async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
122 const videoChannel: VideoChannelModel = res.locals.videoChannel
123 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
125 return res.json(activityPubContextify(activityPubResult))
128 async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
129 const videoComment: VideoCommentModel = res.locals.videoComment
131 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
132 const isPublic = true // Comments are always public
133 const audience = await getAudience(videoComment.Account.Actor, undefined, isPublic)
135 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
137 return res.json(activityPubContextify(videoCommentObject))
140 // ---------------------------------------------------------------------------
142 async function actorFollowing (req: express.Request, actor: ActorModel) {
143 const page = req.query.page || 1
144 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
146 const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
147 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
150 async function actorFollowers (req: express.Request, actor: ActorModel) {
151 const page = req.query.page || 1
152 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
154 const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
155 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)