overviews.ts 3.8 KB

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