1 // Intercept ActivityPub client requests
2 import * as express from 'express'
4 import { database as db } from '../../initializers'
5 import { executeIfActivityPub, localAccountValidator } from '../../middlewares'
6 import { pageToStartAndCount } from '../../helpers'
7 import { AccountInstance, VideoChannelInstance } from '../../models'
8 import { activityPubCollectionPagination } from '../../helpers/activitypub'
9 import { ACTIVITY_PUB, CONFIG } from '../../initializers/constants'
10 import { asyncMiddleware } from '../../middlewares/async'
11 import { videosGetValidator } from '../../middlewares/validators/videos'
12 import { VideoInstance } from '../../models/video/video-interface'
13 import { videoChannelsGetValidator } from '../../middlewares/validators/video-channels'
15 const activityPubClientRouter = express.Router()
17 activityPubClientRouter.get('/account/:name',
18 executeIfActivityPub(localAccountValidator),
19 executeIfActivityPub(asyncMiddleware(accountController))
22 activityPubClientRouter.get('/account/:name/followers',
23 executeIfActivityPub(localAccountValidator),
24 executeIfActivityPub(asyncMiddleware(accountFollowersController))
27 activityPubClientRouter.get('/account/:name/following',
28 executeIfActivityPub(localAccountValidator),
29 executeIfActivityPub(asyncMiddleware(accountFollowingController))
32 activityPubClientRouter.get('/videos/watch/:id',
33 executeIfActivityPub(videosGetValidator),
34 executeIfActivityPub(asyncMiddleware(videoController))
37 activityPubClientRouter.get('/video-channels/:id',
38 executeIfActivityPub(videoChannelsGetValidator),
39 executeIfActivityPub(asyncMiddleware(videoChannelController))
42 // ---------------------------------------------------------------------------
45 activityPubClientRouter
48 // ---------------------------------------------------------------------------
50 async function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
51 const account: AccountInstance = res.locals.account
53 return res.json(account.toActivityPubObject()).end()
56 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
57 const account: AccountInstance = res.locals.account
59 const page = req.query.page || 1
60 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
62 const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi([ account.id ], start, count)
63 const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
65 return res.json(activityPubResult)
68 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
69 const account: AccountInstance = res.locals.account
71 const page = req.query.page || 1
72 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
74 const result = await db.AccountFollow.listAcceptedFollowingUrlsForApi([ account.id ], start, count)
75 const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
77 return res.json(activityPubResult)
80 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
81 const video: VideoInstance = res.locals.video
83 return res.json(video.toActivityPubObject())
86 async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
87 const videoChannel: VideoChannelInstance = res.locals.videoChannel
89 return res.json(videoChannel.toActivityPubObject())