]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/overviews.ts
replace numbers with typed http status codes (#3409)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / overviews.ts
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
11 const overviewsRouter = express.Router()
12
13 overviewsRouter.get('/videos',
14 videosOverviewValidator,
15 optionalAuthenticate,
16 asyncMiddleware(getVideosOverview)
17 )
18
19 // ---------------------------------------------------------------------------
20
21 export { overviewsRouter }
22
23 // ---------------------------------------------------------------------------
24
25 const buildSamples = memoizee(async function () {
26 const [ categories, channels, tags ] = await Promise.all([
27 VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
28 VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
29 TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT)
30 ])
31
32 const result = { categories, channels, tags }
33
34 logger.debug('Building samples for overview endpoint.', { result })
35
36 return result
37 }, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE })
38
39 // This endpoint could be quite long, but we cache it
40 async function getVideosOverview (req: express.Request, res: express.Response) {
41 const attributes = await buildSamples()
42
43 const page = req.query.page || 1
44 const index = page - 1
45
46 const categories: CategoryOverview[] = []
47 const channels: ChannelOverview[] = []
48 const tags: TagOverview[] = []
49
50 await Promise.all([
51 getVideosByCategory(attributes.categories, index, res, categories),
52 getVideosByChannel(attributes.channels, index, res, channels),
53 getVideosByTag(attributes.tags, index, res, tags)
54 ])
55
56 const result: VideosOverview = {
57 categories,
58 channels,
59 tags
60 }
61
62 return res.json(result)
63 }
64
65 async function getVideosByTag (tagsSample: string[], index: number, res: express.Response, acc: TagOverview[]) {
66 if (tagsSample.length <= index) return
67
68 const tag = tagsSample[index]
69 const videos = await getVideos(res, { tagsOneOf: [ tag ] })
70
71 if (videos.length === 0) return
72
73 acc.push({
74 tag,
75 videos
76 })
77 }
78
79 async function getVideosByCategory (categoriesSample: number[], index: number, res: express.Response, acc: CategoryOverview[]) {
80 if (categoriesSample.length <= index) return
81
82 const category = categoriesSample[index]
83 const videos = await getVideos(res, { categoryOneOf: [ category ] })
84
85 if (videos.length === 0) return
86
87 acc.push({
88 category: videos[0].category,
89 videos
90 })
91 }
92
93 async function getVideosByChannel (channelsSample: number[], index: number, res: express.Response, acc: ChannelOverview[]) {
94 if (channelsSample.length <= index) return
95
96 const channelId = channelsSample[index]
97 const videos = await getVideos(res, { videoChannelId: channelId })
98
99 if (videos.length === 0) return
100
101 acc.push({
102 channel: videos[0].channel,
103 videos
104 })
105 }
106
107 async function getVideos (
108 res: express.Response,
109 where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
110 ) {
111 const query = Object.assign({
112 start: 0,
113 count: 12,
114 sort: '-createdAt',
115 includeLocalVideos: true,
116 nsfw: buildNSFWFilter(res),
117 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
118 withFiles: false,
119 countVideos: false
120 }, where)
121
122 const { data } = await VideoModel.listForApi(query)
123
124 return data.map(d => d.toFormattedJSON())
125 }