|
@@ -3,7 +3,7 @@ import {
|
|
|
} from '@tsed/common';
|
|
} from '@tsed/common';
|
|
|
import axios from 'axios';
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
-import { WebAPICallOptions, WebAPICallResult } from '@slack/web-api';
|
|
|
|
|
|
|
+import { WebAPICallResult } from '@slack/web-api';
|
|
|
|
|
|
|
|
import {
|
|
import {
|
|
|
verifyGrowiToSlackRequest, getConnectionStatuses, getConnectionStatus, generateWebClient,
|
|
verifyGrowiToSlackRequest, getConnectionStatuses, getConnectionStatus, generateWebClient,
|
|
@@ -18,8 +18,8 @@ import { OrderRepository } from '~/repositories/order';
|
|
|
|
|
|
|
|
import { InstallerService } from '~/services/InstallerService';
|
|
import { InstallerService } from '~/services/InstallerService';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
import loggerFactory from '~/utils/logger';
|
|
|
-import { findInjectorByType } from '~/services/growi-uri-injector/GrowiUriInjectorFactory';
|
|
|
|
|
-import { injectGrowiUriToView } from '~/utils/injectGrowiUriToView';
|
|
|
|
|
|
|
+import { ViewInteractionPayloadDelegator } from '~/services/growi-uri-injector/ViewInteractionPayloadDelegator';
|
|
|
|
|
+import { ActionsBlockPayloadDelegator } from '~/services/growi-uri-injector/ActionsBlockPayloadDelegator';
|
|
|
|
|
|
|
|
|
|
|
|
|
const logger = loggerFactory('slackbot-proxy:controllers:growi-to-slack');
|
|
const logger = loggerFactory('slackbot-proxy:controllers:growi-to-slack');
|
|
@@ -42,6 +42,12 @@ export class GrowiToSlackCtrl {
|
|
|
@Inject()
|
|
@Inject()
|
|
|
orderRepository: OrderRepository;
|
|
orderRepository: OrderRepository;
|
|
|
|
|
|
|
|
|
|
+ @Inject()
|
|
|
|
|
+ viewInteractionPayloadDelegator: ViewInteractionPayloadDelegator;
|
|
|
|
|
+
|
|
|
|
|
+ @Inject()
|
|
|
|
|
+ actionsBlockPayloadDelegator: ActionsBlockPayloadDelegator;
|
|
|
|
|
+
|
|
|
async requestToGrowi(growiUrl:string, tokenPtoG:string):Promise<void> {
|
|
async requestToGrowi(growiUrl:string, tokenPtoG:string):Promise<void> {
|
|
|
const url = new URL('/_api/v3/slack-integration/proxied/commands', growiUrl);
|
|
const url = new URL('/_api/v3/slack-integration/proxied/commands', growiUrl);
|
|
|
await axios.post(url.toString(), {
|
|
await axios.post(url.toString(), {
|
|
@@ -171,41 +177,32 @@ export class GrowiToSlackCtrl {
|
|
|
return res.send({ relation: createdRelation, slackBotToken: token });
|
|
return res.send({ relation: createdRelation, slackBotToken: token });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- injectGrowiUri(req:GrowiReq, growiUri:string):WebAPICallOptions {
|
|
|
|
|
|
|
+ injectGrowiUri(req: GrowiReq, growiUri: string): void {
|
|
|
|
|
+ if (req.body.view == null && req.body.blocks == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
if (req.body.view != null) {
|
|
if (req.body.view != null) {
|
|
|
- injectGrowiUriToView(req.body, growiUri);
|
|
|
|
|
|
|
+ const parsedElement = JSON.parse(req.body.view);
|
|
|
|
|
+ // delegate to ViewInteractionPayloadDelegator
|
|
|
|
|
+ if (this.viewInteractionPayloadDelegator.shouldHandleToInject(parsedElement)) {
|
|
|
|
|
+ this.viewInteractionPayloadDelegator.inject(parsedElement, growiUri);
|
|
|
|
|
+ req.body.view = JSON.stringify(parsedElement);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- if (req.body.blocks != null) {
|
|
|
|
|
- const parsedBlocks = JSON.parse(req.body.blocks as string);
|
|
|
|
|
-
|
|
|
|
|
- parsedBlocks.forEach((parsedBlock) => {
|
|
|
|
|
- if (parsedBlock.type !== 'actions') {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- parsedBlock.elements.forEach((element) => {
|
|
|
|
|
- const growiUriInjector = findInjectorByType(element.type);
|
|
|
|
|
- if (growiUriInjector != null) {
|
|
|
|
|
- growiUriInjector.inject(element, growiUri);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- return;
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- req.body.blocks = JSON.stringify(parsedBlocks);
|
|
|
|
|
|
|
+ else if (req.body.blocks != null) {
|
|
|
|
|
+ const parsedElement = JSON.parse(req.body.blocks);
|
|
|
|
|
+ // delegate to ActionsBlockPayloadDelegator
|
|
|
|
|
+ if (this.actionsBlockPayloadDelegator.shouldHandleToInject(parsedElement)) {
|
|
|
|
|
+ this.actionsBlockPayloadDelegator.inject(parsedElement, growiUri);
|
|
|
|
|
+ req.body.blocks = JSON.stringify(parsedElement);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- const opt = req.body;
|
|
|
|
|
- opt.headers = req.headers;
|
|
|
|
|
-
|
|
|
|
|
- return opt;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Post('/:method')
|
|
@Post('/:method')
|
|
|
@UseBefore(AddWebclientResponseToRes, verifyGrowiToSlackRequest)
|
|
@UseBefore(AddWebclientResponseToRes, verifyGrowiToSlackRequest)
|
|
|
- async postResult(
|
|
|
|
|
|
|
+ async callSlackApi(
|
|
|
@PathParams('method') method: string, @Req() req: GrowiReq, @Res() res: WebclientRes,
|
|
@PathParams('method') method: string, @Req() req: GrowiReq, @Res() res: WebclientRes,
|
|
|
): Promise<void|string|Res|WebAPICallResult> {
|
|
): Promise<void|string|Res|WebAPICallResult> {
|
|
|
const { tokenGtoPs } = req;
|
|
const { tokenGtoPs } = req;
|
|
@@ -234,7 +231,10 @@ export class GrowiToSlackCtrl {
|
|
|
const client = generateWebClient(token);
|
|
const client = generateWebClient(token);
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- const opt = this.injectGrowiUri(req, relation.growiUri);
|
|
|
|
|
|
|
+ this.injectGrowiUri(req, relation.growiUri);
|
|
|
|
|
+
|
|
|
|
|
+ const opt = req.body;
|
|
|
|
|
+ opt.headers = req.headers;
|
|
|
|
|
|
|
|
await client.apiCall(method, opt);
|
|
await client.apiCall(method, opt);
|
|
|
}
|
|
}
|