2017-11-09 10:51:58 -06:00
|
|
|
// Intercept ActivityPub client requests
|
|
|
|
import * as express from 'express'
|
2018-01-10 10:18:12 -06:00
|
|
|
import { VideoPrivacy } from '../../../shared/models/videos'
|
2018-01-26 04:44:08 -06:00
|
|
|
import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { pageToStartAndCount } from '../../helpers/core-utils'
|
2018-05-11 02:44:04 -05:00
|
|
|
import { ACTIVITY_PUB, CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers'
|
2018-03-27 06:33:56 -05:00
|
|
|
import { buildVideoAnnounce } from '../../lib/activitypub/send'
|
2018-05-25 04:32:36 -05:00
|
|
|
import { audiencify, getAudience } from '../../lib/activitypub/audience'
|
2018-03-22 11:08:02 -05:00
|
|
|
import { createActivityData } from '../../lib/activitypub/send/send-create'
|
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'
|
2018-01-15 02:46:46 -06:00
|
|
|
import { ActorModel } from '../../models/activitypub/actor'
|
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'
|
2018-05-11 02:44:04 -05:00
|
|
|
import { cacheRoute } from '../../middlewares/cache'
|
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)),
|
2017-11-27 07:44:51 -06:00
|
|
|
executeIfActivityPub(accountController)
|
2017-11-09 10:51:58 -06:00
|
|
|
)
|
2018-01-15 02:46:46 -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))
|
|
|
|
)
|
2018-01-15 02:46:46 -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',
|
2018-05-11 02:44:04 -05:00
|
|
|
executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
|
2017-11-27 10:30:46 -06:00
|
|
|
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
|
2017-12-28 04:16:08 -06:00
|
|
|
executeIfActivityPub(asyncMiddleware(videoController))
|
2017-11-27 07:44:51 -06:00
|
|
|
)
|
2018-03-22 11:08:02 -05:00
|
|
|
activityPubClientRouter.get('/videos/watch/:id/activity',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoController))
|
|
|
|
)
|
2018-01-29 03:52:19 -06:00
|
|
|
activityPubClientRouter.get('/videos/watch/:id/announces',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
|
|
|
|
)
|
2017-11-27 07:44:51 -06:00
|
|
|
activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videosShareValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoAnnounceController))
|
2017-11-16 08:22:39 -06:00
|
|
|
)
|
2018-01-29 03:52:19 -06:00
|
|
|
activityPubClientRouter.get('/videos/watch/:id/likes',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoLikesController))
|
|
|
|
)
|
|
|
|
activityPubClientRouter.get('/videos/watch/:id/dislikes',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoDislikesController))
|
|
|
|
)
|
|
|
|
activityPubClientRouter.get('/videos/watch/:id/comments',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoCommentsController))
|
|
|
|
)
|
2017-12-28 04:16:08 -06:00
|
|
|
activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoCommentController))
|
|
|
|
)
|
2018-03-22 11:08:02 -05:00
|
|
|
activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoCommentController))
|
|
|
|
)
|
2017-12-28 04:16:08 -06:00
|
|
|
|
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))
|
|
|
|
)
|
2018-01-15 02:46:46 -06:00
|
|
|
activityPubClientRouter.get('/video-channels/:id/followers',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
|
|
|
|
)
|
|
|
|
activityPubClientRouter.get('/video-channels/:id/following',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
|
|
|
|
)
|
2017-11-16 08:22:39 -06:00
|
|
|
|
2017-11-09 10:51:58 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
activityPubClientRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-27 07:44:51 -06:00
|
|
|
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
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
|
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
|
2018-01-15 02:46:46 -06:00
|
|
|
const activityPubResult = await actorFollowers(req, account.Actor)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(activityPubResult), res)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-01-15 02:46:46 -06:00
|
|
|
const activityPubResult = await actorFollowing(req, account.Actor)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(activityPubResult), res)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
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)
|
2018-01-10 10:18:12 -06:00
|
|
|
const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC)
|
2018-01-26 04:44:08 -06:00
|
|
|
const videoObject = audiencify(videoAll.toActivityPubObject(), audience)
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2018-03-22 11:08:02 -05:00
|
|
|
if (req.path.endsWith('/activity')) {
|
|
|
|
const data = await createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, undefined, audience)
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(data), res)
|
2018-03-22 11:08:02 -05:00
|
|
|
}
|
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(videoObject), res)
|
2017-11-16 08:22:39 -06:00
|
|
|
}
|
|
|
|
|
2017-11-27 07:44:51 -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
|
2018-03-27 06:33:56 -05:00
|
|
|
const object = await buildVideoAnnounce(share.Actor, share, res.locals.video, undefined)
|
2017-11-27 07:44:51 -06:00
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(object), res)
|
2017-11-27 07:44:51 -06:00
|
|
|
}
|
|
|
|
|
2018-01-29 03:52:19 -06:00
|
|
|
async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const video: VideoModel = res.locals.video
|
|
|
|
|
|
|
|
// We need more attributes
|
|
|
|
const videoAll = await VideoModel.loadAndPopulateAll(video.id)
|
|
|
|
const object = videoAll.toAnnouncesActivityPubObject()
|
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(object), res)
|
2018-01-29 03:52:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const video: VideoModel = res.locals.video
|
|
|
|
|
|
|
|
// We need more attributes
|
|
|
|
const videoAll = await VideoModel.loadAndPopulateAll(video.id)
|
|
|
|
const { likesObject } = videoAll.toRatesActivityPubObjects()
|
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(likesObject), res)
|
2018-01-29 03:52:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const video: VideoModel = res.locals.video
|
|
|
|
|
|
|
|
// We need more attributes
|
|
|
|
const videoAll = await VideoModel.loadAndPopulateAll(video.id)
|
|
|
|
const { dislikesObject } = videoAll.toRatesActivityPubObjects()
|
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(dislikesObject), res)
|
2018-01-29 03:52:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const video: VideoModel = res.locals.video
|
|
|
|
|
|
|
|
// We need more attributes
|
|
|
|
const videoAll = await VideoModel.loadAndPopulateAll(video.id)
|
|
|
|
const commentsObject = videoAll.toCommentsActivityPubObject()
|
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(commentsObject), res)
|
2018-01-29 03:52:19 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
|
2017-11-16 08:22:39 -06:00
|
|
|
}
|
2017-12-28 04:16:08 -06:00
|
|
|
|
2018-01-15 02:46:46 -06:00
|
|
|
async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const videoChannel: VideoChannelModel = res.locals.videoChannel
|
|
|
|
const activityPubResult = await actorFollowers(req, videoChannel.Actor)
|
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(activityPubResult), res)
|
2018-01-15 02:46:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const videoChannel: VideoChannelModel = res.locals.videoChannel
|
|
|
|
const activityPubResult = await actorFollowing(req, videoChannel.Actor)
|
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(activityPubResult), res)
|
2018-01-15 02:46:46 -06:00
|
|
|
}
|
|
|
|
|
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)
|
2018-01-26 04:44:08 -06:00
|
|
|
const isPublic = true // Comments are always public
|
|
|
|
const audience = await getAudience(videoComment.Account.Actor, undefined, isPublic)
|
|
|
|
|
|
|
|
const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
|
|
|
|
|
2018-03-22 11:08:02 -05:00
|
|
|
if (req.path.endsWith('/activity')) {
|
|
|
|
const data = await createActivityData(videoComment.url, videoComment.Account.Actor, videoCommentObject, undefined, audience)
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(data), res)
|
2018-03-22 11:08:02 -05:00
|
|
|
}
|
|
|
|
|
2018-03-27 07:28:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(videoCommentObject), res)
|
2017-12-28 04:16:08 -06:00
|
|
|
}
|
2018-01-15 02:46:46 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function actorFollowing (req: express.Request, actor: ActorModel) {
|
|
|
|
const page = req.query.page || 1
|
|
|
|
const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
|
|
|
|
|
|
|
|
const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
|
|
|
|
return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function actorFollowers (req: express.Request, actor: ActorModel) {
|
|
|
|
const page = req.query.page || 1
|
|
|
|
const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
|
|
|
|
|
|
|
|
const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
|
|
|
|
return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
|
|
|
|
}
|
2018-03-27 07:28:33 -05:00
|
|
|
|
|
|
|
function activityPubResponse (data: any, res: express.Response) {
|
|
|
|
return res.type('application/activity+json; charset=utf-8')
|
|
|
|
.json(data)
|
|
|
|
.end()
|
|
|
|
}
|