aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xscripts/travis.sh3
-rw-r--r--server/controllers/feeds.ts44
-rw-r--r--server/middlewares/validators/feeds.ts23
-rw-r--r--server/models/video/video-comment.ts22
-rw-r--r--server/tests/api/feeds/instance-feed.ts91
-rw-r--r--server/tests/api/index-slow.ts1
-rw-r--r--server/tests/feeds/feeds.ts120
-rw-r--r--server/tests/utils/feeds/feeds.ts10
8 files changed, 211 insertions, 103 deletions
diff --git a/scripts/travis.sh b/scripts/travis.sh
index 79be23493..a5f604bb1 100755
--- a/scripts/travis.sh
+++ b/scripts/travis.sh
@@ -9,7 +9,8 @@ fi
9 9
10if [ "$1" = "misc" ]; then 10if [ "$1" = "misc" ]; then
11 npm run build 11 npm run build
12 mocha --timeout 5000 --exit --require ts-node/register/type-check --bail server/tests/client.ts server/tests/activitypub.ts 12 mocha --timeout 5000 --exit --require ts-node/register/type-check --bail server/tests/client.ts server/tests/activitypub.ts \
13 server/tests/feeds/feeds.ts
13elif [ "$1" = "api" ]; then 14elif [ "$1" = "api" ]; then
14 npm run build:server 15 npm run build:server
15 mocha --timeout 5000 --exit --require ts-node/register/type-check --bail server/tests/api/index.ts 16 mocha --timeout 5000 --exit --require ts-node/register/type-check --bail server/tests/api/index.ts
diff --git a/server/controllers/feeds.ts b/server/controllers/feeds.ts
index 3a2b5ecca..c928dfacb 100644
--- a/server/controllers/feeds.ts
+++ b/server/controllers/feeds.ts
@@ -1,20 +1,27 @@
1import * as express from 'express' 1import * as express from 'express'
2import { CONFIG, FEEDS, ROUTE_CACHE_LIFETIME } from '../initializers/constants' 2import { CONFIG, FEEDS, ROUTE_CACHE_LIFETIME } from '../initializers/constants'
3import { asyncMiddleware, feedsValidator, setDefaultSort, videosSortValidator } from '../middlewares' 3import { asyncMiddleware, videoFeedsValidator, setDefaultSort, videosSortValidator, videoCommentsFeedsValidator } from '../middlewares'
4import { VideoModel } from '../models/video/video' 4import { VideoModel } from '../models/video/video'
5import * as Feed from 'pfeed' 5import * as Feed from 'pfeed'
6import { AccountModel } from '../models/account/account' 6import { AccountModel } from '../models/account/account'
7import { cacheRoute } from '../middlewares/cache' 7import { cacheRoute } from '../middlewares/cache'
8import { VideoChannelModel } from '../models/video/video-channel' 8import { VideoChannelModel } from '../models/video/video-channel'
9import { VideoCommentModel } from '../models/video/video-comment'
9 10
10const feedsRouter = express.Router() 11const feedsRouter = express.Router()
11 12
13feedsRouter.get('/feeds/video-comments.:format',
14 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
15 asyncMiddleware(videoCommentsFeedsValidator),
16 asyncMiddleware(generateVideoCommentsFeed)
17)
18
12feedsRouter.get('/feeds/videos.:format', 19feedsRouter.get('/feeds/videos.:format',
13 videosSortValidator, 20 videosSortValidator,
14 setDefaultSort, 21 setDefaultSort,
15 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)), 22 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
16 asyncMiddleware(feedsValidator), 23 asyncMiddleware(videoFeedsValidator),
17 asyncMiddleware(generateFeed) 24 asyncMiddleware(generateVideoFeed)
18) 25)
19 26
20// --------------------------------------------------------------------------- 27// ---------------------------------------------------------------------------
@@ -25,7 +32,36 @@ export {
25 32
26// --------------------------------------------------------------------------- 33// ---------------------------------------------------------------------------
27 34
28async function generateFeed (req: express.Request, res: express.Response, next: express.NextFunction) { 35async function generateVideoCommentsFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
36 let feed = initFeed()
37 const start = 0
38
39 const videoId: number = res.locals.video ? res.locals.video.id : undefined
40
41 const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId)
42
43 // Adding video items to the feed, one at a time
44 comments.forEach(comment => {
45 feed.addItem({
46 title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`,
47 id: comment.url,
48 link: comment.url,
49 content: comment.text,
50 author: [
51 {
52 name: comment.Account.getDisplayName(),
53 link: comment.Account.Actor.url
54 }
55 ],
56 date: comment.createdAt
57 })
58 })
59
60 // Now the feed generation is done, let's send it!
61 return sendFeed(feed, req, res)
62}
63
64async function generateVideoFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
29 let feed = initFeed() 65 let feed = initFeed()
30 const start = 0 66 const start = 0
31 67
diff --git a/server/middlewares/validators/feeds.ts b/server/middlewares/validators/feeds.ts
index b55190559..3c8532bd9 100644
--- a/server/middlewares/validators/feeds.ts
+++ b/server/middlewares/validators/feeds.ts
@@ -7,12 +7,14 @@ import { logger } from '../../helpers/logger'
7import { areValidationErrors } from './utils' 7import { areValidationErrors } from './utils'
8import { isValidRSSFeed } from '../../helpers/custom-validators/feeds' 8import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
9import { isVideoChannelExist } from '../../helpers/custom-validators/video-channels' 9import { isVideoChannelExist } from '../../helpers/custom-validators/video-channels'
10import { isVideoExist } from '../../helpers/custom-validators/videos'
10 11
11const feedsValidator = [ 12const videoFeedsValidator = [
12 param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), 13 param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
13 query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), 14 query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
14 query('accountId').optional().custom(isIdOrUUIDValid), 15 query('accountId').optional().custom(isIdOrUUIDValid),
15 query('accountName').optional().custom(isAccountNameValid), 16 query('accountName').optional().custom(isAccountNameValid),
17 query('videoChannelId').optional().custom(isIdOrUUIDValid),
16 18
17 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 19 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
18 logger.debug('Checking feeds parameters', { parameters: req.query }) 20 logger.debug('Checking feeds parameters', { parameters: req.query })
@@ -26,8 +28,25 @@ const feedsValidator = [
26 } 28 }
27] 29]
28 30
31const videoCommentsFeedsValidator = [
32 param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
33 query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
34 query('videoId').optional().custom(isIdOrUUIDValid),
35
36 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
37 logger.debug('Checking feeds parameters', { parameters: req.query })
38
39 if (areValidationErrors(req, res)) return
40
41 if (req.query.videoId && !await isVideoExist(req.query.videoId, res)) return
42
43 return next()
44 }
45]
46
29// --------------------------------------------------------------------------- 47// ---------------------------------------------------------------------------
30 48
31export { 49export {
32 feedsValidator 50 videoFeedsValidator,
51 videoCommentsFeedsValidator
33} 52}
diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts
index 18398905e..353fb1a0e 100644
--- a/server/models/video/video-comment.ts
+++ b/server/models/video/video-comment.ts
@@ -340,6 +340,28 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
340 return VideoCommentModel.findAndCountAll(query) 340 return VideoCommentModel.findAndCountAll(query)
341 } 341 }
342 342
343 static listForFeed (start: number, count: number, videoId?: number) {
344 const query = {
345 order: [ [ 'createdAt', 'DESC' ] ],
346 start,
347 count,
348 where: {},
349 include: [
350 {
351 attributes: [ 'name' ],
352 model: VideoModel.unscoped(),
353 required: true
354 }
355 ]
356 }
357
358 if (videoId) query.where['videoId'] = videoId
359
360 return VideoCommentModel
361 .scope([ ScopeNames.WITH_ACCOUNT ])
362 .findAll(query)
363 }
364
343 static async getStats () { 365 static async getStats () {
344 const totalLocalVideoComments = await VideoCommentModel.count({ 366 const totalLocalVideoComments = await VideoCommentModel.count({
345 include: [ 367 include: [
diff --git a/server/tests/api/feeds/instance-feed.ts b/server/tests/api/feeds/instance-feed.ts
deleted file mode 100644
index e834e1db1..000000000
--- a/server/tests/api/feeds/instance-feed.ts
+++ /dev/null
@@ -1,91 +0,0 @@
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
5import {
6 getOEmbed,
7 getXMLfeed,
8 getJSONfeed,
9 flushTests,
10 killallServers,
11 ServerInfo,
12 setAccessTokensToServers,
13 uploadVideo,
14 flushAndRunMultipleServers,
15 wait
16} from '../../utils'
17import { runServer } from '../../utils/server/servers'
18import { join } from 'path'
19import * as libxmljs from 'libxmljs'
20
21chai.use(require('chai-xml'))
22chai.use(require('chai-json-schema'))
23chai.config.includeStack = true
24const expect = chai.expect
25
26describe('Test instance-wide syndication feeds', () => {
27 let servers: ServerInfo[] = []
28
29 before(async function () {
30 this.timeout(30000)
31
32 // Run servers
33 servers = await flushAndRunMultipleServers(2)
34
35 await setAccessTokensToServers(servers)
36
37 this.timeout(60000)
38
39 const videoAttributes = {
40 name: 'my super name for server 1',
41 description: 'my super description for server 1',
42 fixture: 'video_short.webm'
43 }
44 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
45
46 await wait(10000)
47 })
48
49 it('should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
50 const rss = await getXMLfeed(servers[0].url)
51 expect(rss.text).xml.to.be.valid()
52
53 const atom = await getXMLfeed(servers[0].url, 'atom')
54 expect(atom.text).xml.to.be.valid()
55 })
56
57 it('should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
58 const json = await getJSONfeed(servers[0].url)
59 expect(JSON.parse(json.text)).to.be.jsonSchema({ 'type': 'object' })
60 })
61
62 it('should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
63 const rss = await getXMLfeed(servers[0].url)
64 const xmlDoc = libxmljs.parseXmlString(rss.text)
65 const xmlEnclosure = xmlDoc.get('/rss/channel/item/enclosure')
66 expect(xmlEnclosure).to.exist
67 expect(xmlEnclosure.attr('type').value()).to.be.equal('application/x-bittorrent')
68 expect(xmlEnclosure.attr('length').value()).to.be.equal('218910')
69 expect(xmlEnclosure.attr('url').value()).to.contain('720.torrent')
70 })
71
72 it('should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
73 const json = await getJSONfeed(servers[0].url)
74 const jsonObj = JSON.parse(json.text)
75 expect(jsonObj.items.length).to.be.equal(1)
76 expect(jsonObj.items[0].attachments).to.exist
77 expect(jsonObj.items[0].attachments.length).to.be.eq(1)
78 expect(jsonObj.items[0].attachments[0].mime_type).to.be.eq('application/x-bittorrent')
79 expect(jsonObj.items[0].attachments[0].size_in_bytes).to.be.eq(218910)
80 expect(jsonObj.items[0].attachments[0].url).to.contain('720.torrent')
81 })
82
83 after(async function () {
84 killallServers(servers)
85
86 // Keep the logs if the test failed
87 if (this['ok']) {
88 await flushTests()
89 }
90 })
91})
diff --git a/server/tests/api/index-slow.ts b/server/tests/api/index-slow.ts
index 5f2f26095..cde546856 100644
--- a/server/tests/api/index-slow.ts
+++ b/server/tests/api/index-slow.ts
@@ -1,6 +1,5 @@
1// Order of the tests we want to execute 1// Order of the tests we want to execute
2import './videos/video-transcoder' 2import './videos/video-transcoder'
3import './feeds/instance-feed'
4import './videos/multiple-servers' 3import './videos/multiple-servers'
5import './server/follows' 4import './server/follows'
6import './server/jobs' 5import './server/jobs'
diff --git a/server/tests/feeds/feeds.ts b/server/tests/feeds/feeds.ts
new file mode 100644
index 000000000..f65148f00
--- /dev/null
+++ b/server/tests/feeds/feeds.ts
@@ -0,0 +1,120 @@
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
5import {
6 doubleFollow,
7 flushAndRunMultipleServers,
8 flushTests,
9 getJSONfeed,
10 getXMLfeed,
11 killallServers,
12 ServerInfo,
13 setAccessTokensToServers,
14 uploadVideo,
15 wait
16} from '../utils'
17import { join } from 'path'
18import * as libxmljs from 'libxmljs'
19import { addVideoCommentThread } from '../utils/videos/video-comments'
20
21chai.use(require('chai-xml'))
22chai.use(require('chai-json-schema'))
23chai.config.includeStack = true
24const expect = chai.expect
25
26describe('Test syndication feeds', () => {
27 let servers: ServerInfo[] = []
28
29 before(async function () {
30 this.timeout(120000)
31
32 // Run servers
33 servers = await flushAndRunMultipleServers(2)
34
35 await setAccessTokensToServers(servers)
36 await doubleFollow(servers[0], servers[1])
37
38 const videoAttributes = {
39 name: 'my super name for server 1',
40 description: 'my super description for server 1',
41 fixture: 'video_short.webm'
42 }
43 const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
44 const videoId = res.body.video.id
45
46 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 1')
47 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoId, 'super comment 2')
48
49 await wait(10000)
50 })
51
52 describe('All feed', function () {
53
54 it('Should be well formed XML (covers RSS 2.0 and ATOM 1.0 endpoints)', async function () {
55 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
56 const rss = await getXMLfeed(servers[ 0 ].url, feed)
57 expect(rss.text).xml.to.be.valid()
58
59 const atom = await getXMLfeed(servers[ 0 ].url, feed, 'atom')
60 expect(atom.text).xml.to.be.valid()
61 }
62 })
63
64 it('Should be well formed JSON (covers JSON feed 1.0 endpoint)', async function () {
65 for (const feed of [ 'video-comments' as 'video-comments', 'videos' as 'videos' ]) {
66 const json = await getJSONfeed(servers[ 0 ].url, feed)
67 expect(JSON.parse(json.text)).to.be.jsonSchema({ 'type': 'object' })
68 }
69 })
70 })
71
72 describe('Videos feed', function () {
73 it('Should contain a valid enclosure (covers RSS 2.0 endpoint)', async function () {
74 for (const server of servers) {
75 const rss = await getXMLfeed(server.url, 'videos')
76 const xmlDoc = libxmljs.parseXmlString(rss.text)
77 const xmlEnclosure = xmlDoc.get('/rss/channel/item/enclosure')
78 expect(xmlEnclosure).to.exist
79 expect(xmlEnclosure.attr('type').value()).to.be.equal('application/x-bittorrent')
80 expect(xmlEnclosure.attr('length').value()).to.be.equal('218910')
81 expect(xmlEnclosure.attr('url').value()).to.contain('720.torrent')
82 }
83 })
84
85 it('Should contain a valid \'attachments\' object (covers JSON feed 1.0 endpoint)', async function () {
86 for (const server of servers) {
87 const json = await getJSONfeed(server.url, 'videos')
88 const jsonObj = JSON.parse(json.text)
89 expect(jsonObj.items.length).to.be.equal(1)
90 expect(jsonObj.items[ 0 ].attachments).to.exist
91 expect(jsonObj.items[ 0 ].attachments.length).to.be.eq(1)
92 expect(jsonObj.items[ 0 ].attachments[ 0 ].mime_type).to.be.eq('application/x-bittorrent')
93 expect(jsonObj.items[ 0 ].attachments[ 0 ].size_in_bytes).to.be.eq(218910)
94 expect(jsonObj.items[ 0 ].attachments[ 0 ].url).to.contain('720.torrent')
95 }
96 })
97 })
98
99 describe('Video comments feed', function () {
100 it('Should contain valid comments (covers JSON feed 1.0 endpoint)', async function () {
101 for (const server of servers) {
102 const json = await getJSONfeed(server.url, 'video-comments')
103
104 const jsonObj = JSON.parse(json.text)
105 expect(jsonObj.items.length).to.be.equal(2)
106 expect(jsonObj.items[ 0 ].html_content).to.equal('super comment 2')
107 expect(jsonObj.items[ 1 ].html_content).to.equal('super comment 1')
108 }
109 })
110 })
111
112 after(async function () {
113 killallServers(servers)
114
115 // Keep the logs if the test failed
116 if (this['ok']) {
117 await flushTests()
118 }
119 })
120})
diff --git a/server/tests/utils/feeds/feeds.ts b/server/tests/utils/feeds/feeds.ts
index 20e68cf3d..ffd23a1ad 100644
--- a/server/tests/utils/feeds/feeds.ts
+++ b/server/tests/utils/feeds/feeds.ts
@@ -1,8 +1,10 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2import { readFileBufferPromise } from '../../../helpers/core-utils' 2import { readFileBufferPromise } from '../../../helpers/core-utils'
3 3
4function getXMLfeed (url: string, format?: string) { 4type FeedType = 'videos' | 'video-comments'
5 const path = '/feeds/videos.xml' 5
6function getXMLfeed (url: string, feed: FeedType, format?: string) {
7 const path = '/feeds/' + feed + '.xml'
6 8
7 return request(url) 9 return request(url)
8 .get(path) 10 .get(path)
@@ -12,8 +14,8 @@ function getXMLfeed (url: string, format?: string) {
12 .expect('Content-Type', /xml/) 14 .expect('Content-Type', /xml/)
13} 15}
14 16
15function getJSONfeed (url: string) { 17function getJSONfeed (url: string, feed: FeedType) {
16 const path = '/feeds/videos.json' 18 const path = '/feeds/' + feed + '.json'
17 19
18 return request(url) 20 return request(url)
19 .get(path) 21 .get(path)