|
|
@@ -1,4 +1,5 @@
|
|
|
const logger = require('@alias/logger')('growi:service:BoltService');
|
|
|
+const mongoose = require('mongoose');
|
|
|
|
|
|
const PAGINGLIMIT = 10;
|
|
|
|
|
|
@@ -49,31 +50,92 @@ class BoltReciever {
|
|
|
|
|
|
const { App } = require('@slack/bolt');
|
|
|
const { WebClient, LogLevel } = require('@slack/web-api');
|
|
|
+const S2sMessage = require('../models/vo/s2s-message');
|
|
|
+const S2sMessageHandlable = require('./s2s-messaging/handlable');
|
|
|
|
|
|
-class BoltService {
|
|
|
+class BoltService extends S2sMessageHandlable {
|
|
|
|
|
|
constructor(crowi) {
|
|
|
+ super();
|
|
|
+
|
|
|
this.crowi = crowi;
|
|
|
+ this.s2sMessagingService = crowi.s2sMessagingService;
|
|
|
this.receiver = new BoltReciever();
|
|
|
+ this.client = null;
|
|
|
|
|
|
- const signingSecret = crowi.configManager.getConfig('crowi', 'slackbot:signingSecret');
|
|
|
- const token = crowi.configManager.getConfig('crowi', 'slackbot:token');
|
|
|
+ this.isBoltSetup = false;
|
|
|
+ this.lastLoadedAt = null;
|
|
|
|
|
|
- const client = new WebClient(token, { logLevel: LogLevel.DEBUG });
|
|
|
- this.client = client;
|
|
|
+ this.initialize();
|
|
|
+ }
|
|
|
|
|
|
- if (token != null || signingSecret != null) {
|
|
|
- logger.debug('SlackBot: setup is done');
|
|
|
- this.bolt = new App({
|
|
|
- token,
|
|
|
- signingSecret,
|
|
|
- receiver: this.receiver,
|
|
|
- });
|
|
|
- this.init();
|
|
|
+ initialize() {
|
|
|
+ this.isBoltSetup = false;
|
|
|
+
|
|
|
+ const token = this.crowi.configManager.getConfig('crowi', 'slackbot:token');
|
|
|
+ const signingSecret = this.crowi.configManager.getConfig('crowi', 'slackbot:signingSecret');
|
|
|
+
|
|
|
+ this.client = new WebClient(token, { logLevel: LogLevel.DEBUG });
|
|
|
+
|
|
|
+ if (token == null || signingSecret == null) {
|
|
|
+ this.bolt = null;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.bolt = new App({
|
|
|
+ token,
|
|
|
+ signingSecret,
|
|
|
+ receiver: this.receiver,
|
|
|
+ });
|
|
|
+ this.setupRoute();
|
|
|
+
|
|
|
+ logger.debug('SlackBot: setup is done');
|
|
|
+
|
|
|
+ this.isBoltSetup = true;
|
|
|
+ this.lastLoadedAt = new Date();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @inheritdoc
|
|
|
+ */
|
|
|
+ shouldHandleS2sMessage(s2sMessage) {
|
|
|
+ const { eventName, updatedAt } = s2sMessage;
|
|
|
+ if (eventName !== 'boltServiceUpdated' || updatedAt == null) {
|
|
|
+ return false;
|
|
|
}
|
|
|
+
|
|
|
+ return this.lastLoadedAt == null || this.lastLoadedAt < new Date(s2sMessage.updatedAt);
|
|
|
}
|
|
|
|
|
|
- init() {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @inheritdoc
|
|
|
+ */
|
|
|
+ async handleS2sMessage() {
|
|
|
+ const { configManager } = this.crowi;
|
|
|
+
|
|
|
+ logger.info('Reset bolt by pubsub notification');
|
|
|
+ await configManager.loadConfigs();
|
|
|
+ this.initialize();
|
|
|
+ }
|
|
|
+
|
|
|
+ async publishUpdatedMessage() {
|
|
|
+ const { s2sMessagingService } = this;
|
|
|
+
|
|
|
+ if (s2sMessagingService != null) {
|
|
|
+ const s2sMessage = new S2sMessage('boltServiceUpdated', { updatedAt: new Date() });
|
|
|
+
|
|
|
+ try {
|
|
|
+ await s2sMessagingService.publish(s2sMessage);
|
|
|
+ }
|
|
|
+ catch (e) {
|
|
|
+ logger.error('Failed to publish update message with S2sMessagingService: ', e.message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ setupRoute() {
|
|
|
this.bolt.command('/growi', async({
|
|
|
command, client, body, ack,
|
|
|
}) => {
|
|
|
@@ -293,20 +355,6 @@ class BoltService {
|
|
|
}
|
|
|
|
|
|
async createModal(command, client, body) {
|
|
|
- const User = this.crowi.model('User');
|
|
|
- const slackUser = await User.findUserByUsername('slackUser');
|
|
|
-
|
|
|
- // if "slackUser" is null, don't show create Modal
|
|
|
- if (slackUser == null) {
|
|
|
- logger.error('Failed to create a page because slackUser is not found.');
|
|
|
- this.client.chat.postEphemeral({
|
|
|
- channel: command.channel_id,
|
|
|
- user: command.user_id,
|
|
|
- blocks: [this.generateMarkdownSectionBlock('*slackUser does not exist.*')],
|
|
|
- });
|
|
|
- throw new Error('/growi command:create: slackUser is not found');
|
|
|
- }
|
|
|
-
|
|
|
try {
|
|
|
await client.views.open({
|
|
|
trigger_id: body.trigger_id,
|
|
|
@@ -349,23 +397,20 @@ class BoltService {
|
|
|
|
|
|
// Submit action in create Modal
|
|
|
async createPageInGrowi(view, body) {
|
|
|
- const User = this.crowi.model('User');
|
|
|
const Page = this.crowi.model('Page');
|
|
|
const pathUtils = require('growi-commons').pathUtils;
|
|
|
|
|
|
const contentsBody = view.state.values.contents.contents_input.value;
|
|
|
|
|
|
try {
|
|
|
- // search "slackUser" to create page in slack
|
|
|
- const slackUser = await User.findUserByUsername('slackUser');
|
|
|
-
|
|
|
let path = view.state.values.path.path_input.value;
|
|
|
// sanitize path
|
|
|
path = this.crowi.xss.process(path);
|
|
|
path = pathUtils.normalizePath(path);
|
|
|
|
|
|
- const user = slackUser._id;
|
|
|
- await Page.create(path, contentsBody, user, {});
|
|
|
+ // generate a dummy id because Operation to create a page needs ObjectId
|
|
|
+ const dummyObjectIdOfUser = new mongoose.Types.ObjectId();
|
|
|
+ await Page.create(path, contentsBody, dummyObjectIdOfUser, {});
|
|
|
}
|
|
|
catch (err) {
|
|
|
this.client.chat.postMessage({
|