aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-11-30 15:06:06 +0100
committerChocobozzz <me@florianbigard.com>2018-11-30 15:06:36 +0100
commit1a8dd4da77468068d1ff7f7bd67f76399ae04e04 (patch)
tree430e92852055117af2a274ce7c6a92ee9f037f0a
parentd7ea359d366f0caaf1efff2b727dd736c1301ddf (diff)
downloadPeerTube-1a8dd4da77468068d1ff7f7bd67f76399ae04e04.tar.gz
PeerTube-1a8dd4da77468068d1ff7f7bd67f76399ae04e04.tar.zst
PeerTube-1a8dd4da77468068d1ff7f7bd67f76399ae04e04.zip
Fix AP redirection
-rw-r--r--server/controllers/activitypub/client.ts28
-rw-r--r--server/tests/api/activitypub/client.ts44
-rw-r--r--server/tests/utils/server/activitypub.ts5
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) {
162 } 162 }
163} 163}
164 164
165async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { 165async function videoController (req: express.Request, res: express.Response) {
166 const video: VideoModel = res.locals.video 166 const video: VideoModel = res.locals.video
167 167
168 if (video.isOwned() === false) return res.redirect(video.url) 168 if (video.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(video.url)
169 169
170 // We need captions to render AP object 170 // We need captions to render AP object
171 video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id) 171 video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id)
@@ -181,17 +181,17 @@ async function videoController (req: express.Request, res: express.Response, nex
181 return activityPubResponse(activityPubContextify(videoObject), res) 181 return activityPubResponse(activityPubContextify(videoObject), res)
182} 182}
183 183
184async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { 184async function videoAnnounceController (req: express.Request, res: express.Response) {
185 const share = res.locals.videoShare as VideoShareModel 185 const share = res.locals.videoShare as VideoShareModel
186 186
187 if (share.Actor.isOwned() === false) return res.redirect(share.url) 187 if (share.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(share.url)
188 188
189 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined) 189 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined)
190 190
191 return activityPubResponse(activityPubContextify(activity), res) 191 return activityPubResponse(activityPubContextify(activity), res)
192} 192}
193 193
194async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) { 194async function videoAnnouncesController (req: express.Request, res: express.Response) {
195 const video: VideoModel = res.locals.video 195 const video: VideoModel = res.locals.video
196 196
197 const handler = async (start: number, count: number) => { 197 const handler = async (start: number, count: number) => {
@@ -206,21 +206,21 @@ async function videoAnnouncesController (req: express.Request, res: express.Resp
206 return activityPubResponse(activityPubContextify(json), res) 206 return activityPubResponse(activityPubContextify(json), res)
207} 207}
208 208
209async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) { 209async function videoLikesController (req: express.Request, res: express.Response) {
210 const video: VideoModel = res.locals.video 210 const video: VideoModel = res.locals.video
211 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video)) 211 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
212 212
213 return activityPubResponse(activityPubContextify(json), res) 213 return activityPubResponse(activityPubContextify(json), res)
214} 214}
215 215
216async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) { 216async function videoDislikesController (req: express.Request, res: express.Response) {
217 const video: VideoModel = res.locals.video 217 const video: VideoModel = res.locals.video
218 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video)) 218 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
219 219
220 return activityPubResponse(activityPubContextify(json), res) 220 return activityPubResponse(activityPubContextify(json), res)
221} 221}
222 222
223async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) { 223async function videoCommentsController (req: express.Request, res: express.Response) {
224 const video: VideoModel = res.locals.video 224 const video: VideoModel = res.locals.video
225 225
226 const handler = async (start: number, count: number) => { 226 const handler = async (start: number, count: number) => {
@@ -235,30 +235,30 @@ async function videoCommentsController (req: express.Request, res: express.Respo
235 return activityPubResponse(activityPubContextify(json), res) 235 return activityPubResponse(activityPubContextify(json), res)
236} 236}
237 237
238async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { 238async function videoChannelController (req: express.Request, res: express.Response) {
239 const videoChannel: VideoChannelModel = res.locals.videoChannel 239 const videoChannel: VideoChannelModel = res.locals.videoChannel
240 240
241 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res) 241 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
242} 242}
243 243
244async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { 244async function videoChannelFollowersController (req: express.Request, res: express.Response) {
245 const videoChannel: VideoChannelModel = res.locals.videoChannel 245 const videoChannel: VideoChannelModel = res.locals.videoChannel
246 const activityPubResult = await actorFollowers(req, videoChannel.Actor) 246 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
247 247
248 return activityPubResponse(activityPubContextify(activityPubResult), res) 248 return activityPubResponse(activityPubContextify(activityPubResult), res)
249} 249}
250 250
251async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { 251async function videoChannelFollowingController (req: express.Request, res: express.Response) {
252 const videoChannel: VideoChannelModel = res.locals.videoChannel 252 const videoChannel: VideoChannelModel = res.locals.videoChannel
253 const activityPubResult = await actorFollowing(req, videoChannel.Actor) 253 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
254 254
255 return activityPubResponse(activityPubContextify(activityPubResult), res) 255 return activityPubResponse(activityPubContextify(activityPubResult), res)
256} 256}
257 257
258async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) { 258async function videoCommentController (req: express.Request, res: express.Response) {
259 const videoComment: VideoCommentModel = res.locals.videoComment 259 const videoComment: VideoCommentModel = res.locals.videoComment
260 260
261 if (videoComment.isOwned() === false) return res.redirect(videoComment.url) 261 if (videoComment.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoComment.url)
262 262
263 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) 263 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
264 const isPublic = true // Comments are always public 264 const isPublic = true // Comments are always public
@@ -276,7 +276,7 @@ async function videoCommentController (req: express.Request, res: express.Respon
276 276
277async function videoRedundancyController (req: express.Request, res: express.Response) { 277async function videoRedundancyController (req: express.Request, res: express.Response) {
278 const videoRedundancy: VideoRedundancyModel = res.locals.videoRedundancy 278 const videoRedundancy: VideoRedundancyModel = res.locals.videoRedundancy
279 if (videoRedundancy.isOwned() === false) return res.redirect(videoRedundancy.url) 279 if (videoRedundancy.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
280 280
281 const serverActor = await getServerActor() 281 const serverActor = await getServerActor()
282 282
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 @@
2 2
3import * as chai from 'chai' 3import * as chai from 'chai'
4import 'mocha' 4import 'mocha'
5import { flushTests, killallServers, makeActivityPubGetRequest, runServer, ServerInfo, setAccessTokensToServers } from '../../utils' 5import {
6 doubleFollow,
7 flushAndRunMultipleServers,
8 flushTests,
9 killallServers,
10 makeActivityPubGetRequest,
11 runServer,
12 ServerInfo,
13 setAccessTokensToServers, uploadVideo
14} from '../../utils'
6 15
7const expect = chai.expect 16const expect = chai.expect
8 17
9describe('Test activitypub', function () { 18describe('Test activitypub', function () {
10 let server: ServerInfo = null 19 let servers: ServerInfo[] = []
20 let videoUUID: string
11 21
12 before(async function () { 22 before(async function () {
13 this.timeout(30000) 23 this.timeout(30000)
14 24
15 await flushTests() 25 await flushTests()
16 26
17 server = await runServer(1) 27 servers = await flushAndRunMultipleServers(2)
18 28
19 await setAccessTokensToServers([ server ]) 29 await setAccessTokensToServers(servers)
30
31 {
32 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
33 videoUUID = res.body.video.uuid
34 }
35
36 await doubleFollow(servers[0], servers[1])
20 }) 37 })
21 38
22 it('Should return the account object', async function () { 39 it('Should return the account object', async function () {
23 const res = await makeActivityPubGetRequest(server.url, '/accounts/root') 40 const res = await makeActivityPubGetRequest(servers[0].url, '/accounts/root')
24 const object = res.body 41 const object = res.body
25 42
26 expect(object.type).to.equal('Person') 43 expect(object.type).to.equal('Person')
@@ -29,7 +46,22 @@ describe('Test activitypub', function () {
29 expect(object.preferredUsername).to.equal('root') 46 expect(object.preferredUsername).to.equal('root')
30 }) 47 })
31 48
49 it('Should return the video object', async function () {
50 const res = await makeActivityPubGetRequest(servers[0].url, '/videos/watch/' + videoUUID)
51 const object = res.body
52
53 expect(object.type).to.equal('Video')
54 expect(object.id).to.equal('http://localhost:9001/videos/watch/' + videoUUID)
55 expect(object.name).to.equal('video')
56 })
57
58 it('Should redirect to the origin video object', async function () {
59 const res = await makeActivityPubGetRequest(servers[1].url, '/videos/watch/' + videoUUID, 302)
60
61 expect(res.header.location).to.equal('http://localhost:9001/videos/watch/' + videoUUID)
62 })
63
32 after(async function () { 64 after(async function () {
33 killallServers([ server ]) 65 killallServers(servers)
34 }) 66 })
35}) 67})
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 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2 2
3function makeActivityPubGetRequest (url: string, path: string) { 3function makeActivityPubGetRequest (url: string, path: string, expectedStatus = 200) {
4 return request(url) 4 return request(url)
5 .get(path) 5 .get(path)
6 .set('Accept', 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8') 6 .set('Accept', 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8')
7 .expect(200) 7 .expect(expectedStatus)
8 .expect('Content-Type', /json/)
9} 8}
10 9
11// --------------------------------------------------------------------------- 10// ---------------------------------------------------------------------------