123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- import cors from 'cors'
- import express from 'express'
- import {
- VideoChapterObject,
- VideoChaptersObject,
- VideoCommentObject,
- VideoPlaylistPrivacy,
- VideoPrivacy,
- VideoRateType
- } from '@peertube/peertube-models'
- import { activityPubCollectionPagination } from '@server/lib/activitypub/collection.js'
- import { getContextFilter } from '@server/lib/activitypub/context.js'
- import { getServerActor } from '@server/models/application/application.js'
- import { MAccountId, MActorId, MChannelId, MVideoId } from '@server/types/models/index.js'
- import { activityPubContextify } from '../../helpers/activity-pub-utils.js'
- import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants.js'
- import { audiencify, getAudience } from '../../lib/activitypub/audience.js'
- import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send/index.js'
- import { buildCreateActivity } from '../../lib/activitypub/send/send-create.js'
- import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike.js'
- import {
- getLocalVideoChaptersActivityPubUrl,
- getLocalVideoCommentsActivityPubUrl,
- getLocalVideoDislikesActivityPubUrl,
- getLocalVideoLikesActivityPubUrl,
- getLocalVideoSharesActivityPubUrl
- } from '../../lib/activitypub/url.js'
- import {
- apVideoChaptersSetCacheKey,
- buildAPVideoChaptersGroupsCache,
- cacheRoute,
- cacheRouteFactory
- } from '../../middlewares/cache/cache.js'
- import {
- activityPubRateLimiter,
- asyncMiddleware,
- ensureIsLocalChannel,
- executeIfActivityPub,
- localAccountValidator,
- videoChannelsNameWithHostValidator,
- videosCustomGetValidator,
- videosShareValidator
- } from '../../middlewares/index.js'
- import {
- getAccountVideoRateValidatorFactory,
- getVideoLocalViewerValidator,
- videoCommentGetValidator
- } from '../../middlewares/validators/index.js'
- import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy.js'
- import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists.js'
- import { AccountVideoRateModel } from '../../models/account/account-video-rate.js'
- import { AccountModel } from '../../models/account/account.js'
- import { ActorFollowModel } from '../../models/actor/actor-follow.js'
- import { VideoCommentModel } from '../../models/video/video-comment.js'
- import { VideoPlaylistModel } from '../../models/video/video-playlist.js'
- import { VideoShareModel } from '../../models/video/video-share.js'
- import { activityPubResponse } from './utils.js'
- import { VideoChapterModel } from '@server/models/video/video-chapter.js'
- import { InternalEventEmitter } from '@server/lib/internal-event-emitter.js'
- const activityPubClientRouter = express.Router()
- activityPubClientRouter.use(cors())
- // Intercept ActivityPub client requests
- activityPubClientRouter.get(
- [ '/accounts?/:name', '/accounts?/:name/video-channels', '/a/:name', '/a/:name/video-channels' ],
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(localAccountValidator),
- asyncMiddleware(accountController)
- )
- activityPubClientRouter.get('/accounts?/:name/followers',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(localAccountValidator),
- asyncMiddleware(accountFollowersController)
- )
- activityPubClientRouter.get('/accounts?/:name/following',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(localAccountValidator),
- asyncMiddleware(accountFollowingController)
- )
- activityPubClientRouter.get('/accounts?/:name/playlists',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(localAccountValidator),
- asyncMiddleware(accountPlaylistsController)
- )
- activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
- executeIfActivityPub,
- activityPubRateLimiter,
- cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
- asyncMiddleware(getAccountVideoRateValidatorFactory('like')),
- asyncMiddleware(getAccountVideoRateFactory('like'))
- )
- activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
- executeIfActivityPub,
- activityPubRateLimiter,
- cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
- asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')),
- asyncMiddleware(getAccountVideoRateFactory('dislike'))
- )
- activityPubClientRouter.get(
- [ '/videos/watch/:id', '/w/:id' ],
- executeIfActivityPub,
- activityPubRateLimiter,
- cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
- asyncMiddleware(videosCustomGetValidator('all')),
- asyncMiddleware(videoController)
- )
- activityPubClientRouter.get('/videos/watch/:id/activity',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videosCustomGetValidator('all')),
- asyncMiddleware(videoController)
- )
- activityPubClientRouter.get('/videos/watch/:id/announces',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
- asyncMiddleware(videoAnnouncesController)
- )
- activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videosShareValidator),
- asyncMiddleware(videoAnnounceController)
- )
- activityPubClientRouter.get('/videos/watch/:id/likes',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
- asyncMiddleware(videoLikesController)
- )
- activityPubClientRouter.get('/videos/watch/:id/dislikes',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
- asyncMiddleware(videoDislikesController)
- )
- activityPubClientRouter.get('/videos/watch/:id/comments',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
- asyncMiddleware(videoCommentsController)
- )
- activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videoCommentGetValidator),
- asyncMiddleware(videoCommentController)
- )
- activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videoCommentGetValidator),
- asyncMiddleware(videoCommentController)
- )
- // ---------------------------------------------------------------------------
- const { middleware: chaptersCacheRouteMiddleware, instance: chaptersApiCache } = cacheRouteFactory()
- InternalEventEmitter.Instance.on('chapters-updated', ({ video }) => {
- if (video.remote) return
- chaptersApiCache.clearGroupSafe(buildAPVideoChaptersGroupsCache({ videoId: video.uuid }))
- })
- activityPubClientRouter.get('/videos/watch/:id/chapters',
- executeIfActivityPub,
- activityPubRateLimiter,
- apVideoChaptersSetCacheKey,
- chaptersCacheRouteMiddleware(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
- asyncMiddleware(videosCustomGetValidator('only-video')),
- asyncMiddleware(videoChaptersController)
- )
- // ---------------------------------------------------------------------------
- activityPubClientRouter.get(
- [ '/video-channels/:nameWithHost', '/video-channels/:nameWithHost/videos', '/c/:nameWithHost', '/c/:nameWithHost/videos' ],
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videoChannelsNameWithHostValidator),
- ensureIsLocalChannel,
- asyncMiddleware(videoChannelController)
- )
- activityPubClientRouter.get('/video-channels/:nameWithHost/followers',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videoChannelsNameWithHostValidator),
- ensureIsLocalChannel,
- asyncMiddleware(videoChannelFollowersController)
- )
- activityPubClientRouter.get('/video-channels/:nameWithHost/following',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videoChannelsNameWithHostValidator),
- ensureIsLocalChannel,
- asyncMiddleware(videoChannelFollowingController)
- )
- activityPubClientRouter.get('/video-channels/:nameWithHost/playlists',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videoChannelsNameWithHostValidator),
- ensureIsLocalChannel,
- asyncMiddleware(videoChannelPlaylistsController)
- )
- activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videoFileRedundancyGetValidator),
- asyncMiddleware(videoRedundancyController)
- )
- activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videoPlaylistRedundancyGetValidator),
- asyncMiddleware(videoRedundancyController)
- )
- activityPubClientRouter.get(
- [ '/video-playlists/:playlistId', '/videos/watch/playlist/:playlistId', '/w/p/:playlistId' ],
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videoPlaylistsGetValidator('all')),
- asyncMiddleware(videoPlaylistController)
- )
- activityPubClientRouter.get('/video-playlists/:playlistId/videos/:playlistElementId',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(videoPlaylistElementAPGetValidator),
- asyncMiddleware(videoPlaylistElementController)
- )
- activityPubClientRouter.get('/videos/local-viewer/:localViewerId',
- executeIfActivityPub,
- activityPubRateLimiter,
- asyncMiddleware(getVideoLocalViewerValidator),
- asyncMiddleware(getVideoLocalViewerController)
- )
- // ---------------------------------------------------------------------------
- export {
- activityPubClientRouter
- }
- // ---------------------------------------------------------------------------
- async function accountController (req: express.Request, res: express.Response) {
- const account = res.locals.account
- return activityPubResponse(activityPubContextify(await account.toActivityPubObject(), 'Actor', getContextFilter()), res)
- }
- async function accountFollowersController (req: express.Request, res: express.Response) {
- const account = res.locals.account
- const activityPubResult = await actorFollowers(req, account.Actor)
- return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
- }
- async function accountFollowingController (req: express.Request, res: express.Response) {
- const account = res.locals.account
- const activityPubResult = await actorFollowing(req, account.Actor)
- return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
- }
- async function accountPlaylistsController (req: express.Request, res: express.Response) {
- const account = res.locals.account
- const activityPubResult = await actorPlaylists(req, { account })
- return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
- }
- async function videoChannelPlaylistsController (req: express.Request, res: express.Response) {
- const channel = res.locals.videoChannel
- const activityPubResult = await actorPlaylists(req, { channel })
- return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
- }
- function getAccountVideoRateFactory (rateType: VideoRateType) {
- return (req: express.Request, res: express.Response) => {
- const accountVideoRate = res.locals.accountVideoRate
- const byActor = accountVideoRate.Account.Actor
- const APObject = rateType === 'like'
- ? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
- : buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
- return activityPubResponse(activityPubContextify(APObject, 'Rate', getContextFilter()), res)
- }
- }
- async function videoController (req: express.Request, res: express.Response) {
- const video = res.locals.videoAll
- if (redirectIfNotOwned(video.url, res)) return
- // We need captions to render AP object
- const videoAP = await video.lightAPToFullAP(undefined)
- const audience = getAudience(videoAP.VideoChannel.Account.Actor, videoAP.privacy === VideoPrivacy.PUBLIC)
- const videoObject = audiencify(await videoAP.toActivityPubObject(), audience)
- if (req.path.endsWith('/activity')) {
- const data = buildCreateActivity(videoAP.url, video.VideoChannel.Account.Actor, videoObject, audience)
- return activityPubResponse(activityPubContextify(data, 'Video', getContextFilter()), res)
- }
- return activityPubResponse(activityPubContextify(videoObject, 'Video', getContextFilter()), res)
- }
- async function videoAnnounceController (req: express.Request, res: express.Response) {
- const share = res.locals.videoShare
- if (redirectIfNotOwned(share.url, res)) return
- const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
- return activityPubResponse(activityPubContextify(activity, 'Announce', getContextFilter()), res)
- }
- async function videoAnnouncesController (req: express.Request, res: express.Response) {
- const video = res.locals.onlyImmutableVideo
- if (redirectIfNotOwned(video.url, res)) return
- const handler = async (start: number, count: number) => {
- const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
- return {
- total: result.total,
- data: result.data.map(r => r.url)
- }
- }
- const json = await activityPubCollectionPagination(getLocalVideoSharesActivityPubUrl(video), handler, req.query.page)
- return activityPubResponse(activityPubContextify(json, 'Collection', getContextFilter()), res)
- }
- async function videoLikesController (req: express.Request, res: express.Response) {
- const video = res.locals.onlyImmutableVideo
- if (redirectIfNotOwned(video.url, res)) return
- const json = await videoRates(req, 'like', video, getLocalVideoLikesActivityPubUrl(video))
- return activityPubResponse(activityPubContextify(json, 'Collection', getContextFilter()), res)
- }
- async function videoDislikesController (req: express.Request, res: express.Response) {
- const video = res.locals.onlyImmutableVideo
- if (redirectIfNotOwned(video.url, res)) return
- const json = await videoRates(req, 'dislike', video, getLocalVideoDislikesActivityPubUrl(video))
- return activityPubResponse(activityPubContextify(json, 'Collection', getContextFilter()), res)
- }
- async function videoCommentsController (req: express.Request, res: express.Response) {
- const video = res.locals.onlyImmutableVideo
- if (redirectIfNotOwned(video.url, res)) return
- const handler = async (start: number, count: number) => {
- const result = await VideoCommentModel.listAndCountByVideoForAP({ video, start, count })
- return {
- total: result.total,
- data: result.data.map(r => r.url)
- }
- }
- const json = await activityPubCollectionPagination(getLocalVideoCommentsActivityPubUrl(video), handler, req.query.page)
- return activityPubResponse(activityPubContextify(json, 'Collection', getContextFilter()), res)
- }
- async function videoChannelController (req: express.Request, res: express.Response) {
- const videoChannel = res.locals.videoChannel
- return activityPubResponse(activityPubContextify(await videoChannel.toActivityPubObject(), 'Actor', getContextFilter()), res)
- }
- async function videoChannelFollowersController (req: express.Request, res: express.Response) {
- const videoChannel = res.locals.videoChannel
- const activityPubResult = await actorFollowers(req, videoChannel.Actor)
- return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
- }
- async function videoChannelFollowingController (req: express.Request, res: express.Response) {
- const videoChannel = res.locals.videoChannel
- const activityPubResult = await actorFollowing(req, videoChannel.Actor)
- return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
- }
- async function videoCommentController (req: express.Request, res: express.Response) {
- const videoComment = res.locals.videoCommentFull
- if (redirectIfNotOwned(videoComment.url, res)) return
- const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
- const isPublic = true // Comments are always public
- let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
- if (videoComment.Account) {
- const audience = getAudience(videoComment.Account.Actor, isPublic)
- videoCommentObject = audiencify(videoCommentObject, audience)
- if (req.path.endsWith('/activity')) {
- const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject as VideoCommentObject, audience)
- return activityPubResponse(activityPubContextify(data, 'Comment', getContextFilter()), res)
- }
- }
- return activityPubResponse(activityPubContextify(videoCommentObject, 'Comment', getContextFilter()), res)
- }
- async function videoChaptersController (req: express.Request, res: express.Response) {
- const video = res.locals.onlyVideo
- if (redirectIfNotOwned(video.url, res)) return
- const chapters = await VideoChapterModel.listChaptersOfVideo(video.id)
- const hasPart: VideoChapterObject[] = []
- if (chapters.length !== 0) {
- for (let i = 0; i < chapters.length - 1; i++) {
- hasPart.push(chapters[i].toActivityPubJSON({ video, nextChapter: chapters[i + 1] }))
- }
- hasPart.push(chapters[chapters.length - 1].toActivityPubJSON({ video: res.locals.onlyVideo, nextChapter: null }))
- }
- const chaptersObject: VideoChaptersObject = {
- id: getLocalVideoChaptersActivityPubUrl(video),
- hasPart
- }
- return activityPubResponse(activityPubContextify(chaptersObject, 'Chapters', getContextFilter()), res)
- }
- async function videoRedundancyController (req: express.Request, res: express.Response) {
- const videoRedundancy = res.locals.videoRedundancy
- if (redirectIfNotOwned(videoRedundancy.url, res)) return
- const serverActor = await getServerActor()
- const audience = getAudience(serverActor)
- const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
- if (req.path.endsWith('/activity')) {
- const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
- return activityPubResponse(activityPubContextify(data, 'CacheFile', getContextFilter()), res)
- }
- return activityPubResponse(activityPubContextify(object, 'CacheFile', getContextFilter()), res)
- }
- async function videoPlaylistController (req: express.Request, res: express.Response) {
- const playlist = res.locals.videoPlaylistFull
- if (redirectIfNotOwned(playlist.url, res)) return
- // We need more attributes
- playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
- const json = await playlist.toActivityPubObject(req.query.page, null)
- const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
- const object = audiencify(json, audience)
- return activityPubResponse(activityPubContextify(object, 'Playlist', getContextFilter()), res)
- }
- function videoPlaylistElementController (req: express.Request, res: express.Response) {
- const videoPlaylistElement = res.locals.videoPlaylistElementAP
- if (redirectIfNotOwned(videoPlaylistElement.url, res)) return
- const json = videoPlaylistElement.toActivityPubObject()
- return activityPubResponse(activityPubContextify(json, 'Playlist', getContextFilter()), res)
- }
- function getVideoLocalViewerController (req: express.Request, res: express.Response) {
- const localViewer = res.locals.localViewerFull
- return activityPubResponse(activityPubContextify(localViewer.toActivityPubObject(), 'WatchAction', getContextFilter()), res)
- }
- // ---------------------------------------------------------------------------
- function actorFollowing (req: express.Request, actor: MActorId) {
- const handler = (start: number, count: number) => {
- return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
- }
- return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
- }
- function actorFollowers (req: express.Request, actor: MActorId) {
- const handler = (start: number, count: number) => {
- return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
- }
- return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
- }
- function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) {
- const handler = (start: number, count: number) => {
- return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count)
- }
- return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
- }
- function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
- const handler = async (start: number, count: number) => {
- const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
- return {
- total: result.total,
- data: result.data.map(r => r.url)
- }
- }
- return activityPubCollectionPagination(url, handler, req.query.page)
- }
- function redirectIfNotOwned (url: string, res: express.Response) {
- if (url.startsWith(WEBSERVER.URL) === false) {
- res.redirect(url)
- return true
- }
- return false
- }
|