ニジカ投稿局 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.

overviews.ts 4.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import express from 'express'
  2. import memoizee from 'memoizee'
  3. import { logger } from '@server/helpers/logger.js'
  4. import { Hooks } from '@server/lib/plugins/hooks.js'
  5. import { getServerActor } from '@server/models/application/application.js'
  6. import { VideoModel } from '@server/models/video/video.js'
  7. import { CategoryOverview, ChannelOverview, TagOverview, VideosOverview } from '@peertube/peertube-models'
  8. import { buildNSFWFilter } from '../../helpers/express-utils.js'
  9. import { MEMOIZE_TTL, OVERVIEWS } from '../../initializers/constants.js'
  10. import { apiRateLimiter, asyncMiddleware, optionalAuthenticate, videosOverviewValidator } from '../../middlewares/index.js'
  11. import { TagModel } from '../../models/video/tag.js'
  12. const overviewsRouter = express.Router()
  13. overviewsRouter.use(apiRateLimiter)
  14. overviewsRouter.get('/videos',
  15. videosOverviewValidator,
  16. optionalAuthenticate,
  17. asyncMiddleware(getVideosOverview)
  18. )
  19. // ---------------------------------------------------------------------------
  20. export { overviewsRouter }
  21. // ---------------------------------------------------------------------------
  22. const buildSamples = memoizee(async function () {
  23. const [ categories, channels, tags ] = await Promise.all([
  24. VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
  25. VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
  26. TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT)
  27. ])
  28. const result = { categories, channels, tags }
  29. logger.debug('Building samples for overview endpoint.', { result })
  30. return result
  31. }, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE })
  32. // This endpoint could be quite long, but we cache it
  33. async function getVideosOverview (req: express.Request, res: express.Response) {
  34. const attributes = await buildSamples()
  35. const page = req.query.page || 1
  36. const index = page - 1
  37. const categories: CategoryOverview[] = []
  38. const channels: ChannelOverview[] = []
  39. const tags: TagOverview[] = []
  40. await Promise.all([
  41. getVideosByCategory(attributes.categories, index, res, categories),
  42. getVideosByChannel(attributes.channels, index, res, channels),
  43. getVideosByTag(attributes.tags, index, res, tags)
  44. ])
  45. const result: VideosOverview = {
  46. categories,
  47. channels,
  48. tags
  49. }
  50. return res.json(result)
  51. }
  52. async function getVideosByTag (tagsSample: string[], index: number, res: express.Response, acc: TagOverview[]) {
  53. if (tagsSample.length <= index) return
  54. const tag = tagsSample[index]
  55. const videos = await getVideos(res, { tagsOneOf: [ tag ] })
  56. if (videos.length === 0) return
  57. acc.push({
  58. tag,
  59. videos
  60. })
  61. }
  62. async function getVideosByCategory (categoriesSample: number[], index: number, res: express.Response, acc: CategoryOverview[]) {
  63. if (categoriesSample.length <= index) return
  64. const category = categoriesSample[index]
  65. const videos = await getVideos(res, { categoryOneOf: [ category ] })
  66. if (videos.length === 0) return
  67. acc.push({
  68. category: videos[0].category,
  69. videos
  70. })
  71. }
  72. async function getVideosByChannel (channelsSample: number[], index: number, res: express.Response, acc: ChannelOverview[]) {
  73. if (channelsSample.length <= index) return
  74. const channelId = channelsSample[index]
  75. const videos = await getVideos(res, { videoChannelId: channelId })
  76. if (videos.length === 0) return
  77. acc.push({
  78. channel: videos[0].channel,
  79. videos
  80. })
  81. }
  82. async function getVideos (
  83. res: express.Response,
  84. where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
  85. ) {
  86. const serverActor = await getServerActor()
  87. const query = await Hooks.wrapObject({
  88. start: 0,
  89. count: 12,
  90. sort: '-createdAt',
  91. displayOnlyForFollower: {
  92. actorId: serverActor.id,
  93. orLocalVideos: true
  94. },
  95. nsfw: buildNSFWFilter(res),
  96. user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
  97. countVideos: false,
  98. ...where
  99. }, 'filter:api.overviews.videos.list.params')
  100. const { data } = await Hooks.wrapPromiseFun(
  101. VideoModel.listForApi.bind(VideoModel),
  102. query,
  103. 'filter:api.overviews.videos.list.result'
  104. )
  105. return data.map(d => d.toFormattedJSON())
  106. }