overviews.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import * as express from 'express'
  2. import { buildNSFWFilter } from '../../helpers/express-utils'
  3. import { VideoModel } from '../../models/video/video'
  4. import { asyncMiddleware } from '../../middlewares'
  5. import { TagModel } from '../../models/video/tag'
  6. import { VideosOverview } from '../../../shared/models/overviews'
  7. import { MEMOIZE_TTL, OVERVIEWS, ROUTE_CACHE_LIFETIME } from '../../initializers/constants'
  8. import { cacheRoute } from '../../middlewares/cache'
  9. import * as memoizee from 'memoizee'
  10. const overviewsRouter = express.Router()
  11. overviewsRouter.get('/videos',
  12. asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.OVERVIEWS.VIDEOS)),
  13. asyncMiddleware(getVideosOverview)
  14. )
  15. // ---------------------------------------------------------------------------
  16. export { overviewsRouter }
  17. // ---------------------------------------------------------------------------
  18. const buildSamples = memoizee(async function () {
  19. const [ categories, channels, tags ] = await Promise.all([
  20. VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
  21. VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD ,OVERVIEWS.VIDEOS.SAMPLES_COUNT),
  22. TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT)
  23. ])
  24. return { categories, channels, tags }
  25. }, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE })
  26. // This endpoint could be quite long, but we cache it
  27. async function getVideosOverview (req: express.Request, res: express.Response) {
  28. const attributes = await buildSamples()
  29. const [ categories, channels, tags ] = await Promise.all([
  30. Promise.all(attributes.categories.map(c => getVideosByCategory(c, res))),
  31. Promise.all(attributes.channels.map(c => getVideosByChannel(c, res))),
  32. Promise.all(attributes.tags.map(t => getVideosByTag(t, res)))
  33. ])
  34. const result: VideosOverview = {
  35. categories,
  36. channels,
  37. tags
  38. }
  39. // Cleanup our object
  40. for (const key of Object.keys(result)) {
  41. result[key] = result[key].filter(v => v !== undefined)
  42. }
  43. return res.json(result)
  44. }
  45. async function getVideosByTag (tag: string, res: express.Response) {
  46. const videos = await getVideos(res, { tagsOneOf: [ tag ] })
  47. if (videos.length === 0) return undefined
  48. return {
  49. tag,
  50. videos
  51. }
  52. }
  53. async function getVideosByCategory (category: number, res: express.Response) {
  54. const videos = await getVideos(res, { categoryOneOf: [ category ] })
  55. if (videos.length === 0) return undefined
  56. return {
  57. category: videos[0].category,
  58. videos
  59. }
  60. }
  61. async function getVideosByChannel (channelId: number, res: express.Response) {
  62. const videos = await getVideos(res, { videoChannelId: channelId })
  63. if (videos.length === 0) return undefined
  64. return {
  65. channel: videos[0].channel,
  66. videos
  67. }
  68. }
  69. async function getVideos (
  70. res: express.Response,
  71. where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
  72. ) {
  73. const query = Object.assign({
  74. start: 0,
  75. count: 12,
  76. sort: '-createdAt',
  77. includeLocalVideos: true,
  78. nsfw: buildNSFWFilter(res),
  79. withFiles: false
  80. }, where)
  81. const { data } = await VideoModel.listForApi(query, false)
  82. return data.map(d => d.toFormattedJSON())
  83. }