0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00
ghost/core/server/data/slack/index.js
Aileen Nowak e19e91044d 🙇 Blog icon utils and publisher.logo for JSON-LD (#8297)
refs #8221, closes #7688, refs #7558

🙇  Improve meta data publisher logo behaviour
This is a follow-up PR for #8285.

Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files.

This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions:
1. If we have a publication **logo**, we'll take that one
2. If we have no publication logo, but an **icon** we'll use this one.
3. If we have none of the above things, we fall back to our default `favicon.ico`

Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given.

I will create another follow-up PR, which checks the extension type for the file and offers it as a util.

🛠  Blog icon util

refs #7688

Serve functionality around the blog icon in its own util:
- getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions
- isIcoImageType -> returns true if file has `.ico` extension
- getIconType -> returns icon-type (`x-icon` or `png`)
- getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]`

📖  Get .ico sizes for meta data & logo improvement

refs #7558
refs #8221

Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files.

Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools.

- add getIconPath util
2017-04-11 18:32:06 +02:00

112 lines
3.2 KiB
JavaScript

var https = require('https'),
url = require('url'),
Promise = require('bluebird'),
errors = require('../../errors'),
logging = require('../../logging'),
utils = require('../../utils'),
blogIconUtils = require('../../utils/blog-icon'),
events = require('../../events'),
api = require('../../api/settings'),
i18n = require('../../i18n'),
schema = require('../schema').checks,
options,
req,
slackData = {};
function getSlackSettings() {
return api.read({context: {internal: true}, key: 'slack'}).then(function (response) {
var slackSetting = response.settings[0].value;
try {
slackSetting = JSON.parse(slackSetting);
} catch (e) {
return Promise.reject(e);
}
return slackSetting[0];
});
}
function makeRequest(reqOptions, reqPayload) {
req = https.request(reqOptions);
reqPayload = JSON.stringify(reqPayload);
req.write(reqPayload);
req.on('error', function (err) {
logging.error(new errors.GhostError({
err: err,
context: i18n.t('errors.data.xml.xmlrpc.pingUpdateFailed.error'),
help: i18n.t('errors.data.xml.xmlrpc.pingUpdateFailed.help', {url: 'http://support.ghost.org'})
}));
});
req.end();
}
function ping(post) {
var message;
// If this is a post, we want to send the link of the post
if (schema.isPost(post)) {
message = utils.url.urlFor('post', {post: post}, true);
} else {
message = post.message;
}
return getSlackSettings().then(function (slackSettings) {
// Quit here if slack integration is not activated
if (slackSettings.url && slackSettings.url !== '') {
// Only ping when not a page
if (post.page) {
return;
}
// Don't ping for the welcome to ghost post.
// This also handles the case where during ghost's first run
// models.init() inserts this post but permissions.init() hasn't
// (can't) run yet.
if (post.slug === 'welcome-to-ghost') {
return;
}
slackData = {
text: message,
unfurl_links: true,
icon_url: blogIconUtils.getIconUrl(true),
username: 'Ghost'
};
// fill the options for https request
options = url.parse(slackSettings.url);
options.method = 'POST';
options.headers = {'Content-type': 'application/json'};
// with all the data we have, we're doing the request now
makeRequest(options, slackData);
} else {
return;
}
});
}
function listener(model) {
ping(model.toJSON());
}
function testPing() {
ping({
message: 'Heya! This is a test notification from your Ghost blog :simple_smile:. Seems to work fine!'
});
}
function listen() {
events.on('post.published', listener);
events.on('slack.test', testPing);
}
// Public API
module.exports = {
listen: listen
};