mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 02:39:33 +02:00
Add RSS feed discovery
This commit is contained in:
parent
02d53b1786
commit
21f0fbde0d
17 changed files with 259 additions and 88 deletions
|
@ -1,5 +1,6 @@
|
|||
import { escapeHTML, maxBy } from '@peertube/peertube-core-utils'
|
||||
import { escapeHTML, getChannelRSSFeeds, getDefaultRSSFeeds, maxBy } from '@peertube/peertube-core-utils'
|
||||
import { HttpStatusCode } from '@peertube/peertube-models'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { AccountModel } from '@server/models/account/account.js'
|
||||
import { ActorImageModel } from '@server/models/actor/actor-image.js'
|
||||
import { VideoChannelModel } from '@server/models/video/video-channel.js'
|
||||
|
@ -7,20 +8,30 @@ import { MAccountHost, MChannelHost } from '@server/types/models/index.js'
|
|||
import express from 'express'
|
||||
import { CONFIG } from '../../../initializers/config.js'
|
||||
import { PageHtml } from './page-html.js'
|
||||
import { TagsHtml } from './tags-html.js'
|
||||
import { TagsHtml, TagsOptions } from './tags-html.js'
|
||||
|
||||
export class ActorHtml {
|
||||
|
||||
static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
|
||||
const accountModelPromise = AccountModel.loadByNameWithHost(nameWithHost)
|
||||
|
||||
return this.getAccountOrChannelHTMLPage(() => accountModelPromise, req, res)
|
||||
return this.getAccountOrChannelHTMLPage({
|
||||
loader: () => accountModelPromise,
|
||||
getRSSFeeds: () => getDefaultRSSFeeds(WEBSERVER.URL, CONFIG.INSTANCE.NAME),
|
||||
req,
|
||||
res
|
||||
})
|
||||
}
|
||||
|
||||
static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
|
||||
const videoChannelModelPromise = VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost)
|
||||
const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost)
|
||||
|
||||
return this.getAccountOrChannelHTMLPage(() => videoChannelModelPromise, req, res)
|
||||
return this.getAccountOrChannelHTMLPage({
|
||||
loader: () => Promise.resolve(videoChannel),
|
||||
getRSSFeeds: () => getChannelRSSFeeds(WEBSERVER.URL, CONFIG.INSTANCE.NAME, videoChannel),
|
||||
req,
|
||||
res
|
||||
})
|
||||
}
|
||||
|
||||
static async getActorHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
|
||||
|
@ -29,16 +40,28 @@ export class ActorHtml {
|
|||
VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost)
|
||||
])
|
||||
|
||||
return this.getAccountOrChannelHTMLPage(() => Promise.resolve(account || channel), req, res)
|
||||
return this.getAccountOrChannelHTMLPage({
|
||||
loader: () => Promise.resolve(account || channel),
|
||||
|
||||
getRSSFeeds: () => account
|
||||
? getDefaultRSSFeeds(WEBSERVER.URL, CONFIG.INSTANCE.NAME)
|
||||
: getChannelRSSFeeds(WEBSERVER.URL, CONFIG.INSTANCE.NAME, channel),
|
||||
|
||||
req,
|
||||
res
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private static async getAccountOrChannelHTMLPage (
|
||||
loader: () => Promise<MAccountHost | MChannelHost>,
|
||||
req: express.Request,
|
||||
private static async getAccountOrChannelHTMLPage (options: {
|
||||
loader: () => Promise<MAccountHost | MChannelHost>
|
||||
getRSSFeeds: (entity: MAccountHost | MChannelHost) => TagsOptions['rssFeeds']
|
||||
req: express.Request
|
||||
res: express.Response
|
||||
) {
|
||||
}) {
|
||||
const { loader, getRSSFeeds, req, res } = options
|
||||
|
||||
const [ html, entity ] = await Promise.all([
|
||||
PageHtml.getIndexHTML(req, res),
|
||||
loader()
|
||||
|
@ -85,7 +108,9 @@ export class ActorHtml {
|
|||
updatedAt: entity.updatedAt
|
||||
},
|
||||
|
||||
forbidIndexation: !entity.Actor.isOwned()
|
||||
forbidIndexation: !entity.Actor.isOwned(),
|
||||
|
||||
rssFeeds: getRSSFeeds(entity)
|
||||
}, {})
|
||||
|
||||
return customHTML
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
import { MVideo } from '@server/types/models/video/video.js'
|
||||
import { TagsHtml } from './tags-html.js'
|
||||
import { MVideoPlaylist } from '@server/types/models/video/video-playlist.js'
|
||||
|
||||
export class CommonEmbedHtml {
|
||||
|
||||
static buildEmptyEmbedHTML (options: {
|
||||
html: string
|
||||
playlist?: MVideoPlaylist
|
||||
video?: MVideo
|
||||
}) {
|
||||
const { html, playlist, video } = options
|
||||
|
||||
let htmlResult = TagsHtml.addTitleTag(html)
|
||||
htmlResult = TagsHtml.addDescriptionTag(htmlResult)
|
||||
|
||||
return TagsHtml.addTags(htmlResult, { forbidIndexation: true }, { playlist, video })
|
||||
}
|
||||
}
|
16
server/core/lib/html/shared/common.ts
Normal file
16
server/core/lib/html/shared/common.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
import { MVideoPlaylist } from '@server/types/models/video/video-playlist.js'
|
||||
import { MVideo } from '@server/types/models/video/video.js'
|
||||
import { TagsHtml } from './tags-html.js'
|
||||
|
||||
export function buildEmptyEmbedHTML (options: {
|
||||
html: string
|
||||
playlist?: MVideoPlaylist
|
||||
video?: MVideo
|
||||
}) {
|
||||
const { html, playlist, video } = options
|
||||
|
||||
let htmlResult = TagsHtml.addTitleTag(html)
|
||||
htmlResult = TagsHtml.addDescriptionTag(htmlResult)
|
||||
|
||||
return TagsHtml.addTags(htmlResult, { forbidIndexation: true }, { playlist, video })
|
||||
}
|
|
@ -1,4 +1,11 @@
|
|||
import { buildFileLocale, escapeHTML, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '@peertube/peertube-core-utils'
|
||||
import {
|
||||
buildFileLocale,
|
||||
escapeHTML,
|
||||
getDefaultLocale,
|
||||
getDefaultRSSFeeds,
|
||||
is18nLocale,
|
||||
POSSIBLE_LOCALES
|
||||
} from '@peertube/peertube-core-utils'
|
||||
import { ActorImageType, HTMLServerConfig } from '@peertube/peertube-models'
|
||||
import { isTestOrDevInstance, root, sha256 } from '@peertube/peertube-node-utils'
|
||||
import { CONFIG } from '@server/initializers/config.js'
|
||||
|
@ -52,7 +59,8 @@ export class PageHtml {
|
|||
|
||||
ogType: 'website',
|
||||
twitterCard: 'summary_large_image',
|
||||
forbidIndexation: false
|
||||
forbidIndexation: false,
|
||||
rssFeeds: getDefaultRSSFeeds(WEBSERVER.URL, CONFIG.INSTANCE.NAME)
|
||||
}, {})
|
||||
|
||||
return customHTML
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { addQueryParams, escapeHTML } from '@peertube/peertube-core-utils'
|
||||
import { addQueryParams, escapeHTML, getDefaultRSSFeeds } from '@peertube/peertube-core-utils'
|
||||
import { HttpStatusCode, VideoPlaylistPrivacy } from '@peertube/peertube-models'
|
||||
import { toCompleteUUID } from '@server/helpers/custom-validators/misc.js'
|
||||
import { Memoize } from '@server/helpers/memoize.js'
|
||||
|
@ -8,7 +8,7 @@ import express from 'express'
|
|||
import validator from 'validator'
|
||||
import { CONFIG } from '../../../initializers/config.js'
|
||||
import { MEMOIZE_TTL, WEBSERVER } from '../../../initializers/constants.js'
|
||||
import { CommonEmbedHtml } from './common-embed-html.js'
|
||||
import { buildEmptyEmbedHTML } from './common.js'
|
||||
import { PageHtml } from './page-html.js'
|
||||
import { TagsHtml } from './tags-html.js'
|
||||
|
||||
|
@ -56,7 +56,7 @@ export class PlaylistHtml {
|
|||
const [ html, playlist ] = await Promise.all([ PageHtml.getEmbedHTML(), playlistPromise ])
|
||||
|
||||
if (!playlist || playlist.privacy === VideoPlaylistPrivacy.PRIVATE) {
|
||||
return CommonEmbedHtml.buildEmptyEmbedHTML({ html, playlist })
|
||||
return buildEmptyEmbedHTML({ html, playlist })
|
||||
}
|
||||
|
||||
return this.buildPlaylistHTML({
|
||||
|
@ -126,7 +126,9 @@ export class PlaylistHtml {
|
|||
twitterCard,
|
||||
|
||||
embed,
|
||||
oembedUrl: this.getOEmbedUrl(playlist, currentQuery)
|
||||
oembedUrl: this.getOEmbedUrl(playlist, currentQuery),
|
||||
|
||||
rssFeeds: getDefaultRSSFeeds(WEBSERVER.URL, CONFIG.INSTANCE.NAME)
|
||||
}, { playlist })
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import { CUSTOM_HTML_TAG_COMMENTS, EMBED_SIZE, WEBSERVER } from '../../../initia
|
|||
import { MVideo, MVideoPlaylist } from '../../../types/models/index.js'
|
||||
import { Hooks } from '../../plugins/hooks.js'
|
||||
|
||||
type Tags = {
|
||||
export type TagsOptions = {
|
||||
forbidIndexation: boolean
|
||||
|
||||
url?: string
|
||||
|
@ -46,6 +46,11 @@ type Tags = {
|
|||
}
|
||||
|
||||
oembedUrl?: string
|
||||
|
||||
rssFeeds?: {
|
||||
title: string
|
||||
url: string
|
||||
}[]
|
||||
}
|
||||
|
||||
type HookContext = {
|
||||
|
@ -81,7 +86,7 @@ export class TagsHtml {
|
|||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static async addTags (htmlStringPage: string, tagsValues: Tags, context: HookContext) {
|
||||
static async addTags (htmlStringPage: string, tagsValues: TagsOptions, context: HookContext) {
|
||||
const metaTags = {
|
||||
...this.generateOpenGraphMetaTagsOptions(tagsValues),
|
||||
...this.generateStandardMetaTagsOptions(tagsValues),
|
||||
|
@ -89,7 +94,7 @@ export class TagsHtml {
|
|||
}
|
||||
const schemaTags = await this.generateSchemaTagsOptions(tagsValues, context)
|
||||
|
||||
const { url, escapedTitle, oembedUrl, forbidIndexation, relMe } = tagsValues
|
||||
const { url, escapedTitle, oembedUrl, forbidIndexation, relMe, rssFeeds } = tagsValues
|
||||
|
||||
const oembedLinkTags: { type: string, href: string, escapedTitle: string }[] = []
|
||||
|
||||
|
@ -134,12 +139,16 @@ export class TagsHtml {
|
|||
tagsStr += `<meta name="robots" content="noindex" />`
|
||||
}
|
||||
|
||||
for (const rssLink of (rssFeeds || [])) {
|
||||
tagsStr += `<link rel="alternate" type="application/rss+xml" title="${escapeAttribute(rssLink.title)}" href="${rssLink.url}" />`
|
||||
}
|
||||
|
||||
return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsStr)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static generateOpenGraphMetaTagsOptions (tags: Tags) {
|
||||
static generateOpenGraphMetaTagsOptions (tags: TagsOptions) {
|
||||
if (!tags.ogType) return {}
|
||||
|
||||
const metaTags = {
|
||||
|
@ -171,7 +180,7 @@ export class TagsHtml {
|
|||
return metaTags
|
||||
}
|
||||
|
||||
static generateStandardMetaTagsOptions (tags: Tags) {
|
||||
static generateStandardMetaTagsOptions (tags: TagsOptions) {
|
||||
return {
|
||||
name: tags.escapedTitle,
|
||||
description: tags.escapedTruncatedDescription,
|
||||
|
@ -179,7 +188,7 @@ export class TagsHtml {
|
|||
}
|
||||
}
|
||||
|
||||
static generateTwitterCardMetaTagsOptions (tags: Tags) {
|
||||
static generateTwitterCardMetaTagsOptions (tags: TagsOptions) {
|
||||
if (!tags.twitterCard) return {}
|
||||
|
||||
const metaTags = {
|
||||
|
@ -207,7 +216,7 @@ export class TagsHtml {
|
|||
return metaTags
|
||||
}
|
||||
|
||||
static generateSchemaTagsOptions (tags: Tags, context: HookContext) {
|
||||
static generateSchemaTagsOptions (tags: TagsOptions, context: HookContext) {
|
||||
if (!tags.schemaType) return
|
||||
|
||||
if (tags.schemaType === 'ProfilePage') {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { addQueryParams, escapeHTML } from '@peertube/peertube-core-utils'
|
||||
import { addQueryParams, escapeHTML, getVideoWatchRSSFeeds } from '@peertube/peertube-core-utils'
|
||||
import { HttpStatusCode, VideoPrivacy } from '@peertube/peertube-models'
|
||||
import { toCompleteUUID } from '@server/helpers/custom-validators/misc.js'
|
||||
import { Memoize } from '@server/helpers/memoize.js'
|
||||
|
@ -10,7 +10,7 @@ import { VideoModel } from '../../../models/video/video.js'
|
|||
import { MVideo, MVideoThumbnail, MVideoThumbnailBlacklist } from '../../../types/models/index.js'
|
||||
import { getActivityStreamDuration } from '../../activitypub/activity.js'
|
||||
import { isVideoInPrivateDirectory } from '../../video-privacy.js'
|
||||
import { CommonEmbedHtml } from './common-embed-html.js'
|
||||
import { buildEmptyEmbedHTML } from './common.js'
|
||||
import { PageHtml } from './page-html.js'
|
||||
import { TagsHtml } from './tags-html.js'
|
||||
|
||||
|
@ -57,7 +57,7 @@ export class VideoHtml {
|
|||
const [ html, video ] = await Promise.all([ PageHtml.getEmbedHTML(), videoPromise ])
|
||||
|
||||
if (!video || isVideoInPrivateDirectory(video.privacy) || video.VideoBlacklist) {
|
||||
return CommonEmbedHtml.buildEmptyEmbedHTML({ html, video })
|
||||
return buildEmptyEmbedHTML({ html, video })
|
||||
}
|
||||
|
||||
return this.buildVideoHTML({
|
||||
|
@ -131,7 +131,9 @@ export class VideoHtml {
|
|||
|
||||
ogType,
|
||||
twitterCard,
|
||||
schemaType
|
||||
schemaType,
|
||||
|
||||
rssFeeds: getVideoWatchRSSFeeds(WEBSERVER.URL, CONFIG.INSTANCE.NAME, video)
|
||||
}, { video })
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue