PeerTube/server/controllers/activitypub/client.ts

120 lines
5.0 KiB
TypeScript
Raw Normal View History

2017-11-09 10:51:58 -06:00
// Intercept ActivityPub client requests
import * as express from 'express'
2017-12-28 04:16:08 -06:00
import { activityPubCollectionPagination } from '../../helpers/activitypub'
import { pageToStartAndCount } from '../../helpers/core-utils'
2017-12-12 10:53:50 -06:00
import { ACTIVITY_PUB, CONFIG } from '../../initializers'
2017-12-14 10:38:41 -06:00
import { buildVideoAnnounceToFollowers } from '../../lib/activitypub/send'
2017-12-12 10:53:50 -06:00
import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares'
2017-12-14 10:38:41 -06:00
import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators'
2017-12-28 04:16:08 -06:00
import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
2017-12-12 10:53:50 -06:00
import { AccountModel } from '../../models/account/account'
2017-12-14 10:38:41 -06:00
import { ActorFollowModel } from '../../models/activitypub/actor-follow'
2017-12-12 10:53:50 -06:00
import { VideoModel } from '../../models/video/video'
import { VideoChannelModel } from '../../models/video/video-channel'
2017-12-28 04:16:08 -06:00
import { VideoCommentModel } from '../../models/video/video-comment'
2017-12-12 10:53:50 -06:00
import { VideoShareModel } from '../../models/video/video-share'
2017-11-09 10:51:58 -06:00
const activityPubClientRouter = express.Router()
2018-01-08 04:30:48 -06:00
activityPubClientRouter.get('/accounts?/:name',
2017-11-27 10:30:46 -06:00
executeIfActivityPub(asyncMiddleware(localAccountValidator)),
executeIfActivityPub(accountController)
2017-11-09 10:51:58 -06:00
)
2017-12-29 12:10:13 -06:00
activityPubClientRouter.get('/accounts/:name/followers',
2017-11-27 10:30:46 -06:00
executeIfActivityPub(asyncMiddleware(localAccountValidator)),
2017-11-09 10:51:58 -06:00
executeIfActivityPub(asyncMiddleware(accountFollowersController))
)
2017-12-29 12:10:13 -06:00
activityPubClientRouter.get('/accounts/:name/following',
2017-11-27 10:30:46 -06:00
executeIfActivityPub(asyncMiddleware(localAccountValidator)),
2017-11-09 10:51:58 -06:00
executeIfActivityPub(asyncMiddleware(accountFollowingController))
)
2017-11-16 08:22:39 -06:00
activityPubClientRouter.get('/videos/watch/:id',
2017-11-27 10:30:46 -06:00
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
2017-12-28 04:16:08 -06:00
executeIfActivityPub(asyncMiddleware(videoController))
)
activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
executeIfActivityPub(asyncMiddleware(videosShareValidator)),
executeIfActivityPub(asyncMiddleware(videoAnnounceController))
2017-11-16 08:22:39 -06:00
)
2017-12-28 04:16:08 -06:00
activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
executeIfActivityPub(asyncMiddleware(videoCommentController))
)
2017-11-16 08:22:39 -06:00
activityPubClientRouter.get('/video-channels/:id',
2017-11-27 10:30:46 -06:00
executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
2017-11-16 08:22:39 -06:00
executeIfActivityPub(asyncMiddleware(videoChannelController))
)
2017-11-09 10:51:58 -06:00
// ---------------------------------------------------------------------------
export {
activityPubClientRouter
}
// ---------------------------------------------------------------------------
function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 10:53:50 -06:00
const account: AccountModel = res.locals.account
2017-11-09 10:51:58 -06:00
2017-12-28 04:16:08 -06:00
return res.json(account.toActivityPubObject())
.end()
2017-11-09 10:51:58 -06:00
}
async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 10:53:50 -06:00
const account: AccountModel = res.locals.account
2017-11-09 10:51:58 -06:00
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)
2017-12-14 10:38:41 -06:00
const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ account.Actor.id ], undefined, 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) {
2017-12-12 10:53:50 -06:00
const account: AccountModel = res.locals.account
2017-11-09 10:51:58 -06:00
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)
2017-12-14 10:38:41 -06:00
const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ account.Actor.id ], undefined, 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
2017-12-28 04:16:08 -06:00
async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 10:53:50 -06:00
const video: VideoModel = res.locals.video
2017-11-16 08:22:39 -06:00
2017-12-28 04:16:08 -06:00
// We need more attributes
const videoAll = await VideoModel.loadAndPopulateAll(video.id)
return res.json(videoAll.toActivityPubObject())
2017-11-16 08:22:39 -06:00
}
async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 10:53:50 -06:00
const share = res.locals.videoShare as VideoShareModel
2017-12-14 10:38:41 -06:00
const object = await buildVideoAnnounceToFollowers(share.Actor, res.locals.video, undefined)
return res.json(object)
}
2017-11-16 08:22:39 -06:00
async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 10:53:50 -06:00
const videoChannel: VideoChannelModel = res.locals.videoChannel
2017-11-16 08:22:39 -06:00
return res.json(videoChannel.toActivityPubObject())
}
2017-12-28 04:16:08 -06:00
async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
const videoComment: VideoCommentModel = res.locals.videoComment
2018-01-05 04:19:25 -06:00
const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
return res.json(videoComment.toActivityPubObject(threadParentComments))
2017-12-28 04:16:08 -06:00
}