From ac610af6af2dd97ade25328e9be3af23d944f96c Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 6 Dec 2017 14:41:03 -0800 Subject: [PATCH] moved og title and description sanitization to model --- helpers/handlebarsHelpers.js | 46 ++++++++++++++++-------------------- models/claim.js | 24 +++++++++++++++---- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/helpers/handlebarsHelpers.js b/helpers/handlebarsHelpers.js index d2b6207d..df8f9732 100644 --- a/helpers/handlebarsHelpers.js +++ b/helpers/handlebarsHelpers.js @@ -12,36 +12,30 @@ module.exports = { ga('send', 'pageview');`; return new Handlebars.SafeString(gaCode); }, - addOpenGraph ({ title, contentType, description, thumbnail, showUrl, source }) { - if (!title || title.trim() === '') { - title = 'Spee.ch'; - } - if (!description || description.trim() === '') { - description = 'Open-source, decentralized image and video sharing.'; - } - const ogTitle = ``; - const ogUrl = ``; - const ogSiteName = ``; - const ogDescription = ``; - const ogImageWidth = ''; - const ogImageHeight = ''; - const basicTags = `${ogTitle} ${ogUrl} ${ogSiteName} ${ogDescription} ${ogImageWidth} ${ogImageHeight}`; - let ogImage = ``; - let ogImageType = ``; - let ogType = ``; + addOpenGraph ({ ogTitle, contentType, ogDescription, thumbnail, showUrl, source }) { + const ogTitleTag = ``; + const ogUrlTag = ``; + const ogSiteNameTag = ``; + const ogDescriptionTag = ``; + const ogImageWidthTag = ''; + const ogImageHeightTag = ''; + const basicTags = `${ogTitleTag} ${ogUrlTag} ${ogSiteNameTag} ${ogDescriptionTag} ${ogImageWidthTag} ${ogImageHeightTag}`; + let ogImageTag = ``; + let ogImageTypeTag = ``; + let ogTypeTag = ``; if (contentType === 'video/mp4') { - const ogVideo = ``; - const ogVideoSecureUrl = ``; - const ogVideoType = ``; - ogImage = ``; - ogImageType = ``; - ogType = ``; - return new Handlebars.SafeString(`${basicTags} ${ogImage} ${ogImageType} ${ogType} ${ogVideo} ${ogVideoSecureUrl} ${ogVideoType}`); + const ogVideoTag = ``; + const ogVideoSecureUrlTag = ``; + const ogVideoTypeTag = ``; + ogImageTag = ``; + // ogImageTypeTag = ``; // note: might not be png. needs to check if gif or jpg etc depending on thubmnail + ogTypeTag = ``; + return new Handlebars.SafeString(`${basicTags} ${ogImageTag} ${ogImageTypeTag} ${ogTypeTag} ${ogVideoTag} ${ogVideoSecureUrlTag} ${ogVideoTypeTag}`); } else { if (contentType === 'image/gif') { - ogType = ``; + ogTypeTag = ``; }; - return new Handlebars.SafeString(`${basicTags} ${ogImage} ${ogImageType} ${ogType}`); + return new Handlebars.SafeString(`${basicTags} ${ogImageTag} ${ogImageTypeTag} ${ogTypeTag}`); } }, addTwitterCard ({ contentType, source, embedUrl, directFileUrl }) { diff --git a/models/claim.js b/models/claim.js index 52c385d1..a8cce414 100644 --- a/models/claim.js +++ b/models/claim.js @@ -1,6 +1,8 @@ const logger = require('winston'); const { returnShortId } = require('../helpers/sequelizeHelpers.js'); const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png'; +const DEFAULT_TITLE = ''; +const DEFAULT_DESCRIPTION = ''; function determineFileExtensionFromContentType (contentType) { switch (contentType) { @@ -19,11 +21,23 @@ function determineFileExtensionFromContentType (contentType) { } }; -function determineThumbnail (storedThumbnail, defaultThumbnail) { - if (storedThumbnail === '') { - return defaultThumbnail; +function ifEmptyReturnOther (value, replacement) { + if (value === '') { + 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) { @@ -31,6 +45,8 @@ function addOpengraphDataToClaim (claim) { claim['showUrl'] = `https://spee.ch/${claim.claimId}/${claim.name}`; claim['source'] = `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) {