Ghost/core/server/services/slack.js
Kevin Ansfield 80fbfd7a85 Revert post.page->post.type handling
no issue
- the column addition/removal can be too slow for large sites
- will be added back in 3.0

---

Revert "Fixed canary api for page/type column"
This reverts commit a5a7e7e919.

Revert "Updated frontend canary url config for page/type"
This reverts commit 19100ec5e6.

Revert "Updated canary api to handle type column correctly (#11006)"
This reverts commit c3e8ba0523.

Revert "Ensured `page` filter works in routes.yaml"
This reverts commit 9037c19e50.

Revert "Replaced usage of mongo util with nql-map-key-values"
This reverts commit 8c5f1d0ef0.

Revert "Added shared nql-map-key-values module"
This reverts commit ef4fd4b8ef.

Revert "Ensured page prop is present on content api response"
This reverts commit cfa0a0862b.

Revert "Fixed failing regression tests"
This reverts commit 9c2bb3811f.

Revert "Updated xmlrpc and slack service to use type column"
This reverts commit 44a02c7d36.

Revert "Updated v0.1 posts api to work with type column"
This reverts commit 2c81d7c914.

Revert "Removed updates to v0.1 specific code"
This reverts commit 08d83c1f53.

Revert "Added missing context from ValidationError"
This reverts commit cd45ab4f54.

Revert "Renamed page->type in the page&posts serializers"
This reverts commit df99e724e3.

Revert "Added mongo helper to input serializers"
This reverts commit fb8eadb4a8.

Revert "Passed mongoTransformer through to NQL"
This reverts commit 0ae3f0fdfc.

Revert "Permitted mongoTransformer option for read methods"
This reverts commit a89376bf26.

Revert "Updated the count plugin to reference the type column"
This reverts commit a52f15d3d3.

Revert "Updated hashes for db integrity check"
This reverts commit bb6b337be3.

Revert "Remove page column and remaining references"
This reverts commit 9d7190d692.

Revert "Added type column to data generator"
This reverts commit e59806cb45.

Revert "Removed references to page column in rss tests"
This reverts commit 04d0f855de.

Revert "Removed page column references in validation tests"
This reverts commit f0afbc5cc0.

Revert "Updated the post model to use the `type` column"
This reverts commit 1189bc823a.

Revert "Updated url service to use type column"
This reverts commit 61612ba8fd.

Revert "Updated the v2 api to deal with type column"
This reverts commit 57afb2de2b.

Revert "Added type property to post model defaults"
This reverts commit dc3345b1c5.

Revert "Added type property to the default post fixtures"
This reverts commit 82d8c38033.

Revert "Added type column to posts table"
This reverts commit 9b85fc6a69.
2019-08-19 12:02:14 +01:00

151 lines
5.3 KiB
JavaScript

var common = require('../lib/common'),
request = require('../lib/request'),
imageLib = require('../lib/image'),
urlUtils = require('../lib/url-utils'),
urlService = require('../../frontend/services/url'),
settingsCache = require('./settings/cache'),
schema = require('../data/schema').checks,
moment = require('moment'),
defaultPostSlugs = [
'welcome',
'the-editor',
'using-tags',
'managing-users',
'private-sites',
'advanced-markdown',
'themes'
];
function getSlackSettings() {
var setting = settingsCache.get('slack');
// This might one day have multiple entries, for now its always a array
// and we return the first item or an empty object
return setting ? setting[0] : {};
}
function ping(post) {
let message,
title,
author,
slackData = {},
slackSettings = getSlackSettings(),
blogTitle = settingsCache.get('title');
// If this is a post, we want to send the link of the post
if (schema.isPost(post)) {
message = urlService.getUrlByResourceId(post.id, {absolute: true});
title = post.title ? post.title : null;
author = post.authors ? post.authors[0] : null;
} else {
message = post.message;
}
// Quit here if slack integration is not activated
if (slackSettings && slackSettings.url && slackSettings.url !== '') {
slackSettings.username = slackSettings.username ? slackSettings.username : 'Ghost';
// Only ping when not a page
if (post.page) {
return;
}
// Don't ping for the default posts.
// 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 (defaultPostSlugs.indexOf(post.slug) > -1) {
return;
}
if (schema.isPost(post)) {
slackData = {
// We are handling the case of test notification here by checking
// if it is a post or a test message to check webhook working.
text: `Notification from *${blogTitle}* :ghost:`,
unfurl_links: true,
icon_url: imageLib.blogIcon.getIconUrl(true),
username: slackSettings.username,
// We don't want to send attachment if it is a test notification.
attachments: [
{
fallback: 'Sorry, content cannot be shown.',
title: title,
title_link: message,
author_name: blogTitle,
image_url: post ? urlUtils.urlFor('image', {image: post.feature_image}, true) : null,
color: '#008952',
fields: [
{
title: 'Description',
value: post.custom_excerpt ? post.custom_excerpt : `${post.html.replace(/<[^>]+>/g, '').split('.').slice(0, 3).join('.')}.`,
short: false
}
]
},
{
fallback: 'Sorry, content cannot be shown.',
color: '#008952',
thumb_url: author ? urlUtils.urlFor('image', {image: author.profile_image}, true) : null,
fields: [
{
title: 'Author',
value: author ? `<${urlService.getUrlByResourceId(author.id, {absolute: true})} | ${author.name}>` : null,
short: true
}
],
footer: blogTitle,
footer_icon: imageLib.blogIcon.getIconUrl(true),
ts: moment().unix()
}
]
};
} else {
slackData = {
text: message,
unfurl_links: true,
icon_url: imageLib.blogIcon.getIconUrl(true),
username: slackSettings.username
};
}
return request(slackSettings.url, {
body: JSON.stringify(slackData),
headers: {
'Content-type': 'application/json'
}
}).catch(function (err) {
common.logging.error(new common.errors.GhostError({
err: err,
context: common.i18n.t('errors.services.ping.requestFailed.error', {service: 'slack'}),
help: common.i18n.t('errors.services.ping.requestFailed.help', {url: 'https://ghost.org/docs/'})
}));
});
}
}
function listener(model, options) {
// CASE: do not ping slack if we import a database
// TODO: refactor post.published events to never fire on importing
if (options && options.importing) {
return;
}
ping(model.toJSON());
}
function testPing() {
ping({
message: 'Heya! This is a test notification from your Ghost blog :smile:. Seems to work fine!'
});
}
function listen() {
common.events.on('post.published', listener);
common.events.on('slack.test', testPing);
}
// Public API
module.exports = {
listen: listen
};