diff options
Diffstat (limited to 'server/controllers/feeds/comment-feeds.ts')
-rw-r--r-- | server/controllers/feeds/comment-feeds.ts | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/server/controllers/feeds/comment-feeds.ts b/server/controllers/feeds/comment-feeds.ts new file mode 100644 index 000000000..bdc53b51f --- /dev/null +++ b/server/controllers/feeds/comment-feeds.ts | |||
@@ -0,0 +1,96 @@ | |||
1 | import express from 'express' | ||
2 | import { toSafeHtml } from '@server/helpers/markdown' | ||
3 | import { cacheRouteFactory } from '@server/middlewares' | ||
4 | import { CONFIG } from '../../initializers/config' | ||
5 | import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants' | ||
6 | import { | ||
7 | asyncMiddleware, | ||
8 | feedsFormatValidator, | ||
9 | setFeedFormatContentType, | ||
10 | videoCommentsFeedsValidator, | ||
11 | videoFeedsValidator | ||
12 | } from '../../middlewares' | ||
13 | import { VideoCommentModel } from '../../models/video/video-comment' | ||
14 | import { buildFeedMetadata, initFeed, sendFeed } from './shared' | ||
15 | |||
16 | const commentFeedsRouter = express.Router() | ||
17 | |||
18 | // --------------------------------------------------------------------------- | ||
19 | |||
20 | const { middleware: cacheRouteMiddleware } = cacheRouteFactory({ | ||
21 | headerBlacklist: [ 'Content-Type' ] | ||
22 | }) | ||
23 | |||
24 | // --------------------------------------------------------------------------- | ||
25 | |||
26 | commentFeedsRouter.get('/feeds/video-comments.:format', | ||
27 | feedsFormatValidator, | ||
28 | setFeedFormatContentType, | ||
29 | cacheRouteMiddleware(ROUTE_CACHE_LIFETIME.FEEDS), | ||
30 | asyncMiddleware(videoFeedsValidator), | ||
31 | asyncMiddleware(videoCommentsFeedsValidator), | ||
32 | asyncMiddleware(generateVideoCommentsFeed) | ||
33 | ) | ||
34 | |||
35 | // --------------------------------------------------------------------------- | ||
36 | |||
37 | export { | ||
38 | commentFeedsRouter | ||
39 | } | ||
40 | |||
41 | // --------------------------------------------------------------------------- | ||
42 | |||
43 | async function generateVideoCommentsFeed (req: express.Request, res: express.Response) { | ||
44 | const start = 0 | ||
45 | const video = res.locals.videoAll | ||
46 | const account = res.locals.account | ||
47 | const videoChannel = res.locals.videoChannel | ||
48 | |||
49 | const comments = await VideoCommentModel.listForFeed({ | ||
50 | start, | ||
51 | count: CONFIG.FEEDS.COMMENTS.COUNT, | ||
52 | videoId: video ? video.id : undefined, | ||
53 | accountId: account ? account.id : undefined, | ||
54 | videoChannelId: videoChannel ? videoChannel.id : undefined | ||
55 | }) | ||
56 | |||
57 | const { name, description, imageUrl, link } = await buildFeedMetadata({ video, account, videoChannel }) | ||
58 | |||
59 | const feed = initFeed({ | ||
60 | name, | ||
61 | description, | ||
62 | imageUrl, | ||
63 | isPodcast: false, | ||
64 | link, | ||
65 | resourceType: 'video-comments', | ||
66 | queryString: new URL(WEBSERVER.URL + req.originalUrl).search | ||
67 | }) | ||
68 | |||
69 | // Adding video items to the feed, one at a time | ||
70 | for (const comment of comments) { | ||
71 | const localLink = WEBSERVER.URL + comment.getCommentStaticPath() | ||
72 | |||
73 | let title = comment.Video.name | ||
74 | const author: { name: string, link: string }[] = [] | ||
75 | |||
76 | if (comment.Account) { | ||
77 | title += ` - ${comment.Account.getDisplayName()}` | ||
78 | author.push({ | ||
79 | name: comment.Account.getDisplayName(), | ||
80 | link: comment.Account.Actor.url | ||
81 | }) | ||
82 | } | ||
83 | |||
84 | feed.addItem({ | ||
85 | title, | ||
86 | id: localLink, | ||
87 | link: localLink, | ||
88 | content: toSafeHtml(comment.text), | ||
89 | author, | ||
90 | date: comment.createdAt | ||
91 | }) | ||
92 | } | ||
93 | |||
94 | // Now the feed generation is done, let's send it! | ||
95 | return sendFeed(feed, req, res) | ||
96 | } | ||