aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/overviews.ts
blob: 37ac152dbe5ee2f47f28da27fc2fa86df3e07ce3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import * as express from 'express'
import { buildNSFWFilter } from '../../helpers/express-utils'
import { VideoModel } from '../../models/video/video'
import { asyncMiddleware } from '../../middlewares'
import { TagModel } from '../../models/video/tag'
import { VideosOverview } from '../../../shared/models/overviews'
import { MEMOIZE_TTL, OVERVIEWS, ROUTE_CACHE_LIFETIME } from '../../initializers/constants'
import { cacheRoute } from '../../middlewares/cache'
import * as memoizee from 'memoizee'

const overviewsRouter = express.Router()

overviewsRouter.get('/videos',
  asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.OVERVIEWS.VIDEOS)),
  asyncMiddleware(getVideosOverview)
)

// ---------------------------------------------------------------------------

export { overviewsRouter }

// ---------------------------------------------------------------------------

const buildSamples = memoizee(async function () {
  const [ categories, channels, tags ] = await Promise.all([
    VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
    VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD ,OVERVIEWS.VIDEOS.SAMPLES_COUNT),
    TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT)
  ])

  return { categories, channels, tags }
}, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE })

// This endpoint could be quite long, but we cache it
async function getVideosOverview (req: express.Request, res: express.Response) {
  const attributes = await buildSamples()

  const [ categories, channels, tags ] = await Promise.all([
    Promise.all(attributes.categories.map(c => getVideosByCategory(c, res))),
    Promise.all(attributes.channels.map(c => getVideosByChannel(c, res))),
    Promise.all(attributes.tags.map(t => getVideosByTag(t, res)))
  ])

  const result: VideosOverview = {
    categories,
    channels,
    tags
  }

  // Cleanup our object
  for (const key of Object.keys(result)) {
    result[key] = result[key].filter(v => v !== undefined)
  }

  return res.json(result)
}

async function getVideosByTag (tag: string, res: express.Response) {
  const videos = await getVideos(res, { tagsOneOf: [ tag ] })

  if (videos.length === 0) return undefined

  return {
    tag,
    videos
  }
}

async function getVideosByCategory (category: number, res: express.Response) {
  const videos = await getVideos(res, { categoryOneOf: [ category ] })

  if (videos.length === 0) return undefined

  return {
    category: videos[0].category,
    videos
  }
}

async function getVideosByChannel (channelId: number, res: express.Response) {
  const videos = await getVideos(res, { videoChannelId: channelId })

  if (videos.length === 0) return undefined

  return {
    channel: videos[0].channel,
    videos
  }
}

async function getVideos (
  res: express.Response,
  where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
) {
  const query = Object.assign({
    start: 0,
    count: 12,
    sort: '-createdAt',
    includeLocalVideos: true,
    nsfw: buildNSFWFilter(res),
    withFiles: false
  }, where)

  const { data } = await VideoModel.listForApi(query, false)

  return data.map(d => d.toFormattedJSON())
}