ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

comment-feeds.ts 2.8 KiB

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