From 1a8dd4da77468068d1ff7f7bd67f76399ae04e04 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 30 Nov 2018 15:06:06 +0100 Subject: [PATCH] Fix AP redirection --- server/controllers/activitypub/client.ts | 28 +++++++-------- server/tests/api/activitypub/client.ts | 44 ++++++++++++++++++++---- server/tests/utils/server/activitypub.ts | 5 ++- 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index d9d385460..1a4e28dc8 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts @@ -162,10 +162,10 @@ function getAccountVideoRate (rateType: VideoRateType) { } } -async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { +async function videoController (req: express.Request, res: express.Response) { const video: VideoModel = res.locals.video - if (video.isOwned() === false) return res.redirect(video.url) + if (video.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(video.url) // We need captions to render AP object video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id) @@ -181,17 +181,17 @@ async function videoController (req: express.Request, res: express.Response, nex return activityPubResponse(activityPubContextify(videoObject), res) } -async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { +async function videoAnnounceController (req: express.Request, res: express.Response) { const share = res.locals.videoShare as VideoShareModel - if (share.Actor.isOwned() === false) return res.redirect(share.url) + if (share.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(share.url) const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined) return activityPubResponse(activityPubContextify(activity), res) } -async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) { +async function videoAnnouncesController (req: express.Request, res: express.Response) { const video: VideoModel = res.locals.video const handler = async (start: number, count: number) => { @@ -206,21 +206,21 @@ async function videoAnnouncesController (req: express.Request, res: express.Resp return activityPubResponse(activityPubContextify(json), res) } -async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) { +async function videoLikesController (req: express.Request, res: express.Response) { const video: VideoModel = res.locals.video const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video)) return activityPubResponse(activityPubContextify(json), res) } -async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) { +async function videoDislikesController (req: express.Request, res: express.Response) { const video: VideoModel = res.locals.video const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video)) return activityPubResponse(activityPubContextify(json), res) } -async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) { +async function videoCommentsController (req: express.Request, res: express.Response) { const video: VideoModel = res.locals.video const handler = async (start: number, count: number) => { @@ -235,30 +235,30 @@ async function videoCommentsController (req: express.Request, res: express.Respo return activityPubResponse(activityPubContextify(json), res) } -async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { +async function videoChannelController (req: express.Request, res: express.Response) { const videoChannel: VideoChannelModel = res.locals.videoChannel return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res) } -async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { +async function videoChannelFollowersController (req: express.Request, res: express.Response) { const videoChannel: VideoChannelModel = res.locals.videoChannel const activityPubResult = await actorFollowers(req, videoChannel.Actor) return activityPubResponse(activityPubContextify(activityPubResult), res) } -async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { +async function videoChannelFollowingController (req: express.Request, res: express.Response) { const videoChannel: VideoChannelModel = res.locals.videoChannel const activityPubResult = await actorFollowing(req, videoChannel.Actor) return activityPubResponse(activityPubContextify(activityPubResult), res) } -async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) { +async function videoCommentController (req: express.Request, res: express.Response) { const videoComment: VideoCommentModel = res.locals.videoComment - if (videoComment.isOwned() === false) return res.redirect(videoComment.url) + if (videoComment.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoComment.url) const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) const isPublic = true // Comments are always public @@ -276,7 +276,7 @@ async function videoCommentController (req: express.Request, res: express.Respon async function videoRedundancyController (req: express.Request, res: express.Response) { const videoRedundancy: VideoRedundancyModel = res.locals.videoRedundancy - if (videoRedundancy.isOwned() === false) return res.redirect(videoRedundancy.url) + if (videoRedundancy.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url) const serverActor = await getServerActor() diff --git a/server/tests/api/activitypub/client.ts b/server/tests/api/activitypub/client.ts index 5ca8bdfd3..ea0682634 100644 --- a/server/tests/api/activitypub/client.ts +++ b/server/tests/api/activitypub/client.ts @@ -2,25 +2,42 @@ import * as chai from 'chai' import 'mocha' -import { flushTests, killallServers, makeActivityPubGetRequest, runServer, ServerInfo, setAccessTokensToServers } from '../../utils' +import { + doubleFollow, + flushAndRunMultipleServers, + flushTests, + killallServers, + makeActivityPubGetRequest, + runServer, + ServerInfo, + setAccessTokensToServers, uploadVideo +} from '../../utils' const expect = chai.expect describe('Test activitypub', function () { - let server: ServerInfo = null + let servers: ServerInfo[] = [] + let videoUUID: string before(async function () { this.timeout(30000) await flushTests() - server = await runServer(1) + servers = await flushAndRunMultipleServers(2) - await setAccessTokensToServers([ server ]) + await setAccessTokensToServers(servers) + + { + const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' }) + videoUUID = res.body.video.uuid + } + + await doubleFollow(servers[0], servers[1]) }) it('Should return the account object', async function () { - const res = await makeActivityPubGetRequest(server.url, '/accounts/root') + const res = await makeActivityPubGetRequest(servers[0].url, '/accounts/root') const object = res.body expect(object.type).to.equal('Person') @@ -29,7 +46,22 @@ describe('Test activitypub', function () { expect(object.preferredUsername).to.equal('root') }) + it('Should return the video object', async function () { + const res = await makeActivityPubGetRequest(servers[0].url, '/videos/watch/' + videoUUID) + const object = res.body + + expect(object.type).to.equal('Video') + expect(object.id).to.equal('http://localhost:9001/videos/watch/' + videoUUID) + expect(object.name).to.equal('video') + }) + + it('Should redirect to the origin video object', async function () { + const res = await makeActivityPubGetRequest(servers[1].url, '/videos/watch/' + videoUUID, 302) + + expect(res.header.location).to.equal('http://localhost:9001/videos/watch/' + videoUUID) + }) + after(async function () { - killallServers([ server ]) + killallServers(servers) }) }) diff --git a/server/tests/utils/server/activitypub.ts b/server/tests/utils/server/activitypub.ts index cf3c1c3b3..eccb198ca 100644 --- a/server/tests/utils/server/activitypub.ts +++ b/server/tests/utils/server/activitypub.ts @@ -1,11 +1,10 @@ import * as request from 'supertest' -function makeActivityPubGetRequest (url: string, path: string) { +function makeActivityPubGetRequest (url: string, path: string, expectedStatus = 200) { return request(url) .get(path) .set('Accept', 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8') - .expect(200) - .expect('Content-Type', /json/) + .expect(expectedStatus) } // ---------------------------------------------------------------------------