From 64626dedd16efb5bd4f37bc6b38d710933fafa32 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Thu, 14 Dec 2017 22:22:37 +0100 Subject: [PATCH] Moved social utility to lib/social refs #9178 - not 100% sure about this, but i think it makes right now the most sense - we have already a url service and creating another lib/url is confusing at the moment - i'll copy the last utility `makeAbsoluteUrls` to the url service for now - see next commit for explanation (!) --- core/server/data/meta/schema.js | 10 +++++----- core/server/data/meta/structured_data.js | 6 +++--- core/server/helpers/facebook_url.js | 2 +- core/server/helpers/proxy.js | 2 +- core/server/helpers/twitter_url.js | 2 +- core/server/lib/social/index.js | 7 +++++++ .../social-urls.js => lib/social/urls.js} | 4 ++-- .../social/urls_spec.js} | 20 +++++++++---------- core/test/unit/services/mail/utils_spec.js | 4 ++-- 9 files changed, 31 insertions(+), 26 deletions(-) create mode 100644 core/server/lib/social/index.js rename core/server/{utils/social-urls.js => lib/social/urls.js} (68%) rename core/test/unit/{utils/social-urls_spec.js => lib/social/urls_spec.js} (51%) diff --git a/core/server/data/meta/schema.js b/core/server/data/meta/schema.js index c82493d816..93a86f2d63 100644 --- a/core/server/data/meta/schema.js +++ b/core/server/data/meta/schema.js @@ -1,6 +1,6 @@ var config = require('../../config'), escapeExpression = require('../../services/themes/engine').escapeExpression, - socialUrls = require('../../utils/social-urls'), + social = require('../../lib/social'), _ = require('lodash'); function schemaImageObject(metaDataVal) { @@ -43,20 +43,20 @@ function trimSameAs(data, context) { sameAs.push(escapeExpression(data.post.author.website)); } if (data.post.author.facebook) { - sameAs.push(socialUrls.facebookUrl(data.post.author.facebook)); + sameAs.push(social.urls.facebook(data.post.author.facebook)); } if (data.post.author.twitter) { - sameAs.push(socialUrls.twitterUrl(data.post.author.twitter)); + sameAs.push(social.urls.twitter(data.post.author.twitter)); } } else if (context === 'author') { if (data.author.website) { sameAs.push(escapeExpression(data.author.website)); } if (data.author.facebook) { - sameAs.push(socialUrls.facebookUrl(data.author.facebook)); + sameAs.push(social.urls.facebook(data.author.facebook)); } if (data.author.twitter) { - sameAs.push(socialUrls.twitterUrl(data.author.twitter)); + sameAs.push(social.urls.twitter(data.author.twitter)); } } diff --git a/core/server/data/meta/structured_data.js b/core/server/data/meta/structured_data.js index 91b87f829a..ffa7cf99e6 100644 --- a/core/server/data/meta/structured_data.js +++ b/core/server/data/meta/structured_data.js @@ -1,4 +1,4 @@ -var socialUrls = require('../../utils/social-urls'); +var social = require('../../lib/social'); function getStructuredData(metaData) { var structuredData, @@ -21,8 +21,8 @@ function getStructuredData(metaData) { 'article:published_time': metaData.publishedDate, 'article:modified_time': metaData.modifiedDate, 'article:tag': metaData.keywords, - 'article:publisher': metaData.blog.facebook ? socialUrls.facebookUrl(metaData.blog.facebook) : undefined, - 'article:author': metaData.authorFacebook ? socialUrls.facebookUrl(metaData.authorFacebook) : undefined, + 'article:publisher': metaData.blog.facebook ? social.urls.facebook(metaData.blog.facebook) : undefined, + 'article:author': metaData.authorFacebook ? social.urls.facebook(metaData.authorFacebook) : undefined, 'twitter:card': card, 'twitter:title': metaData.twitterTitle || metaData.metaTitle, 'twitter:description': metaData.twitterDescription || metaData.excerpt || metaData.metaDescription, diff --git a/core/server/helpers/facebook_url.js b/core/server/helpers/facebook_url.js index f8ef2e663a..2b9044d15f 100644 --- a/core/server/helpers/facebook_url.js +++ b/core/server/helpers/facebook_url.js @@ -14,7 +14,7 @@ module.exports = function facebook_url(username, options) { // eslint-disable-li } if (username) { - return socialUrls.facebookUrl(username); + return socialUrls.facebook(username); } return null; diff --git a/core/server/helpers/proxy.js b/core/server/helpers/proxy.js index aae1b9ff1c..2080c33faa 100644 --- a/core/server/helpers/proxy.js +++ b/core/server/helpers/proxy.js @@ -62,7 +62,7 @@ module.exports = { templates: require('./template'), // Various utils, needs cleaning up / simplifying - socialUrls: require('../utils/social-urls'), + socialUrls: require('../lib/social/urls'), blogIcon: require('../lib/image/blog-icon'), url: require('../services/url').utils, localUtils: require('./utils') diff --git a/core/server/helpers/twitter_url.js b/core/server/helpers/twitter_url.js index 016908f814..d3be3ec8c7 100644 --- a/core/server/helpers/twitter_url.js +++ b/core/server/helpers/twitter_url.js @@ -14,7 +14,7 @@ module.exports = function twitter_url(username, options) { // eslint-disable-lin } if (username) { - return socialUrls.twitterUrl(username); + return socialUrls.twitter(username); } return null; diff --git a/core/server/lib/social/index.js b/core/server/lib/social/index.js new file mode 100644 index 0000000000..70c8926cb0 --- /dev/null +++ b/core/server/lib/social/index.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = { + get urls() { + return require('./urls'); + } +}; diff --git a/core/server/utils/social-urls.js b/core/server/lib/social/urls.js similarity index 68% rename from core/server/utils/social-urls.js rename to core/server/lib/social/urls.js index fffe1fbcc5..5cb40f4845 100644 --- a/core/server/utils/social-urls.js +++ b/core/server/lib/social/urls.js @@ -1,9 +1,9 @@ -module.exports.twitterUrl = function twitterUrl(username) { +module.exports.twitter = function twitter(username) { // Creates the canonical twitter URL without the '@' return 'https://twitter.com/' + username.replace(/^@/, ''); }; -module.exports.facebookUrl = function facebookUrl(username) { +module.exports.facebook = function facebook(username) { // Handles a starting slash, this shouldn't happen, but just in case return 'https://www.facebook.com/' + username.replace(/^\//, ''); }; diff --git a/core/test/unit/utils/social-urls_spec.js b/core/test/unit/lib/social/urls_spec.js similarity index 51% rename from core/test/unit/utils/social-urls_spec.js rename to core/test/unit/lib/social/urls_spec.js index 89ee572228..88a7e0fe5a 100644 --- a/core/test/unit/utils/social-urls_spec.js +++ b/core/test/unit/lib/social/urls_spec.js @@ -1,38 +1,36 @@ var should = require('should'), + social = require('../../../../server/lib/social'); - // Stuff we are testing - socialUrls = require('../../../server/utils/social-urls'); - -describe('Social Urls', function () { +describe('lib/social: urls', function () { it('should have a twitter url function', function () { - should.exist(socialUrls.twitterUrl); + should.exist(social.urls.twitter); }); it('should have a facebook url function', function () { - should.exist(socialUrls.facebookUrl); + should.exist(social.urls.facebook); }); describe('twitter', function () { it('should return a correct concatenated URL', function () { - socialUrls.twitterUrl('myusername').should.eql('https://twitter.com/myusername'); + social.urls.twitter('myusername').should.eql('https://twitter.com/myusername'); }); it('should return a url without an @ sign if one is provided', function () { - socialUrls.twitterUrl('@myusername').should.eql('https://twitter.com/myusername'); + social.urls.twitter('@myusername').should.eql('https://twitter.com/myusername'); }); }); describe('facebook', function () { it('should return a correct concatenated URL', function () { - socialUrls.facebookUrl('myusername').should.eql('https://www.facebook.com/myusername'); + social.urls.facebook('myusername').should.eql('https://www.facebook.com/myusername'); }); it('should return a correct concatenated URL for usernames with slashes', function () { - socialUrls.facebookUrl('page/xxx/123').should.eql('https://www.facebook.com/page/xxx/123'); + social.urls.facebook('page/xxx/123').should.eql('https://www.facebook.com/page/xxx/123'); }); it('should return a correct concatenated URL for usernames which start with a slash', function () { - socialUrls.facebookUrl('/page/xxx/123').should.eql('https://www.facebook.com/page/xxx/123'); + social.urls.facebook('/page/xxx/123').should.eql('https://www.facebook.com/page/xxx/123'); }); }); }); diff --git a/core/test/unit/services/mail/utils_spec.js b/core/test/unit/services/mail/utils_spec.js index 43ab149237..9424d5242c 100644 --- a/core/test/unit/services/mail/utils_spec.js +++ b/core/test/unit/services/mail/utils_spec.js @@ -52,8 +52,8 @@ describe('Mail: Utils', function () { logo: 'http://myblog.com/content/images/blog-logo.jpg', title: 'The Ghost Blog', url: 'http://myblog.com', - twitterUrl: 'http://twitter.com/tryghost', - facebookUrl: 'https://www.facebook.com/ghost', + twitter: 'http://twitter.com/tryghost', + facebook: 'https://www.facebook.com/ghost', unsubscribe: 'http://myblog.com/unsubscribe', post: [ {