From 0bf162d8326c611c6a6bbea91fbf104952333882 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 6 Dec 2017 14:23:28 -0800 Subject: [PATCH] moved open graph data preparation down to model layer --- helpers/handlebarsHelpers.js | 14 +++++++------- helpers/serveHelpers.js | 8 -------- models/claim.js | 31 +++++++++++++++++++++---------- views/layouts/show.handlebars | 4 ++-- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/helpers/handlebarsHelpers.js b/helpers/handlebarsHelpers.js index b2560c03..d2b6207d 100644 --- a/helpers/handlebarsHelpers.js +++ b/helpers/handlebarsHelpers.js @@ -12,7 +12,7 @@ module.exports = { ga('send', 'pageview');`; return new Handlebars.SafeString(gaCode); }, - addOpenGraph (title, mimeType, showUrl, source, description, thumbnail) { + addOpenGraph ({ title, contentType, description, thumbnail, showUrl, source }) { if (!title || title.trim() === '') { title = 'Spee.ch'; } @@ -27,26 +27,26 @@ module.exports = { const ogImageHeight = ''; const basicTags = `${ogTitle} ${ogUrl} ${ogSiteName} ${ogDescription} ${ogImageWidth} ${ogImageHeight}`; let ogImage = ``; - let ogImageType = ``; + let ogImageType = ``; let ogType = ``; - if (mimeType === 'video/mp4') { + if (contentType === 'video/mp4') { const ogVideo = ``; const ogVideoSecureUrl = ``; - const ogVideoType = ``; + const ogVideoType = ``; ogImage = ``; ogImageType = ``; ogType = ``; return new Handlebars.SafeString(`${basicTags} ${ogImage} ${ogImageType} ${ogType} ${ogVideo} ${ogVideoSecureUrl} ${ogVideoType}`); } else { - if (mimeType === 'image/gif') { + if (contentType === 'image/gif') { ogType = ``; }; return new Handlebars.SafeString(`${basicTags} ${ogImage} ${ogImageType} ${ogType}`); } }, - addTwitterCard (mimeType, source, embedUrl, directFileUrl) { + addTwitterCard ({ contentType, source, embedUrl, directFileUrl }) { const basicTwitterTags = ``; - if (mimeType === 'video/mp4') { + if (contentType === 'video/mp4') { const twitterName = ''; const twitterPlayer = ``; const twitterPlayerWidth = ''; diff --git a/helpers/serveHelpers.js b/helpers/serveHelpers.js index 869f98f4..a62e7d38 100644 --- a/helpers/serveHelpers.js +++ b/helpers/serveHelpers.js @@ -23,12 +23,4 @@ module.exports = { const openGraphInfo = module.exports.createOpenGraphInfo(claimInfo); res.status(200).render('showLite', { layout: 'showlite', claimInfo, shortId, openGraphInfo }); }, - createOpenGraphInfo ({ claimId, name, fileExt }) { - return { - embedUrl : `https://spee.ch/embed/${claimId}/${name}`, - showUrl : `https://spee.ch/${claimId}/${name}`, - source : `https://spee.ch/${claimId}/${name}.${fileExt}`, - directFileUrl: `https://spee.ch/${claimId}/${name}.${fileExt}`, - }; - }, }; diff --git a/models/claim.js b/models/claim.js index 092b0e01..52c385d1 100644 --- a/models/claim.js +++ b/models/claim.js @@ -26,6 +26,20 @@ function determineThumbnail (storedThumbnail, defaultThumbnail) { return storedThumbnail; }; +function addOpengraphDataToClaim (claim) { + claim['embedUrl'] = `https://spee.ch/embed/${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['directFileUrl'] = `https://spee.ch/${claim.claimId}/${claim.name}.${claim.fileExt}`; +}; + +function prepareClaimData (claimData) { + claimData['thumbnail'] = determineThumbnail(claimData.thumbnail, DEFAULT_THUMBNAIL); + claimData['fileExt'] = determineFileExtensionFromContentType(claimData.contentType); + claimData = addOpengraphDataToClaim(claimData); + return claimData; +}; + module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => { const Claim = sequelize.define( 'Claim', @@ -337,18 +351,15 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => { .findAll({ where: { name, claimId }, }) - .then(result => { - if (!result) { - return resolve(null); - }; - switch (result.length) { + .then(claimArray => { + switch (claimArray.length) { + case 0: + return resolve(null); case 1: - result[0].dataValues.thumbnail = determineThumbnail(result[0].dataValues.thumbnail, DEFAULT_THUMBNAIL); - result[0].dataValues.fileExt = determineFileExtensionFromContentType(result[0].dataValues.contentType); - return resolve(result[0]); + return resolve(prepareClaimData(claimArray[0].dataValues)); default: - logger.warn(`more than one entry matches that name (${name}) and claimID (${claimId})`); - return resolve(result[0]); + logger.error(`more than one entry matches that name (${name}) and claimID (${claimId})`); + return resolve(prepareClaimData(claimArray[0].dataValues)); } }) .catch(error => { diff --git a/views/layouts/show.handlebars b/views/layouts/show.handlebars index fbf93b80..e0299b8f 100644 --- a/views/layouts/show.handlebars +++ b/views/layouts/show.handlebars @@ -10,8 +10,8 @@ {{#unless claimInfo.nsfw}} - {{{addTwitterCard claimInfo.contentType openGraphInfo.source openGraphInfo.embedUrl openGraphInfo.directFileUrl}}} - {{{addOpenGraph claimInfo.title claimInfo.contentType openGraphInfo.showUrl openGraphInfo.source claimInfo.description claimInfo.thumbnail}}} + {{{addTwitterCard claimInfo }}} + {{{addOpenGraph claimInfo }}} {{/unless}}