2017-11-09 17:51:58 +01:00
|
|
|
// Intercept ActivityPub client requests
|
|
|
|
import * as express from 'express'
|
2018-05-25 16:21:16 +02:00
|
|
|
import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
|
2018-01-26 11:44:08 +01:00
|
|
|
import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
|
2018-05-25 16:21:16 +02:00
|
|
|
import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers'
|
2018-03-27 13:33:56 +02:00
|
|
|
import { buildVideoAnnounce } from '../../lib/activitypub/send'
|
2018-05-25 11:32:36 +02:00
|
|
|
import { audiencify, getAudience } from '../../lib/activitypub/audience'
|
2018-03-22 17:08:02 +01:00
|
|
|
import { createActivityData } from '../../lib/activitypub/send/send-create'
|
2018-08-17 15:45:42 +02:00
|
|
|
import { asyncMiddleware, executeIfActivityPub, localAccountValidator, localVideoChannelValidator } from '../../middlewares'
|
|
|
|
import { videosGetValidator, videosShareValidator } from '../../middlewares/validators'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { AccountModel } from '../../models/account/account'
|
2018-01-15 09:46:46 +01:00
|
|
|
import { ActorModel } from '../../models/activitypub/actor'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { ActorFollowModel } from '../../models/activitypub/actor-follow'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { VideoModel } from '../../models/video/video'
|
|
|
|
import { VideoChannelModel } from '../../models/video/video-channel'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { VideoCommentModel } from '../../models/video/video-comment'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { VideoShareModel } from '../../models/video/video-share'
|
2018-07-24 14:35:11 +02:00
|
|
|
import { cacheRoute } from '../../middlewares/cache'
|
2018-05-25 16:21:16 +02:00
|
|
|
import { activityPubResponse } from './utils'
|
|
|
|
import { AccountVideoRateModel } from '../../models/account/account-video-rate'
|
|
|
|
import {
|
|
|
|
getVideoCommentsActivityPubUrl,
|
|
|
|
getVideoDislikesActivityPubUrl,
|
|
|
|
getVideoLikesActivityPubUrl,
|
|
|
|
getVideoSharesActivityPubUrl
|
|
|
|
} from '../../lib/activitypub'
|
2018-07-12 19:02:00 +02:00
|
|
|
import { VideoCaptionModel } from '../../models/video/video-caption'
|
2017-11-09 17:51:58 +01:00
|
|
|
|
|
|
|
const activityPubClientRouter = express.Router()
|
|
|
|
|
2018-01-08 11:30:48 +01:00
|
|
|
activityPubClientRouter.get('/accounts?/:name',
|
2017-11-27 17:30:46 +01:00
|
|
|
executeIfActivityPub(asyncMiddleware(localAccountValidator)),
|
2017-11-27 14:44:51 +01:00
|
|
|
executeIfActivityPub(accountController)
|
2017-11-09 17:51:58 +01:00
|
|
|
)
|
2018-01-15 09:46:46 +01:00
|
|
|
activityPubClientRouter.get('/accounts?/:name/followers',
|
2017-11-27 17:30:46 +01:00
|
|
|
executeIfActivityPub(asyncMiddleware(localAccountValidator)),
|
2017-11-09 17:51:58 +01:00
|
|
|
executeIfActivityPub(asyncMiddleware(accountFollowersController))
|
|
|
|
)
|
2018-01-15 09:46:46 +01:00
|
|
|
activityPubClientRouter.get('/accounts?/: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',
|
2018-07-24 14:35:11 +02:00
|
|
|
executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
|
2017-11-27 17:30:46 +01:00
|
|
|
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
|
2017-12-28 11:16:08 +01:00
|
|
|
executeIfActivityPub(asyncMiddleware(videoController))
|
2017-11-27 14:44:51 +01:00
|
|
|
)
|
2018-03-22 17:08:02 +01:00
|
|
|
activityPubClientRouter.get('/videos/watch/:id/activity',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoController))
|
|
|
|
)
|
2018-01-29 10:52:19 +01:00
|
|
|
activityPubClientRouter.get('/videos/watch/:id/announces',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
|
|
|
|
)
|
2017-11-27 14:44:51 +01:00
|
|
|
activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videosShareValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoAnnounceController))
|
2017-11-16 15:22:39 +01:00
|
|
|
)
|
2018-01-29 10:52:19 +01: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 11:16:08 +01:00
|
|
|
activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoCommentController))
|
|
|
|
)
|
2018-03-22 17:08:02 +01:00
|
|
|
activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
|
|
|
|
executeIfActivityPub(asyncMiddleware(videoCommentController))
|
|
|
|
)
|
2017-12-28 11:16:08 +01:00
|
|
|
|
2018-08-17 15:45:42 +02:00
|
|
|
activityPubClientRouter.get('/video-channels/:name',
|
|
|
|
executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
|
2017-11-16 15:22:39 +01:00
|
|
|
executeIfActivityPub(asyncMiddleware(videoChannelController))
|
|
|
|
)
|
2018-08-17 15:45:42 +02:00
|
|
|
activityPubClientRouter.get('/video-channels/:name/followers',
|
|
|
|
executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
|
2018-01-15 09:46:46 +01:00
|
|
|
executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
|
|
|
|
)
|
2018-08-17 15:45:42 +02:00
|
|
|
activityPubClientRouter.get('/video-channels/:name/following',
|
|
|
|
executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)),
|
2018-01-15 09:46:46 +01:00
|
|
|
executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
|
|
|
|
)
|
2017-11-16 15:22:39 +01:00
|
|
|
|
2017-11-09 17:51:58 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
activityPubClientRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-27 14:44:51 +01:00
|
|
|
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
|
|
|
|
2018-03-27 14:28:33 +02:00
|
|
|
return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-01-15 09:46:46 +01:00
|
|
|
const activityPubResult = await actorFollowers(req, account.Actor)
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2018-03-27 14:28:33 +02:00
|
|
|
return activityPubResponse(activityPubContextify(activityPubResult), res)
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-01-15 09:46:46 +01:00
|
|
|
const activityPubResult = await actorFollowing(req, account.Actor)
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2018-03-27 14:28:33 +02:00
|
|
|
return activityPubResponse(activityPubContextify(activityPubResult), res)
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
2017-11-16 15:22:39 +01:00
|
|
|
|
2017-12-28 11:16:08 +01:00
|
|
|
async 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
|
|
|
|
2018-07-12 19:02:00 +02:00
|
|
|
// We need captions to render AP object
|
|
|
|
video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id)
|
|
|
|
|
2018-06-12 20:04:58 +02:00
|
|
|
const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC)
|
2018-05-25 16:21:16 +02:00
|
|
|
const videoObject = audiencify(video.toActivityPubObject(), audience)
|
2018-01-10 17:18:12 +01:00
|
|
|
|
2018-03-22 17:08:02 +01:00
|
|
|
if (req.path.endsWith('/activity')) {
|
2018-06-12 20:04:58 +02:00
|
|
|
const data = createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, audience)
|
2018-03-27 14:28:33 +02:00
|
|
|
return activityPubResponse(activityPubContextify(data), res)
|
2018-03-22 17:08:02 +01:00
|
|
|
}
|
|
|
|
|
2018-03-27 14:28:33 +02:00
|
|
|
return activityPubResponse(activityPubContextify(videoObject), res)
|
2017-11-16 15:22:39 +01:00
|
|
|
}
|
|
|
|
|
2017-11-27 14:44:51 +01:00
|
|
|
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
|
2018-03-27 13:33:56 +02:00
|
|
|
const object = await buildVideoAnnounce(share.Actor, share, res.locals.video, undefined)
|
2017-11-27 14:44:51 +01:00
|
|
|
|
2018-03-27 14:28:33 +02:00
|
|
|
return activityPubResponse(activityPubContextify(object), res)
|
2017-11-27 14:44:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 10:52:19 +01:00
|
|
|
async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const video: VideoModel = res.locals.video
|
|
|
|
|
2018-05-25 16:21:16 +02:00
|
|
|
const handler = async (start: number, count: number) => {
|
|
|
|
const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
|
|
|
|
return {
|
|
|
|
total: result.count,
|
|
|
|
data: result.rows.map(r => r.url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
|
2018-01-29 10:52:19 +01:00
|
|
|
|
2018-05-25 16:21:16 +02:00
|
|
|
return activityPubResponse(activityPubContextify(json), res)
|
2018-01-29 10:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const video: VideoModel = res.locals.video
|
2018-05-25 16:21:16 +02:00
|
|
|
const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
|
2018-01-29 10:52:19 +01:00
|
|
|
|
2018-05-25 16:21:16 +02:00
|
|
|
return activityPubResponse(activityPubContextify(json), res)
|
2018-01-29 10:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const video: VideoModel = res.locals.video
|
2018-05-25 16:21:16 +02:00
|
|
|
const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
|
2018-01-29 10:52:19 +01:00
|
|
|
|
2018-05-25 16:21:16 +02:00
|
|
|
return activityPubResponse(activityPubContextify(json), res)
|
2018-01-29 10:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const video: VideoModel = res.locals.video
|
|
|
|
|
2018-05-25 16:21:16 +02:00
|
|
|
const handler = async (start: number, count: number) => {
|
|
|
|
const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
|
|
|
|
return {
|
|
|
|
total: result.count,
|
|
|
|
data: result.rows.map(r => r.url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
|
2018-01-29 10:52:19 +01:00
|
|
|
|
2018-05-25 16:21:16 +02:00
|
|
|
return activityPubResponse(activityPubContextify(json), res)
|
2018-01-29 10:52:19 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-03-27 14:28:33 +02:00
|
|
|
return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
|
2017-11-16 15:22:39 +01:00
|
|
|
}
|
2017-12-28 11:16:08 +01:00
|
|
|
|
2018-01-15 09:46:46 +01: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 14:28:33 +02:00
|
|
|
return activityPubResponse(activityPubContextify(activityPubResult), res)
|
2018-01-15 09:46:46 +01: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 14:28:33 +02:00
|
|
|
return activityPubResponse(activityPubContextify(activityPubResult), res)
|
2018-01-15 09:46:46 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 11:16:08 +01:00
|
|
|
async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const videoComment: VideoCommentModel = res.locals.videoComment
|
|
|
|
|
2018-01-05 11:19:25 +01:00
|
|
|
const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
|
2018-01-26 11:44:08 +01:00
|
|
|
const isPublic = true // Comments are always public
|
2018-06-12 20:04:58 +02:00
|
|
|
const audience = getAudience(videoComment.Account.Actor, isPublic)
|
2018-01-26 11:44:08 +01:00
|
|
|
|
|
|
|
const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
|
|
|
|
|
2018-03-22 17:08:02 +01:00
|
|
|
if (req.path.endsWith('/activity')) {
|
2018-06-12 20:04:58 +02:00
|
|
|
const data = createActivityData(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
|
2018-03-27 14:28:33 +02:00
|
|
|
return activityPubResponse(activityPubContextify(data), res)
|
2018-03-22 17:08:02 +01:00
|
|
|
}
|
|
|
|
|
2018-03-27 14:28:33 +02:00
|
|
|
return activityPubResponse(activityPubContextify(videoCommentObject), res)
|
2017-12-28 11:16:08 +01:00
|
|
|
}
|
2018-01-15 09:46:46 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function actorFollowing (req: express.Request, actor: ActorModel) {
|
2018-05-25 16:21:16 +02:00
|
|
|
const handler = (start: number, count: number) => {
|
|
|
|
return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
|
|
|
|
}
|
2018-01-15 09:46:46 +01:00
|
|
|
|
2018-05-25 16:21:16 +02:00
|
|
|
return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page)
|
2018-01-15 09:46:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function actorFollowers (req: express.Request, actor: ActorModel) {
|
2018-05-25 16:21:16 +02:00
|
|
|
const handler = (start: number, count: number) => {
|
|
|
|
return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
|
|
|
|
}
|
2018-01-15 09:46:46 +01:00
|
|
|
|
2018-05-25 16:21:16 +02:00
|
|
|
return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page)
|
2018-01-15 09:46:46 +01:00
|
|
|
}
|
2018-03-27 14:28:33 +02:00
|
|
|
|
2018-05-25 16:21:16 +02:00
|
|
|
function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) {
|
|
|
|
const handler = async (start: number, count: number) => {
|
|
|
|
const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
|
|
|
|
return {
|
|
|
|
total: result.count,
|
|
|
|
data: result.rows.map(r => r.Account.Actor.url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return activityPubCollectionPagination(url, handler, req.query.page)
|
2018-03-27 14:28:33 +02:00
|
|
|
}
|