Init commit
This commit is contained in:
97
server/core/controllers/feeds/comment-feeds.ts
Normal file
97
server/core/controllers/feeds/comment-feeds.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { toSafeHtml } from '@server/helpers/markdown.js'
|
||||
import { cacheRouteFactory } from '@server/middlewares/index.js'
|
||||
import express from 'express'
|
||||
import { CONFIG } from '../../initializers/config.js'
|
||||
import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants.js'
|
||||
import {
|
||||
asyncMiddleware,
|
||||
feedsAccountOrChannelFiltersValidator,
|
||||
feedsFormatValidator,
|
||||
setFeedFormatContentType,
|
||||
videoCommentsFeedsValidator
|
||||
} from '../../middlewares/index.js'
|
||||
import { VideoCommentModel } from '../../models/video/video-comment.js'
|
||||
import { buildFeedMetadata, initFeed, sendFeed } from './shared/index.js'
|
||||
|
||||
const commentFeedsRouter = express.Router()
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const { middleware: cacheRouteMiddleware } = cacheRouteFactory({
|
||||
headerBlacklist: [ 'Content-Type' ]
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
commentFeedsRouter.get(
|
||||
'/video-comments.:format',
|
||||
feedsFormatValidator,
|
||||
setFeedFormatContentType,
|
||||
cacheRouteMiddleware(ROUTE_CACHE_LIFETIME.FEEDS),
|
||||
asyncMiddleware(feedsAccountOrChannelFiltersValidator),
|
||||
asyncMiddleware(videoCommentsFeedsValidator),
|
||||
asyncMiddleware(generateVideoCommentsFeed)
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
commentFeedsRouter
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
|
||||
const start = 0
|
||||
const video = res.locals.videoAll
|
||||
const account = res.locals.account
|
||||
const videoChannel = res.locals.videoChannel
|
||||
|
||||
const comments = await VideoCommentModel.listForFeed({
|
||||
start,
|
||||
count: CONFIG.FEEDS.COMMENTS.COUNT,
|
||||
videoId: video?.id,
|
||||
videoAccountOwnerId: account?.id,
|
||||
videoChannelOwnerId: videoChannel?.id
|
||||
})
|
||||
|
||||
const { name, description, imageUrl, link } = await buildFeedMetadata({ video, account, videoChannel })
|
||||
|
||||
const feed = await initFeed({
|
||||
name,
|
||||
description,
|
||||
imageUrl,
|
||||
isPodcast: false,
|
||||
link,
|
||||
resourceType: 'video-comments',
|
||||
queryString: new URL(WEBSERVER.URL + req.originalUrl).search
|
||||
})
|
||||
|
||||
// Adding video items to the feed, one at a time
|
||||
for (const comment of comments) {
|
||||
const localLink = WEBSERVER.URL + comment.getCommentStaticPath()
|
||||
|
||||
let title = comment.Video.name
|
||||
const author: { name: string, link: string }[] = []
|
||||
|
||||
if (comment.Account) {
|
||||
title += ` - ${comment.Account.getDisplayName()}`
|
||||
author.push({
|
||||
name: comment.Account.getDisplayName(),
|
||||
link: comment.Account.Actor.url
|
||||
})
|
||||
}
|
||||
|
||||
feed.addItem({
|
||||
title,
|
||||
id: localLink,
|
||||
link: localLink,
|
||||
content: toSafeHtml(comment.text),
|
||||
author,
|
||||
date: comment.createdAt
|
||||
})
|
||||
}
|
||||
|
||||
// Now the feed generation is done, let's send it!
|
||||
return sendFeed(feed, req, res)
|
||||
}
|
||||
25
server/core/controllers/feeds/index.ts
Normal file
25
server/core/controllers/feeds/index.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import express from 'express'
|
||||
import { CONFIG } from '@server/initializers/config.js'
|
||||
import { buildRateLimiter } from '@server/middlewares/index.js'
|
||||
import { commentFeedsRouter } from './comment-feeds.js'
|
||||
import { videoFeedsRouter } from './video-feeds.js'
|
||||
import { videoPodcastFeedsRouter } from './video-podcast-feeds.js'
|
||||
|
||||
const feedsRouter = express.Router()
|
||||
|
||||
const feedsRateLimiter = buildRateLimiter({
|
||||
windowMs: CONFIG.RATES_LIMIT.FEEDS.WINDOW_MS,
|
||||
max: CONFIG.RATES_LIMIT.FEEDS.MAX
|
||||
})
|
||||
|
||||
feedsRouter.use('/feeds', feedsRateLimiter)
|
||||
|
||||
feedsRouter.use('/feeds', commentFeedsRouter)
|
||||
feedsRouter.use('/feeds', videoFeedsRouter)
|
||||
feedsRouter.use('/feeds', videoPodcastFeedsRouter)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
feedsRouter
|
||||
}
|
||||
170
server/core/controllers/feeds/shared/common-feed-utils.ts
Normal file
170
server/core/controllers/feeds/shared/common-feed-utils.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import { Feed } from '@peertube/feed'
|
||||
import { CustomTag, CustomXMLNS, Person } from '@peertube/feed/lib/typings/index.js'
|
||||
import { pick } from '@peertube/peertube-core-utils'
|
||||
import { ActorImageType } from '@peertube/peertube-models'
|
||||
import { mdToPlainText } from '@server/helpers/markdown.js'
|
||||
import { CONFIG } from '@server/initializers/config.js'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { ServerConfigManager } from '@server/lib/server-config-manager.js'
|
||||
import { getServerActor } from '@server/models/application/application.js'
|
||||
import { UserModel } from '@server/models/user/user.js'
|
||||
import { MAccountDefault, MChannelBannerAccountDefault, MUser, MVideoFullLight } from '@server/types/models/index.js'
|
||||
import express from 'express'
|
||||
|
||||
export async function initFeed (parameters: {
|
||||
name: string
|
||||
description: string
|
||||
imageUrl: string
|
||||
isPodcast: boolean
|
||||
nsfw?: boolean
|
||||
guid?: string
|
||||
link?: string
|
||||
locked?: { isLocked: boolean, email: string }
|
||||
author?: {
|
||||
name: string
|
||||
link: string
|
||||
}
|
||||
category?: string
|
||||
language?: string
|
||||
person?: Person[]
|
||||
resourceType?: 'videos' | 'video-comments'
|
||||
queryString?: string
|
||||
medium?: string
|
||||
stunServers?: string[]
|
||||
trackers?: string[]
|
||||
customXMLNS?: CustomXMLNS[]
|
||||
customTags?: CustomTag[]
|
||||
}) {
|
||||
const webserverUrl = WEBSERVER.URL
|
||||
const { name, description, link, imageUrl, category, isPodcast, resourceType, queryString, medium, nsfw } = parameters
|
||||
|
||||
const feed = new Feed({
|
||||
title: name,
|
||||
description: mdToPlainText(description),
|
||||
|
||||
// updated: TODO: somehowGetLatestUpdate, // optional, default = today
|
||||
id: link || webserverUrl,
|
||||
link: link || webserverUrl,
|
||||
|
||||
image: imageUrl,
|
||||
|
||||
favicon: ServerConfigManager.Instance.getFavicon(await getServerActor()).fileUrl,
|
||||
|
||||
copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
|
||||
` and potential licenses granted by each content's rightholder.`,
|
||||
|
||||
generator: `PeerTube - ${webserverUrl}`,
|
||||
|
||||
medium: medium || 'video',
|
||||
|
||||
nsfw: nsfw ?? false,
|
||||
|
||||
feedLinks: {
|
||||
json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
|
||||
atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
|
||||
rss: isPodcast
|
||||
? `${webserverUrl}/feeds/podcast/videos.xml${queryString}`
|
||||
: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
|
||||
},
|
||||
|
||||
...pick(parameters, [
|
||||
'guid',
|
||||
'language',
|
||||
'stunServers',
|
||||
'trackers',
|
||||
'customXMLNS',
|
||||
'customTags',
|
||||
'author',
|
||||
'person',
|
||||
'locked'
|
||||
])
|
||||
})
|
||||
|
||||
if (category) {
|
||||
feed.addCategory(category)
|
||||
}
|
||||
|
||||
return feed
|
||||
}
|
||||
|
||||
export function sendFeed (feed: Feed, req: express.Request, res: express.Response) {
|
||||
const format = req.params.format
|
||||
|
||||
if (format === 'atom' || format === 'atom1') {
|
||||
return res.send(feed.atom1()).end()
|
||||
}
|
||||
|
||||
if (format === 'json' || format === 'json1') {
|
||||
return res.send(feed.json1()).end()
|
||||
}
|
||||
|
||||
if (format === 'rss' || format === 'rss2') {
|
||||
return res.send(feed.rss2()).end()
|
||||
}
|
||||
|
||||
// We're in the ambiguous '.xml' case and we look at the format query parameter
|
||||
if (req.query.format === 'atom' || req.query.format === 'atom1') {
|
||||
return res.send(feed.atom1()).end()
|
||||
}
|
||||
|
||||
return res.send(feed.rss2()).end()
|
||||
}
|
||||
|
||||
export async function buildFeedMetadata (options: {
|
||||
videoChannel?: MChannelBannerAccountDefault
|
||||
account?: MAccountDefault
|
||||
video?: MVideoFullLight
|
||||
}) {
|
||||
const { video, videoChannel, account } = options
|
||||
|
||||
let imageUrl = ServerConfigManager.Instance.getLogoUrl(await getServerActor(), 512)
|
||||
let ownerImageUrl: string
|
||||
let name: string
|
||||
let description: string
|
||||
let email: string
|
||||
let link: string
|
||||
let ownerLink: string
|
||||
let user: MUser
|
||||
|
||||
if (videoChannel) {
|
||||
name = videoChannel.getDisplayName()
|
||||
description = videoChannel.description
|
||||
ownerLink = videoChannel.getClientUrl()
|
||||
link = ownerLink
|
||||
|
||||
if (videoChannel.Actor.hasImage(ActorImageType.AVATAR)) {
|
||||
imageUrl = WEBSERVER.URL + videoChannel.Actor.getMaxQualityImage(ActorImageType.AVATAR).getStaticPath()
|
||||
ownerImageUrl = imageUrl
|
||||
}
|
||||
|
||||
user = await UserModel.loadById(videoChannel.Account.userId)
|
||||
} else if (account) {
|
||||
name = account.getDisplayName()
|
||||
description = account.description
|
||||
ownerLink = account.getClientUrl()
|
||||
link = ownerLink
|
||||
|
||||
if (account.Actor.hasImage(ActorImageType.AVATAR)) {
|
||||
imageUrl = WEBSERVER.URL + account.Actor.getMaxQualityImage(ActorImageType.AVATAR).getStaticPath()
|
||||
ownerImageUrl = imageUrl
|
||||
}
|
||||
|
||||
user = await UserModel.loadById(account.userId)
|
||||
} else if (video) {
|
||||
name = video.name
|
||||
description = video.description
|
||||
link = video.url
|
||||
} else {
|
||||
name = CONFIG.INSTANCE.NAME
|
||||
description = CONFIG.INSTANCE.DESCRIPTION
|
||||
link = WEBSERVER.URL
|
||||
}
|
||||
|
||||
// If the user is local, has a verified email address, and allows it to be publicly displayed
|
||||
// Return it so the owner can prove ownership of their feed
|
||||
if (user && !user.pluginAuth && user.emailVerified && user.emailPublic) {
|
||||
email = user.email
|
||||
}
|
||||
|
||||
return { name, description, imageUrl, ownerImageUrl, email, link, ownerLink }
|
||||
}
|
||||
2
server/core/controllers/feeds/shared/index.ts
Normal file
2
server/core/controllers/feeds/shared/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './video-feed-utils.js'
|
||||
export * from './common-feed-utils.js'
|
||||
83
server/core/controllers/feeds/shared/video-feed-utils.ts
Normal file
83
server/core/controllers/feeds/shared/video-feed-utils.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { getChannelPodcastFeed } from '@peertube/peertube-core-utils'
|
||||
import { VideoIncludeType } from '@peertube/peertube-models'
|
||||
import { mdToPlainText, toSafeHtml } from '@server/helpers/markdown.js'
|
||||
import { CONFIG } from '@server/initializers/config.js'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { Hooks } from '@server/lib/plugins/hooks.js'
|
||||
import { getServerActor } from '@server/models/application/application.js'
|
||||
import { getCategoryLabel } from '@server/models/video/formatter/index.js'
|
||||
import { DisplayOnlyForFollowerOptions } from '@server/models/video/sql/video/index.js'
|
||||
import { VideoModel } from '@server/models/video/video.js'
|
||||
import { MChannelHostOnly, MThumbnail, MUserDefault } from '@server/types/models/index.js'
|
||||
|
||||
export async function getVideosForFeeds (options: {
|
||||
sort: string
|
||||
nsfw: boolean
|
||||
isLocal: boolean
|
||||
include: VideoIncludeType
|
||||
|
||||
accountId?: number
|
||||
videoChannelId?: number
|
||||
displayOnlyForFollower?: DisplayOnlyForFollowerOptions
|
||||
user?: MUserDefault
|
||||
}) {
|
||||
const server = await getServerActor()
|
||||
|
||||
const { data } = await Hooks.wrapPromiseFun(
|
||||
VideoModel.listForApi.bind(VideoModel),
|
||||
{
|
||||
start: 0,
|
||||
count: CONFIG.FEEDS.VIDEOS.COUNT,
|
||||
displayOnlyForFollower: {
|
||||
actorId: server.id,
|
||||
orLocalVideos: true
|
||||
},
|
||||
hasFiles: true,
|
||||
countVideos: false,
|
||||
|
||||
...options
|
||||
},
|
||||
'filter:feed.videos.list.result'
|
||||
)
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
export function getCommonVideoFeedAttributes (video: VideoModel) {
|
||||
const localLink = WEBSERVER.URL + video.getWatchStaticPath()
|
||||
|
||||
const thumbnailModels: MThumbnail[] = []
|
||||
if (video.hasPreview()) thumbnailModels.push(video.getPreview())
|
||||
if (video.hasMiniature()) thumbnailModels.push(video.getMiniature())
|
||||
|
||||
return {
|
||||
title: video.name,
|
||||
link: localLink,
|
||||
description: mdToPlainText(video.getTruncatedDescription()),
|
||||
content: toSafeHtml(video.description),
|
||||
|
||||
date: video.publishedAt,
|
||||
nsfw: video.nsfw,
|
||||
|
||||
category: video.category
|
||||
? [ { name: getCategoryLabel(video.category) } ]
|
||||
: undefined,
|
||||
|
||||
thumbnails: thumbnailModels.map(t => ({
|
||||
url: WEBSERVER.URL + t.getLocalStaticPath(),
|
||||
width: t.width,
|
||||
height: t.height
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
export function getPodcastFeedUrlCustomTag (videoChannel: MChannelHostOnly) {
|
||||
return {
|
||||
name: 'podcast:txt',
|
||||
attributes: {
|
||||
purpose: 'p20url'
|
||||
},
|
||||
// TODO: use remote channel podcast feed URL
|
||||
value: getChannelPodcastFeed(WEBSERVER.URL, videoChannel)
|
||||
}
|
||||
}
|
||||
215
server/core/controllers/feeds/video-feeds.ts
Normal file
215
server/core/controllers/feeds/video-feeds.ts
Normal file
@@ -0,0 +1,215 @@
|
||||
import { Feed } from '@peertube/feed'
|
||||
import { buildDownloadFilesUrl } from '@peertube/peertube-core-utils'
|
||||
import { VideoInclude, VideoResolution } from '@peertube/peertube-models'
|
||||
import { getVideoFileMimeType } from '@server/lib/video-file.js'
|
||||
import { cacheRouteFactory } from '@server/middlewares/index.js'
|
||||
import { VideoModel } from '@server/models/video/video.js'
|
||||
import express from 'express'
|
||||
import { extname } from 'path'
|
||||
import { buildNSFWFilters } from '../../helpers/express-utils.js'
|
||||
import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants.js'
|
||||
import {
|
||||
asyncMiddleware,
|
||||
commonVideosFiltersValidatorFactory,
|
||||
feedsAccountOrChannelFiltersValidator,
|
||||
feedsFormatValidator,
|
||||
setDefaultVideosSort,
|
||||
setFeedFormatContentType,
|
||||
videosSortValidator,
|
||||
videoSubscriptionFeedsValidator
|
||||
} from '../../middlewares/index.js'
|
||||
import {
|
||||
buildFeedMetadata,
|
||||
getCommonVideoFeedAttributes,
|
||||
getPodcastFeedUrlCustomTag,
|
||||
getVideosForFeeds,
|
||||
initFeed,
|
||||
sendFeed
|
||||
} from './shared/index.js'
|
||||
|
||||
const videoFeedsRouter = express.Router()
|
||||
|
||||
const { middleware: cacheRouteMiddleware } = cacheRouteFactory({
|
||||
headerBlacklist: [ 'Content-Type' ]
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
videoFeedsRouter.get(
|
||||
'/videos.:format',
|
||||
videosSortValidator,
|
||||
setDefaultVideosSort,
|
||||
feedsFormatValidator,
|
||||
setFeedFormatContentType,
|
||||
cacheRouteMiddleware(ROUTE_CACHE_LIFETIME.FEEDS),
|
||||
commonVideosFiltersValidatorFactory(),
|
||||
asyncMiddleware(feedsAccountOrChannelFiltersValidator),
|
||||
asyncMiddleware(generateVideoFeed)
|
||||
)
|
||||
|
||||
videoFeedsRouter.get(
|
||||
'/subscriptions.:format',
|
||||
videosSortValidator,
|
||||
setDefaultVideosSort,
|
||||
feedsFormatValidator,
|
||||
setFeedFormatContentType,
|
||||
cacheRouteMiddleware(ROUTE_CACHE_LIFETIME.FEEDS),
|
||||
commonVideosFiltersValidatorFactory(),
|
||||
asyncMiddleware(videoSubscriptionFeedsValidator),
|
||||
asyncMiddleware(generateVideoFeedForSubscriptions)
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
videoFeedsRouter
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function generateVideoFeed (req: express.Request, res: express.Response) {
|
||||
const account = res.locals.account
|
||||
const videoChannel = res.locals.videoChannel
|
||||
|
||||
const { name, description, imageUrl, ownerImageUrl, link, ownerLink } = await buildFeedMetadata({ videoChannel, account })
|
||||
|
||||
const feed = await initFeed({
|
||||
name,
|
||||
description,
|
||||
link,
|
||||
isPodcast: false,
|
||||
imageUrl: ownerImageUrl || imageUrl,
|
||||
author: { name, link: ownerLink },
|
||||
resourceType: 'videos',
|
||||
queryString: new URL(WEBSERVER.URL + req.url).search,
|
||||
|
||||
customXMLNS: [
|
||||
{
|
||||
name: 'podcast',
|
||||
value: 'https://podcastindex.org/namespace/1.0'
|
||||
}
|
||||
],
|
||||
customTags: videoChannel
|
||||
? [ getPodcastFeedUrlCustomTag(videoChannel) ]
|
||||
: []
|
||||
})
|
||||
|
||||
const data = await getVideosForFeeds({
|
||||
...buildNSFWFilters({ req, res }),
|
||||
|
||||
sort: req.query.sort,
|
||||
isLocal: req.query.isLocal,
|
||||
include: req.query.include | VideoInclude.FILES,
|
||||
accountId: account?.id,
|
||||
videoChannelId: videoChannel?.id
|
||||
})
|
||||
|
||||
addVideosToFeed(feed, data)
|
||||
|
||||
// Now the feed generation is done, let's send it!
|
||||
return sendFeed(feed, req, res)
|
||||
}
|
||||
|
||||
async function generateVideoFeedForSubscriptions (req: express.Request, res: express.Response) {
|
||||
const account = res.locals.account
|
||||
const { name, description, imageUrl, link } = await buildFeedMetadata({ account })
|
||||
|
||||
const feed = await initFeed({
|
||||
name,
|
||||
description,
|
||||
link,
|
||||
isPodcast: false,
|
||||
imageUrl,
|
||||
resourceType: 'videos',
|
||||
queryString: new URL(WEBSERVER.URL + req.url).search
|
||||
})
|
||||
|
||||
const data = await getVideosForFeeds({
|
||||
...buildNSFWFilters({ req, res }),
|
||||
|
||||
sort: req.query.sort,
|
||||
isLocal: req.query.isLocal,
|
||||
include: req.query.include | VideoInclude.FILES,
|
||||
displayOnlyForFollower: {
|
||||
actorId: res.locals.user.Account.Actor.id,
|
||||
orLocalVideos: false
|
||||
},
|
||||
user: res.locals.user
|
||||
})
|
||||
|
||||
addVideosToFeed(feed, data)
|
||||
|
||||
// Now the feed generation is done, let's send it!
|
||||
return sendFeed(feed, req, res)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function addVideosToFeed (feed: Feed, videos: VideoModel[]) {
|
||||
/**
|
||||
* Adding video items to the feed object, one at a time
|
||||
*/
|
||||
for (const video of videos) {
|
||||
const formattedVideoFiles = video.getFormattedAllVideoFilesJSON(false)
|
||||
|
||||
const torrents = formattedVideoFiles.map(videoFile => ({
|
||||
title: video.name,
|
||||
url: videoFile.torrentUrl,
|
||||
size_in_bytes: videoFile.size
|
||||
}))
|
||||
|
||||
const videoFiles = formattedVideoFiles.map(videoFile => {
|
||||
return {
|
||||
type: getVideoFileMimeType(extname(videoFile.fileUrl), videoFile.resolution.id === VideoResolution.H_NOVIDEO),
|
||||
medium: 'video',
|
||||
height: videoFile.resolution.id,
|
||||
fileSize: videoFile.size,
|
||||
url: videoFile.fileUrl,
|
||||
framerate: videoFile.fps,
|
||||
duration: video.duration,
|
||||
lang: video.language
|
||||
}
|
||||
})
|
||||
|
||||
const { videoFile: bestFile, separatedAudioFile: bestAudioFile } = video.getMaxQualityAudioAndVideoFiles()
|
||||
const bestFiles = [ bestFile, bestAudioFile ].filter(f => !!f)
|
||||
|
||||
feed.addItem({
|
||||
...getCommonVideoFeedAttributes(video),
|
||||
|
||||
id: WEBSERVER.URL + video.getWatchStaticPath(),
|
||||
author: [
|
||||
{
|
||||
name: video.VideoChannel.getDisplayName(),
|
||||
link: video.VideoChannel.getClientUrl()
|
||||
}
|
||||
],
|
||||
torrents,
|
||||
|
||||
// Enclosure
|
||||
video: bestFiles.length !== 0
|
||||
? {
|
||||
url: buildDownloadFilesUrl({ baseUrl: WEBSERVER.URL, videoFiles: bestFiles.map(f => f.id), videoUUID: video.uuid }),
|
||||
length: bestFiles.reduce((p, f) => p + f.size, 0),
|
||||
type: getVideoFileMimeType('.mp4', bestFile.resolution === VideoResolution.H_NOVIDEO)
|
||||
}
|
||||
: undefined,
|
||||
|
||||
// Media RSS
|
||||
videos: videoFiles,
|
||||
|
||||
embed: {
|
||||
url: WEBSERVER.URL + video.getEmbedStaticPath(),
|
||||
allowFullscreen: true
|
||||
},
|
||||
player: {
|
||||
url: WEBSERVER.URL + video.getWatchStaticPath()
|
||||
},
|
||||
community: {
|
||||
statistics: {
|
||||
views: video.views
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
384
server/core/controllers/feeds/video-podcast-feeds.ts
Normal file
384
server/core/controllers/feeds/video-podcast-feeds.ts
Normal file
@@ -0,0 +1,384 @@
|
||||
import { Feed } from '@peertube/feed'
|
||||
import { CustomTag, CustomXMLNS, LiveItemStatus } from '@peertube/feed/lib/typings/index.js'
|
||||
import { buildDownloadFilesUrl, getResolutionLabel, sortObjectComparator } from '@peertube/peertube-core-utils'
|
||||
import { ActorImageType, VideoFile, VideoInclude, VideoResolution, VideoState } from '@peertube/peertube-models'
|
||||
import { buildUUIDv5FromURL } from '@peertube/peertube-node-utils'
|
||||
import { buildNSFWFilters } from '@server/helpers/express-utils.js'
|
||||
import { CONFIG } from '@server/initializers/config.js'
|
||||
import { InternalEventEmitter } from '@server/lib/internal-event-emitter.js'
|
||||
import { Hooks } from '@server/lib/plugins/hooks.js'
|
||||
import { getVideoFileMimeType } from '@server/lib/video-file.js'
|
||||
import { buildPodcastGroupsCache, cacheRouteFactory, videoFeedsPodcastSetCacheKey } from '@server/middlewares/index.js'
|
||||
import { MVideo, MVideoCaptionVideo, MVideoFullLight } from '@server/types/models/index.js'
|
||||
import express from 'express'
|
||||
import { extname } from 'path'
|
||||
import { MIMETYPES, ROUTE_CACHE_LIFETIME, VIDEO_CATEGORIES, WEBSERVER } from '../../initializers/constants.js'
|
||||
import { asyncMiddleware, setFeedPodcastContentType, videoFeedsPodcastValidator } from '../../middlewares/index.js'
|
||||
import { VideoCaptionModel } from '../../models/video/video-caption.js'
|
||||
import { VideoModel } from '../../models/video/video.js'
|
||||
import { buildFeedMetadata, getCommonVideoFeedAttributes, getPodcastFeedUrlCustomTag, getVideosForFeeds, initFeed } from './shared/index.js'
|
||||
|
||||
const videoPodcastFeedsRouter = express.Router()
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const { middleware: podcastCacheRouteMiddleware, instance: podcastApiCache } = cacheRouteFactory({
|
||||
headerBlacklist: [ 'Content-Type' ]
|
||||
})
|
||||
|
||||
for (const event of ([ 'video-created', 'video-updated', 'video-deleted' ] as const)) {
|
||||
InternalEventEmitter.Instance.on(event, ({ video }) => {
|
||||
if (video.remote) return
|
||||
|
||||
podcastApiCache.clearGroupSafe(buildPodcastGroupsCache({ channelId: video.channelId }))
|
||||
})
|
||||
}
|
||||
|
||||
for (const event of ([ 'channel-updated', 'channel-deleted' ] as const)) {
|
||||
InternalEventEmitter.Instance.on(event, ({ channel }) => {
|
||||
podcastApiCache.clearGroupSafe(buildPodcastGroupsCache({ channelId: channel.id }))
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
videoPodcastFeedsRouter.get(
|
||||
'/podcast/videos.xml',
|
||||
setFeedPodcastContentType,
|
||||
videoFeedsPodcastSetCacheKey,
|
||||
podcastCacheRouteMiddleware(ROUTE_CACHE_LIFETIME.FEEDS),
|
||||
asyncMiddleware(videoFeedsPodcastValidator),
|
||||
asyncMiddleware(generateVideoPodcastFeed)
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
videoPodcastFeedsRouter
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function generateVideoPodcastFeed (req: express.Request, res: express.Response) {
|
||||
const videoChannel = res.locals.videoChannel
|
||||
|
||||
const { name, description, imageUrl, ownerImageUrl, email, link, ownerLink } = await buildFeedMetadata({ videoChannel })
|
||||
|
||||
const nsfwOptions = buildNSFWFilters()
|
||||
|
||||
const data = await getVideosForFeeds({
|
||||
...nsfwOptions,
|
||||
|
||||
sort: '-publishedAt',
|
||||
|
||||
// Prevent podcast feeds from listing videos in other instances
|
||||
// helps prevent duplicates when they are indexed -- only the author should control them
|
||||
isLocal: true,
|
||||
include: VideoInclude.FILES,
|
||||
videoChannelId: videoChannel?.id
|
||||
})
|
||||
|
||||
const language = await VideoModel.guessLanguageOrCategoryOfChannel(videoChannel.id, 'language')
|
||||
const category = await VideoModel.guessLanguageOrCategoryOfChannel(videoChannel.id, 'category')
|
||||
const hasNSFW = nsfwOptions.nsfw !== false
|
||||
? await VideoModel.channelHasNSFWContent(videoChannel.id)
|
||||
: false
|
||||
|
||||
const customTags: CustomTag[] = await Hooks.wrapObject(
|
||||
[ getPodcastFeedUrlCustomTag(videoChannel) ],
|
||||
'filter:feed.podcast.channel.create-custom-tags.result',
|
||||
{ videoChannel }
|
||||
)
|
||||
|
||||
const customXMLNS: CustomXMLNS[] = await Hooks.wrapObject(
|
||||
[],
|
||||
'filter:feed.podcast.rss.create-custom-xmlns.result'
|
||||
)
|
||||
|
||||
const feed = await initFeed({
|
||||
name,
|
||||
description,
|
||||
link,
|
||||
isPodcast: true,
|
||||
imageUrl,
|
||||
|
||||
language: language || 'en',
|
||||
category: categoryToItunes(category),
|
||||
nsfw: hasNSFW,
|
||||
|
||||
guid: buildUUIDv5FromURL(videoChannel.Actor.url),
|
||||
|
||||
locked: email
|
||||
? { isLocked: true, email } // Default to true because we have no way of offering a redirect yet
|
||||
: undefined,
|
||||
|
||||
person: [ { name, href: ownerLink, img: ownerImageUrl } ],
|
||||
author: { name: CONFIG.INSTANCE.NAME, link: WEBSERVER.URL },
|
||||
resourceType: 'videos',
|
||||
queryString: new URL(WEBSERVER.URL + req.url).search,
|
||||
medium: 'video',
|
||||
customXMLNS,
|
||||
customTags
|
||||
})
|
||||
|
||||
await addVideosToPodcastFeed(feed, data)
|
||||
|
||||
// Now the feed generation is done, let's send it!
|
||||
return res.send(feed.podcast()).end()
|
||||
}
|
||||
|
||||
type PodcastMedia =
|
||||
| {
|
||||
type: string
|
||||
length: number
|
||||
bitrate: number
|
||||
sources: { uri: string, contentType?: string }[]
|
||||
title: string
|
||||
language?: string
|
||||
}
|
||||
| {
|
||||
sources: { uri: string }[]
|
||||
type: string
|
||||
title: string
|
||||
}
|
||||
|
||||
async function generatePodcastItem (options: {
|
||||
video: VideoModel
|
||||
liveItem: boolean
|
||||
media: PodcastMedia[]
|
||||
}) {
|
||||
const { video, liveItem, media } = options
|
||||
|
||||
const customTags: CustomTag[] = await Hooks.wrapObject(
|
||||
[],
|
||||
'filter:feed.podcast.video.create-custom-tags.result',
|
||||
{ video, liveItem }
|
||||
)
|
||||
|
||||
const commonAttributes = getCommonVideoFeedAttributes(video)
|
||||
const guid = liveItem
|
||||
? `${video.url}?publishedAt=${video.publishedAt.toISOString()}`
|
||||
: video.url
|
||||
|
||||
const account = video.VideoChannel.Account
|
||||
const person = {
|
||||
name: account.getDisplayName(),
|
||||
href: account.getClientUrl(),
|
||||
|
||||
img: account.Actor.hasImage(ActorImageType.AVATAR)
|
||||
? WEBSERVER.URL + account.Actor.getMaxQualityImage(ActorImageType.AVATAR).getStaticPath()
|
||||
: undefined
|
||||
}
|
||||
|
||||
return {
|
||||
guid,
|
||||
...commonAttributes,
|
||||
|
||||
trackers: video.getTrackerUrls(),
|
||||
|
||||
person: [ person ],
|
||||
|
||||
media,
|
||||
|
||||
socialInteract: [
|
||||
{
|
||||
uri: video.url,
|
||||
protocol: 'activitypub',
|
||||
accountUrl: account.getClientUrl()
|
||||
}
|
||||
],
|
||||
|
||||
duration: video.duration,
|
||||
|
||||
customTags
|
||||
}
|
||||
}
|
||||
|
||||
async function addVideosToPodcastFeed (feed: Feed, videos: VideoModel[]) {
|
||||
const captionsGroup = await VideoCaptionModel.listCaptionsOfMultipleVideos(videos.map(v => v.id))
|
||||
|
||||
for (const video of videos) {
|
||||
if (!video.isLive) {
|
||||
await addVODPodcastItem({ feed, video, captionsGroup })
|
||||
} else if (video.isLive && video.state !== VideoState.LIVE_ENDED) {
|
||||
await addLivePodcastItem({ feed, video })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function addVODPodcastItem (options: {
|
||||
feed: Feed
|
||||
video: VideoModel
|
||||
captionsGroup: { [id: number]: MVideoCaptionVideo[] }
|
||||
}) {
|
||||
const { feed, video, captionsGroup } = options
|
||||
|
||||
const webVideos = video.getFormattedWebVideoFilesJSON(true)
|
||||
.map(f => buildVODWebVideoFile(video, f))
|
||||
.sort(sortObjectComparator('bitrate', 'asc'))
|
||||
|
||||
const streamingPlaylistFiles = buildVODStreamingPlaylists(video)
|
||||
|
||||
// Order matters here, the first media URI will be the "default"
|
||||
// So web videos are default if enabled
|
||||
const media = [ ...webVideos, ...streamingPlaylistFiles ]
|
||||
|
||||
const videoCaptions = buildVODCaptions(video, captionsGroup[video.id])
|
||||
const item = await generatePodcastItem({ video, liveItem: false, media })
|
||||
|
||||
feed.addPodcastItem({ ...item, subTitle: videoCaptions })
|
||||
}
|
||||
|
||||
async function addLivePodcastItem (options: {
|
||||
feed: Feed
|
||||
video: VideoModel
|
||||
}) {
|
||||
const { feed, video } = options
|
||||
|
||||
let status: LiveItemStatus
|
||||
|
||||
switch (video.state) {
|
||||
case VideoState.WAITING_FOR_LIVE:
|
||||
status = LiveItemStatus.pending
|
||||
break
|
||||
case VideoState.PUBLISHED:
|
||||
status = LiveItemStatus.live
|
||||
break
|
||||
}
|
||||
|
||||
const item = await generatePodcastItem({ video, liveItem: true, media: buildLiveStreamingPlaylists(video) })
|
||||
|
||||
feed.addPodcastLiveItem({ ...item, status, start: video.updatedAt.toISOString() })
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function buildVODWebVideoFile (video: MVideo, videoFile: VideoFile) {
|
||||
const sources = [
|
||||
{ uri: videoFile.fileUrl },
|
||||
{ uri: videoFile.torrentUrl, contentType: 'application/x-bittorrent' }
|
||||
]
|
||||
|
||||
if (videoFile.magnetUri) {
|
||||
sources.push({ uri: videoFile.magnetUri })
|
||||
}
|
||||
|
||||
return {
|
||||
type: getAppleMimeType(extname(videoFile.fileUrl), videoFile.resolution.id === VideoResolution.H_NOVIDEO),
|
||||
title: videoFile.resolution.label,
|
||||
length: videoFile.size,
|
||||
bitrate: Math.round(videoFile.size / video.duration * 8),
|
||||
language: video.language,
|
||||
sources
|
||||
}
|
||||
}
|
||||
|
||||
function buildVODStreamingPlaylists (video: MVideoFullLight) {
|
||||
const hls = video.getHLSPlaylist()
|
||||
if (!hls) return []
|
||||
|
||||
const { separatedAudioFile } = video.getMaxQualityAudioAndVideoFiles()
|
||||
|
||||
return [
|
||||
...hls.VideoFiles
|
||||
.sort(sortObjectComparator('resolution', 'asc'))
|
||||
.map(videoFile => {
|
||||
const files = [ videoFile ]
|
||||
|
||||
if (videoFile.resolution !== VideoResolution.H_NOVIDEO && separatedAudioFile) {
|
||||
files.push(separatedAudioFile)
|
||||
}
|
||||
|
||||
return {
|
||||
type: getAppleMimeType(videoFile.extname, videoFile.resolution === VideoResolution.H_NOVIDEO),
|
||||
title: getResolutionLabel(videoFile),
|
||||
length: files.reduce((p, f) => p + f.size, 0),
|
||||
language: video.language,
|
||||
sources: [
|
||||
{
|
||||
uri: buildDownloadFilesUrl({
|
||||
baseUrl: WEBSERVER.URL,
|
||||
videoFiles: files.map(f => f.id),
|
||||
videoUUID: video.uuid,
|
||||
extension: videoFile.hasVideo()
|
||||
? '.mp4'
|
||||
: '.m4a'
|
||||
})
|
||||
}
|
||||
]
|
||||
}
|
||||
}),
|
||||
|
||||
{
|
||||
type: 'application/x-mpegURL',
|
||||
title: 'HLS',
|
||||
sources: [
|
||||
{ uri: hls.getMasterPlaylistUrl(video) }
|
||||
],
|
||||
language: video.language
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
function buildLiveStreamingPlaylists (video: MVideoFullLight) {
|
||||
const hls = video.getHLSPlaylist()
|
||||
|
||||
return [
|
||||
{
|
||||
type: 'application/x-mpegURL',
|
||||
title: `HLS live stream`,
|
||||
sources: [
|
||||
{ uri: hls.getMasterPlaylistUrl(video) }
|
||||
],
|
||||
language: video.language
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
function buildVODCaptions (video: MVideo, videoCaptions: MVideoCaptionVideo[]) {
|
||||
return videoCaptions.map(caption => {
|
||||
const type = MIMETYPES.VIDEO_CAPTIONS.EXT_MIMETYPE[extname(caption.filename)]
|
||||
if (!type) return null
|
||||
|
||||
return {
|
||||
url: caption.getFileUrl(video),
|
||||
language: caption.language,
|
||||
type,
|
||||
rel: 'captions'
|
||||
}
|
||||
}).filter(c => c)
|
||||
}
|
||||
|
||||
function categoryToItunes (category: number) {
|
||||
const itunesMap: { [id in keyof typeof VIDEO_CATEGORIES]: string } = {
|
||||
1: 'Music',
|
||||
2: 'TV & Film',
|
||||
3: 'Leisure',
|
||||
4: 'Arts',
|
||||
5: 'Sports',
|
||||
6: 'Places & Travel',
|
||||
7: 'Video Games',
|
||||
8: 'Society & Culture',
|
||||
9: 'Comedy',
|
||||
10: 'Fiction',
|
||||
11: 'News',
|
||||
12: 'Leisure',
|
||||
13: 'Education',
|
||||
14: 'Society & Culture',
|
||||
15: 'Technology',
|
||||
16: 'Pets & Animals',
|
||||
17: 'Kids & Family',
|
||||
18: 'Food'
|
||||
}
|
||||
|
||||
return itunesMap[category]
|
||||
}
|
||||
|
||||
// Guidelines: https://help.apple.com/itc/podcasts_connect/#/itcb54353390
|
||||
// "The type values for the supported file formats are: audio/x-m4a, audio/mpeg, video/quicktime, video/mp4, video/x-m4v, ..."
|
||||
function getAppleMimeType (extname: string, isAudio: boolean) {
|
||||
if (extname === '.mp4' && isAudio) return 'audio/x-m4a'
|
||||
if (extname === '.mp3') return 'audio/mpeg'
|
||||
|
||||
return getVideoFileMimeType(extname, isAudio)
|
||||
}
|
||||
Reference in New Issue
Block a user