mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Wired up slack notifications handler
This is an example of how we deal with events and handlers
This commit is contained in:
parent
58d8f0d26d
commit
822f6ec496
5 changed files with 52 additions and 24 deletions
|
@ -401,9 +401,15 @@ async function initNestDependencies() {
|
|||
}, {
|
||||
provide: 'knex',
|
||||
useValue: require('./server/data/db/connection')
|
||||
}, {
|
||||
provide: 'SlackNotifications',
|
||||
useValue: require('./server/services/slack-notifications').api
|
||||
}, {
|
||||
provide: 'urlUtils',
|
||||
useValue: urlUtils
|
||||
}, {
|
||||
provide: 'config',
|
||||
useValue: require('./shared/config')
|
||||
}, {
|
||||
provide: 'DomainEvents',
|
||||
useValue: require('@tryghost/domain-events')
|
||||
|
|
|
@ -1,45 +1,32 @@
|
|||
const DomainEvents = require('@tryghost/domain-events');
|
||||
const config = require('../../../shared/config');
|
||||
const logging = require('@tryghost/logging');
|
||||
|
||||
class SlackNotificationsServiceWrapper {
|
||||
/** @type {import('@tryghost/slack-notifications/lib/SlackNotificationsService')} */
|
||||
#api;
|
||||
/** @type {import('@tryghost/slack-notifications/lib/SlackNotifications')} */
|
||||
api;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} deps
|
||||
* @param {string} deps.siteUrl
|
||||
* @param {boolean} deps.isEnabled
|
||||
* @param {URL} deps.webhookUrl
|
||||
*
|
||||
* @returns {import('@tryghost/slack-notifications/lib/SlackNotificationsService')}
|
||||
* @returns {import('@tryghost/slack-notifications/lib/SlackNotifications')}
|
||||
*/
|
||||
static create({siteUrl, isEnabled, webhookUrl}) {
|
||||
static create({siteUrl, webhookUrl}) {
|
||||
const {
|
||||
SlackNotificationsService,
|
||||
SlackNotifications
|
||||
} = require('@tryghost/slack-notifications');
|
||||
|
||||
const slackNotifications = new SlackNotifications({
|
||||
return new SlackNotifications({
|
||||
webhookUrl,
|
||||
siteUrl,
|
||||
logging
|
||||
});
|
||||
|
||||
return new SlackNotificationsService({
|
||||
DomainEvents,
|
||||
logging,
|
||||
config: {
|
||||
isEnabled,
|
||||
webhookUrl
|
||||
},
|
||||
slackNotifications
|
||||
});
|
||||
}
|
||||
|
||||
init() {
|
||||
if (this.#api) {
|
||||
if (this.api) {
|
||||
// Prevent creating duplicate DomainEvents subscribers
|
||||
return;
|
||||
}
|
||||
|
@ -47,12 +34,9 @@ class SlackNotificationsServiceWrapper {
|
|||
const hostSettings = config.get('hostSettings');
|
||||
const urlUtils = require('../../../shared/url-utils');
|
||||
const siteUrl = urlUtils.getSiteUrl();
|
||||
const isEnabled = !!(hostSettings?.milestones?.enabled && hostSettings?.milestones?.url);
|
||||
const webhookUrl = hostSettings?.milestones?.url;
|
||||
|
||||
this.#api = SlackNotificationsServiceWrapper.create({siteUrl, isEnabled, webhookUrl});
|
||||
|
||||
this.#api.subscribeEvents();
|
||||
this.api = SlackNotificationsServiceWrapper.create({siteUrl, webhookUrl});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
declare module '@tryghost/nql'
|
||||
declare module '@tryghost/errors';
|
||||
declare module '@tryghost/slack-notifications'
|
||||
declare module '@tryghost/milestones'
|
||||
|
|
34
ghost/ghost/src/handlers/slack-notifications.handler.ts
Normal file
34
ghost/ghost/src/handlers/slack-notifications.handler.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import {MilestoneCreatedEvent} from '@tryghost/milestones';
|
||||
import {SlackNotifications} from '@tryghost/slack-notifications';
|
||||
import {HandleEvent} from '../common/handle-event.decorator';
|
||||
import {Inject} from '@nestjs/common';
|
||||
|
||||
interface IConfig {
|
||||
// TODO: pass better config
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
get(key: string): any
|
||||
}
|
||||
|
||||
export class SlackNotificationsHandler {
|
||||
service: typeof SlackNotifications;
|
||||
private readonly enabled: boolean;
|
||||
constructor(service: typeof SlackNotifications, @Inject('config') config: IConfig) {
|
||||
this.service = service;
|
||||
const hostSettings = config.get('hostSettings');
|
||||
this.enabled = hostSettings?.milestones?.url && hostSettings?.milestones?.enabled;
|
||||
}
|
||||
|
||||
@HandleEvent(MilestoneCreatedEvent)
|
||||
async notifyMilestones(event: typeof MilestoneCreatedEvent) {
|
||||
if (!this.enabled) {
|
||||
|
||||
}
|
||||
try {
|
||||
await this.service.notifyMilestoneReceived(event.data);
|
||||
} catch (error) {
|
||||
// TODO sort out logging
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
};
|
|
@ -1,6 +1,7 @@
|
|||
import {SnippetsController} from '../http/controllers/snippets.controller';
|
||||
import {SnippetsService} from '../core/snippets/snippets.service';
|
||||
import {KnexSnippetsRepository} from '../db/knex/snippets.repository';
|
||||
import {SlackNotificationsHandler} from '../handlers/slack-notifications.handler';
|
||||
|
||||
class AppModuleClass {}
|
||||
|
||||
|
@ -12,6 +13,7 @@ export const AppModule = {
|
|||
provide: 'SnippetsRepository',
|
||||
useClass: KnexSnippetsRepository
|
||||
},
|
||||
SnippetsService
|
||||
SnippetsService,
|
||||
SlackNotificationsHandler
|
||||
]
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue