X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Factivitypub%2Fclient.ts;h=e44f1c6ab8440ac742c4bb768bcc8be4a2bd1660;hb=610d0be13b3d01f653ef269271dd667a57c85ef2;hp=d36d10de1b3d33bc63bec0bc25e12bf2f57be458;hpb=97567dd81f508dd6295ac4d73d849aa2ce0a6549;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index d36d10de1..e44f1c6ab 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts @@ -1,5 +1,5 @@ -// Intercept ActivityPub client requests import * as express from 'express' +import * as cors from 'cors' import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants' @@ -14,9 +14,8 @@ import { videosCustomGetValidator, videosShareValidator } from '../../middlewares' -import { getAccountVideoRateValidator, videoCommentGetValidator } from '../../middlewares/validators' +import { getAccountVideoRateValidatorFactory, videoCommentGetValidator } from '../../middlewares/validators' import { AccountModel } from '../../models/account/account' -import { ActorModel } from '../../models/activitypub/actor' import { ActorFollowModel } from '../../models/activitypub/actor-follow' import { VideoModel } from '../../models/video/video' import { VideoCommentModel } from '../../models/video/video-comment' @@ -25,21 +24,25 @@ import { cacheRoute } from '../../middlewares/cache' import { activityPubResponse } from './utils' import { AccountVideoRateModel } from '../../models/account/account-video-rate' import { - getRateUrl, getVideoCommentsActivityPubUrl, getVideoDislikesActivityPubUrl, getVideoLikesActivityPubUrl, getVideoSharesActivityPubUrl -} from '../../lib/activitypub' +} from '../../lib/activitypub/url' import { VideoCaptionModel } from '../../models/video/video-caption' import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy' -import { getServerActor } from '../../helpers/utils' import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike' import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists' import { VideoPlaylistModel } from '../../models/video/video-playlist' import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' +import { MAccountId, MActorId, MVideoAPWithoutCaption, MVideoId } from '@server/typings/models' +import { getServerActor } from '@server/models/application/application' +import { getRateUrl } from '@server/lib/activitypub/video-rates' const activityPubClientRouter = express.Router() +activityPubClientRouter.use(cors()) + +// Intercept ActivityPub client requests activityPubClientRouter.get('/accounts?/:name', executeIfActivityPub, @@ -63,18 +66,18 @@ activityPubClientRouter.get('/accounts?/:name/playlists', ) activityPubClientRouter.get('/accounts?/:name/likes/:videoId', executeIfActivityPub, - asyncMiddleware(getAccountVideoRateValidator('like')), - getAccountVideoRate('like') + asyncMiddleware(getAccountVideoRateValidatorFactory('like')), + getAccountVideoRateFactory('like') ) activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId', executeIfActivityPub, - asyncMiddleware(getAccountVideoRateValidator('dislike')), - getAccountVideoRate('dislike') + asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')), + getAccountVideoRateFactory('dislike') ) activityPubClientRouter.get('/videos/watch/:id', executeIfActivityPub, - asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)), + asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)), asyncMiddleware(videosCustomGetValidator('only-video-with-rights')), asyncMiddleware(videoController) ) @@ -85,7 +88,7 @@ activityPubClientRouter.get('/videos/watch/:id/activity', ) activityPubClientRouter.get('/videos/watch/:id/announces', executeIfActivityPub, - asyncMiddleware(videosCustomGetValidator('only-video')), + asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), asyncMiddleware(videoAnnouncesController) ) activityPubClientRouter.get('/videos/watch/:id/announces/:actorId', @@ -95,17 +98,17 @@ activityPubClientRouter.get('/videos/watch/:id/announces/:actorId', ) activityPubClientRouter.get('/videos/watch/:id/likes', executeIfActivityPub, - asyncMiddleware(videosCustomGetValidator('only-video')), + asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), asyncMiddleware(videoLikesController) ) activityPubClientRouter.get('/videos/watch/:id/dislikes', executeIfActivityPub, - asyncMiddleware(videosCustomGetValidator('only-video')), + asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), asyncMiddleware(videoDislikesController) ) activityPubClientRouter.get('/videos/watch/:id/comments', executeIfActivityPub, - asyncMiddleware(videosCustomGetValidator('only-video')), + asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), asyncMiddleware(videoCommentsController) ) activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', @@ -122,7 +125,7 @@ activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity activityPubClientRouter.get('/video-channels/:name', executeIfActivityPub, asyncMiddleware(localVideoChannelValidator), - asyncMiddleware(videoChannelController) + videoChannelController ) activityPubClientRouter.get('/video-channels/:name/followers', executeIfActivityPub, @@ -148,13 +151,13 @@ activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistT activityPubClientRouter.get('/video-playlists/:playlistId', executeIfActivityPub, - asyncMiddleware(videoPlaylistsGetValidator), + asyncMiddleware(videoPlaylistsGetValidator('all')), asyncMiddleware(videoPlaylistController) ) activityPubClientRouter.get('/video-playlists/:playlistId/:videoId', executeIfActivityPub, asyncMiddleware(videoPlaylistElementAPGetValidator), - asyncMiddleware(videoPlaylistElementController) + videoPlaylistElementController ) // --------------------------------------------------------------------------- @@ -192,7 +195,7 @@ async function accountPlaylistsController (req: express.Request, res: express.Re return activityPubResponse(activityPubContextify(activityPubResult), res) } -function getAccountVideoRate (rateType: VideoRateType) { +function getAccountVideoRateFactory (rateType: VideoRateType) { return (req: express.Request, res: express.Response) => { const accountVideoRate = res.locals.accountVideoRate @@ -208,18 +211,19 @@ function getAccountVideoRate (rateType: VideoRateType) { async function videoController (req: express.Request, res: express.Response) { // We need more attributes - const video = await VideoModel.loadForGetAPI(res.locals.video.id) + const video = await VideoModel.loadForGetAPI({ id: res.locals.onlyVideoWithRights.id }) as MVideoAPWithoutCaption if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url) // We need captions to render AP object - video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id) + const captions = await VideoCaptionModel.listVideoCaptions(video.id) + const videoWithCaptions = Object.assign(video, { VideoCaptions: captions }) - const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC) - const videoObject = audiencify(video.toActivityPubObject(), audience) + const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC) + const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience) if (req.path.endsWith('/activity')) { - const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience) + const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience) return activityPubResponse(activityPubContextify(data), res) } @@ -231,13 +235,13 @@ async function videoAnnounceController (req: express.Request, res: express.Respo if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url) - const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined) + const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined) - return activityPubResponse(activityPubContextify(activity), res) + return activityPubResponse(activityPubContextify(activity, 'Announce'), res) } async function videoAnnouncesController (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.onlyImmutableVideo const handler = async (start: number, count: number) => { const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count) @@ -252,21 +256,21 @@ async function videoAnnouncesController (req: express.Request, res: express.Resp } async function videoLikesController (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.onlyImmutableVideo const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video)) return activityPubResponse(activityPubContextify(json), res) } async function videoDislikesController (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.onlyImmutableVideo const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video)) return activityPubResponse(activityPubContextify(json), res) } async function videoCommentsController (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.onlyImmutableVideo const handler = async (start: number, count: number) => { const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count) @@ -280,7 +284,7 @@ async function videoCommentsController (req: express.Request, res: express.Respo return activityPubResponse(activityPubContextify(json), res) } -async function videoChannelController (req: express.Request, res: express.Response) { +function videoChannelController (req: express.Request, res: express.Response) { const videoChannel = res.locals.videoChannel return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res) @@ -301,19 +305,22 @@ async function videoChannelFollowingController (req: express.Request, res: expre } async function videoCommentController (req: express.Request, res: express.Response) { - const videoComment = res.locals.videoComment + const videoComment = res.locals.videoCommentFull if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url) const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) const isPublic = true // Comments are always public - const audience = getAudience(videoComment.Account.Actor, isPublic) + let videoCommentObject = videoComment.toActivityPubObject(threadParentComments) - const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience) + 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, audience) - return activityPubResponse(activityPubContextify(data), res) + if (req.path.endsWith('/activity')) { + const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience) + return activityPubResponse(activityPubContextify(data), res) + } } return activityPubResponse(activityPubContextify(videoCommentObject), res) @@ -330,14 +337,14 @@ async function videoRedundancyController (req: express.Request, res: express.Res if (req.path.endsWith('/activity')) { const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience) - return activityPubResponse(activityPubContextify(data), res) + return activityPubResponse(activityPubContextify(data, 'CacheFile'), res) } - return activityPubResponse(activityPubContextify(object), res) + return activityPubResponse(activityPubContextify(object, 'CacheFile'), res) } async function videoPlaylistController (req: express.Request, res: express.Response) { - const playlist = res.locals.videoPlaylist + const playlist = res.locals.videoPlaylistFull // We need more attributes playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId) @@ -349,8 +356,8 @@ async function videoPlaylistController (req: express.Request, res: express.Respo return activityPubResponse(activityPubContextify(object), res) } -async function videoPlaylistElementController (req: express.Request, res: express.Response) { - const videoPlaylistElement = res.locals.videoPlaylistElement +function videoPlaylistElementController (req: express.Request, res: express.Response) { + const videoPlaylistElement = res.locals.videoPlaylistElementAP const json = videoPlaylistElement.toActivityPubObject() return activityPubResponse(activityPubContextify(json), res) @@ -358,7 +365,7 @@ async function videoPlaylistElementController (req: express.Request, res: expres // --------------------------------------------------------------------------- -async function actorFollowing (req: express.Request, actor: ActorModel) { +async function actorFollowing (req: express.Request, actor: MActorId) { const handler = (start: number, count: number) => { return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) } @@ -366,7 +373,7 @@ async function actorFollowing (req: express.Request, actor: ActorModel) { return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) } -async function actorFollowers (req: express.Request, actor: ActorModel) { +async function actorFollowers (req: express.Request, actor: MActorId) { const handler = (start: number, count: number) => { return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count) } @@ -374,7 +381,7 @@ async function actorFollowers (req: express.Request, actor: ActorModel) { return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) } -async function actorPlaylists (req: express.Request, account: AccountModel) { +async function actorPlaylists (req: express.Request, account: MAccountId) { const handler = (start: number, count: number) => { return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count) } @@ -382,7 +389,7 @@ async function actorPlaylists (req: express.Request, account: AccountModel) { return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) } -function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) { +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 {