PeerTube/server/controllers/activitypub/client.ts

91 lines
3.5 KiB
TypeScript
Raw Normal View History

2017-11-09 10:51:58 -06:00
// Intercept ActivityPub client requests
import * as express from 'express'
import { database as db } from '../../initializers'
import { executeIfActivityPub, localAccountValidator } from '../../middlewares'
import { pageToStartAndCount } from '../../helpers'
2017-11-16 08:22:39 -06:00
import { AccountInstance, VideoChannelInstance } from '../../models'
2017-11-09 10:51:58 -06:00
import { activityPubCollectionPagination } from '../../helpers/activitypub'
2017-11-21 11:23:10 -06:00
import { ACTIVITY_PUB, CONFIG } from '../../initializers/constants'
2017-11-09 10:51:58 -06:00
import { asyncMiddleware } from '../../middlewares/async'
2017-11-16 08:22:39 -06:00
import { videosGetValidator } from '../../middlewares/validators/videos'
import { VideoInstance } from '../../models/video/video-interface'
import { videoChannelsGetValidator } from '../../middlewares/validators/video-channels'
2017-11-09 10:51:58 -06:00
const activityPubClientRouter = express.Router()
activityPubClientRouter.get('/account/:name',
executeIfActivityPub(localAccountValidator),
executeIfActivityPub(asyncMiddleware(accountController))
)
2017-11-14 10:31:26 -06:00
activityPubClientRouter.get('/account/:name/followers',
2017-11-09 10:51:58 -06:00
executeIfActivityPub(localAccountValidator),
executeIfActivityPub(asyncMiddleware(accountFollowersController))
)
2017-11-14 10:31:26 -06:00
activityPubClientRouter.get('/account/:name/following',
2017-11-09 10:51:58 -06:00
executeIfActivityPub(localAccountValidator),
executeIfActivityPub(asyncMiddleware(accountFollowingController))
)
2017-11-16 08:22:39 -06:00
activityPubClientRouter.get('/videos/watch/:id',
executeIfActivityPub(videosGetValidator),
executeIfActivityPub(asyncMiddleware(videoController))
)
activityPubClientRouter.get('/video-channels/:id',
executeIfActivityPub(videoChannelsGetValidator),
executeIfActivityPub(asyncMiddleware(videoChannelController))
)
2017-11-09 10:51:58 -06:00
// ---------------------------------------------------------------------------
export {
activityPubClientRouter
}
// ---------------------------------------------------------------------------
async function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
const account: AccountInstance = res.locals.account
return res.json(account.toActivityPubObject()).end()
}
async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
const account: AccountInstance = res.locals.account
const page = req.query.page || 1
2017-11-09 10:51:58 -06:00
const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi([ account.id ], start, count)
2017-11-21 11:23:10 -06:00
const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
2017-11-09 10:51:58 -06:00
return res.json(activityPubResult)
}
async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
const account: AccountInstance = res.locals.account
const page = req.query.page || 1
2017-11-09 10:51:58 -06:00
const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
const result = await db.AccountFollow.listAcceptedFollowingUrlsForApi([ account.id ], start, count)
2017-11-21 11:23:10 -06:00
const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
2017-11-09 10:51:58 -06:00
return res.json(activityPubResult)
}
2017-11-16 08:22:39 -06:00
async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
const video: VideoInstance = res.locals.video
return res.json(video.toActivityPubObject())
}
async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
const videoChannel: VideoChannelInstance = res.locals.videoChannel
return res.json(videoChannel.toActivityPubObject())
}