import express from 'express'
-import { readFile } from 'fs-extra'
+import { pathExists, readFile } from 'fs-extra'
import { join } from 'path'
import validator from 'validator'
import { isTestOrDevInstance } from '@server/helpers/core-utils'
import { VideoModel } from '../models/video/video'
import { VideoChannelModel } from '../models/video/video-channel'
import { VideoPlaylistModel } from '../models/video/video-playlist'
-import { MAccountActor, MChannelActor } from '../types/models'
+import { MAccountActor, MChannelActor, MVideo, MVideoPlaylist } from '../types/models'
import { getActivityStreamDuration } from './activitypub/activity'
import { getBiggestActorImage } from './actor-image'
+import { Hooks } from './plugins/hooks'
import { ServerConfigManager } from './server-config-manager'
type Tags = {
}
}
+type HookContext = {
+ video?: MVideo
+ playlist?: MVideoPlaylist
+}
+
class ClientHtml {
private static htmlCache: { [path: string]: string } = {}
const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image'
const schemaType = 'VideoObject'
- customHtml = ClientHtml.addTags(customHtml, {
+ customHtml = await ClientHtml.addTags(customHtml, {
url,
originUrl,
escapedSiteName: escapeHTML(siteName),
ogType,
twitterCard,
schemaType
- })
+ }, { video })
return customHtml
}
const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary'
const schemaType = 'ItemList'
- customHtml = ClientHtml.addTags(customHtml, {
+ customHtml = await ClientHtml.addTags(customHtml, {
url,
originUrl,
escapedSiteName: escapeHTML(siteName),
ogType,
twitterCard,
schemaType
- })
+ }, { playlist: videoPlaylist })
return customHtml
}
const twitterCard = 'summary'
const schemaType = 'ProfilePage'
- customHtml = ClientHtml.addTags(customHtml, {
+ customHtml = await ClientHtml.addTags(customHtml, {
url,
originUrl,
escapedTitle: escapeHTML(title),
twitterCard,
schemaType,
disallowIndexation: !entity.Actor.isOwned()
- })
+ }, {})
return customHtml
}
}
private static async addAsyncPluginCSS (htmlStringPage: string) {
- const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
+ if (!pathExists(PLUGIN_GLOBAL_CSS_PATH)) {
+ logger.info('Plugin Global CSS file is not available (generation may still be in progress), ignoring it.')
+ return htmlStringPage
+ }
+
+ let globalCSSContent: Buffer
+
+ try {
+ globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
+ } catch (err) {
+ logger.error('Error retrieving the Plugin Global CSS file, ignoring it.', { err })
+ return htmlStringPage
+ }
+
if (globalCSSContent.byteLength === 0) return htmlStringPage
const fileHash = sha256(globalCSSContent)
return metaTags
}
- private static generateSchemaTags (tags: Tags) {
+ private static async generateSchemaTags (tags: Tags, context: HookContext) {
const schema = {
'@context': 'http://schema.org',
'@type': tags.schemaType,
schema['uploadDate'] = tags.embed.createdAt
if (tags.embed.duration) schema['duration'] = tags.embed.duration
- if (tags.embed.views) schema['iterationCount'] = tags.embed.views
schema['thumbnailUrl'] = tags.image.url
schema['contentUrl'] = tags.url
}
- return schema
+ return Hooks.wrapObject(schema, 'filter:html.client.json-ld.result', context)
}
- private static addTags (htmlStringPage: string, tagsValues: Tags) {
+ private static async addTags (htmlStringPage: string, tagsValues: Tags, context: HookContext) {
const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues)
const standardMetaTags = this.generateStandardMetaTags(tagsValues)
const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues)
- const schemaTags = this.generateSchemaTags(tagsValues)
+ const schemaTags = await this.generateSchemaTags(tagsValues, context)
const { url, escapedTitle, embed, originUrl, disallowIndexation } = tagsValues