1 // Intercept ActivityPub client requests
2 import * as express from 'express'
3 import { activityPubCollectionPagination } from '../../helpers/activitypub'
4 import { pageToStartAndCount } from '../../helpers/core-utils'
5 import { ACTIVITY_PUB, CONFIG } from '../../initializers'
6 import { buildVideoAnnounceToFollowers } from '../../lib/activitypub/send'
7 import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares'
8 import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators'
9 import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
10 import { AccountModel } from '../../models/account/account'
11 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
12 import { VideoModel } from '../../models/video/video'
13 import { VideoChannelModel } from '../../models/video/video-channel'
14 import { VideoCommentModel } from '../../models/video/video-comment'
15 import { VideoShareModel } from '../../models/video/video-share'
17 const activityPubClientRouter = express.Router()
19 activityPubClientRouter.get('/accounts?/:name',
20 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
21 executeIfActivityPub(accountController)
24 activityPubClientRouter.get('/accounts/:name/followers',
25 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
26 executeIfActivityPub(asyncMiddleware(accountFollowersController))
29 activityPubClientRouter.get('/accounts/:name/following',
30 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
31 executeIfActivityPub(asyncMiddleware(accountFollowingController))
34 activityPubClientRouter.get('/videos/watch/:id',
35 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
36 executeIfActivityPub(asyncMiddleware(videoController))
39 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
40 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
41 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
44 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
45 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
46 executeIfActivityPub(asyncMiddleware(videoCommentController))
49 activityPubClientRouter.get('/video-channels/:id',
50 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
51 executeIfActivityPub(asyncMiddleware(videoChannelController))
54 // ---------------------------------------------------------------------------
57 activityPubClientRouter
60 // ---------------------------------------------------------------------------
62 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
63 const account: AccountModel = res.locals.account
65 return res.json(account.toActivityPubObject())
69 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
70 const account: AccountModel = res.locals.account
72 const page = req.query.page || 1
73 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
75 const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ account.Actor.id ], undefined, start, count)
76 const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
78 return res.json(activityPubResult)
81 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
82 const account: AccountModel = res.locals.account
84 const page = req.query.page || 1
85 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
87 const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ account.Actor.id ], undefined, start, count)
88 const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
90 return res.json(activityPubResult)
93 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
94 const video: VideoModel = res.locals.video
96 // We need more attributes
97 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
98 return res.json(videoAll.toActivityPubObject())
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(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(videoChannel.toActivityPubObject())
114 async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
115 const videoComment: VideoCommentModel = res.locals.videoComment
117 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
118 return res.json(videoComment.toActivityPubObject(threadParentComments))