aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-30 14:58:00 +0200
committerChocobozzz <me@florianbigard.com>2018-08-31 09:19:58 +0200
commit2d3741d6d92e9bd1f41694c7442a6d1da434e1f2 (patch)
tree93a1e609e14bc14ca9e77a6661ddc9c0e461d6f3 /server/controllers/api
parentd9eaee3939bf2e93e5d775d32bce77842201faba (diff)
downloadPeerTube-2d3741d6d92e9bd1f41694c7442a6d1da434e1f2.tar.gz
PeerTube-2d3741d6d92e9bd1f41694c7442a6d1da434e1f2.tar.zst
PeerTube-2d3741d6d92e9bd1f41694c7442a6d1da434e1f2.zip
Videos overview page: first version
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/index.ts2
-rw-r--r--server/controllers/api/overviews.ts97
2 files changed, 99 insertions, 0 deletions
diff --git a/server/controllers/api/index.ts b/server/controllers/api/index.ts
index e928a7478..8a58b5466 100644
--- a/server/controllers/api/index.ts
+++ b/server/controllers/api/index.ts
@@ -10,6 +10,7 @@ import { badRequest } from '../../helpers/express-utils'
10import { videoChannelRouter } from './video-channel' 10import { videoChannelRouter } from './video-channel'
11import * as cors from 'cors' 11import * as cors from 'cors'
12import { searchRouter } from './search' 12import { searchRouter } from './search'
13import { overviewsRouter } from './overviews'
13 14
14const apiRouter = express.Router() 15const apiRouter = express.Router()
15 16
@@ -28,6 +29,7 @@ apiRouter.use('/video-channels', videoChannelRouter)
28apiRouter.use('/videos', videosRouter) 29apiRouter.use('/videos', videosRouter)
29apiRouter.use('/jobs', jobsRouter) 30apiRouter.use('/jobs', jobsRouter)
30apiRouter.use('/search', searchRouter) 31apiRouter.use('/search', searchRouter)
32apiRouter.use('/overviews', overviewsRouter)
31apiRouter.use('/ping', pong) 33apiRouter.use('/ping', pong)
32apiRouter.use('/*', badRequest) 34apiRouter.use('/*', badRequest)
33 35
diff --git a/server/controllers/api/overviews.ts b/server/controllers/api/overviews.ts
new file mode 100644
index 000000000..56f921ce5
--- /dev/null
+++ b/server/controllers/api/overviews.ts
@@ -0,0 +1,97 @@
1import * as express from 'express'
2import { buildNSFWFilter } from '../../helpers/express-utils'
3import { VideoModel } from '../../models/video/video'
4import { asyncMiddleware, executeIfActivityPub } from '../../middlewares'
5import { TagModel } from '../../models/video/tag'
6import { VideosOverview } from '../../../shared/models/overviews'
7import { OVERVIEWS, ROUTE_CACHE_LIFETIME } from '../../initializers'
8import { cacheRoute } from '../../middlewares/cache'
9
10const overviewsRouter = express.Router()
11
12overviewsRouter.get('/videos',
13 executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.OVERVIEWS.VIDEOS))),
14 asyncMiddleware(getVideosOverview)
15)
16
17// ---------------------------------------------------------------------------
18
19export { overviewsRouter }
20
21// ---------------------------------------------------------------------------
22
23// This endpoint could be quite long, but we cache it
24async function getVideosOverview (req: express.Request, res: express.Response) {
25 const attributes = await buildSamples()
26 const result: VideosOverview = {
27 categories: await Promise.all(attributes.categories.map(c => getVideosByCategory(c, res))),
28 channels: await Promise.all(attributes.channels.map(c => getVideosByChannel(c, res))),
29 tags: await Promise.all(attributes.tags.map(t => getVideosByTag(t, res)))
30 }
31
32 // Cleanup our object
33 for (const key of Object.keys(result)) {
34 result[key] = result[key].filter(v => v !== undefined)
35 }
36
37 return res.json(result)
38}
39
40async function buildSamples () {
41 const [ categories, channels, tags ] = await Promise.all([
42 VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
43 VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD ,OVERVIEWS.VIDEOS.SAMPLES_COUNT),
44 TagModel.getRandomSamples(OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT)
45 ])
46
47 return { categories, channels, tags }
48}
49
50async function getVideosByTag (tag: string, res: express.Response) {
51 const videos = await getVideos(res, { tagsOneOf: [ tag ] })
52
53 if (videos.length === 0) return undefined
54
55 return {
56 tag,
57 videos
58 }
59}
60
61async function getVideosByCategory (category: number, res: express.Response) {
62 const videos = await getVideos(res, { categoryOneOf: [ category ] })
63
64 if (videos.length === 0) return undefined
65
66 return {
67 category: videos[0].category,
68 videos
69 }
70}
71
72async function getVideosByChannel (channelId: number, res: express.Response) {
73 const videos = await getVideos(res, { videoChannelId: channelId })
74
75 if (videos.length === 0) return undefined
76
77 return {
78 channel: videos[0].channel,
79 videos
80 }
81}
82
83async function getVideos (
84 res: express.Response,
85 where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
86) {
87 const { data } = await VideoModel.listForApi(Object.assign({
88 start: 0,
89 count: 10,
90 sort: '-createdAt',
91 includeLocalVideos: true,
92 nsfw: buildNSFWFilter(res),
93 withFiles: false
94 }, where))
95
96 return data.map(d => d.toFormattedJSON())
97}