moved og title and description sanitization to model

This commit is contained in:
bill bittner 2017-12-06 14:41:03 -08:00
parent 0bf162d832
commit ac610af6af
2 changed files with 40 additions and 30 deletions

View file

@ -12,36 +12,30 @@ module.exports = {
ga('send', 'pageview');</script>`; ga('send', 'pageview');</script>`;
return new Handlebars.SafeString(gaCode); return new Handlebars.SafeString(gaCode);
}, },
addOpenGraph ({ title, contentType, description, thumbnail, showUrl, source }) { addOpenGraph ({ ogTitle, contentType, ogDescription, thumbnail, showUrl, source }) {
if (!title || title.trim() === '') { const ogTitleTag = `<meta property="og:title" content="${ogTitle}" >`;
title = 'Spee.ch'; const ogUrlTag = `<meta property="og:url" content="${showUrl}" >`;
} const ogSiteNameTag = `<meta property="og:site_name" content="Spee.ch" >`;
if (!description || description.trim() === '') { const ogDescriptionTag = `<meta property="og:description" content="${ogDescription}" >`;
description = 'Open-source, decentralized image and video sharing.'; const ogImageWidthTag = '<meta property="og:image:width" content="600" >';
} const ogImageHeightTag = '<meta property="og:image:height" content="315" >';
const ogTitle = `<meta property="og:title" content="${title}" >`; const basicTags = `${ogTitleTag} ${ogUrlTag} ${ogSiteNameTag} ${ogDescriptionTag} ${ogImageWidthTag} ${ogImageHeightTag}`;
const ogUrl = `<meta property="og:url" content="${showUrl}" >`; let ogImageTag = `<meta property="og:image" content="${source}" >`;
const ogSiteName = `<meta property="og:site_name" content="Spee.ch" >`; let ogImageTypeTag = `<meta property="og:image:type" content="${contentType}" >`;
const ogDescription = `<meta property="og:description" content="${description}" >`; let ogTypeTag = `<meta property="og:type" content="article" >`;
const ogImageWidth = '<meta property="og:image:width" content="600" >';
const ogImageHeight = '<meta property="og:image:height" content="315" >';
const basicTags = `${ogTitle} ${ogUrl} ${ogSiteName} ${ogDescription} ${ogImageWidth} ${ogImageHeight}`;
let ogImage = `<meta property="og:image" content="${source}" >`;
let ogImageType = `<meta property="og:image:type" content="${contentType}" >`;
let ogType = `<meta property="og:type" content="article" >`;
if (contentType === 'video/mp4') { if (contentType === 'video/mp4') {
const ogVideo = `<meta property="og:video" content="${source}" >`; const ogVideoTag = `<meta property="og:video" content="${source}" >`;
const ogVideoSecureUrl = `<meta property="og:video:secure_url" content="${source}" >`; const ogVideoSecureUrlTag = `<meta property="og:video:secure_url" content="${source}" >`;
const ogVideoType = `<meta property="og:video:type" content="${contentType}" >`; const ogVideoTypeTag = `<meta property="og:video:type" content="${contentType}" >`;
ogImage = `<meta property="og:image" content="${thumbnail}" >`; ogImageTag = `<meta property="og:image" content="${thumbnail}" >`;
ogImageType = `<meta property="og:image:type" content="image/png" >`; // ogImageTypeTag = `<meta property="og:image:type" content="image/png" >`; // note: might not be png. needs to check if gif or jpg etc depending on thubmnail
ogType = `<meta property="og:type" content="video" >`; ogTypeTag = `<meta property="og:type" content="video" >`;
return new Handlebars.SafeString(`${basicTags} ${ogImage} ${ogImageType} ${ogType} ${ogVideo} ${ogVideoSecureUrl} ${ogVideoType}`); return new Handlebars.SafeString(`${basicTags} ${ogImageTag} ${ogImageTypeTag} ${ogTypeTag} ${ogVideoTag} ${ogVideoSecureUrlTag} ${ogVideoTypeTag}`);
} else { } else {
if (contentType === 'image/gif') { if (contentType === 'image/gif') {
ogType = `<meta property="og:type" content="video.other" >`; ogTypeTag = `<meta property="og:type" content="video.other" >`;
}; };
return new Handlebars.SafeString(`${basicTags} ${ogImage} ${ogImageType} ${ogType}`); return new Handlebars.SafeString(`${basicTags} ${ogImageTag} ${ogImageTypeTag} ${ogTypeTag}`);
} }
}, },
addTwitterCard ({ contentType, source, embedUrl, directFileUrl }) { addTwitterCard ({ contentType, source, embedUrl, directFileUrl }) {

View file

@ -1,6 +1,8 @@
const logger = require('winston'); const logger = require('winston');
const { returnShortId } = require('../helpers/sequelizeHelpers.js'); const { returnShortId } = require('../helpers/sequelizeHelpers.js');
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png'; const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
const DEFAULT_TITLE = '';
const DEFAULT_DESCRIPTION = '';
function determineFileExtensionFromContentType (contentType) { function determineFileExtensionFromContentType (contentType) {
switch (contentType) { switch (contentType) {
@ -19,11 +21,23 @@ function determineFileExtensionFromContentType (contentType) {
} }
}; };
function determineThumbnail (storedThumbnail, defaultThumbnail) { function ifEmptyReturnOther (value, replacement) {
if (storedThumbnail === '') { if (value === '') {
return defaultThumbnail; return replacement;
} }
return storedThumbnail; return value;
}
function determineThumbnail (storedThumbnail, defaultThumbnail) {
return ifEmptyReturnOther(storedThumbnail, defaultThumbnail);
};
function determineOgTitle (storedTitle, defaultTitle) {
return ifEmptyReturnOther(storedTitle, defaultTitle);
};
function determineOgDescription (storedDescription, defaultDescription) {
return ifEmptyReturnOther(storedDescription, defaultDescription);
}; };
function addOpengraphDataToClaim (claim) { function addOpengraphDataToClaim (claim) {
@ -31,6 +45,8 @@ function addOpengraphDataToClaim (claim) {
claim['showUrl'] = `https://spee.ch/${claim.claimId}/${claim.name}`; claim['showUrl'] = `https://spee.ch/${claim.claimId}/${claim.name}`;
claim['source'] = `https://spee.ch/${claim.claimId}/${claim.name}.${claim.fileExt}`; claim['source'] = `https://spee.ch/${claim.claimId}/${claim.name}.${claim.fileExt}`;
claim['directFileUrl'] = `https://spee.ch/${claim.claimId}/${claim.name}.${claim.fileExt}`; claim['directFileUrl'] = `https://spee.ch/${claim.claimId}/${claim.name}.${claim.fileExt}`;
claim['ogTitle'] = determineOgTitle(claim.title, DEFAULT_TITLE);
claim['ogDescription'] = determineOgDescription(claim.description, DEFAULT_DESCRIPTION);
}; };
function prepareClaimData (claimData) { function prepareClaimData (claimData) {