diff --git a/helpers/handlebarsHelpers.js b/helpers/handlebarsHelpers.js index 8ae90462..2fe869d8 100644 --- a/helpers/handlebarsHelpers.js +++ b/helpers/handlebarsHelpers.js @@ -1,5 +1,57 @@ const Handlebars = require('handlebars'); -const { site, analytics } = require('../config/speechConfig.js'); +const { site, analytics, claim: claimDefaults } = require('../config/speechConfig.js'); + +function determineOgTitle (storedTitle, defaultTitle) { + return ifEmptyReturnOther(storedTitle, defaultTitle); +}; + +function determineOgDescription (storedDescription, defaultDescription) { + return ifEmptyReturnOther(storedDescription, defaultDescription); +}; + +function ifEmptyReturnOther (value, replacement) { + if (value === '') { + return replacement; + } + return value; +} + +function determineContentTypeFromFileExtension (fileExtension) { + switch (fileExtension) { + case 'jpeg': + case 'jpg': + return 'image/jpeg'; + case 'png': + return 'image/png'; + case 'gif': + return 'image/gif'; + case 'mp4': + return 'video/mp4'; + default: + return 'image/jpeg'; + } +}; + +function determineOgThumbnailContentType (thumbnail) { + if (thumbnail) { + if (thumbnail.lastIndexOf('.') !== -1) { + return determineContentTypeFromFileExtension(thumbnail.substring(thumbnail.lastIndexOf('.'))); + } + } + return ''; +} + +function createOpenGraphDataFromClaim (claim, defaultTitle, defaultDescription) { + let openGraphData = {}; + openGraphData['embedUrl'] = `${site.host}/${claim.claimId}/${claim.name}`; + openGraphData['showUrl'] = `${site.host}/${claim.claimId}/${claim.name}`; + openGraphData['source'] = `${site.host}/${claim.claimId}/${claim.name}.${claim.fileExt}`; + openGraphData['directFileUrl'] = `${site.host}/${claim.claimId}/${claim.name}.${claim.fileExt}`; + openGraphData['ogTitle'] = determineOgTitle(claim.title, defaultTitle); + openGraphData['ogDescription'] = determineOgDescription(claim.description, defaultDescription); + openGraphData['ogThumbnailContentType'] = determineOgThumbnailContentType(claim.thumbnail); + return openGraphData; +}; module.exports = { placeCommonHeaderTags () { @@ -16,7 +68,8 @@ module.exports = { ga('send', 'pageview');`; return new Handlebars.SafeString(gaCode); }, - addOpenGraph ({ ogTitle, contentType, ogDescription, thumbnail, showUrl, source, ogThumbnailContentType }) { + addOpenGraph (claim) { + const { ogTitle, contentType, ogDescription, thumbnail, showUrl, source, ogThumbnailContentType } = createOpenGraphDataFromClaim(claim, claimDefaults.defaultTitle, claimDefaults.defaultDescription); const ogTitleTag = ``; const ogUrlTag = ``; const ogSiteNameTag = ``; @@ -42,7 +95,8 @@ module.exports = { return new Handlebars.SafeString(`${basicTags} ${ogImageTag} ${ogImageTypeTag} ${ogTypeTag}`); } }, - addTwitterCard ({ contentType, source, embedUrl, directFileUrl }) { + addTwitterCard (claim) { + const { contentType, embedUrl, directFileUrl } = createOpenGraphDataFromClaim(claim, claimDefaults.defaultTitle, claimDefaults.defaultDescription); const basicTwitterTags = ``; if (contentType === 'video/mp4') { const twitterName = ''; diff --git a/models/claim.js b/models/claim.js index 2bc066d1..4a8286dc 100644 --- a/models/claim.js +++ b/models/claim.js @@ -1,8 +1,7 @@ const logger = require('winston'); const { returnShortId } = require('../helpers/sequelizeHelpers.js'); -const { claim, site } = require('../config/speechConfig.js'); -const { defaultTitle, defaultThumbnail, defaultDescription } = claim; -const { host } = site; +const { claim } = require('../config/speechConfig.js'); +const { defaultThumbnail } = claim; function determineFileExtensionFromContentType (contentType) { switch (contentType) { @@ -21,68 +20,17 @@ function determineFileExtensionFromContentType (contentType) { } }; -function determineContentTypeFromFileExtension (fileExtension) { - switch (fileExtension) { - case 'jpeg': - case 'jpg': - return 'image/jpeg'; - case 'png': - return 'image/png'; - case 'gif': - return 'image/gif'; - case 'mp4': - return 'video/mp4'; - default: - logger.debug('setting unknown file type as type image/jpeg'); - return 'image/jpeg'; - } -}; - -function ifEmptyReturnOther (value, replacement) { - if (value === '') { - return replacement; - } - 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 determineOgThumbnailContentType (thumbnail) { - if (thumbnail) { - if (thumbnail.lastIndexOf('.') !== -1) { - return determineContentTypeFromFileExtension(thumbnail.substring(thumbnail.lastIndexOf('.'))); - } + if (storedThumbnail === '') { + return defaultThumbnail; } - return ''; -} - -function addOpengraphDataToClaim (claim) { - claim['host'] = host; - claim['embedUrl'] = `${host}/${claim.claimId}/${claim.name}`; - claim['showUrl'] = `${host}/${claim.claimId}/${claim.name}`; - claim['source'] = `${host}/${claim.claimId}/${claim.name}.${claim.fileExt}`; - claim['directFileUrl'] = `${host}/${claim.claimId}/${claim.name}.${claim.fileExt}`; - claim['ogTitle'] = determineOgTitle(claim.title, defaultTitle); - claim['ogDescription'] = determineOgDescription(claim.description, defaultDescription); - claim['ogThumbnailContentType'] = determineOgThumbnailContentType(claim.thumbnail); - return claim; + return storedThumbnail; }; function prepareClaimData (claim) { // logger.debug('preparing claim data based on resolved data:', claim); claim['thumbnail'] = determineThumbnail(claim.thumbnail, defaultThumbnail); claim['fileExt'] = determineFileExtensionFromContentType(claim.contentType); - claim = addOpengraphDataToClaim(claim); return claim; };