2018-08-30 07:58:00 -05:00
|
|
|
import * as express from 'express'
|
|
|
|
import { buildNSFWFilter } from '../../helpers/express-utils'
|
|
|
|
import { VideoModel } from '../../models/video/video'
|
2018-09-03 11:14:05 -05:00
|
|
|
import { asyncMiddleware } from '../../middlewares'
|
2018-08-30 07:58:00 -05:00
|
|
|
import { TagModel } from '../../models/video/tag'
|
|
|
|
import { VideosOverview } from '../../../shared/models/overviews'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { MEMOIZE_TTL, OVERVIEWS, ROUTE_CACHE_LIFETIME } from '../../initializers/constants'
|
2018-08-30 07:58:00 -05:00
|
|
|
import { cacheRoute } from '../../middlewares/cache'
|
2018-09-14 04:52:23 -05:00
|
|
|
import * as memoizee from 'memoizee'
|
2018-08-30 07:58:00 -05:00
|
|
|
|
|
|
|
const overviewsRouter = express.Router()
|
|
|
|
|
|
|
|
overviewsRouter.get('/videos',
|
2018-09-03 11:14:05 -05:00
|
|
|
asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.OVERVIEWS.VIDEOS)),
|
2018-08-30 07:58:00 -05:00
|
|
|
asyncMiddleware(getVideosOverview)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export { overviewsRouter }
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-09-14 07:57:59 -05:00
|
|
|
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 })
|
|
|
|
|
2018-08-30 07:58:00 -05:00
|
|
|
// This endpoint could be quite long, but we cache it
|
|
|
|
async function getVideosOverview (req: express.Request, res: express.Response) {
|
|
|
|
const attributes = await buildSamples()
|
2018-09-14 04:52:23 -05:00
|
|
|
|
|
|
|
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)))
|
|
|
|
])
|
|
|
|
|
2018-08-30 07:58:00 -05:00
|
|
|
const result: VideosOverview = {
|
2018-09-14 04:52:23 -05:00
|
|
|
categories,
|
|
|
|
channels,
|
|
|
|
tags
|
2018-08-30 07:58:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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[] }
|
|
|
|
) {
|
2018-09-14 04:52:23 -05:00
|
|
|
const query = Object.assign({
|
2018-08-30 07:58:00 -05:00
|
|
|
start: 0,
|
2019-04-05 10:00:09 -05:00
|
|
|
count: 12,
|
2018-08-30 07:58:00 -05:00
|
|
|
sort: '-createdAt',
|
|
|
|
includeLocalVideos: true,
|
|
|
|
nsfw: buildNSFWFilter(res),
|
|
|
|
withFiles: false
|
2018-09-14 04:52:23 -05:00
|
|
|
}, where)
|
|
|
|
|
|
|
|
const { data } = await VideoModel.listForApi(query, false)
|
2018-08-30 07:58:00 -05:00
|
|
|
|
|
|
|
return data.map(d => d.toFormattedJSON())
|
|
|
|
}
|