PeerTube/server/controllers/activitypub/client.ts

121 lines
4.8 KiB
TypeScript
Raw Normal View History

2017-11-09 17:51:58 +01:00
// Intercept ActivityPub client requests
import * as express from 'express'
2017-12-12 17:53:50 +01:00
import { activityPubCollectionPagination, pageToStartAndCount } from '../../helpers'
import { ACTIVITY_PUB, CONFIG } from '../../initializers'
import { buildVideoChannelAnnounceToFollowers } from '../../lib/activitypub/send'
import { buildVideoAnnounceToFollowers } from '../../lib/index'
2017-12-12 17:53:50 +01:00
import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares'
import {
videoChannelsGetValidator,
videoChannelsShareValidator,
videosGetValidator,
videosShareValidator
} from '../../middlewares/validators'
import { AccountModel } from '../../models/account/account'
import { AccountFollowModel } from '../../models/account/account-follow'
import { VideoModel } from '../../models/video/video'
import { VideoChannelModel } from '../../models/video/video-channel'
import { VideoChannelShareModel } from '../../models/video/video-channel-share'
import { VideoShareModel } from '../../models/video/video-share'
2017-11-09 17:51:58 +01:00
const activityPubClientRouter = express.Router()
activityPubClientRouter.get('/account/:name',
2017-11-27 17:30:46 +01:00
executeIfActivityPub(asyncMiddleware(localAccountValidator)),
executeIfActivityPub(accountController)
2017-11-09 17:51:58 +01:00
)
2017-11-14 17:31:26 +01:00
activityPubClientRouter.get('/account/:name/followers',
2017-11-27 17:30:46 +01:00
executeIfActivityPub(asyncMiddleware(localAccountValidator)),
2017-11-09 17:51:58 +01:00
executeIfActivityPub(asyncMiddleware(accountFollowersController))
)
2017-11-14 17:31:26 +01:00
activityPubClientRouter.get('/account/:name/following',
2017-11-27 17:30:46 +01:00
executeIfActivityPub(asyncMiddleware(localAccountValidator)),
2017-11-09 17:51:58 +01:00
executeIfActivityPub(asyncMiddleware(accountFollowingController))
)
2017-11-16 15:22:39 +01:00
activityPubClientRouter.get('/videos/watch/:id',
2017-11-27 17:30:46 +01:00
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
executeIfActivityPub(videoController)
)
activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
executeIfActivityPub(asyncMiddleware(videosShareValidator)),
executeIfActivityPub(asyncMiddleware(videoAnnounceController))
2017-11-16 15:22:39 +01:00
)
activityPubClientRouter.get('/video-channels/:id',
2017-11-27 17:30:46 +01:00
executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
2017-11-16 15:22:39 +01:00
executeIfActivityPub(asyncMiddleware(videoChannelController))
)
activityPubClientRouter.get('/video-channels/:id/announces/:accountId',
executeIfActivityPub(asyncMiddleware(videoChannelsShareValidator)),
executeIfActivityPub(asyncMiddleware(videoChannelAnnounceController))
)
2017-11-09 17:51:58 +01:00
// ---------------------------------------------------------------------------
export {
activityPubClientRouter
}
// ---------------------------------------------------------------------------
function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 17:53:50 +01:00
const account: AccountModel = res.locals.account
2017-11-09 17:51:58 +01:00
return res.json(account.toActivityPubObject()).end()
}
async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 17:53:50 +01:00
const account: AccountModel = res.locals.account
2017-11-09 17:51:58 +01:00
const page = req.query.page || 1
2017-11-09 17:51:58 +01:00
const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
2017-12-12 17:53:50 +01:00
const result = await AccountFollowModel.listAcceptedFollowerUrlsForApi([ account.id ], undefined, start, count)
2017-11-21 18:23:10 +01:00
const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
2017-11-09 17:51:58 +01:00
return res.json(activityPubResult)
}
async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 17:53:50 +01:00
const account: AccountModel = res.locals.account
2017-11-09 17:51:58 +01:00
const page = req.query.page || 1
2017-11-09 17:51:58 +01:00
const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
2017-12-12 17:53:50 +01:00
const result = await AccountFollowModel.listAcceptedFollowingUrlsForApi([ account.id ], undefined, start, count)
2017-11-21 18:23:10 +01:00
const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
2017-11-09 17:51:58 +01:00
return res.json(activityPubResult)
}
2017-11-16 15:22:39 +01:00
function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 17:53:50 +01:00
const video: VideoModel = res.locals.video
2017-11-16 15:22:39 +01:00
return res.json(video.toActivityPubObject())
}
async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 17:53:50 +01:00
const share = res.locals.videoShare as VideoShareModel
const object = await buildVideoAnnounceToFollowers(share.Account, res.locals.video, undefined)
return res.json(object)
}
async function videoChannelAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 17:53:50 +01:00
const share = res.locals.videoChannelShare as VideoChannelShareModel
const object = await buildVideoChannelAnnounceToFollowers(share.Account, share.VideoChannel, undefined)
return res.json(object)
}
2017-11-16 15:22:39 +01:00
async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 17:53:50 +01:00
const videoChannel: VideoChannelModel = res.locals.videoChannel
2017-11-16 15:22:39 +01:00
return res.json(videoChannel.toActivityPubObject())
}