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