X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fclient-html.ts;h=84b4ca97def8477dd034e2d85a7366e36c4e8a7e;hb=10ef089102f2225c5ec3ed426bc612e4f2bc8655;hp=6ddaa82c83725486da80f39ca58cb121707ee0de;hpb=dc48fdbe68e9dd3a3a6028181e61d8595d98e654;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/client-html.ts b/server/lib/client-html.ts index 6ddaa82c8..84b4ca97d 100644 --- a/server/lib/client-html.ts +++ b/server/lib/client-html.ts @@ -1,13 +1,15 @@ -import * as express from 'express' +import express from 'express' import { readFile } from 'fs-extra' import { join } from 'path' import validator from 'validator' +import { escapeHTML } from '@shared/core-utils/renderer' +import { HTMLServerConfig } from '@shared/models' import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n' -import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '../../shared/models/http/http-error-codes' import { VideoPlaylistPrivacy, VideoPrivacy } from '../../shared/models/videos' import { isTestInstance, sha256 } from '../helpers/core-utils' -import { escapeHTML } from '@shared/core-utils/renderer' import { logger } from '../helpers/logger' +import { mdToPlainText } from '../helpers/markdown' import { CONFIG } from '../initializers/config' import { ACCEPT_HEADERS, @@ -19,11 +21,13 @@ import { WEBSERVER } from '../initializers/constants' import { AccountModel } from '../models/account/account' +import { getActivityStreamDuration } from '../models/video/formatter/video-format-utils' import { VideoModel } from '../models/video/video' import { VideoChannelModel } from '../models/video/video-channel' -import { getActivityStreamDuration } from '../models/video/video-format-utils' import { VideoPlaylistModel } from '../models/video/video-playlist' import { MAccountActor, MChannelActor } from '../types/models' +import { ServerConfigManager } from './server-config-manager' +import { toCompleteUUID } from '@server/helpers/custom-validators/misc' type Tags = { ogType: string @@ -40,6 +44,8 @@ type Tags = { originUrl: string description: string + disallowIndexation?: boolean + embed?: { url: string createdAt: string @@ -75,7 +81,9 @@ class ClientHtml { return customHtml } - static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) { + static async getWatchHTMLPage (videoIdArg: string, req: express.Request, res: express.Response) { + const videoId = toCompleteUUID(videoIdArg) + // Let Angular application handle errors if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) { res.status(HttpStatusCode.NOT_FOUND_404) @@ -94,13 +102,13 @@ class ClientHtml { } let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name)) - customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description)) + customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(video.description)) const url = WEBSERVER.URL + video.getWatchStaticPath() const originUrl = video.url const title = escapeHTML(video.name) const siteName = escapeHTML(CONFIG.INSTANCE.NAME) - const description = escapeHTML(video.description) + const description = mdToPlainText(video.description) const image = { url: WEBSERVER.URL + video.getPreviewStaticPath() @@ -133,7 +141,9 @@ class ClientHtml { return customHtml } - static async getWatchPlaylistHTMLPage (videoPlaylistId: string, req: express.Request, res: express.Response) { + static async getWatchPlaylistHTMLPage (videoPlaylistIdArg: string, req: express.Request, res: express.Response) { + const videoPlaylistId = toCompleteUUID(videoPlaylistIdArg) + // Let Angular application handle errors if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) { res.status(HttpStatusCode.NOT_FOUND_404) @@ -152,13 +162,13 @@ class ClientHtml { } let customHtml = ClientHtml.addTitleTag(html, escapeHTML(videoPlaylist.name)) - customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(videoPlaylist.description)) + customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(videoPlaylist.description)) - const url = videoPlaylist.getWatchUrl() + const url = WEBSERVER.URL + videoPlaylist.getWatchStaticPath() const originUrl = videoPlaylist.url const title = escapeHTML(videoPlaylist.name) const siteName = escapeHTML(CONFIG.INSTANCE.NAME) - const description = escapeHTML(videoPlaylist.description) + const description = mdToPlainText(videoPlaylist.description) const image = { url: videoPlaylist.getThumbnailUrl() @@ -195,11 +205,22 @@ class ClientHtml { } static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { - return this.getAccountOrChannelHTMLPage(() => AccountModel.loadByNameWithHost(nameWithHost), req, res) + const accountModelPromise = AccountModel.loadByNameWithHost(nameWithHost) + return this.getAccountOrChannelHTMLPage(() => accountModelPromise, req, res) } static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { - return this.getAccountOrChannelHTMLPage(() => VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost), req, res) + const videoChannelModelPromise = VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost) + return this.getAccountOrChannelHTMLPage(() => videoChannelModelPromise, req, res) + } + + static async getActorHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { + const [ account, channel ] = await Promise.all([ + AccountModel.loadByNameWithHost(nameWithHost), + VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost) + ]) + + return this.getAccountOrChannelHTMLPage(() => Promise.resolve(account || channel), req, res) } static async getEmbedHTML () { @@ -208,11 +229,14 @@ class ClientHtml { if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] const buffer = await readFile(path) + const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig() let html = buffer.toString() html = await ClientHtml.addAsyncPluginCSS(html) html = ClientHtml.addCustomCSS(html) html = ClientHtml.addTitleTag(html) + html = ClientHtml.addDescriptionTag(html) + html = ClientHtml.addServerConfig(html, serverConfig) ClientHtml.htmlCache[path] = html @@ -236,13 +260,13 @@ class ClientHtml { } let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName())) - customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(entity.description)) + customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(entity.description)) const url = entity.getLocalUrl() const originUrl = entity.Actor.url const siteName = escapeHTML(CONFIG.INSTANCE.NAME) const title = escapeHTML(entity.getDisplayName()) - const description = escapeHTML(entity.description) + const description = mdToPlainText(entity.description) const image = { url: entity.Actor.getAvatarUrl(), @@ -263,7 +287,8 @@ class ClientHtml { image, ogType, twitterCard, - schemaType + schemaType, + disallowIndexation: !entity.Actor.isOwned() }) return customHtml @@ -274,14 +299,15 @@ class ClientHtml { if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] const buffer = await readFile(path) + const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig() let html = buffer.toString() - if (paramLang) html = ClientHtml.addHtmlLang(html, paramLang) html = ClientHtml.addManifestContentHash(html) html = ClientHtml.addFaviconContentHash(html) html = ClientHtml.addLogoContentHash(html) html = ClientHtml.addCustomCSS(html) + html = ClientHtml.addServerConfig(html, serverConfig) html = await ClientHtml.addAsyncPluginCSS(html) ClientHtml.htmlCache[path] = html @@ -354,6 +380,14 @@ class ClientHtml { return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag) } + private static addServerConfig (htmlStringPage: string, serverConfig: HTMLServerConfig) { + // Stringify the JSON object, and then stringify the string object so we can inject it into the HTML + const serverConfigString = JSON.stringify(JSON.stringify(serverConfig)) + const configScriptTag = `` + + return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.SERVER_CONFIG, configScriptTag) + } + private static async addAsyncPluginCSS (htmlStringPage: string) { const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH) if (globalCSSContent.byteLength === 0) return htmlStringPage @@ -378,7 +412,7 @@ class ClientHtml { } metaTags['og:url'] = tags.url - metaTags['og:description'] = tags.description + metaTags['og:description'] = mdToPlainText(tags.description) if (tags.embed) { metaTags['og:video:url'] = tags.embed.url @@ -394,7 +428,7 @@ class ClientHtml { private static generateStandardMetaTags (tags: Tags) { return { name: tags.title, - description: tags.description, + description: mdToPlainText(tags.description), image: tags.image.url } } @@ -457,7 +491,7 @@ class ClientHtml { const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues) const schemaTags = this.generateSchemaTags(tagsValues) - const { url, title, embed, originUrl } = tagsValues + const { url, title, embed, originUrl, disallowIndexation } = tagsValues const oembedLinkTags: { type: string, href: string, title: string }[] = [] @@ -505,6 +539,10 @@ class ClientHtml { // SEO, use origin URL tagsString += `` + if (disallowIndexation) { + tagsString += `` + } + return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString) } } @@ -516,18 +554,17 @@ function sendHTML (html: string, res: express.Response) { } async function serveIndexHTML (req: express.Request, res: express.Response) { - if (req.accepts(ACCEPT_HEADERS) === 'html' || - !req.headers.accept) { + if (req.accepts(ACCEPT_HEADERS) === 'html' || !req.headers.accept) { try { await generateHTMLPage(req, res, req.params.language) return } catch (err) { logger.error('Cannot generate HTML page.', err) - return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500) + return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end() } } - return res.sendStatus(HttpStatusCode.NOT_ACCEPTABLE_406) + return res.status(HttpStatusCode.NOT_ACCEPTABLE_406).end() } // ---------------------------------------------------------------------------