はじまりの大地
このコミットが含まれているのは:
@@ -0,0 +1,96 @@
|
||||
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 = 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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
import { Feed } from '@peertube/feed'
|
||||
import { CustomTag, CustomXMLNS, Person } from '@peertube/feed/lib/typings/index.js'
|
||||
import { maxBy, pick } from '@peertube/peertube-core-utils'
|
||||
import { ActorImageType } from '@peertube/peertube-models'
|
||||
import { mdToOneLinePlainText } from '@server/helpers/markdown.js'
|
||||
import { CONFIG } from '@server/initializers/config.js'
|
||||
import { WEBSERVER } from '@server/initializers/constants.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 function initFeed (parameters: {
|
||||
name: string
|
||||
description: string
|
||||
imageUrl: string
|
||||
isPodcast: boolean
|
||||
link?: string
|
||||
locked?: { isLocked: boolean, email: string }
|
||||
author?: {
|
||||
name: string
|
||||
link: string
|
||||
imageUrl: 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, isPodcast, resourceType, queryString, medium } = parameters
|
||||
|
||||
return new Feed({
|
||||
title: name,
|
||||
description: mdToOneLinePlainText(description),
|
||||
// updated: TODO: somehowGetLatestUpdate, // optional, default = today
|
||||
id: link || webserverUrl,
|
||||
link: link || webserverUrl,
|
||||
image: imageUrl,
|
||||
favicon: webserverUrl + '/client/assets/images/favicon.png',
|
||||
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',
|
||||
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, [ 'stunServers', 'trackers', 'customXMLNS', 'customTags', 'author', 'person', 'locked' ])
|
||||
})
|
||||
}
|
||||
|
||||
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 = WEBSERVER.URL + '/client/assets/images/icons/icon-96x96.png'
|
||||
let accountImageUrl: string
|
||||
let name: string
|
||||
let userName: string
|
||||
let description: string
|
||||
let email: string
|
||||
let link: string
|
||||
let accountLink: string
|
||||
let user: MUser
|
||||
|
||||
if (videoChannel) {
|
||||
name = videoChannel.getDisplayName()
|
||||
description = videoChannel.description
|
||||
link = videoChannel.getClientUrl()
|
||||
accountLink = videoChannel.Account.getClientUrl()
|
||||
|
||||
if (videoChannel.Actor.hasImage(ActorImageType.AVATAR)) {
|
||||
const videoChannelAvatar = maxBy(videoChannel.Actor.Avatars, 'width')
|
||||
imageUrl = WEBSERVER.URL + videoChannelAvatar.getStaticPath()
|
||||
}
|
||||
|
||||
if (videoChannel.Account.Actor.hasImage(ActorImageType.AVATAR)) {
|
||||
const accountAvatar = maxBy(videoChannel.Account.Actor.Avatars, 'width')
|
||||
accountImageUrl = WEBSERVER.URL + accountAvatar.getStaticPath()
|
||||
}
|
||||
|
||||
user = await UserModel.loadById(videoChannel.Account.userId)
|
||||
userName = videoChannel.Account.getDisplayName()
|
||||
} else if (account) {
|
||||
name = account.getDisplayName()
|
||||
description = account.description
|
||||
link = account.getClientUrl()
|
||||
accountLink = link
|
||||
|
||||
if (account.Actor.hasImage(ActorImageType.AVATAR)) {
|
||||
const accountAvatar = maxBy(account.Actor.Avatars, 'width')
|
||||
imageUrl = WEBSERVER.URL + accountAvatar?.getStaticPath()
|
||||
accountImageUrl = 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, userName, description, imageUrl, accountImageUrl, email, link, accountLink }
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './video-feed-utils.js'
|
||||
export * from './common-feed-utils.js'
|
||||
@@ -0,0 +1,66 @@
|
||||
import { VideoIncludeType } from '@peertube/peertube-models'
|
||||
import { mdToOneLinePlainText, toSafeHtml } from '@server/helpers/markdown.js'
|
||||
import { CONFIG } from '@server/initializers/config.js'
|
||||
import { WEBSERVER } from '@server/initializers/constants.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 { 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 VideoModel.listForApi({
|
||||
start: 0,
|
||||
count: CONFIG.FEEDS.VIDEOS.COUNT,
|
||||
displayOnlyForFollower: {
|
||||
actorId: server.id,
|
||||
orLocalVideos: true
|
||||
},
|
||||
hasFiles: true,
|
||||
countVideos: false,
|
||||
|
||||
...options
|
||||
})
|
||||
|
||||
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: mdToOneLinePlainText(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
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
import express from 'express'
|
||||
import { extname } from 'path'
|
||||
import { Feed } from '@peertube/feed'
|
||||
import { cacheRouteFactory } from '@server/middlewares/index.js'
|
||||
import { VideoModel } from '@server/models/video/video.js'
|
||||
import { VideoInclude, VideoResolution } from '@peertube/peertube-models'
|
||||
import { buildNSFWFilter } from '../../helpers/express-utils.js'
|
||||
import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants.js'
|
||||
import {
|
||||
asyncMiddleware,
|
||||
commonVideosFiltersValidator,
|
||||
feedsFormatValidator,
|
||||
setDefaultVideosSort,
|
||||
setFeedFormatContentType,
|
||||
feedsAccountOrChannelFiltersValidator,
|
||||
videosSortValidator,
|
||||
videoSubscriptionFeedsValidator
|
||||
} from '../../middlewares/index.js'
|
||||
import { buildFeedMetadata, getCommonVideoFeedAttributes, getVideosForFeeds, initFeed, sendFeed } from './shared/index.js'
|
||||
import { getVideoFileMimeType } from '@server/lib/video-file.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),
|
||||
commonVideosFiltersValidator,
|
||||
asyncMiddleware(feedsAccountOrChannelFiltersValidator),
|
||||
asyncMiddleware(generateVideoFeed)
|
||||
)
|
||||
|
||||
videoFeedsRouter.get('/subscriptions.:format',
|
||||
videosSortValidator,
|
||||
setDefaultVideosSort,
|
||||
feedsFormatValidator,
|
||||
setFeedFormatContentType,
|
||||
cacheRouteMiddleware(ROUTE_CACHE_LIFETIME.FEEDS),
|
||||
commonVideosFiltersValidator,
|
||||
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, accountImageUrl, link, accountLink } = await buildFeedMetadata({ videoChannel, account })
|
||||
|
||||
const feed = initFeed({
|
||||
name,
|
||||
description,
|
||||
link,
|
||||
isPodcast: false,
|
||||
imageUrl,
|
||||
author: { name, link: accountLink, imageUrl: accountImageUrl },
|
||||
resourceType: 'videos',
|
||||
queryString: new URL(WEBSERVER.URL + req.url).search
|
||||
})
|
||||
|
||||
const data = await getVideosForFeeds({
|
||||
sort: req.query.sort,
|
||||
nsfw: buildNSFWFilter(res, req.query.nsfw),
|
||||
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 = initFeed({
|
||||
name,
|
||||
description,
|
||||
link,
|
||||
isPodcast: false,
|
||||
imageUrl,
|
||||
resourceType: 'videos',
|
||||
queryString: new URL(WEBSERVER.URL + req.url).search
|
||||
})
|
||||
|
||||
const data = await getVideosForFeeds({
|
||||
sort: req.query.sort,
|
||||
nsfw: buildNSFWFilter(res, req.query.nsfw),
|
||||
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
|
||||
}
|
||||
})
|
||||
|
||||
feed.addItem({
|
||||
...getCommonVideoFeedAttributes(video),
|
||||
|
||||
id: WEBSERVER.URL + video.getWatchStaticPath(),
|
||||
author: [
|
||||
{
|
||||
name: video.VideoChannel.getDisplayName(),
|
||||
link: video.VideoChannel.getClientUrl()
|
||||
}
|
||||
],
|
||||
torrents,
|
||||
|
||||
// Enclosure
|
||||
video: videoFiles.length !== 0
|
||||
? {
|
||||
url: videoFiles[0].url,
|
||||
length: videoFiles[0].fileSize,
|
||||
type: videoFiles[0].type
|
||||
}
|
||||
: undefined,
|
||||
|
||||
// Media RSS
|
||||
videos: videoFiles,
|
||||
|
||||
embed: {
|
||||
url: WEBSERVER.URL + video.getEmbedStaticPath(),
|
||||
allowFullscreen: true
|
||||
},
|
||||
player: {
|
||||
url: WEBSERVER.URL + video.getWatchStaticPath()
|
||||
},
|
||||
community: {
|
||||
statistics: {
|
||||
views: video.views
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
import { Feed } from '@peertube/feed'
|
||||
import { CustomTag, CustomXMLNS, LiveItemStatus } from '@peertube/feed/lib/typings/index.js'
|
||||
import { maxBy, sortObjectComparator } from '@peertube/peertube-core-utils'
|
||||
import { ActorImageType, VideoFile, VideoInclude, VideoResolution, VideoState } from '@peertube/peertube-models'
|
||||
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 { buildNSFWFilter } from '../../helpers/express-utils.js'
|
||||
import { MIMETYPES, ROUTE_CACHE_LIFETIME, 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, 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, userName, description, imageUrl, accountImageUrl, email, link, accountLink } = await buildFeedMetadata({ videoChannel })
|
||||
|
||||
const data = await getVideosForFeeds({
|
||||
sort: '-publishedAt',
|
||||
nsfw: buildNSFWFilter(),
|
||||
// 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 customTags: CustomTag[] = await Hooks.wrapObject(
|
||||
[],
|
||||
'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 = initFeed({
|
||||
name,
|
||||
description,
|
||||
link,
|
||||
isPodcast: true,
|
||||
imageUrl,
|
||||
|
||||
locked: email
|
||||
? { isLocked: true, email } // Default to true because we have no way of offering a redirect yet
|
||||
: undefined,
|
||||
|
||||
person: [ { name: userName, href: accountLink, img: accountImageUrl } ],
|
||||
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 account = video.VideoChannel.Account
|
||||
|
||||
const author = {
|
||||
name: account.getDisplayName(),
|
||||
href: account.getClientUrl()
|
||||
}
|
||||
|
||||
const commonAttributes = getCommonVideoFeedAttributes(video)
|
||||
const guid = liveItem
|
||||
? `${video.uuid}_${video.publishedAt.toISOString()}`
|
||||
: commonAttributes.link
|
||||
|
||||
let personImage: string
|
||||
|
||||
if (account.Actor.hasImage(ActorImageType.AVATAR)) {
|
||||
const avatar = maxBy(account.Actor.Avatars, 'width')
|
||||
personImage = WEBSERVER.URL + avatar.getStaticPath()
|
||||
}
|
||||
|
||||
return {
|
||||
guid,
|
||||
...commonAttributes,
|
||||
|
||||
trackers: video.getTrackerUrls(),
|
||||
|
||||
author: [ author ],
|
||||
person: [
|
||||
{
|
||||
...author,
|
||||
|
||||
img: personImage
|
||||
}
|
||||
],
|
||||
|
||||
media,
|
||||
|
||||
socialInteract: [
|
||||
{
|
||||
uri: video.url,
|
||||
protocol: 'activitypub',
|
||||
accountUrl: account.getClientUrl()
|
||||
}
|
||||
],
|
||||
|
||||
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', 'desc'))
|
||||
|
||||
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: getVideoFileMimeType(extname(videoFile.fileUrl), videoFile.resolution.id === VideoResolution.H_NOVIDEO),
|
||||
title: videoFile.resolution.label,
|
||||
length: videoFile.size,
|
||||
bitrate: videoFile.size / video.duration * 8,
|
||||
language: video.language,
|
||||
sources
|
||||
}
|
||||
}
|
||||
|
||||
function buildVODStreamingPlaylists (video: MVideoFullLight) {
|
||||
const hls = video.getHLSPlaylist()
|
||||
if (!hls) return []
|
||||
|
||||
return [
|
||||
{
|
||||
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)
|
||||
}
|
||||
新しい課題から参照
ユーザをブロックする