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 } from '../../models'
8 import { activityPubCollectionPagination } from '../../helpers/activitypub'
9 import { ACTIVITY_PUB } from '../../initializers/constants'
10 import { asyncMiddleware } from '../../middlewares/async'
12 const activityPubClientRouter = express.Router()
14 activityPubClientRouter.get('/account/:name',
15 executeIfActivityPub(localAccountValidator),
16 executeIfActivityPub(asyncMiddleware(accountController))
19 activityPubClientRouter.get('/account/:name/followers',
20 executeIfActivityPub(localAccountValidator),
21 executeIfActivityPub(asyncMiddleware(accountFollowersController))
24 activityPubClientRouter.get('/account/:name/following',
25 executeIfActivityPub(localAccountValidator),
26 executeIfActivityPub(asyncMiddleware(accountFollowingController))
29 // ---------------------------------------------------------------------------
32 activityPubClientRouter
35 // ---------------------------------------------------------------------------
37 async function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
38 const account: AccountInstance = res.locals.account
40 return res.json(account.toActivityPubObject()).end()
43 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
44 const account: AccountInstance = res.locals.account
46 const page = req.params.page || 1
47 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
49 const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi(account.id, start, count)
50 const activityPubResult = activityPubCollectionPagination(req.url, page, result)
52 return res.json(activityPubResult)
55 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
56 const account: AccountInstance = res.locals.account
58 const page = req.params.page || 1
59 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
61 const result = await db.AccountFollow.listAcceptedFollowingUrlsForApi(account.id, start, count)
62 const activityPubResult = activityPubCollectionPagination(req.url, page, result)
64 return res.json(activityPubResult)